Building maven project fails on invalid target version 17.0.5 - java

I am upgrading application from Java 11 to Java 17.0.5
In my java project, I have set below properties
<properties>
<maven.compiler.source>17.0.5</maven.compiler.source>
<maven.compiler.target>17.0.5</maven.compiler.target>
<java.version>17.0.5<java.version>
</properties>
On building the project, it failed saying
invalid target release 17.0.5
java -version prints
"Java version 17.0.5"
and mvn -version prints
Java 17.0.5
Please advise

The version is only the major version. So you have to specify only 17 as version
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<java.version>17<java.version>
</properties>

Related

compiling with java17 even though compiler compliance pointed to java8 in spring tool suite 4

I have downloaded Spring Tool Suite4 IDE, Java17 Came Along with that IDE. But i want to work on Java8.
I installed only java8 in my system and started working on to print just "Hello Spring Boot".
When i run the sample project the code is being compiled with java17 and trying to run with java8(which i have given in execution environments and build path)
I got error like below.
Exception in thread "main" java.lang.UnsupportedClassVersionError: org/springframework/boot/SpringApplication has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:473)
at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at com.aditya.springboot.HelloSpringBootProjectApplication.main(HelloSpringBootProjectApplication.java:10)
I changed the compiler compliance from 17 to 1.8 in windows-\>preferences-\>Compiler-\>JDK Compliance..,
I pointed the Installed JREs to from jre 17(default) to jdk 1.8 and
I changed the compiler compliance from 17 to 1.8 in project-\>properties-\>java compiler also.
I tried uninstalling the STS4 and reinstalling it, but still i am getting same error.
As i am using maven project i tried changing \<java.version\> to 1.8 from 17 in pom.xml file too.
Can anyone help me from where STS is taking java17 inorder to compile my code, i haven't installed java17 in my system also. I have pointed JAVA_HOME in enviroment variables(user level variable but not system level varaiable) to java8 only.
I think you might need to adjust your pom.xml if you are using it to configure the java version. However, you need to make sure that the spring boot version that you are using is compatible with java 8.
In your pom.xml add the following:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version> // your spring boot version
</parent>
............
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source> // the version you desire
<target>1.8</target> // the version you desire
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
for gradle:
plugins {
java
}
group = "com.group"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8 // desired version
java.targetCompatibility = JavaVersion.VERSION_1_8 // desired version
I found an answer
The problem is with java version in pom.xml file of Maven:
If i am creating a maven project with spring boot version 3+, pom.xml is taking java version as 17 by default. Even if we change the version to 1.8 in pom.xml it is showing error as version mismatch because pom.xml internally taking java17. When i changed the java version to 9 in pom.xml it compiled and ran successfully. so we need to use java9+ for spring boot 3+.
But still i want to use java 1.8 for project so i degraded springboot version:
For creating the same maven project i used spring boot version 2+(i.e 2.7.8).
Now maven by default took 1.8 version with out giving chance for us to modify.
My project ran with 1.8 successfully.

Maven ignoring variables in child pom

i been stuck with this for a while.
I am trying to automate the build and deploy (in nexus3) of several java modules and remove the need for manually set the module version in the projects pom.
My issue is that maven is ignoring the variables inside the pom files.
Parent pom:
<properties>
<revision>local</revision>
</properties>
<name>lib-cloud</name>
<groupId>com.company.lib</groupId>
<artifactId>lib-cloud</artifactId>
<version>${revision}</version>
<packaging>pom</packaging>
<modules>
<module>lib-cloud-main</module>
</modules>
and the child pom:
<properties>
<unrelatedVAR>2017.3.3</unrelatedVAR>
</properties>
<parent>
<groupId>com.company.lib</groupId>
<artifactId>lib-cloud</artifactId>
<version>${revision}</version>
</parent>
<artifactId>lib-cloud-main</artifactId>
<name>lib-cloud-main</name>
and then i build this with:
mvn -Drevision=3.1 clean package deploy
This build the code just fine, create a lib-cloud-main/target/lib-cloud-main-3.1.jar
In side the jar, i can already see that
lib-cloud-main-3.1.jar/maven/com.company.lib/lib-cloud-main/pom.xml have
<version>${revision}</version>
As version, instead 3.1 as i would expected, but still this artifact manages to be saved in nexus.
If i try to use it, however, maven trow the error when trying to get the pom:
[ERROR] Failed to execute goal on project core-backend-api: Could not resolve dependencies for project com.company.core:core-backend-api:jar:local: Failed to collect dependencies at com.company.lib:lib-cloud-main:jar:3.1: Failed to read artifact descriptor for com.company.lib:lib-cloud-main:jar:3.1: Could not transfer artifact com.company.lib:lib-cloud:pom:${revision} from/to company-maven-public (http://mvn.company.local/repository/maven-public/): Failed to transfer file: http://mvn.company.local/repository/maven-public/com/company/lib/lib-cloud/$%7Brevision%7D/lib-cloud-$%7Brevision%7D.pom. Return code is: 400 , ReasonPhrase:Invalid repository path.
I even tried to use
mvn release:update-versions -DdevelopmentVersion=3.1
As a way to overwrite the poms with a static value before the build but this methos STILL fail to touch the tag in any project children pom.
What i am doing wrong?
Apache Maven 3.5.4 (Red Hat 3.5.4-4) Maven home: /usr/share/maven Java
version: 1.8.0_212, vendor: Oracle Corporation, runtime:
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.212.b04-0.fc29.x86_64/jre
Default locale: pt_BR, platform encoding: UTF-8 OS name: "linux",
version: "5.0.17-200.fc29.x86_64", arch: "amd64", family: "unix"
You should use release plugin to do this.
mvn release:update-versions -DautoVersionSubmodules=true

Could not transfer artifact (https://repo.maven.apache.org/maven2): Received fatal alert: protocol_version -> [Help 1]

I am new to Maven and trying to setup my first project in Maven but receiving the below error message when I am doing " Run as -> Maven install " in Eclipse. Below is my settings.xml and pom.xml
Settings.xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>C:\Users\Iam\.m2\repository</localRepository>
<profiles>
<profile>
<repositories>
<repository>
<id>central</id>
<url>http://central.maven.org/maven2/</url>
</repository>
</repositories>
<pluginRepositories>
</pluginRepositories>
</profile>
</profiles>
</settings>
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mytest</groupId>
<artifactId>MySpringBootMaven</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.11.RELEASE</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version><!--$NO-MVN-MAN-VER$-->
<configuration>
<warnName>Test.war</warnName>
</configuration>
</plugin>
</plugins>
</build>
</project>
Error message:
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.eclipse.aether.resolution.ArtifactDescriptorException: Failed to read artifact descriptor for org.springframework.boot:spring-boot-maven-plugin:jar:1.5.11.RELEASE
at org.apache.maven.repository.internal.DefaultArtifactDescriptorReader.loadPom(DefaultArtifactDescriptorReader.java:302)
at org.apache.maven.repository.internal.DefaultArtifactDescriptorReader.readArtifactDescriptor(DefaultArtifactDescriptorReader.java:218)
at org.eclipse.aether.internal.impl.DefaultRepositorySystem.readArtifactDescriptor(DefaultRepositorySystem.java:287)
at org.apache.maven.plugin.internal.DefaultPluginDependenciesResolver.resolve(DefaultPluginDependenciesResolver.java:103)
... 27 more
Caused by: org.eclipse.aether.resolution.ArtifactResolutionException: Could not transfer artifact org.springframework.boot:spring-boot-maven-plugin:pom:1.5.11.RELEASE from/to central (https://repo.maven.apache.org/maven2): Received fatal alert: protocol_version
at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:444)
at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolveArtifacts(DefaultArtifactResolver.java:246)
at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolveArtifact(DefaultArtifactResolver.java:223)
at org.apache.maven.repository.internal.DefaultArtifactDescriptorReader.loadPom(DefaultArtifactDescriptorReader.java:287)
... 30 more
Caused by: org.eclipse.aether.transfer.ArtifactTransferException: Could not transfer artifact org.springframework.boot:spring-boot-maven-plugin:pom:1.5.11.RELEASE from/to central (https://repo.maven.apache.org/maven2): Received fatal alert: protocol_version
How to resolve the Received fatal alert: protocol_version ? My java version is 1.7 and maven version is 3.3.3
Sonatype no longer supports TLSv1.1 and below (effective, June 18th, 2018). My guess is that you are using TLSv1.1 protocol or below.
The documentation I listed gives you 4 options:
Upgrade your Java runtime, for example with OpenJDK builds or Oracle paying support
Configure your Java runtime to enable TLS 1.2 by adding -Dhttps.protocols=TLSv1.2
Use a repository manager that uses a Java version supporting TLS 1.2
Revert back to http until you can acheive one of the above remediation steps.
I fixed it myself by just using -Dhttps.protocols=TLSv1.2 as a VM argument.
 
For a permanent solution(mostly required in Java 7) - in you build directory(where you do the mvn command from) add directory:
.mvn (in cmd mkdir .mvn)
and in it create file
jvm.config
and in put the following line:
-Dhttps.protocols=TLSv1.2
The simplest solution is to configure your JVM runtime arguments. On eclipse, you can do it as following :
go to windows>preferences>java>installed JREs
click on the Intalled JRE/JDK you are using for your project
click on "edit" on le right,
and add -Dhttps.protocols=TLSv1.2 in the Default VM arguments Input field.
see the screenshot :
The Package you are trying to install doesn't support TLS1.1 and you might be by default using TLS 1.1. Configure your Java runtime to enable TLS 1.2 by adding -Dhttps.protocols=TLSv1.2 to your Maven Build command would solve the problem.
For Example: mvn clean install -Dhttps.protocols=TLSv1.2
I had upgraded from Java 1.7 to Java 1.8 and got the same error.
To resolve this error, I checked the mvn version using mvn -version and found that JAVA_HOME was still using the Java 1.7. I changed the JAVA_HOME in the environment variable to point to Java 1.8 jre in JDK
Hope this helps someone.
I set my settings.xml file in the .m2 directory just as the one here: https://github.com/alipay/sofa-rpc/pull/190/files
Then exec mvn install and all dependencies started to download. I haven't had a settings.xml file before and I am not using any proxy.
Hope it helps somebody.
PD. I have jdk 1.7 and Maven 3.3.9
I resolved this error in SpringToolSuit by adding settings.xml file in .m2 folder which is located in Users folder of C drive and by updating Project by
right click on pom.xml -> Maeven -> Update Project.
Make sure you select Force update of snapshots/releases.
It needs both : Configuring the JVM runtime arguments as well as having a settings.xml, after I did both it worked. Updating only the JVM runtime argument did not help.
In my case the automatic update of maven dependencies were disabled in eclipse.
Solution:
Go to
Windows->Preferences->Maven
and uncheck the check box
"Do not automatically update dependencies from remote repositories"
Then update the project
Right click on project->maven->update project
I had the same issue, I tried most of the answers here but still did not work. My issue was Proxy settings on my IDE. So it is also important to remember to check proxy settings. File > Settings > System Settings > HTTP Proxy - Select Auto-Detect proxy settings
On my new laptop (Win10) I have downgraded maven 3.8.7->3.6.3 and jdk 19->1.8
Maven 3.8.7 is blocking a lot of old repositories, one sometimes does need.

maven-compiler-plugin:3.1:compile(1 errors) in eclipse [duplicate]

This question already has answers here:
How to force maven update?
(26 answers)
Closed 6 years ago.
I want to learn java spring, so I choose Eclipse Mars2 Version and I want to build a java project using Maven. But I get errors
maven-compiler-plugin:3.1:compile(1 errors)
maven-compiler-plugin:3.1:testCompile(1 errors)
Then I check my maven and java path in my Windows 10 64 bit system variable.
I configured like this :
JAVA_HOME => C:\Program Files\Java\jdk1.8.0_71
M2_HOME => C:\apache-maven
MAVEN_HOME => C:\apache-maven
And in systen variabel path :
%JAVA_HOME%\bin
%M2_HOME%\bin
Everything is fine, I checked it by java -version and mvn-version
Also, I have installed mvn plugin in eclipse in install new software
to maven :
Name: m2e
Location: http://download.eclipse.org/technology/m2e/releases
But I still failed. The errors is still pop up.
Please advise. Thanks.
UPDATE
I test to build a project maven-archetype-quickstart version 1.1
execution default-compile, in Access/pom.xml
execution default-testCompile, in Access/pom.xml
UPDATE AGAIN
<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.tresnamuda</groupId>
<artifactId>access</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>access</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Eclipse detect error in first line which is :
project xmlns="http://maven.apache.org/POM/4.0.0" .....
also in line 8.
This is the complete error :
It seems like there is corruption in your local repository.
Please try:
Delete the local repository, if you use Windows, delete the folder C:\Users\your-user-name\.m2\repository
In Eclipse, right click on the project name and click Maven\Update project ... (or use hotkey ALT+F5)

How does maven find Java compiler?

I am new to learn maven by following this example given by sonatype's book.This is a simplest pom.xml file (works fine). But my question is, how does maven (mvn install) find Java compiler (javac) installed on my machine? This POM file doesn't specify JDK in any way. Thanks for explanation.
EDIT:
As a comparison, I am following next example in the book and this time, I received a compilation error because it can't find javax.My java file is in, as suggested in the book: src/main/java/org/sonatype/mavenbook/web/SimpleServlet.java
Now as I run mvn compile, and i got error message below. Why now maven can't find the compiler:
ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project simple-webapp: Compilation failure: Compilation failure:
[ERROR] /home/abigail/study/simple-webapp/src/main/java/org/sonatype/mavenbook/web/SimpleServlet.java:[4,1] package javax.servlet does not exist
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/maven- v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sonatype.mavenbook.simpleweb</groupId>
<artifactId>simple-webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>simple-webapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>simple-webapp</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
If you go to this path, say with maven version 3.2.3
apache-maven-3.2.3-bin\apache-maven-3.2.3\bin
opne the mvn.bat, you can see below
#REM Maven2 Start Up Batch script
#REM Required ENV vars:
#REM JAVA_HOME - location of a JDK home dir
Clearly it picks JAVA_HOME, from your environment variables. Below code snippet from the file.
#REM ==== START VALIDATION ====
if not "%JAVA_HOME%" == "" goto OkJHome
:OkJHome
if exist "%JAVA_HOME%\bin\java.exe" goto chkMHome
:chkMHome
if not "%M2_HOME%"=="" goto valMHome
for issue, javax.servlet
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
or whichever available
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
Maven uses the environment variable JAVA_HOME to find the JDK.
Since maven itself is a java app, it would just default to the jdk that runs the maven goal.
For more compilation options, you'd have to use:
http://maven.apache.org/plugins/maven-compiler-plugin/
Take a look at this also:
http://maven.apache.org/plugins/maven-compiler-plugin/examples/compile-using-different-jdk.html
EDIT:
To your followup question:
If you look in mvn executable (by default in: /usr/bin/mvn), you'd see it is a rather lengthy bash scriptie that spends a significant amount of time finding where the jdk is. Of course, you can set the JAVA_HOME system property and "help" the script, but even if you don't have it, it is going to attempt finding it, and only when it fails, it is going to spew an error.
From the /usr/bin/mvn:
if [ -z "$JAVA_HOME" ]; then
javaExecutable="`which javac`"
if [ -n "$javaExecutable" -a ! "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
# readlink(1) is not available as standard on Solaris 10.
readLink=`which readlink`
if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
javaExecutable="`readlink -f \"$javaExecutable\"`"
javaHome="`dirname \"$javaExecutable\"`"
javaHome=`expr "$javaHome" : '\(.*\)/bin'`
JAVA_HOME="$javaHome"
export JAVA_HOME
fi
fi
And
if [ -z "$JAVACMD" ] ; then
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
else
JAVACMD="`which java`"
fi
fi
Actually, most of the code of the script is concerned with determining which java to use and where that java is located. However, once you run the mvn executable, that doesn't limit it to using the same jdk for compilation/running that it itself is run with. Herein is where the mvn compiler plugin comes into play, where you can specify additional jdk installations to use.
So i just got maven to pick up different Java location from my default:
Java8
[tomek:~] $ java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
but maven picked up Java9
[tomek:~] $ mvn -version
Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 9-internal, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-9-openjdk-amd64
JAVA_HOME was not set:
[tomek:~] $ echo "Java HOME: $JAVA_HOME"
Java HOME:
So I suspect it just bring first alternative on ubuntu (ignoring users choice):
[tomek:~] $ sudo update-alternatives --config java
There are 3 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority
Status
------------------------------------------------------------
0 /usr/lib/jvm/java-9-openjdk-amd64/bin/java 1091 auto mode
* 1 /opt/java/jdk1.8.0_144/bin/java 1 manual mode
2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode
3 /usr/lib/jvm/java-9-openjdk-amd64/bin/java 1091 manual mode

Categories