Using maven to build multiple resource assemblies that are very similar

I am having a problem with this very redundant maven configuration.

The application I'm working on is sometimes created for environments that need alternative resource packages. The current way to prepare resources for deployment is to add them to a single .zip file. The build wizard then deploys the compressed resources to the server. For some environments, alternative versions of some resources must be used. The folder structure in the project is as follows:

  src / main / resources
   - resource1
   - resource2
   - resource2
   ENV1 /
     -resource1 (environment-specific)
   ENV2 /
     -resource1 (environment-specific)
     -resource3 (environment-specific)

In most environments, the resource zip file should contain the files resource1 , resource2 , resource3 . For ENV1 zip file must contain ENV1/resource1 , resource2 , resource3 . For ENV3 zip file requires ENV2/resource1 , resource2 , ENV2/resource3 .

I am currently doing this with an assembly descriptor for each environment and a separate execution in my POM file:

  <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <executions> <execution> <id>zip-normal-resources</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <descriptor>assembly-descriptor.xml</descriptor> </configuration> </execution> <execution> <id>zip-ENV1-resources</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <descriptor>assembly-descriptor-ENV1.xml</descriptor> </configuration> </execution> 

And each descriptor looks very similar. "Normal" assembly descriptor:

 <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>Resources</id> <includeBaseDirectory>false</includeBaseDirectory> <formats> <format>zip</format> </formats> <fileSets> <fileSet> <directory>src/main/resources</directory> <includes> <include>*.properties</include> <include>*.xml</include> </includes> <outputDirectory>/</outputDirectory> <filtered>true</filtered> </fileSet> <fileSet> <directory>${project.build.outputDirectory}/resources</directory> <outputDirectory>/</outputDirectory> <includes> <include>**/*</include> </includes> </fileSet> </fileSets> </assembly> 

Environment specific descriptor:

 <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>Resources-ENV1</id> <includeBaseDirectory>false</includeBaseDirectory> <formats> <format>zip</format> </formats> <fileSets> <fileSet> <directory>src/main/resources</directory> <includes> <include>*.properties</include> <include>*.xml</include> </includes> <!-- exclude environment-specific files --> <excludes> <exclude>resource1</exclude> </excludes> <outputDirectory>/</outputDirectory> <filtered>true</filtered> </fileSet> <fileSet> <directory>src/main/resources/ENV1</directory> <includes> <include>*.properties</include> <include>*.xml</include> </includes> <outputDirectory>/</outputDirectory> <filtered>true</filtered> </fileSet> <fileSet> <directory>${project.build.outputDirectory}/resources</directory> <outputDirectory>/</outputDirectory> <includes> <include>**/*</include> </includes> </fileSet> </fileSets> </assembly> 

This solution works, but it is very inefficient. I do not like to have several assemblies for each environment that are almost identical, with the exception of the environment names. Is there any way to simplify this?

+7
maven maven-assembly-plugin
source share
2 answers

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 as follows:

 <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 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> 

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

+10
source share

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

  • Create the same zip archive for all environments; resources must be retrieved or available depending on the environment (server host name, environment variables, etc.).

  • Create one JAVA / Groovy class that will build all the necessary assemblies for the environment and run it through the maven-exec-plugin.

+2
source share

All Articles