18

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>
Honza Zidek
  • 9,204
  • 4
  • 72
  • 118
Brian
  • 4,328
  • 13
  • 58
  • 103

8 Answers8

16

Maven relies on the JAVA_HOME environment variable being set. Set it according to your OS and re-run your build.

Edit: Use export to set the variable.

export JAVA_HOME=/path/to/java
JamesB
  • 7,774
  • 2
  • 22
  • 21
12

As a MacOS user, open a Terminal and create or edit a file .bash_profile (in the directory of your user home) and put the following snippet there:

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

Save that file and reopen a Terminal instance. If you execute export as a command there, you should see a valid JAVA_HOME with the location of your JDK 8 installation. Restart your IDE afterwards and maven should be able to build you nice java target 1.8 code.

MWiesner
  • 8,868
  • 11
  • 36
  • 70
9

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>
Luan Nguyen
  • 101
  • 3
  • 5
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Please read this [how-to-answer](http://stackoverflow.com/help/how-to-answer). – thewaywewere Jun 08 '17 at 09:21
8

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>
Bharat Bhagat
  • 381
  • 1
  • 5
  • 14
7

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.

Rick Deckard
  • 123
  • 2
  • 5
  • 1
    I agree there must be a bug. I searched and searched and finally found this, and even this isn't working for me. I think this is the problem though. I can't get maven to call the Java compiler correctly, even with the compilerArguments configuration you mentioned. Something is forcing it to use 1.5 on my system and it's really frustrating. – Tim Jul 10 '18 at 17:18
2

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>
joan
  • 39
  • 5
0

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.

DominikStyp
  • 360
  • 6
  • 10
-1

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

  • I don't know what did you find, but we write 2017 and there is still no version 3.5.1 of Maven... – Bartis Áron Mar 12 '17 at 11:06
  • @BartisÁron you may want to check the versions available for maven-compiler-plugin: https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin – Stultuske Nov 16 '17 at 08:24
  • Strange. Now I see those versions. I don't know what did I see back then... – Bartis Áron Nov 17 '17 at 11:37