Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven - Is it possible to build a project specifying multiple profiles simultaneously?

Tags:

maven

maven-3

We are developing a Maven archetype for applications that only consume web services. This archetype offers three profiles, one for each environment (dev, pre, pro).

The point is that we would like to offer the possibility of having ORM dependencies included optionally (JPA, Hibernate) for those project that may require them in the future. We had though of creating an additional profile containing those dependencies.

When we build our project we use mvn package -Denvironment=dev. Is it possible to specify more than one profile such as: mvn package -Denvironment=dev,orm?

like image 446
codependent Avatar asked Mar 14 '23 02:03

codependent


1 Answers

Yes, this is possible. But it seems you are confused about how profiles are activated in the first place.

The command

mvn package -Denvironment=dev

will not activate any profile without further configuration. In your case, it works because there must be a profile definition in your POM that is activated by the presence of the system property environment with a value of dev. The configuration you have would look like:

<profiles>
  <profile>
    <activation>
      <property>
        <name>environment</name>
        <value>dev</value>
      </property>
    </activation>
  </profile>
</profiles>

This is the magic that makes the profile activates when you pass the system property with -Denvironment. With that in mind, you can activate multiple profiles with the same idea: declare multiple <profile> element that are activated by the presence of a system property.

<profiles>
  <profile>
    <activation>
      <property>
        <name>myAwesomeProperty1</name>
        <value>true</value>
      </property>
    </activation>
  </profile>
  <profile>
    <activation>
      <property>
        <name>myAwesomeProperty2</name>
        <value>true</value>
      </property>
    </activation>
  </profile>
</profiles>

The above configuration would activate both profile if myAwesomeProperty1 and myAwesomeProperty2 is a system property with the value true.

In this particular case though, it seems that what you want is to activate a build depending on your environment so it could perhaps be a better idea to activate the profiles based on the -P command line switch, instead of a system property.

From Introduction to Build Profiles:

Profiles can be explicitly specified using the -P CLI option.

This option takes an argument that is a comma-delimited list of profile-ids to use. When this option is specified, the profile(s) specified in the option argument will be activated in addition to any profiles which are activated by their activation configuration or the <activeProfiles> section in settings.xml.

mvn groupId:artifactId:goal -P profile-1,profile-2

With this solution, you invoke Maven with multiple profile ids. That is to say, if you have in your configuration

<profiles>
  <profile>
    <id>profile-1</id>
    <!-- rest of config -->
  </profile>
  <profile>
    <id>profile-2</id>
    <!-- rest of config -->
  </profile>
</profiles>

The above invocation would activate both profile-1 and profile-2.

like image 117
Tunaki Avatar answered Apr 26 '23 17:04

Tunaki