【问题标题】:With maven how to bundle all dependencies AND my Java project filesin the output jar when using `maven-assembly-plugin`?使用 maven-assembly-plugin 时,如何使用 maven 将所有依赖项和我的 Java 项目文件捆绑在输出 jar 中?
【发布时间】:2020-03-25 06:24:22
【问题描述】:

我正在使用maven-assembly-plugin 从我的 maven 项目中创建一个输出 jar。我的pom.xml 的相关部分是这样的:

.
.
.
<dependencies>
    <dependency>
      .
      .
      .
    </dependency>
    .
    .
    .
</dependencies>

<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        .
        .
        .
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>my.package.TheApp</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
        .
        .
        .
    </plugins>
</build>
.
.
.

当我运行 `mvn clean install assembly:single` 时,生成的 jar 文件确实包含依赖项,但它确实 包含我的项目文件。使用

运行 jar
java -jar TheApp-0.1.2-SNAPSHOT-jar-with-dependencies.jar

我收到消息Error: Could not find or load main class my.package.TheApp那么我怎样才能包含我的项目文件呢?(我想设置useProjectArtifact,但是according to the maven-assembly-plugin doco这个属性默认是true。)

【问题讨论】:

标签: java maven maven-assembly-plugin


【解决方案1】:

通过在 Stack Overflow 上闲逛我 found the solution

运行 mvn clean compile assembly:single 而不是 mvn clean install assembly:single。 :-)

或者甚至更好地将pom.xml 程序集绑定到打包阶段(感谢@khmarbaise!):

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  .
  .
  .
  <executions>
    <execution>
      <id>make-assembly</id> <!-- this is used for inheritance merges -->
      <phase>package</phase> <!-- bind to the packaging phase -->
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

然后运行mvn clean package(或mvn clean install,如果你愿意的话)。

【讨论】:

  • 将插件绑定到生命周期,这样您就可以使用mvn clean package 代替添加补充目标...而install 不是必需的...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-09
  • 2014-12-16
  • 1970-01-01
  • 1970-01-01
  • 2012-08-15
  • 1970-01-01
  • 2014-03-12
相关资源
最近更新 更多