2016-02-11 89 views
3

我很难使用Maven Shade插件,因为我想将我的着色jar安装到与父pom(而不是本地src/target目录)相同的文件夹中。maven shaded jar:更改输出位置

布局: 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我想阴影罐子搬迁到guide文件夹。

不幸的是,我尝试使用

<outputDirectory>/home/name/Desktop/maven_project/guide/</outputDirectory>  

但这只是将原本的-JAR的目录。

问题:有关如何将阴影的jar移动到那里的任何想法(甚至删除过程中的原始jar)?

+1

你配置Jar插件或Shade插件的'outputDirectory'条目? –

+2

你应该改变[outputDirectory'](https://maven.apache.org/plugins/maven-shade-plugin/shade-mojo.html#outputDirectory)。附注:硬编码值是脆弱的。为什么不使用'../ guide'? – Tunaki

+0

是的,输出目录是在阴影插件@ A.DiMatteo – Max

回答

4

默认情况下,Maven Shade Plugin取代了构建生成的原始jar,并创建了一个副本,其前缀为,原始为

可以通过outputDirectory,outputFilefinalName配置条目来配置替换和重定位。

应用以下配置:

<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] ------------------------------------------------------------------------ 
+1

除了删除默认jar外,两者都按照预期工作。我得到一个错误:maven-shade-plugin:2.4.3:阴影(默认)@projB --- [错误]项目主要工件不存在。这可能有以下 [错误]原因: – Max

+0

[错误] - 您已直接从命令行调用目标。这不是支持的 [错误]。请通过您的POM中的 [ERROR] 元素将目标添加到默认生命周期,并使用“mvn package”使其运行。 [错误] - 您已将目标绑定到“包”之前的生命周期阶段。请 [错误]从您的POM中删除此绑定,以便目标将在 [错误]运行正确的阶段。 [错误] - 您删除了生成主工件的maven-jar-plugin的配置。 – Max

+0

@Max你如何调用Maven?一个简单的'mvn clean package'应该就足够了。 – Tunaki