更改 jar 文件名以输出具有依赖项的 maven jar?



我创建了一个 JAR,其中包含来自多个项目的代码,其中包含来自 maven-assembly-pluginjar-with-dependenciesdescriptorRef的代码,如下所述:使用 Maven 将依赖项包含在 jar 中。

但是如何让它生成输出文件foo-1.0.jar而不是丑陋的foo-1.0-SNAPSHOT-jar-with-dependencies.jar?我不需要不包含其他项目的默认 JAR。

在 maven-assembly-plugin 中,您可以在程序集执行的配置标记中添加一个名为appendAssemblyId的可选参数(默认情况下设置为true)。
使用此标记将生成两个警告,指示您可能会覆盖工件的主构建(由 maven-jar-plugin 完成)。
如果不想在appendAssemblyId设置为false的情况下重写此 jar,则可以决定在另一个具有属性outputDirectory的文件夹中生成程序集。
或者,如果您同意在 jar 名称末尾附加一些内容,另一种解决方案是创建您自己的程序集描述符。

(有关现有参数或如何创建自己的程序集描述符的更多信息,您可以在此处查看插件文档:https://maven.apache.org/plugins/maven-assembly-plugin/assembly-mojo.html)

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<executions>
<execution>
<id>jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
<outputDirectory>${project.build.directory}/my-assembly/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

编辑:我编辑了我的答案,以使其更完整。

这是我最终做的事情。在我的pom.xml

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<executions>
<execution>
<id>all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptors>
<descriptor>all.xml</descriptor>
</descriptors> 
</configuration>
</plugin>

我指定了一个自行开发的程序集描述符,通过将程序集文件jar-with-dependencies.xml从 https://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html 复制到本地文件all.xml,将idjar-with-dependencies更改为all来获得该描述符。瞧,现在生成的文件名是foo-1.0-SNAPSHOT-all.jar,这适用于我的目的。

最新更新