Using maven to build multiple resources assemblies that are very similar

好久不见. 提交于 2019-11-30 14:02:14

Assuming the following structure:

src/
└── main
    ├── assembly
    │   └── iterator.xml
    └── environment
        ├── dev
        │   └── server.properties
        ├── pretest
        │   └── server.properties
        ├── production
        │   └── server.properties
        ├── qa
        │   └── server.properties
        └── test
            └── server.properties

where you can use the following assembly-descriptor like this:

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

using the iterator-maven-plugin will solve the problem:

  <build>
    <plugins>
      <plugin>
        <groupId>com.soebes.maven.plugins</groupId>
        <artifactId>iterator-maven-plugin</artifactId>
        <version>0.2</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>executor</goal>
            </goals>
            <configuration>
              <items>
                <item>dev</item>
                <item>test</item>
                <item>qa</item>
                <item>production</item>
                <item>pretest</item>
              </items>
              <pluginExecutors>
                <pluginExecutor>
                  <goal>single</goal>
                  <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                  </plugin>
                  <configuration>
                    <descriptors>
                      <descriptor>${project.basedir}/src/main/assembly/iterator.xml</descriptor>
                    </descriptors>
                  </configuration>
                </pluginExecutor>
              </pluginExecutors>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

The iterator-maven-plugin is available via maven central. I think you can adapt the above configuration/structure to your problem.

Having messed with a similar problem for a long time, I got rid of tons of XML code, using the following workarounds:

  1. Create the same zip archive for all the environments; resources should be extracted or accessed depending on environment (server's hostname, environment variables, etc.)

  2. Create a single JAVA/Groovy class that will build all the necessary environment-specific assemblies and run it through maven-exec-plugin execution.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!