在Maven中生成多个工件 [英] generate multiple artifacts in maven

查看:105
本文介绍了在Maven中生成多个工件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须为Maven构建的单个应用程序创建多个软件包.最初,我使用-Denv通过Maven参数生成不同的包,然后使用资源包括/排除资源:

I have to create multiple packages for a single application built by Maven. At first I use the -Denv to generate different packages by Maven arguments and I use the resource to include/exclude the resources:

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.properties</include>
            <include>**/${env}.ini></include>
        </includes>
        <filtering>true</filtering>
    </resource>
</resources>

然后我可以使用

mvn clean package -Denv=dev
mvn clean package -Denv=prod1
mvn clean package -Denv=prod2
mvn clean package -Denv=prod3
.....

但是,似乎必须将不同的软件包一一打包,这意味着Maven将花费太多时间来构建所有软件包,因为我们有近22种不同的环境来部署应用程序.

However it seems that different packages have to be packaged one by one, which means Maven will spent too much time to build all the packages since we have almost 22 different environments to deploy the application.

此外,每个mvn .. package几乎都会产生与类,资源相同的结果,除了某些特定资源(例如{$env}.ini)外,我认为一次又一次地运行compile是浪费的.

Furthermore, each mvn .. package almost generate the same result like classes,resources except some specified resources like {$env}.ini, I think it is a waste to run the compile again and again.

最好将所有源代码和资源编译到目标目录,然后相应地生成不同的包,所以我认为maven-assembly-plugin,通常我们会这样使用它:

It would be better if we compile all the sources, and resources to the target directory, then generate different packages accordingly, so I thought the maven-assembly-plugin, generally we will use it like this:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <descriptors>
                <descriptor>/src/main/assembly/env.xml</descriptor>
                <descriptor>/src/main/assembly/prod1.xml</descriptor>
                <descriptor>/src/main/assembly/prod2.xml</descriptor>
                ......
            </descriptors>
        </configuration>
</plugin>

然后我们必须创建22个描述符,但是这些描述符彼此之间太相似,这意味着重复的配置太多了,一旦我想对所有描述符进行一些一般性的更改,这将很困难.

Then we have to create 22 descriptors, however these descriptors are too similar with each other, which means there are too many repeated configuration, and it will be difficult once I want to make some general change to all the descriptors.

所以我想知道是否有一个很好的选择来解决这种要求?

So I wonder if there is a good alternative to solve this kind of requirements?

推荐答案

您可以使用 iterator-maven-plugin 解决此类问题:

You can use the iterator-maven-plugin to solve this problem like this:

 <plugin>
    <groupId>com.soebes.maven.plugins</groupId>
    <artifactId>iterator-maven-plugin</artifactId>
    <version>0.3</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>iterator</goal>
        </goals>
        <configuration>
          <items>
            <item>test</item>
            <item>prod</item>
            <item>dev</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}/@item@.xml</descriptor>
                </descriptors>
              </configuration>
            </pluginExecutor>
          </pluginExecutors>
        </configuration>
      </execution>
    </executions>
  </plugin>

如果您使用这样的描述符:${project.basedir}/@item@.xml名称将被每个迭代步骤替换,因此您可以有多个描述符...但是在您的情况下,我假设您拥有非常相似的描述符,因此您拥有相同的描述符没有占位符的描述符仅包含细微的差异,可以在描述符本身中解决以下问题:

If you use the descriptor like this: ${project.basedir}/@item@.xml the name will be replaced by every single iteration step so you can have several descriptors...but in your case i assume that you have very similar descriptors so you have the same descriptors without a placeholder which contains only slight differences which can be solve like this in the descriptor itself:

<assembly 
  xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">

  <id>${item}</id>
  <formats>
    <format>war</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <unpack>true</unpack>
      <useProjectArtifact>true</useProjectArtifact>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <outputDirectory>WEB-INF</outputDirectory>
      <directory>${basedir}/src/main/environment/${item}/</directory>
      <includes>
        <include>**</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>

因此,您只需要一个描述符.如果您有任何问题,请创建问题请求.

as a result you need only a single descriptor. If you have any issues please create issue requests.

这篇关于在Maven中生成多个工件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆