'Ignore maven execution block which uses toolchain if ~/.m2/toolchains.xml is not present

I'm working on an open-source library which must work correctly on jre7. Since java 9 has been released, we decided to provide our modules with module-info.java files so that users can make use of them if they prefer to use the library with sdk 9 on jre9 (in this case they'll need to manually put in pom.xml this piece of code:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
      <source>9</source>
      <target>9</target>
    </configuration>
  </plugin>

I've tried to implement the solution provided by maven (https://maven.apache.org/plugins/maven-compiler-plugin/examples/module-info.html) to compile the entire code with jdk7 and to compile only module-info.java files with jdk9. Here is a piece of pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <executions>
      <execution>
        <id>default-compile</id>
        <configuration>
          <!-- compile everything to ensure module-info contains right entries -->
          <!-- required when JAVA_HOME is JDK 8 or below -->
          <jdkToolchain>
            <version>9</version>
          </jdkToolchain>
          <release>9</release>
        </configuration>
      </execution>
      <execution>
        <id>base-compile</id>
        <goals>
          <goal>compile</goal>
        </goals>
        <!-- recompile everything for target VM except the module-info.java -->
        <configuration>
          <excludes>
            <exclude>module-info.java</exclude>
          </excludes>
        </configuration>
      </execution>
    </executions>
    <!-- defaults for compile and testCompile -->
    <configuration>
      <!-- jdkToolchain required when JAVA_HOME is JDK 9 or above -->
      <jdkToolchain>
        <version>[1.7,9)</version>
      </jdkToolchain>
      <source>1.7</source>
      <target>1.7</target>
    </configuration>
  </plugin>

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-toolchains-plugin</artifactId>
    <version>1.1</version>
    <executions>
      <execution>
        <goals>
          <goal>toolchain</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <toolchains>
        <jdk>
          <version>9</version>
        </jdk>
      </toolchains>
    </configuration>
  </plugin>

And I've properly configured ~/.m2/toolchains.xml file. It works perfectly well. But the thing is we need to skip the first execution block, where everything's compiled with jdk9 with module-info, because we don't want to make our users create toolchains.xml file and set up java 9. So what I am probably looking for is the workaround to skip the following code if toolchains.xml is not present in ~/.m2 directory and only execute the second execution block, in which module-info.java files are ignored. Is that even possible? I realise we can still keep two branches (one private with two executional blocks and use of toolchains and another public, which ignores module-info.java files completely, but it doesn't seem an elegant solution.

<execution>
    <id>default-compile</id>
    <configuration>
      <!-- compile everything to ensure module-info contains right entries -->
      <!-- required when JAVA_HOME is JDK 8 or below -->
      <jdkToolchain>
        <version>9</version>
      </jdkToolchain>
      <release>9</release>
    </configuration>
  </execution>


Solution 1:[1]

  1. You can activate/deactivate certain parts of a pom using Maven profiles.
  2. Wouldn't it be better to provide two different versions of your library, e.g. with version numbers 1.0.0-JDK7 and 1.0.0-JDK9. Then the consumer of your library can choose the right one for their purposes.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 J Fabian Meier