Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 799
ant Avatar asked Mar 08 '10 11:03

ant


People also ask

How do I activate a single Maven profile?

Profiles can be activated in the Maven settings, via the <activeProfiles> section. This section takes a list of <activeProfile> elements, each containing a profile-id inside. Profiles listed in the <activeProfiles> tag would be activated by default every time a project use it.

What are Maven build profiles?

A profile in Maven is an alternative set of configuration values which set or override default values. Using a profile, you can customize a build for different environments. Profiles are configured in the pom. xml and are given an identifier.


1 Answers

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:

  1. 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).
  2. 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
like image 106
Pascal Thivent Avatar answered Sep 24 '22 19:09

Pascal Thivent