How to configure Maven to create two versions of an artifact, each for a different target JRE

I have a maven module that I need to use in the J2ME client and on the EJB server. In the client, I need to compile it for target 1.1 and on the server for target 1.6.

I also need to deploy version 1.6 to the Nexus repository, so members working on a server project can include this dependency without downloading the source code.

I read at http://java.dzone.com/articles/maven-profile-best-practices that using profiles is not the best way to do this, but the author did not say that the best way.

Here is my pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>proj-parent</artifactId> <groupId>br.com.comp.proj</groupId> <version>0.0.4-SNAPSHOT</version> </parent> <artifactId>proj-cryptolib</artifactId> <name>proj - Cryto Lib</name> <dependencies> <dependency> <groupId>br.com.comp</groupId> <artifactId>comp-proj-mobile-messages</artifactId> <version>0.0.2-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.3</source> <target>1.1</target> <fork>true</fork> </configuration> </plugin> </plugins> </build> </project> 
+8
java maven target
source share
3 answers

You can configure this through the Maven compiler plugin.

Take a look at the Maven compiler plugin documentation .

You can enable this with various profiles, for example.

If you want to have different target versions, you can simply use the target variable. Something like that:

 <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.3</source> <target>${TARGET_VERSION}</target> <fork>true</fork> </configuration> </plugin> 
+5
source share

As Haleim says, you need to do this in two steps: one for compilation and one for cans.

For the compiler

 <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.5</version> <executions> <execution> <configuration> <source>1.3</source> <target>1.5</target> <fork>true</fork> <outputDirectory>${project.build.outputDirectory}_jdk5</outputDirectory> </configuration> </execution> <execution> <configuration> <source>1.3</source> <target>1.6</target> <fork>true</fork> <outputDirectory>${project.build.outputDirectory}_jdk6</outputDirectory> </configuration> </execution> </executions> </plugin> 

And then for the jar plugin

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.3.1</version> <executions> <execution> <goals> <goal>jar</goal> </goals> <configuration> <classesDirectory>${project.build.outputDirectory}_jdk5</classesDirectory> <classifier>jdk5</classifier> </configuration> </execution> <execution> <goals> <goal>jar</goal> </goals> <configuration> <classesDirectory>${project.build.outputDirectory}_jdk6</classesDirectory> <classifier>jdk6</classifier> </configuration> </execution> </executions> </plugin> 

you can access the bank you want by adding the <classifier> element to your dependency. eg.

 <dependency> <groupId>br.com.comp.proj</groupId> <artifactId>proj-cryptolib</artifactId> <version>0.0.4-SNAPSHOT</version> <classifier>jdk5</classifier> </dependency> 
+3
source share

In addition to my comment on wjans answer as you requested more details.

The following compiler plugin would execute twice to create two different sets of class files identified by the so-called classifier (basically a marker for Maven to know what you're talking about when one project can create multiple artifacts).

Roughly, something like:

 <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.5</version> <executions> <execution> <configuration> <source>1.3</source> <target>1.5</target> <fork>true</fork> <classifier>jdk5</classifier> </configuration> </execution> <execution> <configuration> <source>1.3</source> <target>1.6</target> <fork>true</fork> <classifier>jdk6</classifier> </configuration> </execution> </executions> </plugin> 

Please note that people are sometimes unhappy with using classifier s, as they use profiles , as they may mean that your project must be viewed in several projects or that you are harming your build mobility.

+1
source share

All Articles