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

如何将Java带依赖Jar运行命令转换为Maven Profile?

搞定Maven Profile整合exec-maven-plugin执行带依赖JAR的问题

嘿,我看你遇到的问题核心是之前的Profile配置没踩对两个关键点:一是执行阶段太早(compile阶段还没生成带依赖的JAR),二是用了exec:java目标导致类加载环境和你手动执行java -cp不一样。现在把你找到的有效配置正确整合到Profile里就解决了,具体方案如下:

完整的Profile整合配置

我把有效的exec配置放到runExample Profile里,同时补全了maven-assembly-plugin的配置(确保能生成带依赖的JAR),调整了执行阶段,还移除了原配置里冲突的内容:

<profiles>
  <profile>
    <id>runExample</id>
    <build>
      <plugins>
        <!-- 先确保maven-assembly-plugin正确生成带依赖的JAR -->
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>3.6.0</version>
          <executions>
            <execution>
              <id>package-with-deps</id>
              <phase>package</phase>
              <goals>
                <goal>single</goal>
              </goals>
              <configuration>
                <descriptorRefs>
                  <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <finalName>module-jar-with-dependencies</finalName>
                <appendAssemblyId>false</appendAssemblyId>
              </configuration>
            </execution>
          </executions>
        </plugin>

        <!-- 整合有效的exec-maven-plugin配置 -->
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <version>1.6.0</version>
          <executions>
            <execution>
              <id>run-example</id>
              <!-- 绑定到package阶段,确保JAR已经生成 -->
              <phase>package</phase>
              <goals>
                <!-- 使用exec目标,完全模拟你手动执行的java命令 -->
                <goal>exec</goal>
              </goals>
              <configuration>
                <executable>java</executable>
                <arguments>
                  <argument>-cp</argument>
                  <argument>target/module-jar-with-dependencies.jar</argument>
                  <argument>module.Launcher</argument>
                  <argument>--project</argument>
                  <argument>example</argument>
                  <argument>--network</argument>
                  <argument>toy_ags_network.sif</argument>
                </arguments>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>

怎么执行?

有两种方式:

  • 一键式执行:运行 mvn clean package -P runExample,Maven会自动完成清理、编译、打包生成带依赖的JAR,然后立刻执行你的示例程序,和你手动敲java -cp命令效果完全一样。
  • 单独运行:要是已经打包好JAR了,只想跑示例,直接敲 mvn exec:exec -P runExample 就行。

为啥这么改?

  • 执行阶段选package:因为maven-assembly-plugin是在这个阶段生成带依赖的JAR,要是选compile的话,JAR还没生成呢,肯定报类找不到。
  • 用exec:exec而不是exec:java:exec:java是在Maven自己的类加载器里跑,和你手动用系统Java命令的环境不一样,而exec:exec就是直接调用系统的java命令,完美匹配你原来的执行环境,不会有依赖加载问题。
  • 补全assembly插件配置:确保能正确生成你需要的module-jar-with-dependencies.jar,要是你原来已经有这个配置了,可以直接替换成你自己的版本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 09:23:44