Hi,

> I am not quite sure why the java14ge profile should only compile and run
> a single test and not all tests. The more intuitive way to organise the
> build would be to run all relevant tests for all JDKs, i.e. the JDK 14
> tests being a superset of the JDK 8 tests. But you asked for a "logical
> XOR", i.e. either/or solution, so I am recommending you to use
> 
> mvn help:effective-pom
> 
> in order to inspect your configuration. If you look at the effective
> POM, you will see that you put your JDK8 compiler configuration with
> testExcludes into the general plugin configuration section rather than
> in the one for goal testCompile. That parameter only exists there,
> though.
> 
> You can also combine with -P java8ge,!java14ge or -P !java8ge,java14ge
> in order to explicitly check for a certain profile version and override
> auto activation.
> 
> One way to make your respective test compilations mutually exclusive
> would be to override the default execution default-testCompile which
> would otherwise be included in each profile unless deactivated via
> <phase>none</phase>. So how about this?


I'd suggest a different solution. It looks more complex, but in the end 
lets you clearly differ between "normal" Java-8-compatible code and code 
that has to use Java >= 14:

Basically use three profiles:
- The first is used as long as you're using Java 8
  It only instructs m-compiler-p to use JDK 1.8 as source and target for 
your code

- The second is active when you use Java >= 9:
  It does the same as the first, but uses the "release" flag of the 
compiler

- The third uses a feature of m-compiler-p to create a multirelease output 
with only a single project; see 
https://github.com/apache/maven-compiler-plugin/blob/master/src/it/multirelease-patterns/singleproject-runtime/pom.xml
:
  Normal code under src/main/java and src/test/java is compiled with the 
default settings from profile 1 or 2, and additional code under 
src/main/java14 and src/test/java14 is compiled against Java 14.



<properties>
 <maven.compiler.plugin.version>3.8.1</maven.compiler.plugin.version>
</properties>

<profiles>
        <profile>
                <id>jdk8</id>
                <activation>
                        <jdk>[,9)</jdk>
                </activation>
                <build>
                        <pluginManagement>
                                <plugins>
                                        <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-compiler-plugin</artifactId>
 <version>${maven.compiler.plugin.version}</version>
                                                <configuration>
 <source>1.8</source>
 <target>1.8</target>
                                                </configuration>
                                        </plugin>
                                </plugins>
                        </pluginManagement>
                </build>
        </profile>

        <profile>
                <id>jdk9+</id>
                <activation>
                        <jdk>[9,)</jdk>
                </activation>
                <build>
                        <pluginManagement>
                                <plugins>
                                        <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-compiler-plugin</artifactId>
 <version>${maven.compiler.plugin.version}</version>
                                                <configuration>
 <release>8</release>
                                                </configuration>
                                        </plugin>
                                </plugins>
                        </pluginManagement>
                </build>
        </profile>

        <!-- Java >=14 as default: -->

        <profile>
                <id>jdk14</id>
                <activation>
                        <jdk>[14,)</jdk>
                        <file>
 <exists>${basedir}/src/main/java14</exists>
                        </file>
                </activation>
                <build>
                        <pluginManagement>
                                <plugins>
                                        <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-compiler-plugin</artifactId>
 <version>${maven.compiler.plugin.version}</version>
                                                <executions>
                                                        <!-- see 
https://github.com/apache/maven-compiler-plugin/blob/master/src/it/multirelease-patterns/singleproject-runtime/pom.xml:
 
-->
                                                        <execution>
 <id>jdk14-compile</id>
                                                                <goals>
 <goal>compile</goal>
                                                                </goals>
 <configuration>
 <release>14</release>
 <compileSourceRoots>
 <compileSourceRoot>${project.basedir}/src/main/java14</compileSourceRoot>
 </compileSourceRoots>
 <multiReleaseOutput>true</multiReleaseOutput>
 </configuration>
                                                        </execution>
                                                        <execution>
 <id>jdk14-testcompile</id>
                                                                <goals>
 <goal>testCompile</goal>
                                                                </goals>
 <configuration>
 <release>14</release>
 <testCompileSourceRoots>
 
<testCompileSourceRoot>${project.basedir}/src/test/java14</compileSourceRoot>
 </testCompileSourceRoots>
 <compilerArgs>
 <arg>--enable-preview</arg>
 </compilerArgs>
 </configuration>
                                                        </execution>
                                                </executions>
                                        </plugin>

                                        <!-- mark the Jar as multi-release 
jar so that it is placed -->
                                        <!-- by the compiler on the module 
path instead of the -->
                                        <!-- classpath: -->

                                        <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-jar-plugin</artifactId>
                                                <executions>
                                                        <execution>
 <id>default-jar</id>
 <configuration>
 <archive>
 <manifestEntries>
        <Multi-Release>true</Multi-Release>
 </manifestEntries>
 </archive>
 </configuration>
                                                        </execution>
                                                </executions>
                                        </plugin>
                                </plugins>
                        </pluginManagement>
                </build>
        </profile>
</profiles>




HTH

Thorsten

Reply via email to