Using maven to build multiple resources assemblies that are very similar

后端 未结 2 822
时光取名叫无心
时光取名叫无心 2021-01-02 17:54

I\'m having a problem with this very redundant maven configuration.

The application I\'m working on sometimes gets built for environments that need alternate resourc

相关标签:
2条回答
  • 2021-01-02 18:23

    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.

    0 讨论(0)
  • 2021-01-02 18:27

    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.

    0 讨论(0)
提交回复
热议问题