1

I am new to Java/Maven, and I am trying to build a maven Spring Boot project, it was earlier working and was successfully creating jar package as well. But it suddenly stopped working and started giving Maven Compile error.

I know it has something to do with the pom.xml file. My best guess : Some changes in this file has caused it to fail.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <groupId>com.loanglide</groupId>
    <artifactId>drools-helloworld</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>drools-helloworld</name>
    <description>Springboot application</description>

    <properties>
        <maven.compiler.source>12.0.1</maven.compiler.source>
        <maven.compiler.target>12.0.1</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <kie.version>6.1.0.Final</kie.version>
    </properties>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.spring.platform</groupId>
                <artifactId>platform-bom</artifactId>
                <version>1.1.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.kie</groupId>
            <artifactId>kie-ci</artifactId>
            <version>6.1.0.Final</version>
        </dependency>
        <dependency>
          <groupId>javax.xml.bind</groupId>
          <artifactId>jaxb-api</artifactId>
          <version>2.3.0</version>
        </dependency>
        <dependency>
         <groupId>org.mybatis</groupId>
         <artifactId>mybatis</artifactId>
         <version>3.3.0</version>
       </dependency>
       <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.16</version>
        <exclusions>
            <exclusion>
                <groupId>com.google.protobuf</groupId>
                <artifactId>protobuf-java</artifactId>
            </exclusion>
        </exclusions>
       </dependency>
       <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
       <dependency>
           <groupId>org.freemarker</groupId>
           <artifactId>freemarker</artifactId>
           <version>2.3.28</version>
           <exclusions>
            <exclusion>
                <groupId>freemarker<!-- Legacy org-less group --></groupId>
                <artifactId>freemarker</artifactId>
            </exclusion>
           </exclusions>
       </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
             <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.7.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-project-info-reports-plugin</artifactId>
                <version>3.0.0</version>
            </plugin>
        </plugins>
    </build>
</project>

Error

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.905 s
[INFO] Finished at: 2019-08-08T01:52:48+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project springboot-helloworld: Fatal error compiling: error: invalid target release: 12.0.1 -> [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

Java Version

java version "12.0.1" 2019-04-16
Java(TM) SE Runtime Environment (build 12.0.1+12)
Java HotSpot(TM) 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)

I have tried many stackoverflow answer to similar question, but still not able to resolve this issue.

Ravi Shankar Bharti
  • 8,922
  • 5
  • 28
  • 52

2 Answers2

2

If you are using Java 12, add the following to Maven pom.xml.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <release>12</release>
    </configuration>
</plugin>
Sambit
  • 7,625
  • 7
  • 34
  • 65
  • 1
    Do I need to run a maven clean after this as well? – Ravi Shankar Bharti Aug 07 '19 at 20:41
  • 3
    Yes, you have to run as mvn clean package or mvn clean install. – Sambit Aug 07 '19 at 20:43
  • Started giving this error after i ran java-jar `jar file` Exception in thread "main" java.lang.UnsupportedClassVersionError: com/loanglide/Drools has been compiled by a more recent version of the Java Runtime (class file version 56.0), this version of the Java Runtime only recognizes class file versions up to 55.0 – Ravi Shankar Bharti Aug 07 '19 at 21:07
  • 1
    This error comes up when you are using a jar file which is compiled in higher version of java and you have the project in lower version of java. – Sambit Aug 07 '19 at 21:10
  • Thanks for the help man, anywhere i can understand the concepts of maven properly. I hate to be stuck in similar problems at times. If you can provide me some link where i can learn maven in deep and some common pitfalls, it would be great. – Ravi Shankar Bharti Aug 07 '19 at 21:13
  • You can find lot of tutorials on Maven, now mostly used is Gradle. If you have time, you can learn both Maven and Gradle. – Sambit Aug 07 '19 at 21:16
1

The properties maven.compiler.source and maven.compiler.target are used by many plugins on default lifecycle, as compiler, as you can see at this answer.

You must try:

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

But if you want to override only this goal, you can configure it like:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <release>12</release>
    </configuration>
</plugin>

As you are new with Java, keep in mind the new versioning schema so even you have installed the 12 version you can target the 11 LTS target if no new language feature is intended to be used.

bleidi
  • 100
  • 8