Error trying to build a jar with springboot and maven

79 Views Asked by At

I'm developing a simple REST service to return HARDCODE data and I'm stuck trying to build the jar of the project. After I check the REST worked well I tried to create the JAR but I couldn't.

Currently this is my pom.xml file.

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>technical_test</groupId>
    <artifactId>technical_test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>technical_test</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
        <maven.compiler.release>17</maven.compiler.release>
        <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.12.1</version>
                <configuration>
                    <Relase>17</Relase>
                    <target>17</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Currently this is the error when I executed mvn clean package

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.12.1:compile (default-compile) on project technical_test: Fatal error compiling: error: release version 17 not supported.

Initially the project was with JAVA 21 but I got the same error

Ok, is not supported. How can I found a supported versión (in maven documentation is not clear for my) or how can I fix this error?

I'm new in this, so I'll be please with your help

2

There are 2 best solutions below

0
khmarbaise On BEST ANSWER

I strongly recommend to use it like this:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>technical_test</groupId>
    <artifactId>technical_test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>technical_test</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
   </build>
</project>

I have used java.version in that setup but that's no even required because it's defined in the parent.

I also strongly recommend to remove hibernate-jpa-2.1-api in that version because Spring Boot already offers an appropriate version... which is at version: 6.4.1.Final... Check also the documentation... also the groupId has changed for Hibernate...

  <dependency>
    <groupId>org.hibernate.orm</groupId>
     ...
  </dependency>

This is also valid for servlet-api ... that has been changed in the namespace... (groupId) based on Jakarta EE Change

Update

The output for mvn --version should like this:

Apache Maven 3.9.6 (bc0240f3c744dd6b6ec2920b3cd08dcc295161ae)
Maven home: /Users/../tools/maven
Java version: 17.0.10, vendor: Eclipse Adoptium, runtime: /Users/.../.sdkman/candidates/java/17.0.10-tem
Default locale: en_DE, platform encoding: UTF-8
OS name: "mac os x", version: "14.0", arch: "aarch64", family: "mac"
5
Mohamed amine ben hassen On

The error message indicates that Maven is unable to compile your code with Java version 17, as it seems that the Maven compiler plugin you're using doesn't support it. so either Update Maven Compiler Plugin or use a lower java version, also Relase should be release in the configuration of the maven-compiler-plugin.

       <configuration>
            <release>17</release>
            <target>17</target>
        </configuration>