1

I want to make a build with Maven within a pom which can be built with two different profiles.

In other words, my task is to force the build in order that the build produces two different builds in the target folder as like I will execute "maven install -P" two times.

So, to be more clear:

  • one single invocation
  • two different results based on the profile associated in that time

I've tried the exec-maven-plugin but I'm not sure if is the correct way, because it's not doing what I desire.

How can I perform that?

Here's the pom I'm trying. The tasks are:

  • copy resources from another project
  • replace content based on the profile
  • Build both profiles.

pom.xml

<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>
    <artifactId>MavenDoubleInstallation</artifactId>

    <profiles>
        <profile>
            <id>profileA</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <env>profileA</env>
                <token>/path/for/profileA</token>
            </properties>
        </profile>
        <profile>
            <id>profileB</id>
            <properties>
                <env>profileB</env>
                <token>/path/for/profileB</token>
            </properties>
        </profile>
    </profiles>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <includeEmptyDirs>true</includeEmptyDirs>
                </configuration>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/classes/${env}/result</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.basedir}/../SourceProject/resources/result</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.google.code.maven-replacer-plugin</groupId>
                <artifactId>replacer</artifactId>
                <version>1.5.3</version>
                <executions>
                    <execution>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>replace</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <basedir>${basedir}/target/classes/${env}/result</basedir>
                    <includes>
                        <include>**/*.sh</include>
                    </includes>
                    <replacements>
                        <replacement>
                            <token>%%TOKEN%%</token>
                            <value>${token}</value>
                        </replacement>
                    </replacements>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <executions>
                    <execution>
                        <id>build-profileA</id>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>maven</executable>
                            <arguments>
                                <argument>-P profileA -X</argument>
                            </arguments>
                        </configuration>
                    </execution>
                    <execution>
                        <id>build-profileB</id>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>maven</executable>
                            <arguments>
                                <argument>-P profileB -X</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>
Alessandro C
  • 3,310
  • 9
  • 46
  • 82

0 Answers0