Maven can't compile java 1.8

2019-03-18 04:07发布

I'm trying to use maven to build a jar but I keep getting the error

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 outputs

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"

So it looks like its pointing to the 1.6jdk

but I have the 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.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/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>

8条回答
来,给爷笑一个
2楼-- · 2019-03-18 04:16

If anybody has Eclipse Kepler SR2 and has an issue with Java 1.8 while building project with Maven, try to go to Eclipse Marketplace and search for plugin called Java 8 support for m2e for Eclipse Kepler SR2 this helped me, and now Maven compiles Java 1.8 source without problem.

查看更多
再贱就再见
3楼-- · 2019-03-18 04:17

I noticed that it seems that maven-compiler-plugin ignores the configurations

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

In fact, building my project with -X maven's option, I see that in the configuration of the maven-compiler-plugin the source and target were forced to be 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>

I solved the problem after much much searches and tries, by forcing the java compiler to use the desired source and target:

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

This adds to the parameters of javac the two parameters above, overwriting the default.

In my case, all works well.

Just a note: I see that with the above configuration, maven calls the javac in this way:

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

The first two "-source" and "-target" parameters are the default parameters that the maven-compiler-plugin uses; the second couple of parameters are the parameters added by the plugin because of the configuration "compilerArguments" described above. It seems that javac just uses the last couple of parameters (if a parameter is repeated more times, the last one overwrites all the previouses), but I'm not sure that this can be considered a standard and "sure" behavior.

I used different versions of maven-compiler-plugin, different version of maven. I checked in deep all my configurations (e.g. system variables, every single script used in execution of java or maven and so on) and I'm sure that all is ok.

It's strange, but my conclusion is that there is a bug in the maven-compiler-plugin with java 8.

查看更多
太酷不给撩
4楼-- · 2019-03-18 04:18

Yes it seems a bug in the maven-compiler-plugin.When I upgraded from 3.1 to 3.5.1, it works.

查看更多
放我归山
5楼-- · 2019-03-18 04:24

Because Maven does not know version of jdk you want to build. You can try with

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
查看更多
对你真心纯属浪费
6楼-- · 2019-03-18 04:26

I'm not sure but can you try to update your maven.plugin version

try

<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>
查看更多
姐就是有狂的资本
7楼-- · 2019-03-18 04:28

you can try configuration in compiler plugin. Its working on my local

      <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>
查看更多
登录 后发表回答