Question

I wrote and tested a custom enforcer rule to do distribution specific builds on various Linux distributions. It tests well with the mvn enforcer:enforce command, with the provided pom.xml build snippet.

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>1.0-beta-1</version>
    <dependencies>
      <dependency>
        <groupId>org.example</groupId>
        <artifactId>maven-enforcer-rule-redhat6x</artifactId>
        <version>1.0</version>
      </dependency>
    </dependencies>
    <configuration>
      <rules>
        <redhat6x implementation="org.example.enforcer.Redhat6x">
          <compatible>true</compatible>
        </redhat6x>
      </rules>
    </configuration>
    <executions>
      <execution>
        <id>enforce</id>
      </execution>
    </executions>
    <goals>
      <goal>enforce</goal>
    </goals>
  </plugin>
</plugins>

After racking my brain and doing a lot of experimental testing, I cannot seem to find how to use this custom enforcer rule as a profile activation selector.

  <profiles>
    <profile>
      <id>RedHat6x</id>
      <activation>
      <!-- Something goes here, but what?  This doesn't work -->
            <redhat6x implementation="com.cisco.tesdw.enforcer.Redhat6x">
              <compatible>true</compatible>
            </redhat6x>
      </activation>
    </profile>
  </profiles>

There are some hints that profile activation uses maven-enforcer-rules as detailed in the Introduction to Profiles page under the section "How can I tell which profiles are in effect during a build" section. Namely, every profile activation which has multiple string values (os name, etc) is referred to the corresponding maven enforcer rule. However, it seems that direct inclusion of a custom profile activator isn't obvious in the pom.xml, and adding such would likely require a pom version update.

Maven3 is also extensible in ways that are very flexible, is it possible to hook inclusion of my enforcer rule by the maven extensions mechanism? There is documentation on how to include custom lifecycle participants; but, I fear that profile activation may have already occurred by the time the build starts. Documentation is sparse, but the javadoc indicates that AbstractMavenLifecycleParticipant.afterProjectsRead(MavenSession session) is called "after all MavenProject instances have been created". This leaves some doubt in my mind whether it is called is before of after profile activation. I suspect after, or how would one get a properly configured MavenProject?

Could someone tell me if profile activation customization is even remotely possible?

Was it helpful?

Solution

You just need to move plugin configuration under profile:

<profiles>
    <profile>
      <id>RedHat6x</id>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-enforcer-plugin</artifactId>
          <version>1.0-beta-1</version>
          <dependencies>
            <dependency>
              <groupId>org.example</groupId>
              <artifactId>maven-enforcer-rule-redhat6x</artifactId>
              <version>1.0</version>
            </dependency>
          </dependencies>
          <configuration>
            <rules>
              <redhat6x implementation="org.example.enforcer.Redhat6x">
                <compatible>true</compatible>
              </redhat6x>
            </rules>
          </configuration>
          <executions>
            <execution>
              <id>redhat-enforce</id>
            </execution>
          </executions>
          <goals>
            <goal>enforce</goal>
          </goals>
        </plugin>
      </plugins>
    </profile>
</profiles>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top