Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven can't compile java 1.8

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>
like image 523
Brian Avatar asked Jan 15 '15 21:01

Brian


People also ask

Does Maven support Java 18?

Maven JDK 1.8 use in Eclipse As soon as a new Maven build takes place, the JDK version reverts back to 1.5. The only way to make this a permanent change is to edit the Maven compiler plugin POM entry and force Eclipse and Maven to use Java 1.8.

Is Java 8 and Java 1.8 the same?

In short – 8 is product version number and 1.8 is the developer version number (or internal version number). The product is the same, JDK 8, anyways.

How do I set Java version in POM xml?

xml file. This parent POM allows us to configure default plugins and multiple properties including the Java version — by default, the Java version is 1.8. By setting the java. version property, we declare that the source and the target Java versions are both equal to 1.9.

What version of Java is Maven using?

Maven Configuration The Maven tool uses JDK version 11.0.


4 Answers

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
like image 115
JamesB Avatar answered Oct 19 '22 17:10

JamesB


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.

like image 34
MWiesner Avatar answered Oct 19 '22 17:10

MWiesner


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>
like image 9
Luan Nguyen Avatar answered Oct 19 '22 18:10

Luan Nguyen


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>
like image 8
Bharat Bhagat Avatar answered Oct 19 '22 18:10

Bharat Bhagat