21

I have a project that is composed of two different modules. I do not have control over the pom files of these modules and can therefore not change them. I have full control over the project pom file. Project id not defined as a parent in the modules.

Folder structure:

project
 + module1
 + module2

As part of a work around I need to execute two different goals for both modules, so that module1 gets installed into the local repository, but not module2. This is just an example. My actual problem contains more levels with more than 2 modules per level.

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

I was "dreaming" of something along the lines of

mvn install -Dspecial=module:compile

Is this possible and if yes, how do I do that?

2 Answers 2

36

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

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

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

  • mvn -pl module1,module3 somegoal
  • mvn -pl module2 someothergoal
0
4

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

For 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 running mvn -Pmodule2:ignore-compile package. You will notice that source compilation (but only this goal/execution !) will be 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 with command: mvn -Dmodule2:ignore-compile package

Finally, an interesting capability 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 ignore module2: mvn '-P!with:module2' package

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.