默認情況下,Maven Shade Plugin取代了構建生成的原始jar,並創建了一個副本,其前綴爲,原始爲。
可以通過outputDirectory
,outputFile
和finalName
配置條目來配置替換和重定位。
應用以下配置:
<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>
我們:
- 首先禁用默認罐子的產生,通過您的要求的要求,並指定由this dedicated SO Q/A
- 然後配置陰影插件將其輸出重定位到上面的
guide
文件夾(通過相對路徑,更好的方法也可以通過@Tunaki建議)
- 還配置
finalName
元素以禁用替換(這也會影響重定位,也就是(前綴)原始jar將被重新定位)。按照official documentation的finalName是
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 if shadedArtifactAttached
is being used.
這樣,Maven將產生僅在配置的位置的陰影罐子。
另一種方法,是使用了outputFile
配置項,其中規定:
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
and createDependencyReducedPom
to be ignored when used.
因此,你可以改變上面的配置:
<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>
而且具有完全相同的行爲。
附註:你真的在改變這裏的構建行爲。如果某人從模塊文件夾本身構建了一個模塊,他/她就不會在target
文件夾中找到預期內容,而是在父文件夾中找到預期內容(有點意外)。
更新
應用上述配置和從命令行調用只有陰影插件
mvn shade:shade
不過,您將有以下問題:
[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] ------------------------------------------------------------------------
你配置Jar插件或Shade插件的'outputDirectory'條目? –
你應該改變[outputDirectory'](https://maven.apache.org/plugins/maven-shade-plugin/shade-mojo.html#outputDirectory)。附註:硬編碼值是脆弱的。爲什麼不使用'../ guide'? – Tunaki
是的,輸出目錄是在陰影插件@ A.DiMatteo – Max