amphibient
amphibient

Reputation: 31308

Using profiles to exclude WAR modules from 'mvn install'

My parent pom defines 7 modules, 5 of which are dependency jars, and two are wars that depend on those jars.

Question: Is it possible to use maven profiles (or another solution) to define which modules are included when mvn install is run against the parent pom to exclude the two war packages?

I would then like to have a different profile (or another solution) to package the two wars. If that profile is run, the dependency jar modules should be rebuilt and installed only if they are missing from the repository.

Upvotes: 1

Views: 961

Answers (1)

A_Di-Matteo
A_Di-Matteo

Reputation: 27862

You could use the build-helper-maven-plugin in your parent pom.xml file to create a new property based on the packaging (at runtime it would change from pom for the parent, to jar and war for the modules). This new propery could then be used to skip the maven-install-plugin dynamically.

A simple example:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.10</version>
    <executions>
        <execution>
            <id>build-helper-regex-is-packaging-war</id>
            <phase>validate</phase>
            <goals>
                <goal>regex-property</goal>
            </goals>
            <configuration>
                <name>only.when.war.is.used</name>
                <value>${project.packaging}</value>
                <regex>war</regex>
                <replacement>true</replacement>
                <failIfNoMatch>false</failIfNoMatch>
            </configuration>
        </execution>
    </executions>
</plugin>

<plugin>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5.2</version>
    <configuration>
        <skip>${only.when.war.is.used}</skip>
    </configuration>
</plugin>

Doing so, the dynamic ${only.when.war.is.used} property would be set to true only when project.packaging would have value war and as such effectively skip the maven-install-plugin executions via its skip option.


You could then move this behavior to a profile and have different settings for jar and war, keeping them in a common place: the root pom.xml, thanks to their dynamic behavior.


Concerning the ability to detect whether an artifact has already been installed or not, there is no such an option on the official plugin documentation and I don't think you could have such a behavior by simply using the plugin.

You could however use the maven profile activation mechanism in case a file is missing (the installed file) and activate the profile accordingly.

You could have in a dynamic way (based only on standard properties) the following approach:

<profiles>
  <profile>
    <activation>
      <file>
        <missing>${settings.localRepository}/${project.groupId}/${project.artifactId}/${project.build.fileName}.${project.packaging}</missing>
      </file>
    </activation>
    ...
  </profile>
</profiles>

Upvotes: 1

Related Questions