Eclipse resetting compiler settings to java 1.5 - java

I have just cloned a git repository for a maven project and then imported the project into STS as a maven project.
I set up the project and sub-modules as java 1.8 projects and then ran a maven update and then noticed that all the java 1.5 compiler settings seem to have been reapplied.
I cant figure out why eclipse is resetting this, even if I uncheck 'Enable project specific settings' it still reverts back to having this checked and for java 5 to be the default.
I read a post about setting the version in the maven-compiler-plugin configuration but this project does not currently have any configuration for that plugin in any of the pom files.

In your pom.xml put this:
<project>
...
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
...
</project>
The reason you'll have to do this is that "the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with". See Maven Compiler Plugin for further details.
When you run Maven update command from Eclipse without setting the explicit java version, the project will take the default Maven settings, that's why you end up with 1.5 Java version.

Eclipse Photon Release (2018) only seems to recognize up to Java 10. Using
<project>
...
<properties>
<jdk.version>11</jdk.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
...
</project>
caused it to revert to 1.5. Replacing "11" with "10" was the best I could do to avoid compiler errors.

Related

Eclipse JAX-RS (REST Web Services) 2.1 requires java 1.8 or newer [duplicate]

i'm new to web service and i'm trying to do a project using jax-rs rest and spring in eclipse.
I use java 1.8 but eclipse shows me an error that jax-rs 2.0 requires java 1.6 or newer error and my project won't work
This is the project explorer and error's screenshot. I tried to google it but can't get any english solutions
Edit : It seems like the screenshot's quality is low if i try to display it so here is the imgur link for the screenshot for better quality
http://i.imgur.com/YYyoeUX.png
Maven projects come with a bunch of plugins applied implicitly to the build. One of them being the maven-compiler-plugin. Unfortunately the Java version for the plugin defaults to 1.5. Most Maven project you see will override the Java version simply by declaring the plugin (in your pom.xml file) and configuring the Java version
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Eclipse (with m2e plugin) will set the compiler compliance level to the setting in the plugin. You can view this by going to
Right click on the project -> properties -> Java Compiler -> Look at compliance level
UPDATE
Instead of the above compiler plugin configuration, you can also simply just do
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
The default plugin configuration will look for these properties. It's a lot less verbose than re-declaring the plugin.
After adding the properties or the compiler plugin to your pom.xml, you might need to update the project. See Manish's answer.
I know the topic is old but I had the same problem and it's hard to find informations on it.
So set the properties wasn't enough for me, I had also to delete the project from Eclipse, then delete the .project file and reimport the project as a Maven project (even if I created it as a Maven project already) like it is answer on this topic.
those who are doing with properties way and if it is not working do this step.
in Pom.xml
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
After that do this:
project folder-> maven--> update project--> check force update-->ok
After that do
project folder-->run as-->mvn build with goal eclipse:eclipse
you don't need to re-import the project as suggested in other answer/
Just got to the path of the project and go to settings and find the xml file named "org.eclipse.wst.common.project.facet.core" and change the version of java from there

Update Project version automatically in Maven 3.6.0

I am using Maven 3.6.0 in a project where the version of my project is set using a revision property as described in https://maven.apache.org/maven-ci-friendly.html and I want to automatically update the project's version in command line.
I tried to look on the side of maven release plugin (https://maven.apache.org/maven-release/maven-release-plugin/update-versions-mojo.html) using the release:update-versions command, which seems to work, but instead of updating the property revision it updates the version property.
Current setup :
<version>${revision}</version>
<properties>
<revision>1.1-SNAPSHOT</revision>
</properties>
When running mvn release:update-versions :
<version>1.2-SNAPSHOT</version>
<properties>
<revision>1.1-SNAPSHOT</revision>
</properties>
Expected:
<version>${revision}</version>
<properties>
<revision>1.2-SNAPSHOT</revision>
</properties>
Following is the explanation why this is happening:
When mvn release:update-versions is executed then it will bump the version by first fetching value of revision property. This command does not care about updating property value as well.
In general, property value in maven can be updated using -D flag within command line as follows:
mvn release -Drevision=1.2-SNAPSHOT
Following links could also be helpful:
https://maven.apache.org/maven-release/maven-release-plugin/examples/non-interactive-release.html
https://maven.apache.org/maven-release/maven-release-plugin/update-versions-mojo.html
Finally, I achieved to get the same result by using maven versions plugin, but it's not using ${revision} tag.
mvn versions:set -DnextSnapshot=true
Versions plugin is much more flexible than release plugin for managing your project's version.

Can't change Java compiler JDK compliance in only one project in Spring Tool Suite

I recently got a new PC and imported the projects I work on in my workspace.
After that I noticed that one of the projects has the wrong JDK compliance level. I wanted to change away from "Use compliance from execution enirvonment 'JavaSE-1.7' on the 'Java Build path'" settings but can't change it. Clicking "apply" doesn't change a thing. But with the other projects it is possible. (But they have "Use compliance from execution enirvonment 'J2SE-1.5' on the 'Java Build path'")
What am I doing wrong? My colleague installed everything the same way I did and she can change it.
The problem is that I can't start my non-working problem because of this wrong version.
PS: I'm on Spring Tool Suite 3.8.4
Assuming your project is a maven project. You should set the Java source and target levels via your pom.xml file. Something like this:
<project>
[...]
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
[...]
</project>
Once you've done that... If m2e (eclipse maven tooling) does not automatically update your project's compliance level in Eclipse, you may need to tell m2e to update project configuration via project's context menu:
Right click the project.
Select menu 'Maven >> Update Project'
References:
maven docs

Run Maven archetype "quickstart" in Java 10?

When I create a new IntelliJ 2018.1 project from the Maven archetype maven-archetype-quickstart version 1.3 using Oracle 10.0.1 on macOS Sierra, I get this error when doing a Maven install.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.20.1:test (default-test) on project h2e2: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.20.1:test failed.: NullPointerException -> [Help 1]
I am running this directly after creating the new project. I am writing no code. I can run the default line:
public static void main( String[] args ) { System.out.println( "Hello World!" ); }
…by choosing Run 'App.main()' from the context menu clicked in the code editor. That works, but the Maven install fails.
Executing clean and install in the Maven pane of IntelliJ does not solve the problem.
If I switch IntelliJ File > Project Structure > Project > Project SDK > from 10.0.1 to 1.8.0_171 (and set Project language level to 8 - Lambdas, type annotations etc.), then the Maven install succeeds.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
If I return that setting to Java 10, the Maven install fails again, with same error.
I am running external Maven 3.5.3 rather than IntelliJ’s bundled older version, for no particular reason.
My older existing projects based on Java 8 have no problem. My only problem is trying to create new Maven-based quickstart projects using Java 10.
Later version of maven-surefire-plugin
It seems the archetype is not yet updated for Java 10.
According to this blog post: http://joshlong.com/jl/blogPost/java-10.html, you can do the following to get it up and running...
.... I want to use Java 10, though, so I opened the pom.xml file and
changed the java.version property value to be 10. The Maven surefire
plugin broke the build next; it was complaining about not being able
to parse the version of the JDK. I overrode the version by redefining
the property for the plugin's version:
2.21.0.
The important part here is I think updating to the higher version of surefire, as mentioned in the comments. As of 2018-10, the current version is 2.22.1.
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
The Answer by Maarten is correct and crucial.
Set compiler source/target to 10
In addition to that Answer’s solution updating the version maven-surefire-plugin, to run that Maven "Quickstart" archetype based project on Java 10, you will need to change the POM file to indicate Java 10 is to be used by the compiler.
Change this:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
to replace the pair of 1.7 occurrences to 10:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>
</properties>

Maven compiler plugin specification

If my JDK version is 1.8 and I suppose my project will be running on JDK 1.8. Do I still need to specify this maven property in my pom file?
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
I thought if I am using Jdk 1.8 and I want my project to be compatible with JDK 1.7, then I need this specification. Is that true?
You must always specify these properties, otherwise Maven will default them to 1.5.

Categories