2016-09-28 26 views
2

我有一個使用Maven編譯的Java項目,最後使用maven-assembly-plugin將編譯的JAR文件,DLL等打包成10個不同的ZIP文件。每個ZIP文件適用於不同的環境(具有不同的DLL),但其內容通常是相同的。如何擁有Maven Assembly Plugin的自定義屬性?

現在我使用10個不同的assembly.xml文件來創建這10個ZIP文件。

問題是這些XML幾乎是相同的,唯一的區別是DLL中路徑中有1個字。這種文件的例子:(在現實中,它是更長的時間)

<assembly> 
    <id>{someVariable}</id> 
    <formats> 
    <format>zip</format> 
    </formats> 
    <fileSets> 
    <!-- Copying DLLs: --> 
    <fileSet> 
     <directory>target/dll/{someVariable}</directory> 
     <outputDirectory>dll</outputDirectory> 
     <includes> 
      <include>*.dll</include> 
     </includes> 
    </fileSet> 
    </fileSets> 
</assembly> 

正如你可以看到,我想在更多的地方這是所期望的功能,但我不能使它工作使用{someVariable}。希望這是可能的,這是我的問題的核心。我想用同樣的assembly.xml文件,像這樣的{someVariable}不同的值執行它10倍總:

<plugin> 
<artifactId>maven-assembly-plugin</artifactId> 
<executions> 
    <execution> 
     <id>make-the-zip</id> 
     <goals> 
      <goal>single</goal> 
     </goals> 
     <phase>package</phase> 
     <configuration> 
      <descriptors> 
       <descriptor>src/assembly/myCommonAssembly.xml</descriptor> 
      </descriptors> 
      <properties> 
       <someVariable>my-value</someVariable> 
      </properties> 
     </configuration> 
    </execution> 
    </executions> 
</plugin> 

這可能嗎?供參考:部分<properties>不起作用,我只是想表明我想做什麼。

我知道我可以在poml.xml中創建屬性並在assembly.xml中使用它們,但它不能解決我的問題,因爲我仍然必須創建10個不同的assembly.xml文件。

This是我找到的最好的建議,但它不是答案。

+0

您可以嘗試在此處查看:https://github.com/khmarbaise/multienv-maven-plugin? – khmarbaise

回答

2

您可以使用iterator-maven-plugin,以遍歷所有不同的屬性值。這個魔咒有iterator目標,這使得遍歷一組給定的屬性,並把它們作爲作爲Maven的屬性:

的迭代器的Maven的插件將注入電流值,這意味着你可以使用一個屬性這個屬性來參數化你的構建。

在你的情況,你可以有:

<plugin> 
    <groupId>com.soebes.maven.plugins</groupId> 
    <artifactId>iterator-maven-plugin</artifactId> 
    <version>0.4</version> 
    <executions> 
    <execution> 
     <phase>package</phase> 
     <goals> 
     <goal>iterator</goal> 
     </goals> 
     <configuration> 
     <items> 
      <item>my-value-1</item> 
      <item>my-value-2</item> 
      <item>my-value-3</item> 
     </items> 
     <pluginExecutors> 
      <pluginExecutor> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-assembly-plugin</artifactId> 
       <version>2.6</version> 
      </plugin> 
      <goal>single</goal> 
      <configuration> 
       <descriptors> 
       <descriptor>${project.basedir}/src/assembly/myCommonAssembly.xml</descriptor> 
       </descriptors> 
      </configuration> 
      </pluginExecutor> 
     </pluginExecutors> 
     </configuration> 
    </execution> 
    </executions> 
</plugin> 

package階段,這樣的配置會遍歷3個給定的值,my-value-1my-value-3,並執行每一次的Maven Assembly插件。對於給定的執行,可以使用${item}檢索當前迭代值。因此,您的常見組裝描述符將變爲:

<assembly> 
    <id>${item}</id> 
    <formats> 
    <format>zip</format> 
    </formats> 
    <fileSets> 
    <!-- Copying DLLs: --> 
    <fileSet> 
     <directory>target/dll/${item}</directory> 
     <outputDirectory>dll</outputDirectory> 
     <includes> 
      <include>*.dll</include> 
     </includes> 
    </fileSet> 
    </fileSets> 
</assembly> 
+0

謝謝,這看起來很有希望。我只有一些API錯誤,我希望它不是由舊的Java 1.3引起的: 無法執行目標com.soebes.maven.plugins:iterator-maven-plugin:0.4:iterator(默認): 執行默認值爲目標com.soebes.maven.plugins:iterator-maven-plugin:0.4:迭代器失敗: 無法在插件'com.soebes.maven.plugins:iterator-maven-plugin:0.4'中加載mojo'iterator'與API不兼容: org.codehaus.plexus.component.repository.exception.ComponentLookupException:com/soebes/maven/plugins/iterator/IteratorMojo: 不受支持的major.minor版本51。0 – Racky

+0

@Racky Ouch。是的Java 1.3是_very_老。我沒有在Java 7或8中運行它的問題。您使用的是哪個版本的Maven?該插件至少需要Maven 3,而Maven 3.3需要至少1.7。 – Tunaki

+0

所以現在它似乎工作。我將iterator-maven-plugin的版本更改爲0.3。所以謝謝。 – Racky

相關問題