Building same project in Maven with different artifactid (based on JDK used)

The Maven way to do this is not to change the finalName of the artifact but to use a classifier. For example:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <classifier>${envClassifier}</classifier>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
  <profiles>
    <profile>
      <id>jdk16</id>
      <activation>
        <jdk>1.6</jdk>
      </activation>
      <properties>
        <envClassifier>jdk16</envClassifier>
      </properties>
    </profile>
    <profile>
      <id>jdk15</id>
      <activation>
        <jdk>1.5</jdk>
      </activation>
      <properties>
        <envClassifier>jdk15</envClassifier>
      </properties>
    </profile>
  </profiles>
</project>

The JAR artifact will be named ${finalName}-${envClassifier}.jar and included as a dependency using the following syntax:

<dependency>
  <groupId>com.mycompany</groupId>
  <artifactId>my-project</artifactId>
  <version>1.0</version>
  <classifier>jdk16</classifier>
</dependency>

You’ll have to call the Maven build twice to produce both jars (a decent CI engine can do that).

Leave a Comment