Building the same project in Maven with various artifacts (based on JDK)

I have a scenario in which my project needs to be compiled in different JDKs, and the resulting artifact name should be different depending on the JDK used. For example, if the project name is MyProject, and I call mvn install, it needs to be compiled in JDK 1.4, as well as JDK 1.5, and finally I get two banks of the same project (MyProjectJDK14-1.0 and MyProjectJDK15-1.0). Can this be achieved?

+20
java maven-2
Jun 22 '10 at 10:07
source share
5 answers

Maven’s way of doing this is not to change the finalName 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 called ${finalName}-${envClassifier}.jar and is 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 will need to call the Maven assembly twice to create both cans (this can be done by a decent CI engine).

+31
Jun 22 '10 at 15:09
source share

What you can do is define two profiles , one per JDK used. Each profile will be activated for which the 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> that will be used to indicate the generated JAR file.

Thus, if you create an application using JDK 1.4, the generated JAR will be called myBuild-jdk1.4.jar .

If your final package is built using assembly, you can simply modify the internal <build> block inside the profiles to customize the assembly plugin (e.g. <finalName> ).




Regarding your comment: indeed, this procedure will require two separate assemblies on Maven, since you need to recompile the entire project when changing the JDK version. One of the conditions of Maven2 is that one project = one artifact. You want to have one project with two artifacts.

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

+6
Jun 22 '10 at 10:26
source share

Use Maven profiles. Add this section to 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 one to learn more about profiles.

+2
Jun 22 '10 at 10:27
source share

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

I decided that this requires different arifactIds, not classifiers.

You can achieve this by setting the property in the settings, and then referring to it 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> 
+1
Jun 04 '14 at 16:40
source share

Actually there is a way to create more than one WAR with one assembly (I think this works for the JAR as well): you can use the assembly plugin with several executions for different descriptors.

0
Dec 29 '10 at 8:44
source share



All Articles