maven - disable dedicated goals for modules in multi-module build - java

I use a multi module build file to trigger the build of a rather complex java project. Now I want to create the eclipse project files - but not for all modules. Is it possible to define on a pom, that it shall ignore the eclipse:eclipse goal?
The modules definition of the multi-module pom, that I call with mvn eclipse:eclipse looks like that:
<modules>
<module>../project1/generate/pom.xml</module>
<module>../project1/pom.xml</module>
<module>../project2/generate/pom.xml</module>
<module>../project2/pom.xml</module>
<!-- and many more projects with or without generate sub modules -->
</modules>
Of course the pom is not only needed for creating eclipse projects so I can't just remove the modules that cause trouble with that goal.
The reason for asking: eclipse:eclipse executed on the generate modules will change some attributes in manifest files with the effect that the next build breaks. And I'm tired of correcting them manually all the time ;)

You can use Maven Profiles for this.
Remove your defined modules and add something like this:
<profiles>
<profile>
<id>full</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>../project1/generate/pom.xml</module>
<module>../project1/pom.xml</module>
<module>../project2/generate/pom.xml</module>
<module>../project2/pom.xml</module>
</modules>
</profile>
<profile>
<id>eclipse</id>
<modules>
<module>../project1/pom.xml</module>
<module>../project2/pom.xml</module>
</modules>
</profile>
</profiles>
Then when you want to generate eclipse settings:
mvn -Peclipse eclipse:eclipse

You can use Maven Advanced Reactor Options (since Maven 2.1):
For instance, to target only project1 and project2:
mvn --projects project1, project2 eclipse:eclipse
to target project1 and all its dependencies:
mvn --projects project1 --also-make eclipse:eclipse
This provides a standardised way for reactoring multi-mudule projects and does not requires modification in pom.xml.

Related

IntelliJ resolve dependencies of profile

I have a Java project with multiple modules. In this project, one module has a dependency to another one, which is only needed for a specific profile and hence is defined like that:
<profile>
<id>myProfile</id>
<dependencies>
<dependency>
<groupId>MyGroupId</groupId>
<!-- ... -->
</dependency>
</dependencies>
<!-- ... -->
</profile>
This works fine when building manually with maven like that:
mvn clean install -P myProfile
When using the IntelliJ build however, the dependency doesn't get resolved.
I've tried the option to delegate IDE build/run actions to maven, adding a property for maven in Build, Execution, Deployment > Build Tools > Maven > Runner (namely -P -> myProfile), and much more which is most likely not of interest.
Is it possible to configure IntelliJ to resolve the dependencies for a specific profile?
To help Intellij Idea to understand about your maven profile and maven object mode, you set as default profile in maven so that by default it will be recognized and run by any IDE. I provide below the code snippet.
<profile>
<id>firstProfile</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
..... Other code goes
</profile>
So inside profile, use this <activeByDefault>true</activeByDefault>. It may solve the problem.

Maven package:mvn package

I have a maven project, but the father and son structure, I would like to perform mvn package in the parent structure at the same time want to specify the sub-project pom.xml file profile an id
You can define the sub-project module in your parent pom file profile as below:
<profiles>
<profile>
<id>all-deps</id>
<modules>
<module>sub-project</module>
</modules>
</profile>
</profiles>
Now you can use following maven commands to include the sub-project from parent
mvn package --activate-profiles all-deps

Mulit-module application maven

I've got multi module maven project, where main project depend on sub-module. Every dependency of sub-module is define by version like this: ${pom.version}. I use maven release plug-in. If I try to prepare release, I've got an error about missing version of sub-module.
Example:
main pom is on version 1.0, I try to release it. Maven build every sub-module to version 1.1, then try to build parent, and then crash. Because it can't find sub-module-1.1.
I don't know how to tell maven to build, and immediate install to local-repo every sub-module witch it build. I use maven2.
My pom:
<modelVersion>4.0.0</modelVersion>
<groupId>com.voncuver</groupId>
<artifactId>voncuver</artifactId>
<packaging>pom</packaging>
<version>1.1-SNAPSHOT</version>
<name>multimodule</name>
<modules>
<module>mod1</module>
<module>mod2</module>
</modules>
(...)
<dependencyManagement>
<dependencies>
<dependency>
<artifactId>mod1</artifactId>
<groupId>com.voncuver</groupId>
<version>${pom.version}</version>
</dependency>
<dependency>
<artifactId>mod2</artifactId>
<groupId>com.voncuver</groupId>
<version>${pom.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
(...)
You should probably post a bit more of your project structure, but typically a multimodule project looks like this:
project
mod1
mod2
mod3
pom.xml
The main pom.xml would have "pom" packaging type, and have a section in it to build everything else:
<packaging>pom</packaging>
<modules>
<module>mod1</module>
<module>mod2</module>
<module>mod3</module>
</modules>
Then, the surest way to make sure things build properly is to execute:
mvn clean install
Without the "install", it's highly possible that things might not be found in the maven reactor, especially depending on what version of maven you are using (and a few other factors).

Maven Aggregate POM with Goal?

I have a Maven POM that aggregates several modules.
<project [stuff]>
<modelVersion>4.0.0</modelVersion>
<groupId>com.fuhu.osg</groupId>
<artifactId>UserManagement</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<name>UserManagement</name>
<modules>
<module>core</module>
<module>war</module>
<module>ejbs</module>
<module>ear</module>
</modules>
</project>
I want to execute a goal that doesn't apply to the modules against the top-level POM. Something like mvn db-migrate:create. As is, it seems like this attempts to run said command against the sub-projects, which is correct for every OTHER goal, but not for this one.
Is there a way to make a Maven POM that is both an Aggregate for some goals and an ordinary project for others?
You might be helped by Maven build profiles. It's easy to configure one submodule to be invoked when using a certain profile.
http://maven.apache.org/guides/introduction/introduction-to-profiles.html
...
<profiles>
<profile>
<id>db</id>
<modules>
<module>core</module>
</modules>
</profile>
<profile>
<id>all</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>core</module>
<module>war</module>
<module>ejbs</module>
<module>ear</module>
</modules>
</profile>
...
Start your db task with the db profile with something like:
$ mvn -Pdb db-migrate:create
Auto activation of profiles is possible using system environment etc. Sadly I can't find a maven property for the command line goal, which would enable auto activation of a profile when that specific goal is run.

Maven profile for single module

I have a multi module maven project, which builds successfully, I'd like to build just one of the modules I have. How would I do that with profiles ? I could do it from console in two ways, one way is go to the child module and mvn package or I could use reactor to build just one module.
Can I do the same thing with profiles? By modifying POM? Thank you
EDIT
If is impossible from POM, can I do it from settings.xml ?
To implement this with profiles, you could use two profiles, one <activeByDefault> with all modules and another one with the wanted module only. Something like this:
<profiles>
<profile>
<id>all</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>module-1</module>
...
<module>module-n</module>
</modules>
</profile>
<profile>
<id>module-2</id>
<modules>
<module>module-2</module>
</modules>
</profile>
<profiles>
And then call it like this:
mvn -Pmodule-2 package
Two things to note here:
You need to move the <modules> from the POM in a "default" profile (because <modules> from a profile are only additive, they do not hide the modules declared in the POM).
By marking it as <activeByDefault>, the "default" profile will be picked if nothing else is active but deactivated if something else is.
One could even parametrize the name of the module and pass it as property:
<profiles>
...
<profile>
<id>module-x</id>
<activation>
<property>
<name>module-name</name>
</property>
</activation>
<modules>
<module>${module-name}</module>
</modules>
</profile>
<profiles>
And invoke maven like this:
mvn -Dmodule-name=module-2 package
But this is a poor implementation IMHO, I prefer the -pl "advanced" reactor options (less xml, much more power and flexibility):
mvn -pl module-2 package
To overcome additivity nature of maven default <modules> working with <profiles>, you can use reactor with particular profile, e.g.:
mvn -pl module-2 -Pprofile-name package
This will package module-2 defined in profile-name and not in default profile.

Categories

Resources