Maven inheritance isn't working with pluginManagement

1.9k views Asked by At

I have this plugin configured in parent pom

 <properties>
        <java.version>1.7</java.version>
        <maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
 </properties>
 ...
<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <compilerArgument>-XDignore.symbol.file</compilerArgument>
                </configuration>
            </plugin>
         </plugins>
    </pluginManagement>
</build>

And this in the child

<build>
    <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
    </plugins>
</build>

When i run maven update project on both projects , i expect that child change JRE to 1.7, but it doesn't, i and i cannot figure it out why. If i put the configuration in the child then it works... Do you know what am i doing wrong?

2

There are 2 answers

0
cjackson On

I was having the same issue in my build process. The build was failing with the following error:

[INFO] Compiling 2 source files to /project/target/test-classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /project/src/test/java/com/example/MyClass.java:[18,63] lambda expressions are not supported in -source 1.7
(use -source 8 or higher to enable lambda expressions)

and the reason was due to the compilation of test files not having the correct java level set. The following configuration worked for me:

In the parent:

<properties>
  <java.version>1.8</java.version>
  <maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
</properties>
...
<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${maven-compiler-plugin.version}</version>
        <configuration>
          <source>${java.version}</source>
          <target>${java.version}</target>
          <testSource>${java.version}</testSource>
          <testTarget>${java.version}</testTarget>
        </configuration>
      </plugin>
    </plugins>
  </pluginManagement>
</build>

In the child:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
    </plugin>
  </plugins>
</build>
0
lars On

A typical issue is that the maven-compiler-plugin version is too old. The release option was introduced in version 3.6. Try specifying a newer version of maven-compiler-plugin in the parent POM and use the release option to specify the Java version. From JDK 9, the release option replaces source and target.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.11.0</version>
  <configuration>
    <release>11</release>
  </configuration>
</plugin>