Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Build multiple profiles in one go

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

like image 436
Prabhjot Avatar asked Feb 08 '11 12:02

Prabhjot


People also ask

How do Maven profiles work?

A profile in Maven is an alternative set of configuration values which set or override default values. Using a profile, you can customize a build for different environments. Profiles are configured in the pom. xml and are given an identifier.


1 Answers

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.

like image 50
Sean Patrick Floyd Avatar answered Sep 28 '22 05:09

Sean Patrick Floyd