2016-02-11 88 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