2

I have a project with one main module that use pom packaging and several submodules that are defined to use jar packaging. I want to have some of them ready to run on JDK7 and other for JDK8 (and eventually embed those for JDK7 as dependencies in the JDK8 counterpart).

The goal is to release the submodules as independent jars (e.g. on Maven Central)

Is this approach correct? Any drawback?

cb4
  • 6,689
  • 7
  • 45
  • 57
mat_boy
  • 12,998
  • 22
  • 72
  • 116

2 Answers2

4

You can use the toolchains plugin:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-toolchains-plugin</artifactId>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>toolchain</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <toolchains>
                    <jdk>
                        <version>${java.min.version}</version>
                        <vendor>oracle</vendor>
                    </jdk>
                </toolchains>
            </configuration>
        </plugin>

You need to have a toolchains file which configures the toolchains you want to use: https://maven.apache.org/plugins/maven-toolchains-plugin/toolchains/jdk.html

You also need to specify the java version in the compiler plugin etc.

Puce
  • 37,247
  • 13
  • 80
  • 152
1

There is absolutely nothing against doing that. However, if you have a module that needs compiling with Java 8, you need to make sure Maven is also launched with Java 8.

Then, you need to override the configuration of the maven-compiler-plugin for each module and define the correct Java version (<source> and <target> properties).

After that, you can use a Java 7 dependency in a Java 8 project without any problems.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • I guess that the parent pom must use JDK8, in any case. Right? – mat_boy Dec 14 '15 at 14:13
  • @mat_boy No, the parent POM is of `pom` packaging: so there is nothing to compile. – Tunaki Dec 14 '15 at 14:13
  • This doesn't prevent you from using APIs only available in Java 8 in a Java 7 module, though. See cross-compilation: http://docs.oracle.com/javase/8/docs/technotes/tools/unix/javac.html#BHCIJIEG – Puce Dec 14 '15 at 14:24