Search code examples
javamavenmaven-assembly-plugin

Creating different "distributions" of an project


I am switching (or at least trying to) from Ant to Maven. I am building a complex project, resulting in one WAR file, expanded into a single folder.

Then, in Ant I am creating 10 different "distributions", which means I copy some propertie files, CSS and HTML files from a "config/" directory to the folder, create a and repeat the step with the next "config/" folder.

In the end, I have 10 ZIP files with customized ressources for each customer.

The and code looks like that (might not be the best ant code, but works perfectly):

<!-- Distributionen -->
<target name="distribution-customer1" depends="jar-with-dependencies">
    <property name="dirname" value="customer1" />
    <antcall target="distribution">
        <param name="dirname" value="${dirname}" />
    </antcall>
</target>
<target name="distribution-customer2" depends="jar-with-dependencies">
    <property name="dirname" value="customer2" />
    <antcall target="distribution">
        <param name="dirname" value="${dirname}" />
    </antcall>
</target>
<target name="distribution-customer3">
    <property name="dirname" value="customer3" />
    <antcall target="distribution">
        <param name="dirname" value="${dirname}" />
    </antcall>
    <!-- Startdateien mit Port 8080 statt Port 80 -->
    <copy todir="${root.dir}/distribution/${dirname}/" overwrite="yes">
        <fileset dir="${root.dir}/configs/${dirname}/" includes="myproject_starten**" />
    </copy>
    <!-- Nachdem wir Dateien geändert haben, nochmals WAR und ZIP generieren -->
    <antcall target="create_war_and_zip">
        <param name="dirname" value="${dirname}" />
    </antcall>
</target>
<!-- /Distributionen -->

<!-- Über Antcall aufrufen, nicht direkt! -->
<target name="distribution" depends="jar-with-dependencies">

    <!-- Altes Verzeichnis löschen -->
    <delete dir="${root.dir}/distribution/${dirname}/" />
    <!-- Neu anlegen -->
    <mkdir dir="${root.dir}/distribution/${dirname}/" />
    <!-- Alles vom Template rüber kopieren -->
    <copy todir="${root.dir}/distribution/${dirname}/" overwrite="yes">
        <fileset dir="${root.dir}/distribution/myproject_template/">
        </fileset>
    </copy>
    <!-- Service.ini mit der Version aus dem individuellen Config-Verzeichnis 
        überschreiben -->
    <copy todir="${root.dir}/distribution/${dirname}" overwrite="yes">
        <fileset dir="${root.dir}/configs/${dirname}/">
            <include name="*.ini" />
        </fileset>
    </copy>
    <!-- Alte Propertydateien löschen -->
    <copy todir="${root.dir}/distribution/${dirname}/myproject/WEB-INF/classes" overwrite="yes">
        <fileset dir="${root.dir}/configs/${dirname}/">
            <include name="*.properties" />
        </fileset>
    </copy>
    <!-- ggf. Log4J Config überschreiben -->
    <copy todir="${root.dir}/distribution/${dirname}/myproject/WEB-INF/classes" overwrite="yes">
        <fileset dir="${root.dir}/configs/${dirname}/">
            <include name="*.xml" />
        </fileset>
    </copy>
    <!-- Custom CSS kopieren -->
    <copy todir="${root.dir}/distribution/${dirname}/myproject/assets/css" overwrite="yes">
        <fileset dir="${root.dir}/configs/${dirname}/assets/css/">
            <include name="*.css" />
        </fileset>
    </copy>

    <!-- Custom Images kopieren -->
    <copy todir="${root.dir}/distribution/${dirname}/myproject/assets/images" overwrite="yes">
        <fileset dir="${root.dir}/configs/${dirname}/assets/images/">
            <include name="*.png" />
        </fileset>
    </copy>

    <!-- Daten kopieren (Falls vorhanden, für Demo)-->
    <copy todir="${root.dir}/distribution/${dirname}/data" overwrite="yes" failonerror="false">
        <fileset dir="${root.dir}/configs/${dirname}/data/"></fileset>
    </copy>

    <copy todir="${root.dir}/distribution/${dirname}/myproject/bilder" overwrite="yes" failonerror="false">
        <fileset dir="${root.dir}/configs/${dirname}/bilder/"></fileset>
    </copy>

    <delete file="${root.dir}/distribution/${dirname}/readme.internal.md" />

    <antcall target="create_war_and_zip">
        <param name="dirname" value="${dirname}" />
    </antcall>

</target>

<target name="copy-to-distribution" depends="jar-with-dependencies">
    <delete dir="${root.dir}/distribution/myproject_template/myproject/" />
    <mkdir dir="${root.dir}/distribution/myproject_template/myproject/" />
    <mkdir dir="${root.dir}/distribution/myproject_template/myproject/bilder" />
    <mkdir
        dir="${root.dir}/distribution/myproject_template/myproject/bilder/details" />
    <mkdir
        dir="${root.dir}/distribution/myproject_template/myproject/bilder/originals" />
    <mkdir
        dir="${root.dir}/distribution/myproject_template/myproject/bilder/thumbnails" />
    <copy todir="${root.dir}/distribution/myproject_template/myproject/">
        <fileset
            dir="${build.dir}/dist/${ant.project.name}-with-dependencies-jar/" />
    </copy>
</target>

How can I do something like that with maven? Should I use profiles? Should I even use maven for that task?

Any help is welcome!

Thank you, schube


Solution

  • You need to use war-overlays here is an example:

    A parent pom, to hold all the child projects together:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.greg</groupId>
      <artifactId>war-overlay-example</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>pom</packaging>
    
      <modules>
        <module>base-war</module>
        <module>dist1-war</module>
      </modules>
    
    </project>
    

    A base war that project, for any common stuff:

    <?xml version="1.0"?>
    <project
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
        xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>com.greg</groupId>
            <artifactId>war-overlay-example</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
    
        <artifactId>base-war</artifactId>
        <packaging>war</packaging>
    
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <finalName>base-war</finalName>
        </build>
    
    </project>
    

    And as many distribution wars to change anything in the base-war. Anything included in this project will replace anything in the base-war. You can have nothing and get the full base-war or insert individual files.

    <?xml version="1.0"?>
    <project
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
        xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>com.greg</groupId>
            <artifactId>war-overlay-example</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
    
        <artifactId>dist1-war</artifactId>
        <packaging>war</packaging>
    
        <dependencies>
            <dependency>
                <groupId>com.greg</groupId>
                <artifactId>base-war</artifactId>
                <version>${project.version}</version>
                <type>war</type>
                <scope>runtime</scope>
            </dependency>
        </dependencies>
    
        <build>
            <finalName>dist1-war</finalName>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.1.1</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                        <overlays>
                            <overlay>
                                <groupId>com.greg</groupId>
                                <artifactId>base-war</artifactId>
                            </overlay>
                        </overlays>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    

    Working example here