Maven使用JDK 6构建失败,出现UnsupportedClassVersionError错误。

3

我正在尝试使用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$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.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>

这个问题有点棘手,解释起来有些困难。如果需要更多明确的信息,请不要犹豫询问。 - Alexis Dufrenoy
2
你不能使用JDK7并且只是在pom.xml文件中减少编译版本吗? - Andremoniy
1
请查看 https://dev59.com/N10Z5IYBdhLWcg3wfgYR。 - Justin.Cooke
1
mvn -v 命令会显示什么? - Tome
1
我猜你已经检查过3.2.5版本是Eclipse使用的版本了(参见此处 https://dev59.com/t1sW5IYBdhLWcg3wATHe)。 - Tome
显示剩余6条评论
1个回答

4

如下的错误信息:

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

通常表示使用JDK7编译的代码正在被不兼容的JRE版本(很可能是6或更低版本)执行。

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

如果系统中安装了不同版本的Maven,则必须强制使用所需的版本。

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

  • 命令行:将M2_HOME环境变量设置为指向该安装位置,并确保可执行文件为3.2.5(PATH环境变量)
  • IDE:检查IDE是否配置为使用所需的Maven版本。请参阅IntelliJEclipse中如何操作

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接