Maven无法编译java 1.8

问题描述 投票:14回答:8

我正在尝试使用maven来构建一个jar但我不断收到错误

ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile 
(default-compile) on project Application: Fatal error compiling: invalid target release: 1.8 -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

mvn -v输出

mvn -version
Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-14T09:29:23-08:00)
Maven home: /usr/local/Cellar/maven/3.2.5/libexec
Java version: 1.6.0_65, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x", version: "10.10.1", arch: "x86_64", family: "mac"

所以看起来它指向1.6jdk

但我有1.8 jdk

java -version

java -version
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

POM.hml

 <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>
    <groupId>com.brian.ridecellchallenge</groupId>
    <artifactId>Application</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.group.id.Launcher1</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>


    </build>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.6</version>
        </dependency>
    </dependencies>
</project>
maven maven-assembly-plugin maven-shade-plugin
8个回答
14
投票

Maven依赖于设置的JAVA_HOME环境变量。根据您的操作系统进行设置并重新运行您的构建。

编辑:使用导出来设置变量。

export JAVA_HOME=/path/to/java

11
投票

作为MacOS用户,打开Terminal并创建或编辑文件.bash_profile(在用户主目录中)并将以下代码段放在那里:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

保存该文件并重新打开终端实例。如果在那里执行export作为命令,您应该看到一个有效的JAVA_HOME,其中包含JDK 8安装的位置。之后重新启动IDE,maven应该能够构建你很好的java target 1.8代码。


7
投票

你可以在编译器插件中尝试配置。它在我的本地工作

      <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <executable><!--path-to-your-java8-home-directoru--></executable>
                <compilerVersion>1.8</compilerVersion>
            </configuration>
        </plugin>

6
投票

因为Maven不知道你想要构建的jdk版本。你可以试试

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

5
投票

我注意到maven-compiler-plugin似乎忽略了配置

<source>1.8</source>
<target>1.8</target>

事实上,使用-X maven的选项构建我的项目,我看到在maven-compiler-plugin的配置中,源和目标被强制为1.6:

[DEBUG] --------------------------------------------------------------
[DEBUG] Goal:          org.apache.maven.plugins:maven-compiler-plugin:3.3:testCompile (default-testCompile)
[DEBUG] Style:         Regular
[DEBUG] Configuration: <?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <basedir default-value="${basedir}"/>
 <buildDirectory default-value="${project.build.directory}"/>
 <classpathElements default-value="${project.testClasspathElements}"/>
 <compileSourceRoots default-value="${project.testCompileSourceRoots}"/>
 <compilerId default-value="javac">${maven.compiler.compilerId}</compilerId>
 <compilerReuseStrategy default-value="${reuseCreated}">${maven.compiler.compilerReuseStrategy}    </compilerReuseStrategy>
 <compilerVersion>${maven.compiler.compilerVersion}</compilerVersion>
 <debug default-value="true">${maven.compiler.debug}</debug>
 <debuglevel>${maven.compiler.debuglevel}</debuglevel>
 <encoding default-value="${project.build.sourceEncoding}">UTF-8</encoding>
 <executable>${maven.compiler.executable}</executable>
 <failOnError default-value="true">${maven.compiler.failOnError}</failOnError>
 <forceJavacCompilerUse default-value="false">${maven.compiler.forceJavacCompilerUse}</forceJavacCompilerUse>
 <fork default-value="false">${maven.compiler.fork}</fork>
 <generatedTestSourcesDirectory default-value="${project.build.directory}/generated-test-sources/test-annotations"/>
 <maxmem>${maven.compiler.maxmem}</maxmem>
 <meminitial>${maven.compiler.meminitial}</meminitial>
 <mojoExecution default-value="${mojoExecution}"/>
 <optimize default-value="false">${maven.compiler.optimize}</optimize>
 <outputDirectory default-value="${project.build.testOutputDirectory}"/>
 <project default-value="${project}"/>
 <session default-value="${session}"/>
 <showDeprecation default-value="false">${maven.compiler.showDeprecation}</showDeprecation>
 <showWarnings default-value="false">${maven.compiler.showWarnings}</showWarnings>
 <skip>${maven.test.skip}</skip>
 <skipMultiThreadWarning default-value="false">${maven.compiler.skipMultiThreadWarning}</skipMultiThreadWarning>
 <source default-value="1.5">1.6</source>
 <staleMillis default-value="0">${lastModGranularityMs}</staleMillis>
 <target default-value="1.5">1.6</target>
 <testSource>${maven.compiler.testSource}</testSource>
 <testTarget>${maven.compiler.testTarget}</testTarget>
 <useIncrementalCompilation default-value="true">${maven.compiler.useIncrementalCompilation}</useIncrementalCompilation>
  <verbose default-value="false">${maven.compiler.verbose}</verbose>

经过多次搜索和尝试,我通过强制java编译器使用所需的源和目标来解决问题:

<compilerArguments>
  <source>1.8</source>
  <target>1.8</target>
</compilerArguments>

这增加了上面两个参数的javac参数,覆盖了默认值。

就我而言,一切运作良好。

请注意:我看到上述配置,maven以这种方式调用javac:

-d [...] -g -nowarn -target 1.6 -source 1.6 -encoding UTF-8 -source 1.8 -target 1.8

前两个“-source”和“-target”参数是maven-compiler-plugin使用的默认参数;第二对参数是插件添加的参数,因为上面描述了配置“compilerArguments”。似乎javac只使用了最后几个参数(如果一个参数重复多次,最后一个覆盖所有的prevouses),但我不确定这可以被认为是标准和“确定”的行为。

我使用了不同版本的maven-compiler-plugin,不同版本的maven。我深入检查了所有配置(例如系统变量,执行java或maven时使用的每个脚本等),我确信一切正常。

这很奇怪,但我的结论是maven-compiler-plugin中存在一个带有java 8的错误。


2
投票

我不确定,但您可以尝试更新您的maven.plugin版本

尝试

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

0
投票

如果有任何人拥有Eclipse Kepler SR2并且在使用Maven构建项目时遇到Java 1.8问题,请尝试访问Eclipse Marketplace并搜索针对Eclipse Kepler SR2的m2e的Java 8支持插件这对我有帮助,现在Maven编译Java 1.8源代码没有问题。


-1
投票

是的,它似乎是maven-compiler-plugin中的一个错误。当我从3.1升级到3.5.1时,它可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.