简体   繁体   中英

generate multiple artifacts in 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>

Then I can use

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

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.

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.

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>

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?

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM