2016-09-28 21 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

相关问题