由于 UnsupportedClassVersionError,使用 JDK 6 的 Maven 构建失败

Maven build with JDK 6 fails due to UnsupportedClassVersionError

我正在尝试使用 JDK 6(在 Eclipse Luna,Java EE 版本中)使用 Maven 3.2.5 进行构建,但我不断收到以下错误:

Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenCli : Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access[=11=]0(URLClassLoader.java:58)
    at java.net.URLClassLoader.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClassFromSelf(ClassRealm.java:401)
    at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:42)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:271)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:254)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:239)
    at org.codehaus.plexus.classworlds.launcher.Launcher.getMainClass(Launcher.java:144)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:266)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)

经过调查,Maven下载了plexus-classworlds-2.5.2-sources.jar,这是为JDK 7编译的,这就是问题的原因。 Maven 3.2.5 应该与 JDK 6 一起使用,所以我不明白发生了什么。

这里是pom.xml:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>fr.sacem.fwk</groupId>
        <artifactId>frmwk-parent</artifactId>
        <version>5.3.13</version>
    </parent>

    <artifactId>sacem-fwkweb</artifactId>
    <packaging>jar</packaging>
    <name>Sacem Framework Web</name>
    <description>Socle technique de la Sacem</description>

    <dependencies>
           <!-- FwkCore -->
        <dependency>
            <groupId>fr.sacem.fwk</groupId>
            <artifactId>sacem-fwkcore</artifactId>
            <version>${project.version}</version>
        </dependency>

        <!-- Wesso -->
        <dependency>
            <groupId>fr.sacem.admap</groupId>
            <artifactId>wesso-api</artifactId>
            <version>3.5.0</version>
        </dependency>

        <!-- Apache Commons -->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-digester</groupId>
            <artifactId>commons-digester</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-jxpath</groupId>
            <artifactId>commons-jxpath</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
        </dependency>
        <!-- 
        <dependency>
            <groupId>commons-resources</groupId>
            <artifactId>commons-resources</artifactId>
            <version>X.Y.Z</version>
        </dependency>
         -->
        <dependency>
            <groupId>commons-validator</groupId>
            <artifactId>commons-validator</artifactId>
        </dependency>


        <!-- Gestion des Logs -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </dependency>

        <!-- POI -->
        <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi</artifactId>
        </dependency>
        <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi-contrib</artifactId>
        </dependency>

        <!-- Struts -->
        <dependency>
              <!-- 
              <groupId>struts</groupId>
               -->
              <groupId>fr.sacem.struts</groupId>
            <artifactId>struts</artifactId>
        </dependency>

        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xercesImpl</artifactId>
        </dependency>

        <!-- JUnit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>fr.sacem.fwk</groupId>
            <artifactId>fwkunit</artifactId>
            <scope>test</scope>
        </dependency>

         <!-- API JavaEE
         <dependency>
             <groupId>javaee</groupId>
             <artifactId>javaee-api</artifactId>
             <scope>provided</scope>
         </dependency>
          -->

         <!-- API Servlet -->
         <dependency>
             <groupId>javax.servlet</groupId>
             <artifactId>servlet-api</artifactId>
             <scope>provided</scope>
         </dependency>

        <!-- API JSP -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- API JTA -->
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- YUI Compressor -->
        <dependency>
            <groupId>com.yahoo.platform.yui</groupId>
            <artifactId>yuicompressor</artifactId>
            <version>2.4.7</version>
            <scope>provided</scope>
            <type>jar</type>
        </dependency>
    </dependencies>

    <properties>
        <project.build.sourceEncoding>ISO-8859-1</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <frmwk.project.build.sourceEncoding>${project.build.sourceEncoding}</frmwk.project.build.sourceEncoding>
        <frmwk.project.copyright>Copyright 2002-2015 SACEM.</frmwk.project.copyright>

        <frmwk.web.directory.build>${basedir}/build</frmwk.web.directory.build>

        <frmwk.web.directory.source.java>${basedir}/WEB-INF/sources/java/dev</frmwk.web.directory.source.java>
        <frmwk.web.directory.classes>${frmwk.web.directory.build}/classes</frmwk.web.directory.classes>

        <frmwk.web.directory.test.sources>${basedir}/WEB-INF/sources/java/tests</frmwk.web.directory.test.sources>
        <frmwk.web.directory.test.config>${basedir}/WEB-INF/config</frmwk.web.directory.test.config>
        <frmwk.web.directory.test.resources>${basedir}/WEB-INF/ressources</frmwk.web.directory.test.resources>
        <frmwk.web.directory.test.classes>${frmwk.web.directory.build}/test-classes</frmwk.web.directory.test.classes>

        <frmwk.web.directory.deploy>${basedir}/WEB-INF/deploiement</frmwk.web.directory.deploy>
        <frmwk.web.directory.templates>${frmwk.web.directory.deploy}/script/templates</frmwk.web.directory.templates>

        <frmwk.resources.jsp>${project.artifactId}-${project.version}-jsp</frmwk.resources.jsp>
        <frmwk.resources.tools.yuicompressor>${maven.dependency.com.yahoo.platform.yui.yuicompressor.jar.path}</frmwk.resources.tools.yuicompressor>

    </properties>

    <build>
        <directory>${frmwk.web.directory.build}</directory>
        <sourceDirectory>${frmwk.web.directory.source.java}</sourceDirectory>

        <resources>
            <resource>
                <directory>${frmwk.web.directory.source.java}</directory>
                <filtering>false</filtering>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
        <outputDirectory>${frmwk.web.directory.classes}</outputDirectory>

        <testSourceDirectory>${frmwk.web.directory.test.sources}</testSourceDirectory>
        <testResources>
            <testResource>
                <directory>${frmwk.web.directory.test.resources}</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*</include>
                </includes>
            </testResource>
            <testResource>
                <directory>${frmwk.web.directory.test.config}</directory>
                <filtering>false</filtering>
                <includes>
                    <include>**/*</include>
                </includes>
            </testResource>
        </testResources>
        <testOutputDirectory>${frmwk.web.directory.test.classes}</testOutputDirectory>

        <plugins>
            <!-- - - - - - - - - - - - - - - - - - 
                    JAVADOC
                 - - - - - - - - - - - - - - - - - -->
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-javadoc-plugin</artifactId>
              <configuration>
                    <javadocExecutable>${frmwk.javadoc.executable}</javadocExecutable>
                    <docfilessubdirs>true</docfilessubdirs>

                    <locale>fr_FR</locale>

                    <version>true</version>
                    <author>true</author> 
                    <use>true</use> 
                    <private>false</private> 
                    <doctitle>${project.name} ${project.version}</doctitle> 
                    <windowtitle>${project.name} ${project.version}</windowtitle> 
                    <header>&lt;b&gt;${project.name} ${project.version}&lt;/b&gt;</header> 
                    <bottom>${frmwk.project.copyright}</bottom>

                    <stylesheetfile>${frmwk.web.directory.templates}/javadoc-stylesheet/stylesheet.css</stylesheetfile>
              </configuration>
              <executions>
                <execution>
                  <id>attach-javadocs</id>
                  <goals>
                    <goal>jar</goal>
                  </goals>
                </execution>
              </executions>
            </plugin>

            <!-- - - - - - - - - - - - - - - - - - 
                    COMPILATION JAVA 1.6
                 - - - - - - - - - - - - - - - - - -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${frmwk.compile.source}</source>
                    <target>${frmwk.compile.target}</target>
                    <verbose>${frmwk.compile.verbose}</verbose>
                    <debug>${frmwk.compile.debug}</debug>
                    <encoding>${frmwk.project.build.sourceEncoding}</encoding>
                    <showDeprecation>${frmwk.compile.deprecation}</showDeprecation>
                    <optimize>${frmwk.compile.optimize}</optimize>

                    <fork>${frmwk.compile.fork}</fork>

                    <executable>${frmwk.compile.executable}</executable>
                    <compilerVersion>${frmwk.compile.compilerVersion}</compilerVersion>
                </configuration>
            </plugin>
            <!-- - - - - - - - - - - - - - - - - - 
                    SOURCES JAVA
                 - - - - - - - - - - - - - - - - - -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- ================================= 
                    PREPARER LIVRAISON AMELY
                ================================= -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <!-- - - - - - - - - - - - - - - - - - 
                          target: copy-jars-to-delivery-dir                      
                         - - - - - - - - - - - - - - - - - -->
                    <execution>
                        <id>copy-jars-to-delivery-dir</id>
                        <phase>install</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target name="Jar" description="Livrer la librairie">
                                <!-- S'assurer de l'existence du dossier de livraison -->
                                <mkdir dir="${frmwk.project.delivery.dir}" />

                                <!-- Copier les jar -->
                                <copy todir="${frmwk.project.delivery.dir}" overwrite="true" >
                                    <fileset dir="${frmwk.web.directory.build}">
                                        <filename name="*.jar"/>
                                    </fileset>
                                </copy>
                            </target>
                        </configuration>
                    </execution>
                    <!-- - - - - - - - - - - - - - - - - - 
                          target: copy-javadoc-to-delivery-dir                      
                         - - - - - - - - - - - - - - - - - -->
                    <execution>
                        <id>copy-javadoc-to-delivery-dir</id>
                        <phase>install</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target name="Javadoc" description="Livrer la documentation en ligne (api)">
                                <!-- S'assurer de l'existence du dossier dedestination -->
                                <mkdir dir="${frmwk.project.delivery.dir}/Javadoc/${project.artifactId}" />

                                <!-- Copier le dossier -->
                                <copy todir="${frmwk.project.delivery.dir}/Javadoc/${project.artifactId}" overwrite="true" >
                                    <fileset dir="${frmwk.web.directory.build}/apidocs">
                                        <filename name="**/*"/>
                                    </fileset>
                                </copy>

                                <!-- Copier le template de dossier Amely -->
                                <filter token="projectVersion" value="${project.version}"/>
                                <filter token="projectName"    value="${project.artifactId}"/>
                                <copy file="${frmwk.web.directory.templates}/amely/Javadoc/project.name/folder.amely"
                                      tofile="${frmwk.project.delivery.dir}/Javadoc/${project.artifactId}/folder.amely"
                                      filtering="true"/>
                            </target>
                        </configuration>
                    </execution>
                    <!-- - - - - - - - - - - - - - - - - - 
                          target: zip-sources-non-public                     
                         - - - - - - - - - - - - - - - - - -->
                    <execution>
                        <id>zip-sources-non-public</id>
                        <phase>package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target name="SourcesNonPublic" description="Livrer les sources (non public)">
                                <mkdir dir="${frmwk.project.delivery.dir}/SourcesNonPublic" />

                                <!-- Zipper l'intégralité du projet -->
                                <zip destfile="${frmwk.project.delivery.dir}/SourcesNonPublic/${project.artifactId}_${project.version}.srcnonpublic.zip">
                                    <fileset dir="${basedir}">
                                        <include name=".settings/**/*"/>
                                        <include name="commun/**/*"/>
                                        <include name="fwk/**/*"/>
                                        <include name="WEB-INF/**/*"/>
                                        <include name=".*"/>

                                        <exclude name="build/"/>
                                        <exclude name="*.log"/>
                                    </fileset>
                                </zip>
                            </target>
                        </configuration>
                    </execution>
                    <!-- - - - - - - - - - - - - - - - - - 
                          target: fwkweb-jsp-zip                      
                         - - - - - - - - - - - - - - - - - -->
                    <execution>
                        <id>fwkweb-jsp-zip</id>
                        <phase>package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target name="jsp-build-zip">
                                <delete dir="${frmwk.project.delivery.dir}/${frmwk.resources.jsp}.zip" failonerror="false" />

                                <zip zipfile="${frmwk.project.delivery.dir}/${frmwk.resources.jsp}.zip">
                                    <zipfileset dir="${basedir}" prefix="src/main/webapp">
                                        <include name="fwk/**/*"/>
                                    </zipfileset>
                                    <zipfileset dir="${basedir}/WEB-INF/ressources" prefix="src/main/resources">
                                        <include name="*.properties"/>
                                    </zipfileset>
                                </zip>
                            </target>
                        </configuration>
                    </execution>
            </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <!-- ================================= 
                          target: FRMWK-${project.version}.zip              
                         ================================= -->
                    <execution>
                        <id>attach-artifacts</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attach-artifact</goal>
                        </goals>
                        <configuration>
                            <artifacts>
                                <artifact>
                                    <file>${frmwk.project.delivery.dir}/${frmwk.resources.jsp}.zip</file>
                                    <type>zip</type>
                                    <classifier>jsp</classifier>
                                </artifact>
                            </artifacts>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


    <profiles>
        <!--
        - - - - - - - - - - - - - - - - - - -
            Profile: FRMWK
        - - - - - - - - - - - - - - - - - - - -
        -->
        <profile>
            <id>FRMWK</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!-- Paramètre de build des modules FRMWK -->
                <frmwk.compile.executable>${JAVA_1_6_HOME}/bin/${JAVAC_EXE}</frmwk.compile.executable>
                <frmwk.javadoc.executable>${JAVA_1_6_HOME}/bin/${JAVADOC_EXE}</frmwk.javadoc.executable>

                <!-- Options de compilation -->
                <frmwk.compile.source>1.6</frmwk.compile.source>
                <frmwk.compile.target>${frmwk.compile.source}</frmwk.compile.target>
                <frmwk.compile.verbose>true</frmwk.compile.verbose>
                <frmwk.compile.debug>true</frmwk.compile.debug>
                <frmwk.compile.deprecation>true</frmwk.compile.deprecation>
                <frmwk.compile.optimize>true</frmwk.compile.optimize>
                <frmwk.compile.fork>true</frmwk.compile.fork>
                <frmwk.compile.compilerVersion>${frmwk.compile.source}</frmwk.compile.compilerVersion>

                <frmwk.project.delivery.dir>${FRMWK_DELIVRY_HOME}/V${project.version}</frmwk.project.delivery.dir>
            </properties>
        </profile>
    </profiles>

</project>

一条错误消息,例如

Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenCli : Unsupported major.minor version 51.0

将始终指示使用 JDK 7 编译的代码正在由不兼容的 JRE 版本(很可能是 6 或更低版本)执行。

在您的特定用例(项目 需要 JDK6)中,您必须确保使用的最高 Maven 版本是 3.2.5(当前最新的官方 JDK6 兼容的 Maven 版本)。

如果您的系统托管不同版本的 Maven,您必须在此处强制使用所使用的版本。

并且,根据 Maven 的启动方式,您可以通过以下方式强制使用 Maven 版本:

  • 命令行:设置 M2_HOME 环境变量以指向该安装并确保可执行文件是 3.2.5 (PATH 环境变量)
  • IDE:检查 IDE 是否配置为使用您需要的 Maven 版本。查看如何做到这一点 here for IntelliJ and there for Eclipse