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

Spring Boot与Dropwizard共存的Maven项目如何单独运行Dropwizard应用

问题解答

为什么当前命令会启动Spring Boot应用

  • jar包默认主类是Spring Boot启动类。你项目使用了spring-boot-maven-plugin打包时,插件会默认将标注@SpringBootApplication的类设为jar包META-INF/MANIFEST.MF文件中的Main-Class属性,执行java -jar命令时会优先调用这个默认主类,你后面附带的参数会直接传给Spring Boot启动类,不会被识别为要启动其他类。
  • 启动类路径写错且不完整。你命令中写的是com.package.dropwizard,和你实际的Dropwizard代码包路径com.project.dropwizard不符,且你只写了包名,没有指定继承io.dropwizard.Application的具体启动类名,就算默认主类优先级不生效也无法正确启动Dropwizard。

可以单独运行Dropwizard应用,操作方法如下

场景1:临时运行,无需修改打包配置

直接用类路径指定主类运行,命令如下:

java -cp target/jarfile.jar com.project.dropwizard.你的Dropwizard启动类全类名 server config.yml

如果上述命令因为Spring Boot fat jar的类加载结构问题运行失败,可以用Spring Boot提供的PropertiesLauncher指定启动类:

java -Dloader.main=com.project.dropwizard.你的Dropwizard启动类全类名 org.springframework.boot.loader.PropertiesLauncher server config.yml

场景2:长期使用,打包为独立可执行jar

修改Maven的pom.xml配置,分别为两个应用打独立的可执行jar,配置示例如下:

<build>
  <plugins>
    <!-- 打包Dropwizard独立jar -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <executions>
        <execution>
          <id>dropwizard-jar</id>
          <phase>package</phase>
          <goals>
            <goal>jar</goal>
          </goals>
          <configuration>
            <classifier>dropwizard</classifier>
            <archive>
              <manifest>
                <mainClass>com.project.dropwizard.你的Dropwizard启动类全类名</mainClass>
              </manifest>
            </archive>
          </configuration>
        </execution>
      </executions>
    </plugin>
    <!-- 打包Spring Boot独立jar -->
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <configuration>
        <mainClass>com.project.springboot.你的SpringBoot启动类全类名</mainClass>
        <classifier>spring-boot</classifier>
      </configuration>
      <executions>
        <execution>
          <goals>
            <goal>repackage</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

配置完成后执行mvn package,target目录下会生成两个带后缀的jar包,运行Dropwizard直接执行:

java -jar target/jarfile-dropwizard.jar server config.yml

内容的提问来源于stack exchange,提问作者Developer kl

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 10:12:01