You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dropwizard配置在Shaded Jar中失效:配置解析异常问题

解决Dropwizard Shaded Jar运行时配置解析失败的问题

我之前也碰到过一模一样的状况!直接运行主类时一切正常,可打包成Shaded Jar后执行java -jar target/mlsdata-1.0.jar server configuration.yaml就报ConfigurationParsingException,说没法把http解析成ConnectorFactory的子类型。核心原因其实是Maven Shade插件在打包过程中,破坏了Dropwizard依赖的Jackson多态类型元数据或者服务配置文件——Dropwizard靠Jackson的@JsonTypeInfo注解来识别配置里的http对应的具体Connector实现类,默认的Shade打包逻辑会弄丢这些关键信息。

下面给你几个亲测有效的解决步骤:

1. 修复Maven Shade插件的配置

在你的pom.xml里,给Shade插件添加必要的Transformer和资源保留规则,确保Dropwizard需要的元数据不丢失:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.4.1</version> <!-- 建议使用最新稳定版本 -->
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <!-- 保留Jar包的主类入口 -->
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>你的主类全路径,比如com.example.MlsdataApplication</mainClass>
                            </transformer>
                            <!-- 合并META-INF/services下的服务配置文件,Dropwizard靠这个加载实现类 -->
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                            <!-- 保留Jackson的模块信息,避免多态类型解析失败 -->
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/com.fasterxml.jackson.databind.Module</resource>
                            </transformer>
                        </transformers>
                        <!-- 排除签名文件,避免打包时的冲突 -->
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

2. 核对配置文件格式

虽然直接运行主类没问题,但还是确认下configuration.yaml的写法是否符合Dropwizard的要求,比如:

server:
  applicationConnectors:
    - type: http
      port: 8080
  adminConnectors:
    - type: http
      port: 8081

注意type字段是小写的http,Dropwizard对这个字段的大小写是敏感的,别写成大写或者拼写错误。

3. 确认依赖完整性

检查pom.xml里是否包含了Dropwizard的核心依赖,比如:

<dependencies>
    <dependency>
        <groupId>io.dropwizard</groupId>
        <artifactId>dropwizard-core</artifactId>
        <version>你的Dropwizard版本,比如2.1.9</version>
    </dependency>
</dependencies>

如果使用了自定义Connector,也要确保对应的依赖被正确包含到Shaded Jar中。

完成以上配置后,执行mvn clean package重新打包,再运行你的Jar包,应该就能正常解析配置文件了。

内容的提问来源于stack exchange,提问作者sudarshan kakumanu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:23:21