Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a different goal for one module of a multi module project?

Tags:

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?

like image 430
Peter Schuetze Avatar asked Nov 05 '10 21:11

Peter Schuetze


People also ask

How do you create a multi-module project?

POM Aggregator (Parent POM) Let's understand the structure of the multi-module application that we have created. Step 1: Create a Maven Project with the name spring-boot-multimodule. Step 2: Open the pom. xml (parent pom) file and change the packaging type jar to pom.

What is the packaging type of an aggregator pom in a multi-module Maven project?

2. Maven's Multi-Module Project. A multi-module project is built from an aggregator POM that manages a group of submodules. In most cases, the aggregator is located in the project's root directory and must have packaging of type pom.

Why do we need multi-module projects?

Advantages of a Multi-Module ProjectIt provides a great ability to build all sub-modules only with a single command. We can run the build command from the parent module. While building the application, the build system takes care of the build order. Deployment of the applications gets very convenient and flexible.

How can I add an existing Maven project to another Maven project as a module?

Just do a regular "Import existing maven project into workspace" to get this done. Show activity on this post. If you use M2e with Eclipse you do not need to do that, because Eclipse resolves dependencies across the workspace. You just need to have the two projects open and your dependencies declared correctly.


2 Answers

(...) 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
like image 149
Pascal Thivent Avatar answered Oct 15 '22 11:10

Pascal Thivent


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

like image 30
LoganMzz Avatar answered Oct 15 '22 12:10

LoganMzz