compiling down a version - eclipse, maven - java

I was recently stung by some code making it through our deployment cycle without throwing any compile errors when it should have (we thought)...
The code in question being using the new static method Integer.compare which is since Java 1.7.
The server environment is run on Java 1.6. While our development environments have Java 1.7 installed.
Our assumption was that setting the project preferences to JavaSE-1.6 compliance would at least give us compile warnings on the code in question, however no warning or error is visible in eclipse.
Project > properties > java compiler > JDK Compliance > Use compliance from execution environment 'JavaSE-1.6' on the java build path
Secondarily to that, we use maven to compile the final deployment. The pom is clearly directed to comply with the 1.6 compiler:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<optimize>true</optimize>
</configuration>
</plugin>
However the maven build runs successfully with the problem code.
How can I tell both maven and eclipse to fail when code will not work in an earlier Jvm than it is being compiled by?
Thanks, Paul.

Use the maven animal sniffer plugin to tell you when you use APIs that aren't backward compatible. I'm also told that Java 1.7 has a feature for this, but I have no personal experience with it.

Install java 1.6 in the development environment, then right click on the project in eclipse an go to Properties->Java Build Path. Go to the Libraries tab and remove the java 1.7 JRE, then add the java 1.6 JRE.
I'm not familiar enough with maven to answer that half.

Related

Eclipse project: Maven Eclipse Plugin set wrong JRE class path container

I have an Eclipse Mars 2 Java8, Maven 3.5.4 based workspace.
I build the project files with mvn eclipse:clean eclipse:eclipse, and watch the following maven output:
[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-**1.7**
The default JRE for the workspace isn't Java 7, it is Java 8, like you can see looking at the following Eclipse config file:
<workspace>\.metadata\.plugins\org.eclipse.core.runtime\.settings\org.eclipse.jdt.launching.prefs.
that contains the following data:
org.eclipse.jdt.launching.PREF_VM_XML=<?xml version\="1.0" encoding\="UTF-8" standalone\="no"?>
<vmSettings defaultVM\="57,org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType13,1538040823497*">
<vmType id\="org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType">
<vm id\="1431694854640" name\="jdk1.7.0_40" path\="C\:\\Program Files\\Java\\jdk1.7.0_40"/>
<vm id\="1447417000092" name\="jdk1.6.0_45" path\="C\:\\Program Files\\Java\\jdk1.6.0_45"/>
<vm id\="1538040823497" name\="jdk1.8.0_65" path\="C\:\\Program Files\\Java\\jdk1.8.0_65"/>
</vmType>
</vmSettings>
As you may notice by looking at the above configuration, the default VM has the vm id "1538040823497", which is named jdk1.8.0_65, and resides in C\:\\Program Files\\Java\\jdk1.8.0_65.
This VM is correctly registered as Workspace Default in Eclipse Preferences, Java/Installed JREs, is marked as a "perfect match" within the Execution Environment JAVASE-1.8.
I cannot see anything why Maven Eclipse Plugin considers
org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7 as the correct choice, and not JAVASE-1.8.
I already deleted all JREs and registered them newly.
Now there's exactly one 1.6, 1.7 and 1.8 Environment, each with a "perfect match", and 1.8 is checked as default (as you can see above).
I have to manually correct the project each time I generated it (Edit Build Path, change JRE library from 7 to 8), since all files using Java 8 features like streams or lambda functions signal compiler errors unless I assign the correct JRE manually.
I already tried and manipulate org.eclipse.jdt.launching.prefs, redefine all JREs, pray, curse or ask an Ouija board, to no avail, always Java 7 is assigned by the eclipse plugin (version 2.10, by the way).
Any ideas, anyone?
In your pom file, try to use this:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
You may also reference this link:
https://dzone.com/articles/maven-eclipse-and-java-9
Or if all fails, try eclipse oxygen, a newer version (not the newest release but much better than Mars IMO) that has Maven Integration plugin pre-installed, all you need is to add a m2e-connector plugin.

Avoid the "no JREs installed in the workspace that are strictly compatible" warning in Eclipse for multiple Java versions

I know that there are already a lot of questions about this warning, but my question is a little bit different. I understand that I can use this configuration in my pom.xml to fix it
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
This gets rid of the warning, but only if I have Java 7 installed and configured in Eclipse. If for example I only have Java 8, I still see the warning unless I change the version in the configuration above to 1.8:
Build path specifies execution environment JavaSE-1.7. There are no
JREs installed in the workspace that are strictly compatible with this
environment.
My project is compatible with Java 7, 8, 9 and 10. What I want to achieve is to avoid this warning if any of these versions of Java is installed without changing the version in my pom.xml to match the currently installed Java version every time. Something to tell Eclipse that Java 7 or anything above is good.
The project still compiles and works fine with the warning, it's just distracting as it marks the project as having issues. I know that I can hide this type of warnings in Eclipse settings, but is there a way to get rid of it by only using some configuration in pom.xml?
Eclipse uses the value <target>1.7</target> to decide which execution environment to include in the build path, as part of its Maven integration.
The execution environments are mapped to JREs installed in Eclipse (i.e. referenced in Preferences > Java > Installed JREs).
The mapping itself is configured in Preferences > Java > Installed JREs > Runtime Environments. Eclipse will automatically match an execution environment (e.g. JavaSE-1.7) to an installed JRE (e.g. jdk1.7.0_80). If there is no exact match for the execution environment specified in your project, it will give you a warning like the one you're getting.
Therefore, to get rid of the warning, you must either:
Install the Java version that is an exact match for your <target> (i.e. jdk1.7) and add it under Preferences > Java > Installed JREs > Runtime Environments
Configure the Java version in your pom to one for which there is an exact match
My project is compatible with Java 7, 8, 9 and 10. What I want to achieve is to avoid this warning if any of these versions of Java is installed
When configuring the execution environment to use, Eclipse only cares for the <target> value. You need to make sure that there is a JRE installed in Eclipse whose version is a perfect match for the value of that property. Otherwise, the warning will not go away, period.
I don't have the intention to target some specific version, the default target would work fine for me.
No, it wouldn't, because the default target is 1.5. Target configuration is a non-optional configuration property, there is no 'target everything' option.
You could remove the Maven-configured execution environment from the build path and add a JRE directly (project properties > Java Build Path > Libraries > Add library... > JRE System Library > Alternate JRE). This will also make the warning go away, but it will be back as soon as you run Maven project update. Such a configuration shouldn't really be used, as it ties your project to a specific installation of Java on your machine.

Errors regarding Jdk5 in intellij even though pom.xml explicitly sets source to 7

I have opened a maven project in Intellij (14.1.4 Ultimate) and the JDK is not being recognized. When attempting to run one of the programs the following error occurs - and in any case the file has a bunch of "red"s in it when viewed in the IDE:
Error:(55, 50) java: diamond operator is not supported in -source 1.5
(use -source 7 or higher to enable diamond operator)
This is strange because the normal settings are in place to use java7:
In pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
In the project - JDK level is set to 7:
Likewise in the Module:
But we get all sorts of issues when attempting to compile (even after reimporting maven projects):
Note: this project does build/run on command line using maven.
Also note: I have already tried blowing away the Intellij project and rebuilding from scratch. No change in behavior.
Update The following is a result of following (accepted) answer from #Peter Lawrey. We can see that the jdk got mysteriously set to 1.5. I have updated it to 1.7 manually.
Sometimes IntelliJ gets confused though I don't know why. The setting you need is
File -> Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler
Find your module and change it to version 1.7 instead of 1.5.
I have projects with many modules and the same parent pom which sets this and sometimes just one of the many modules will think it's 1.5 when the rest are 1.8. i.e. the same configuration across many modules.

IntelliJ imports project from POM using wrong JDK version

We have a Maven based Android build, and we just made the switch from JDK 6 to 7.
This came with its share of IntelliJ problems though. What happens is that every time it detects a change in the POM, and reimports/refreshes the project, it returns to selecting the old "Module SDK", the one that's configured to use Java 6:
Even if I manually delete these SDKs from the "Platform Settings" dialog, they keep reappearing as "Maven Android API 19 Platform (N)" where N is the number used to disambiguate it from all the other (identical) SDKs.
I should mention that we do specify in the POM that Java 7 is targeted. I tried to set both the compiler plugin language level, and the maven.compiler.* properties (not sure if that accomplishes the same thing or not), without luck:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
shouldn't IntelliJ pick that up and always configure the project to use a Java 7 SDK? Am I missing something?
I noticed that the problem disappears when I remove any references to 1.6 SDKs entirely in IntelliJ. Not surprising I guess, but also not viable since I have other projects that still rely on the presence of a Java 6 SDK.
I encountered a very similar issue with Maven projects I'd created using IntelliJ (version 14.x in my case). I'd configured IntelliJ to use JDK 8 in the Project Settings but the IDE continued to highlight issues in my code (e.g. complaining about the usage of #Override).
It turns out that the Maven Settings take precedence here, which in my case defaulted to JDK 1.5 (hence the IDE redlines). Changing the settings here does resolve the issue, but only temporarily because they revert back whenever the Maven projects are reimported, or when IntelliJ is restarted.
The permanent fix is to explicitly declared the JDK version in your Maven pom file, as explained in these items.
stop IntelliJ IDEA to switch java language level everytime the pom is reloaded (or change the default project language level) by #vikingsteve
IDEA: javac: source release 1.7 requires target release 1.7 by #bacchus
Here's what they've said you need to add to your pom.xml file.
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
These settings get reflected in the Maven Settings in the IDE and resolved the issue for me.
It will pick up the jdk that you choose in project structure please change it there.
File > project structure > project setting > project > project sdk choose 1.7.
If 1.7 is not present go to
File > project structure > Platform setting > SDKs addd 1.7 there.
It's also important to note that you need to change the runner (jdk level) of your maven.
Maven > Runner > JRE

Jenkins Maven compilation fails due to JDK features unsupported? [duplicate]

This question already has an answer here:
Maven - use -source 5 or higher to enable... while building the project
(1 answer)
Closed 8 years ago.
Whenever I deploy a Jenkins build with Maven, it says that I have the 1.3 JDK, even though I have 1.7 installed.
Here is a picture of the error log I get when using an enhanced for loop:
http://gyazo.com/d1c7e297199dbf8a8b6ba23efa5733ba.png
However, I clearly have 1.7 JDK installed.
http://gyazo.com/27b85ba9ea25579aa714b0e2586fd618.png
If anyone knows why this issue occurs and how to fix it, please post below.
Sorry I don't have a ton of formatted information, but I have no idea why this could possibly be happening as I do not have such an outdated JDK and have the most recent updates from Oracle. Maven and Jenkins are also updated completely.
Thanks.
The complaint of maven is not about your jdk version, but about the -source parameter of the compiler. Older Maven versions use a version of the maven-compiler-plugin, which uses -source and -target 1.3 by default.
So, what you need to do is configure you project to use higher source and target levels in order to use your for each loop:
(taken from https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html)
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
Note that simply setting the version of the maven-compiler-plugin would actually be sufficient, since for version 3.1 the default is 1.5, however it is good style to explicitly include the source and target version in your master pom.
You have the project set to compile with the default jdk according to your stack trace. Your default is your %JAVA_HOME% environment variable on the server which is probably set to an older jdk. In order to compile with the 1.7 jdk you have to specify this in your project config. Change jdk : (Default) to your 1.7 name.. which is jdk.
Or you can just go to the maven jdk and change it from (inherit from project) to jdk.
There is already an issue ticket open with jenkins related to this: https://issues.jenkins-ci.org/browse/JENKINS-755

Categories

Resources