[ACCEPTED]-How to execute a different goal for one module of a multi module project?-maven-2

Accepted answer
Score: 32

(...) To minimize the configuration necessary 9 for my CI system, I want to run this in 8 one maven call.

I'm afraid this won't be 7 possible. As I explained in this answer, the phase 6 or goal invoked as part of a multi modules 5 build is run against all modules.

If you 4 want to run a different phase or goal for 3 a subset of the sub-modules, you'll have 2 to call maven twice, maybe using the --projects, -pl options 1 to pick the right subset:

  • mvn -pl module1,module3 somegoal
  • mvn -pl module2 someothergoal
Score: 4

The only way to control which goals/executions (not phases) are 9 executed during a Maven build whatever your 8 project has module or not, is through profiles.

For 7 example :

/pom.xml

<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.company</groupId>
    <artifactId>parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>module1</module>
        <module>module2</module>
    </modules>
</project>

/module1/pom.xml

<project>
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.company</groupId>
        <artifactId>parent</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <artifactId>module1</artifactId>
</project>

/module2/pom.xml

<project>
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.company</groupId>
        <artifactId>parent</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <artifactId>module2</artifactId>

    <profiles>
        <profile>
            <id>module2:ignore-compile</id>

            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>default-compile</id>
                                <phase>none</phase>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

When 6 running mvn -Pmodule2:ignore-compile package. You will notice that source compilation 5 (but only this goal/execution !) will be 4 ignored for module2.

You can also use activation:

<profiles>
    <profile>
        <id>module2:ignore-compile</id>

        <activation>
            <property>
                <name>module2:ignore-compile</name>
            </property>
        </activation>
        ....
    </profile>
</profiles>

Then 3 with command: mvn -Dmodule2:ignore-compile package

Finally, an interesting capability 2 is to change module through a profile:

/pom.xml

<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.company</groupId>
    <artifactId>parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>module1</module>
    </modules>

    <profiles>
        <profile>
            <id>with:module2</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>

            <modules>
                <module>module2</module>
            </modules>
        </profile>
    </profiles>
</project>

To 1 ignore module2: mvn '-P!with:module2' package

More Related questions