Maven阴影罐:更改输出位置 [英] maven shaded jar: change output location
问题描述
我在使用Maven Shade插件时遇到了困难,因为我希望将着色的jar安装到与Parent pom相同的文件夹中(而不是本地src/target
目录).
I'm having difficulty using the Maven Shade Plugin because I would like to have my shaded jar be installed to the same folder as the Parent pom (instead of the local src/target
directory).
布局: maven_project
The layout: maven_project
guide/
parent_pom.xml
projA/
pom.xml
projB/
pom.xml
/target
original-projB-0.0.3.jar
projB-0.0.3.jar (shaded jar)
我必须导出项目并使其他人更容易地运行可执行的jar,我想将阴影的jar重新放置到guide
文件夹中.
I have to export the project and to make it easier for others to run the executable jar I want to relocate the shaded jar to the guide
folder.
不幸的是,我尝试使用
<outputDirectory>/home/name/Desktop/maven_project/guide/</outputDirectory>
但这只会将原始jar移到目录中.
but this only moved the original-jar to the directory.
问题:是否有关于如何将阴影罐子移到那里(甚至在此过程中删除原始罐子)的任何想法?
Question: Any ideas on how to move the shaded jar there instead (and even delete the original jar in the process)?
推荐答案
Maven阴影默认情况下,插件会替换由构建生成的原始jar,并为其创建一个以 original 为前缀的副本.
The Maven Shade Plugin by default replaces the original jar generated by the build and creates a copy of it prefixed with original.
可以通过outputDirectory
,outputFile
和finalName
配置条目来配置替换和重定位.
Replacement and relocation can be configured via the outputDirectory
, outputFile
and finalName
configuration entries.
应用以下配置:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default-jar</id>
<phase />
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${project.artifactId}-${project.version}-something</finalName>
<outputDirectory>../guide</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
我们是:
- 首先按照您的要求并由所建议) @Tunaki )
- 也配置
finalName
元素以禁用替换(从意义上说,(已前缀的)原始jar也将被重定位,这也影响重定位).根据官方文档,finalName是
- Firstly disable the generation of the default jar, as requested by your requirements and specified by this dedicated SO Q/A
- Then configuring the Shade Plugin to relocate its output to the upper
guide
folder (via relative path, better approach as also suggested by @Tunaki) - Also configuring the
finalName
element in order to disable replacement (which also affects relocation, in the sense that also the (prefixed) original jar will be relocated). As per official documentation the finalName is
带阴影的artifactId的名称.如果您想更改本机工件的名称,则可以使用
<build><finalName>
设置.如果将其设置为不同于<build><finalName>
的值,则即使正在使用shadedArtifactAttached
,也不会执行文件替换.
The name of the shaded artifactId. If you like to change the name of the native artifact, you may use the
<build><finalName>
setting. If this is set to something different than<build><finalName>
, no file replacement will be performed, even ifshadedArtifactAttached
is being used.
这样,Maven将仅在配置的位置生成阴影jar.
As such, Maven will generate only the shaded jar at the configured location.
另一种方法是使用outputFile
配置条目,该条目指定:
Another approach, would be to use the outputFile
configuration entry, which specifies:
阴影工件的输出文件的路径.设置此参数后,创建的存档既不会替换项目的主工件,也不会被附加.因此,使用此参数会使参数
finalName
,shadedArtifactAttached,shadedClassifierName
和createDependencyReducedPom
在使用时被忽略.
The path to the output file for the shaded artifact. When this parameter is set, the created archive will neither replace the project's main artifact nor will it be attached. Hence, this parameter causes the parameters
finalName
, shadedArtifactAttached,shadedClassifierName
andcreateDependencyReducedPom
to be ignored when used.
因此,您可以将上面的配置更改为:
Hence you could change the configuration above to:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default-jar</id>
<phase />
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>../guide/${project.artifactId}-${project.version}-shaded.jar</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
并且具有完全相同的行为.
And have exactly the same behavior.
侧面说明:您实际上是在这里更改构建的行为.如果某人仅从模块文件夹本身构建单个模块,则他/她将不会在target
文件夹中找到期望的内容,而该文件夹将位于父文件夹中(有点意外).
Side note: you are really changing the behavior of the build here. If somebody builds only a single module, from the module folder itself, he/she would not find the expected content on the target
folder, which would be on the parent one instead (a bit of a surprise).
更新
应用上面的配置并从命令行仅调用Shade插件
Update
Applying the configuration above and invoking only the Shade Plugin from the command line
mvn shade:shade
但是,您将遇到以下问题:
You will however have the following issue:
[INFO] --- maven-shade-plugin:2.4.3:shade (default-cli) @ test-addjar ---
[ERROR] The project main artifact does not exist. This could have the following
[ERROR] reasons:
[ERROR] - You have invoked the goal directly from the command line. This is not
[ERROR] supported. Please add the goal to the default lifecycle via an
[ERROR] <execution> element in your POM and use "mvn package" to have it run.
[ERROR] - You have bound the goal to a lifecycle phase before "package". Please
[ERROR] remove this binding from your POM such that the goal will be run in
[ERROR] the proper phase.
[ERROR] - You removed the configuration of the maven-jar-plugin that produces the main artifact.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
这篇关于Maven阴影罐:更改输出位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!