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?
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.
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.
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.
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.
(...) 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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With