Maven compiler plugin specification - java

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.

Related

I want to use sqlserver dependency in maven project(jar) for jre6 which version I should use

I am using jdk1.6 for maven project and I want to use sqlserver dependency in pom.xml file,which version of sqlserver I should write in pom file tag of maven project. I can not change my jdk version as client is having the same environment.
As maven has officially stopped the support of jdk1.6 I requested client to upgrade their environment, and executed on jdk1.8. It worked on 1.8 version.

What if I don't specify source and target version of java in maven pom.xml?

Anyone please tell me what is the default version of source and target version of java used by maven when its not specified in pom.xml
Maven compiles with the maven compiler plugin. The default version is java 1.5 for source and target.
https://maven.apache.org/plugins/maven-compiler-plugin/

Eclipse resetting compiler settings to java 1.5

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.

Maven project with JavaFX (with jar file in `lib`)

I'm setting up a maven project, that is going to use JavaFX. Since I've heard JavaFX does not come with all versions of Java, I downloaded and put the jfxrt.jar file in a lib directory in my project.
1) How do I specify that the dependency (ie., JavaFX) should not be downloaded, but which is located in lib?
2) Does this then mean the project can be built on any machine with JDK (and not necessary JDK 1.7 - update 9+) ?
2019 Update for Recent JavaFX Versions
As of Java 11, JavaFX has been decoupled from the JDK, so JavaFX libraries are no longer automatically bundled together with a JDK install (as they were for instance in the Oracle JDK 8 distribution). Instead, the JavaFX system is now designed as a set of separate libraries independent from the JDK, which can be downloaded from a repository by a tool such as Maven for use by your application.
openjfx.io have a very short tutorial on using JavaFX 11+ with Maven:
https://openjfx.io/openjfx-docs/#maven
This tutorial recommends use of the OpenJFX JavaFX Maven Plugin and provides the following sample maven pom.xml project file for a "hello, world" style application.
<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.openjfx</groupId>
<artifactId>hellofx</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>12.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.2</version>
<configuration>
<mainClass>HelloFX</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
Note that the org.openjfx JavaFX maven plugin used for Java 11+ is different from the com.zenjava JavaFX maven plugin used for Java 8 and 9, so use the appropriate one for your Java version.
Suggested Approach
For building JavaFX apps with Maven:
Use the com.zenjava maven JavaFX plugin with Java 8 and 9 AND/OR
Use Java 8 or 9 and don't specify any kind of Maven dependency for JavaFX OR
Use the org.openjfx maven JavaFX plugin with Java 11+.
Unlike Java 7, with Java 8 it is unnecessary to set JavaFX as a maven dependency, because JavaFX is on the standard Java runtime classpath (similar to the way Swing is today).
Opinion
I think the approach in your question of placing the jfxrt.jar file in a project lib directory is flawed.
The reasons it is flawed are:
JavaFX also includes native libraries, so you would need to include them as well (in a location where jfxrt.jar would be able to find them).
A given version of JavaFX will only be certified to operate against the version of the JDK it is shipped with, so the JavaFX runtime you place in your project's lib directory may not work against a later JDK version, such as JDK 8.
Future versions of the JDK such as JDK 8 will include JavaFX on the default classpath of the JDK, so you may get conflicts with the version you place in your lib directory.
In summary, JavaFX should be treated as part of the Java runtime system, not as a project library.
JavaFX System Dependency Sample
If you must use Java 7 and you don't want to use the JavaFX maven plugin, then you can take the less recommended approach:
Reference the JavaFX library from the jdk (not your project lib directory) as a maven system dependency.
This sample is provided for Java 7 only and is not required (and will not work as is) for Java 8. Java 8, places the jfxrt.jar in ${java.home}/lib/ext/jfxrt.jar, which is on the default Java runtime classpath, making a system dependency for Java 8 irrelevant.
<dependency>
<groupId>javafx</groupId>
<artifactId>jfxrt</artifactId>
<version>${java.version}</version>
<scope>system</scope>
<systemPath>${java.home}/lib/jfxrt.jar</systemPath>
</dependency>
If you package your application using a system dependency like this, then you need will need to write some execution instructions for your users so that they can add jfxrt.jar to the runtime classpath when running your application on Java 7. This is a very good reason not to use this approach.
If you choose to use the maven system dependency approach, you may also wish to use the maven antrun plugin to embed calls to the JavaFX ant tasks as in this sample. You may also want to use the maven enforcer plugin to ensure that your application is built against at least the minimum required version of Java containing a JavaFX version required by your application to work.

Maven creates class file 50.0, should be 49.0

This is my setup:
JDK 1.5 and JDK 1.6 installed, JAVA_HOME points to JDK 1.6
pom.xml uses maven-compiler-plugin with <source>1.5</source> and <target>1.5</target>
mvn --version displays Java version: 1.6.0_06
Maven 2.2.1
Is it correct that Maven 2.2.1 creates class files with file version 50.0 with this configuration?
if I understand Maven correctly, it just means it runs on the 1.6 runtime. The settings in the pom should determine the generated classfile format.

Categories