简体   繁体   中英

Maven toolchain not getting triggered in build

I'm using the latest maven 3.3.9 which enforces java 8, and trying to compile a project in java 7 using a java 7 maven toolchain.

As per: The maven guide I've put in the maven toolchain plugin to try and tell it to use java 7 to compile the app. My project setup is as follows:

  • top_module --> (have put in maven toolchain plugin in this pom.xml)

    ---submodule1

    ---submodule2

This is my parent pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>parent</groupId>
        <artifactId>build-base</artifactId>
        <version>1.0.17</version>
    </parent>

    <groupId>top_module</groupId>
    <artifactId>top_module</artifactId>
    <version>1</version>
    <packaging>pom</packaging>
    <name>${project.artifactId}</name>

    <modules>
        <module>submodule1</module>
        <module>submodule2</module>
    </modules>

    <properties>


        ...

    </properties>

    <dependencyManagement>
        <dependencies>

            <dependency>
                ...
            </dependency>
            <dependency>
                ...
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            ...
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <!-- Ensure we compile as the correct jdk version -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                </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>1.7</version>
                                <vendor>sun</vendor>
                            </jdk>
                        </toolchains>
                    </configuration>
                </plugin>

                <!-- Maven WAR Plugin -->
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>${maven.war.plugin.version}</version>
                    <inherited>true</inherited>
                    <configuration>
                        <archiveClasses>true</archiveClasses>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-enforcer-plugin</artifactId>
                                        <versionRange>[1.0.0,)</versionRange>
                                        <goals>
                                            <goal>enforce</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore />
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>

            </plugins>
        </pluginManagement>
    </build>

    ...

</project>

The build doesn't run any toolchain related steps and conks out when running mvn clean install due to a plugin being incompatible with java 8, which obviously means the toolchain isn't getting run. Do I need anything else in the pom?

This is my toolchains.xml file in my M2_HOME:

<?xml version="1.0" encoding="UTF8"?>
    <toolchains>
      <!-- JDK toolchains -->
      <toolchain>
        <type>jdk</type>
        <provides>
          <version>1.6</version>
          <vendor>sun</vendor>
        </provides>
        <configuration>
          <jdkHome>C:\Java\jdk1.6.0_45</jdkHome>
        </configuration>
      </toolchain>

            <toolchain>
        <type>jdk</type>
        <provides>
          <version>1.7</version>
          <vendor>sun</vendor>
        </provides>
        <configuration>
          <jdkHome>C:\Java\jdk1.7.0_79</jdkHome>
        </configuration>
      </toolchain>
    </toolchains>

The problem is simply related that you have defined all plugins in pluginManagement but you need to define them like this:

<build>
  <pluginManagement>
    <plugins>
      <!-- Ensure we compile as the correct jdk version -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-toolchains-plugin</artifactId>
        <version>1.1</version>
      </plugin>

      <!-- Maven WAR Plugin -->
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>${maven.war.plugin.version}</version>
        <inherited>true</inherited>
        <configuration>
          <archiveClasses>true</archiveClasses>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.eclipse.m2e</groupId>
        <artifactId>lifecycle-mapping</artifactId>
        <version>1.0.0</version>
        <configuration>
          <lifecycleMappingMetadata>
            <pluginExecutions>
              <pluginExecution>
                <pluginExecutionFilter>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-enforcer-plugin</artifactId>
                  <versionRange>[1.0.0,)</versionRange>
                  <goals>
                    <goal>enforce</goal>
                  </goals>
                </pluginExecutionFilter>
                <action>
                  <ignore />
                </action>
              </pluginExecution>
            </pluginExecutions>
          </lifecycleMappingMetadata>
        </configuration>
      </plugin>

    </plugins>
  </pluginManagement>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-toolchains-plugin</artifactId>
      <executions>
        <execution>
          <goals>
            <goal>toolchain</goal>
          </goals>
          <configuration>
            <toolchains>
              <jdk>
                <version>1.7</version>
                <vendor>sun</vendor>
              </jdk>
            </toolchains>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>


</build>

...

</project>

pluginManagement is intended to define versions and default configurations but no binding to the lifeycle which you need to do for the maven-toolchains-plugin. In pluginManagement you should define all plugins which their versions which you are using which means maven-resources-plugin, maven-install-plugin, maven-deploy-plugin, maven-enforcer-plugin etc. This might be a good candidate for a corporate parent pom which defines all those things. And you are going the best way to separate the JDK which is used by Maven and the JDK which is used by your code.

Apart from that Maven 3.3.9 needs JDK 7 as minimum and not JDK 8

If you just want maven to use the java 7 jdk all the time, the toolchain plugin may be more than you need. it may be enough to set JAVA_HOME

Make sure that JAVA_HOME is set to the location of your JDK, eg export JAVA_HOME=/usr/java/jdk1.7.* and that $JAVA_HOME/bin is in your PATH environment variable. ( Or if from eclipse, set it as the workspace default )

If you want to switch jdk from just 1 project, you can also use the compilerVersion tag https://maven.apache.org/plugins/maven-compiler-plugin/examples/compile-using-different-jdk.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM