Maven Build multiple profiles in one go

Prabhjot picture Prabhjot · Feb 8, 2011 · Viewed 22.5k times · Source

It is our policy to only build 1 deployable jar. all environment-specific configurations are kept separate, and we build them all together at once. so under our current Ant process, we have a properties file for each environment, loop over them, and create a set of config files for each environment.

In my current POM XML, I'm able to build only one profile supplied at the Command-line. Is it possible to achieve through Maven?

Here are some of the relevant part of POM.xml

<!-- Define profiles here and make DEV as default profile -->
<profiles>

    <!-- dev Profile -->
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>

    <!-- qa Profile -->
    <profile>
        <id>qa</id>
        <properties>
            <env>qa</env>
        </properties>
    </profile>

    <!-- prod Profile -->
    <profile>
        <id>prod</id>
        <properties>
            <env>prod</env>
        </properties>
    </profile>

</profiles>
...


<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.4.3</version>

    <executions>
        <execution>

            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>

            <configuration>

                <filters>
                    <filter>env/${env}.properties</filter>
                </filters>

                <outputDirectory>${project.build.directory}/config/${env}
                </outputDirectory>
                <resources>
                    <resource>

                        <filtering>true</filtering>

                        <directory>${basedir}/src/main/config/default
                        </directory>
                        <includes>
                            <include>*.xml</include>
                            <include>*.properties</include>
                        </includes>
                    </resource>

.....

Thanks, Prabhjot

Answer

Sean Patrick Floyd picture Sean Patrick Floyd · Feb 8, 2011

Maven is not like ant. With ant, you can basically do what you want when you want to do it. With maven, there is a clear and documented build life cycle, and it's targeted at building one component (and possibly attaching other artifacts to the build).

What you plan to do is however to build one component multiple times, but with different parameters. This does not fit into the maven lifecycle. So what you will need to do is to cause some external process to do the iteration and call maven repeatedly with different parameters.

The classic way to accomplish this would be to use a shell script, but you can also use the Maven Invoker to launch a separate process from a Java or Maven context.