1

I need to change the classpath value used by maven-compiler-plugin to compile tests and i can't find any way to do it...

I've read :

But these solutions are not good for me.

More precisely, we're building client jar to use this API and we need to build this client twice :

  • One with source & target to 1.8 for new client
  • One with source & target to 1.7 for client still using JDK7

Here's the configuration of the maven-compiler-plugin into the pluginManagement of my parent POM :

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <includes>
            <include>**/*.java</include>
        </includes>
        <target>1.8</target>
        <source>1.8</source>
        <fork>true</fork>
        <encoding>UTF-8</encoding>
    </configuration>
    <executions>
        <execution>
            <id>jdk7</id>
            <phase>none</phase>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <fork>true</fork>
                <outputDirectory>${project.build.directory}/classes/jdk7/</outputDirectory>
            </configuration>
        </execution>
        <execution>
            <id>jdk8</id>
            <phase>none</phase>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <fork>true</fork>
                <outputDirectory>${project.build.directory}/classes/jdk8/</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

The phase is set to "none" to disable these executions by default and they are executed only for the client.

This is the configuration of my client POM :

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <executions>
        <execution>
            <id>jdk7</id>
            <phase>compile</phase>
        </execution>
        <execution>
            <id>jdk8</id>
            <phase>compile</phase>
        </execution>
        <execution>
            <id>default-compile</id>
            <phase>none</phase>
        </execution>
    </executions>
</plugin>

As you can see, i've disabled the execution "default-compile", because it's compiling a 3rd times the source directly in the target/classes folder and it's quite dirty in my case. I only want JDK7 classes in target/classes/jdk7 and JDK8 classes in target/classes/jdk8.

The problem is that the "default-testCompile" execution of maven-compiler-plugin uses for classpath is :

  • target/test-classes
  • target/classes
  • All dependencies jars

The compilation fails because maven-compiler-plugin is not looking for class files in the folder target/classes/jdk8.

I've tried to add compilerArguments & compilerArgs to the configuration but it didn't work...

How can i add "target/classes/jdk8" or change the "target/classes" to "target/classes/jdk8" to the classpath used by default-testCompile ?

Note : I know that if i don't disable the "default-compile", it will work because the classes will be in "target/classes" but i don't want to compile 3 times the sources...

Community
  • 1
  • 1
Matthieu Saleta
  • 1,388
  • 1
  • 11
  • 17
  • Are the classes really generated under `target/classes/jdk7` (resp. `jdk8`)? `maven-compiler-plugin` does not have an `` option. This is normally configured with the `` attribute. – Tunaki Jun 16 '16 at 15:00
  • Indeed this option doesn't not exist but i've found it on another post and it works well. I've tried to add the property "-d" in compilerArgs but it was not working. – Matthieu Saleta Jun 16 '16 at 15:11
  • Hmm. Are you using a profile to switch from JDK? If so, can you post that config? – Tunaki Jun 16 '16 at 15:18
  • No i don't. I misspoke about JDK7 and JDK8. I compile with JDK8 but i want to construct a jar compliant with JDK7 (source & target to 1.7) and an other compliant with JDK8 (source & target to 1.8) in the same maven build. – Matthieu Saleta Jun 16 '16 at 15:33
  • @MatthieuSaleta when performing cross-compilation like in this case (using JDK 8 and target 1.7) please read carefully [this Stack Overflow thread](http://stackoverflow.com/q/35913775/5606016) – A_Di-Matteo Jun 17 '16 at 14:49

2 Answers2

0

You can use profiles to use different configuration. You have to define two profiles one to use jdk8 configuration and other use jdk 7 configuration. Eg:

<profiles>
    <profile>
        <id>jdk7</id>
        <activation>
            <property>
                <name>jdk7</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5.1</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>jdk8</id>
        <activation>
            <property>
                <name>jdk8</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

So, when you want to build with jdk7 just call with -P jdk7 and when you want to build with jdk8 call with -P jdk8. Eg:

mvn clean install -P jdk7
techtabu
  • 23,241
  • 3
  • 25
  • 34
  • That would surely work but that's not what i want to do. My goal is to construct 2 clients libraries in the same build, so i need to use 2 differents classes folder, different from ./target/classes. – Matthieu Saleta Jun 17 '16 at 14:32
0

The only way i found to resolve the problem is to modify the maven-compiler-plugin.

I've added the property "additionalClasspathElements" in the TestCompilerMojo.

I will make a Pull Request soon.

Matthieu Saleta
  • 1,388
  • 1
  • 11
  • 17