Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

maven

target

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

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

I've read at http://java.dzone.com/articles/maven-profile-best-practices that using profiles is not the best way of doing this, but the author didn't say what's 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>
like image 975
Alex Oliveira Avatar asked Jun 06 '12 18:06

Alex Oliveira


People also ask

How do I customize a Maven build for different environments?

A Build profile is a set of configuration values, which can be used to set or override default values of Maven build. Using a build profile, you can customize build for different environments such as Production v/s Development environments. Profiles are specified in pom.

Which two classes of artifacts does Maven define?

Examples of artifacts produced by Maven for a project include: JARs, source and binary distributions, WARs. Each artifact is identified by a group id, an artifact ID, a version, an extension and a classifier (extension+classifier may be named by a type).

What is groupId and artifactId in Maven project example?

groupId – a unique base name of the company or group that created the project. artifactId – a unique name of the project. version – a version of the project. packaging – a packaging method (e.g. WAR/JAR/ZIP)

What is the default package type for a Maven project build artifact?

2. Default Packaging Types. Maven offers many default packaging types that include a jar, war, ear, pom, rar, ejb, and maven-plugin.


2 Answers

As Haylem suggests thought you'll need to do it in two steps, one for the compile and one for the jars.

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 then refer to the required jar by adding a <classifier> element to your dependency. e.g.

<dependency>
    <groupId>br.com.comp.proj</groupId>
    <artifactId>proj-cryptolib</artifactId>
    <version>0.0.4-SNAPSHOT</version>
    <classifier>jdk5</classifier>
</dependency>
like image 84
user2623688 Avatar answered Nov 03 '22 01:11

user2623688


You can configure this via the Maven compiler plugin.

Take a look at the Maven compiler plugin documentation.

You could enable this via different profiles for instance.

If you only want to have different target versions you could simply use a variable target. Something like this:

<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>
like image 25
wjans Avatar answered Nov 03 '22 01:11

wjans