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).


What you can do is to define two profiles, one per JDK used. Each profile will be activated regarding which JDK is used:

<profiles>
    <profile>
        <id>profile-for-jdk1.4</id>
        <activation>
            <activeByDefault>false</activeByDefault>
            <jdk>1.4</jdk>
        </activation>
        <build>
            <finalName>myBuild-jdk1.4</finalName>
        </build>
    </profile>
    <profile>
        <id>profile-for-jdk1.5</id>
        <activation>
            <activeByDefault>false</activeByDefault>
            <jdk>1.5</jdk>
        </activation>
        <build>
            <finalName>myBuild-jdk1.5</finalName>
        </build>
    </profile>
</profiles>

Then, in each profile, you define a specific <finalName>, which will be used to name the generated JAR file.

Thus, if you build your application using JDK 1.4, the generated JAR will be named myBuild-jdk1.4.jar.

If your final package is built using an assembly, you can simply change the <build> block inside profiles to configure the assembly plugin (for example to <finalName>).


Regarding your comment: Indeed, this procedure will need two separate builds on Maven, as you have to recompile the whole project when changing the JDK version. One of the Maven2 convention is that one project = one artifact. What you want is to have one project with two artifacts.

Eventually, one solution is to use Hudson to build your application, and especially the matrix feature of this tool, which allows you to run multiple builds with various parameters, in your case the JDK.


Use Maven Profiles. Add this section inside the project tag of your pom.xml:

<profiles>
  <profile>
    <activation>
      <jdk>1.4</jdk>
    </activation>
    <build>
       <finalName>${project.artifactId}-${project.version}-JDK1.4</finalName>
    </build>
  </profile>
  <profile>
    <activation>
      <jdk>1.5</jdk>
    </activation>
    <build>
       <finalName>${project.artifactId}-${project.version}-JDK1.5</finalName>
    </build>
  </profile>
</profiles>

See this to know more about profiles.


A similar problem is the different variants of the JDBC api used in different versions of the JDK.

I decided that these needed different arifactIds rather than classifiers.

You can achieve this by setting a property in settings and then referencing this in the artifactId tag:

<project>
  <modelVersion>4.0.0</modelVersion>
  <artifactId>throwing-jdbc-${jdbc.version}</artifactId>
  <name>Throwing JDBC</name>
  <profiles>
    <profile>
      <id>jdbc3</id>
      <activation>
        <activeByDefault>false</activeByDefault>
        <jdk>[1.3,1.4,1.5]</jdk>
      </activation>
      <properties>
        <jdbc.version>3.0</jdbc.version>
      </properties>
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.7</version>
            <configuration>
              <sources>
                <source>src/jdbc3-variants/java</source>
              </sources>
            </configuration>
            <executions>
              <execution>
                <goals>
                  <goal>add-source</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>jdbc4</id>
      <activation>
        <activeByDefault>false</activeByDefault>
        <jdk>1.6</jdk>
      </activation>
      <properties>
        <jdbc.version>4.0</jdbc.version>
      </properties>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <source>1.6</source>
              <target>1.6</target>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <configuration>
              <sources>
                <source>src/jdbc4/java</source>
                <source>src/jdbc4-variants/java</source>
              </sources>
            </configuration>
            <executions>
              <execution>
                <goals>
                  <goal>add-source</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>jdbc41</id>
      <activation>
        <activeByDefault>false</activeByDefault>
        <jdk>1.7</jdk>
      </activation>
      <properties>
        <jdbc.version>4.1</jdbc.version>
      </properties>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <source>1.7</source>
              <target>1.7</target>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <configuration>
              <sources>
                <source>src/jdbc4/java</source>
                <source>src/jdbc4.1/java</source>
                <source>src/jdbc4.1-variants/java</source>
              </sources>
            </configuration>
            <executions>
              <execution>
                <goals>
                  <goal>add-source</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <version>2.2.1</version>
        <executions>
          <execution>
            <phase>verify</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>