maven shaded jar:更改输出位置
maven shaded jar: change output location
我在使用 Maven Shade 插件时遇到困难,因为我想将我的着色 jar 安装到与父 pom 相同的文件夹(而不是本地 src/target
目录)。
布局:
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
文件夹。
不幸的是,我尝试使用
<outputDirectory>/home/name/Desktop/maven_project/guide/</outputDirectory>
但这只是将原始 jar 移动到目录中。
问题:关于如何将阴影 jar 移到那里(甚至在此过程中删除原始 jar)有什么想法吗?
默认情况下,Maven Shade Plugin 会替换构建生成的原始 jar 并创建其副本,并以 original.
为前缀
可以通过 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
指定禁用默认jar的生成
- 然后配置 Shade 插件以将其输出重新定位到上层
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 将只在配置的位置生成阴影 jar。
另一种方法是使用 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>
并且具有完全相同的行为。
旁注:您实际上是在更改此处构建的行为。如果有人只构建一个模块,从模块文件夹本身,he/she 将不会在 target
文件夹中找到预期的内容,而是在父文件夹中(有点意外)。
更新
应用上面的配置并仅从命令行调用 Shade 插件
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] ------------------------------------------------------------------------
我在使用 Maven Shade 插件时遇到困难,因为我想将我的着色 jar 安装到与父 pom 相同的文件夹(而不是本地 src/target
目录)。
布局: 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
文件夹。
不幸的是,我尝试使用
<outputDirectory>/home/name/Desktop/maven_project/guide/</outputDirectory>
但这只是将原始 jar 移动到目录中。
问题:关于如何将阴影 jar 移到那里(甚至在此过程中删除原始 jar)有什么想法吗?
默认情况下,Maven Shade Plugin 会替换构建生成的原始 jar 并创建其副本,并以 original.
为前缀可以通过 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 指定禁用默认jar的生成
- 然后配置 Shade 插件以将其输出重新定位到上层
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 ifshadedArtifactAttached
is being used.
因此,Maven 将只在配置的位置生成阴影 jar。
另一种方法是使用 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
andcreateDependencyReducedPom
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>
并且具有完全相同的行为。
旁注:您实际上是在更改此处构建的行为。如果有人只构建一个模块,从模块文件夹本身,he/she 将不会在 target
文件夹中找到预期的内容,而是在父文件夹中(有点意外)。
更新
应用上面的配置并仅从命令行调用 Shade 插件
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] ------------------------------------------------------------------------