- Maven Java Compiler Version
- Maven Java Compiler for Java 8 and Earlier
- Maven Java Compiler Properties
- Maven Java Compiler Plugin
- Full Maven POM File With Java Compiler Version Set
- Maven Java Compiler for Java 9 and Later
- Enable Preview Features
- Setting Java Compiler Version in Maven
- Prerequisite
- Setting the java compiler via maven property
- Setting the java compiler via the plugin config
- Compiler Error Message
- Error Message
- Maven Compile Command
- Maven Console Log
- Example Maven Repo
- Github
- Clone the Repo
- Project pom.xml
- Related Articles:
Maven Java Compiler Version
The default Java compiler version used by Maven is Java 1.5 . Why that is still the default Java compiler version is a good question, since Java 5 was released in 2004. To make Maven compile your Java code with a newer version of the Java compiler, you need to specify the Java compiler explicitly in your project’s POM file ( pom.xml ).
Exactly how you specify the Java compiler version depends on whether you are using Java 8 or earlier, or Java 9 or later. From Java 9 Java got modules which complicates the compilation somewhat in the short run, but should help in the long run.
Maven Java Compiler for Java 8 and Earlier
From Java 8 and earlier, there are two ways to set the Java compiler version in a Maven POM file:
Both of these methods to set the Java compiler version in Maven will be explained in the following sections.
Maven Java Compiler Properties
The first, newest and easiest way to set the Java compiler version in your Maven POM file, is via the Maven Java compiler properties. Here is how the Maven Java compiler properties look:
These properties have to be included in the properties element of your POM file. I usually have the properties element as the last element in my POM files.
Maven Java Compiler Plugin
The second, oldest, and more verbose to set the Java compiler version in your Maven POM files, is via the Maven Java compiler plugin. Here is how the configuration of the Maven Java compiler plugin looks:
org.apache.maven.plugins maven-compiler-plugin 3.6.1 1.8
Full Maven POM File With Java Compiler Version Set
For your convenience, here is a full Maven POM file with the Java compiler version set using both of the above mechanisms. Please note, that only one of the mechanisms should be used in your POM file. The example only shows both examples so you can see where in the POM file they are to be placed.
4.0.0 com.nanosai grid-ops 0.8.0 jar junit junit 4.12 test org.apache.maven.plugins maven-compiler-plugin 3.6.1 1.8 UTF-8 1.8 1.8
Maven Java Compiler for Java 9 and Later
For Java 9 and later, you need to use a slight variant of the Java compiler plugin. Instead of the source and target properties, you need to use the release property. Here is an example of how the Maven Java compiler plugin looks with a release property instead:
org.apache.maven.plugins maven-compiler-plugin 3.8.0 11
This example sets the Java version to 11 — inside the release property. Java 11 is the latest long-term-support (LTS) version of Java at the time of writing.
Notice also that the version of the Maven Java compiler plugin has changed from 3.6.1 to 3.8.0 .
Please keep in mind, that when you upgrade to Java 9+ you will most likely get more issues, like assuring that all Maven plugins and dependencies your Java project uses, are also Java 9+ compatible. Going into detail about how this is done is beyond the scope of this Maven Java compiler tutorial though.
Enable Preview Features
Some of the later Java versions (Java 10, 12 and 13 at least) have had some features which were in preview. A feature in preview is a feature that the Java SDK team has implemented but are not yet sure if should stay in the Java SDK long term. These features are not enabled in the Java SDK by default. To use them you must explicitly enable them. To enable Java preview features in your Maven POM file, insert the compilerArgs element into the compiler configuration, as shown below:
org.apache.maven.plugins maven-compiler-plugin 3.8.0 11 --enable-preview
Setting Java Compiler Version in Maven
This document demonstrates ways to set the java compiler version in maven via the maven.compiler.target property and the maven-compiler-plugin configuration section.
Prerequisite
Setting the java compiler via maven property
Setting the java compiler via the plugin config
- For maven-compiler-version below version 3.8.0 has the default compiler set to 1.5
- Since maven-compiler-version 3.8.0, the default java compiler version is now 1.6
- See also: maven-compiler-plugin doc.
1.10 3.8.0 org.apache.maven.plugins maven-compiler-plugin $ $ $
Compiler Error Message
The following is displayed when a compiler mismatch is detected during compile phase. In this example, the java compiler used (1.7) is less than the required version (1.10).
$ java -version java version "1.7.0_40" Java(TM) SE Runtime Environment (build 1.7.0_40-b43) Java HotSpot(TM) 64-Bit Server VM (build 24.0-b56, mixed mode)
Error Message
The error message as shown on Line 25 of the Maven Console Log
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project mvn-demo-java-compiler-version: Fatal error compiling: invalid target release: 1.10 -> [Help 1]
Maven Compile Command
Maven Console Log
Example Maven Repo
Github
Clone the Repo
$ git clone https://github.com/kapresoft/mvn_set_java_compiler_version.git
Project pom.xml
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"> 4.0.0 com.kapresoft.demo mvn-demo-java-compiler-version jar 1.0-SNAPSHOT mvn-demo-java-compiler-version http://maven.apache.org 1.10 $ $ UTF-8 UTF-8 3.8.0 junit junit 3.8.1 test
org.apache.maven.plugins maven-compiler-plugin $$ $ -->Related Articles:
Object copying is a fundamental aspect of Java programming, finding relevance and utility in diverse contexts. Whether it’s creating independent copies of objects, maintaining object state, or avoiding unintended side effects, understanding efficient and reliable cloning strategies is essential.
When it comes to software development, testing plays a crucial role in ensuring the quality and reliability of the codebase. Test coverage, in particular, is a metric that measures the extent to which the source code of a program has been tested.
In the world of Java development, optimizing code efficiency and reducing boilerplate is a constant pursuit. To address these challenges, various tools and libraries have emerged, and one such popular option is Lombok—a Java library that offers annotations for code generation, resulting in benefits such as time-saving and code simplification. However, as with any tool, there are both advantages and drawbacks to consider.
Java is a versatile programming language that has gained widespread popularity for its platform independence and robustness. In this comprehensive guide, we will delve into the various aspects of Java programming, covering essential concepts, tools, and best practices.
Java vs Kotlin: A Comprehensive Comparison Java and Kotlin are two popular programming languages that have gained significant traction in the software development industry. In this article, we will compare and contrast these languages, exploring their features, advantages, and use cases.
This article discusses converting a string of key-value pairs that are delimited by a specific character, known as a delimiter, into a Map in Java.
Maven and Gradle are two of the most popular build automation tools for Java-based projects. Both tools are designed to simplify the build process, manage dependencies, and facilitate project organization.
In this article, we will provide an overview of virtual threads in Java and their use in concurrent programming. We will define what virtual threads are and how they differ from normal threads. Additionally, we will discuss the benefits of virtual threads over traditional concurrency approaches and provide code examples to illustrate the differences between the two.
When you design an object-oriented system from top to bottom, sometimes the objects that represent the “domain” (what the system is about) don’t match the objects that represent the “entities” (what the system stores). To solve this problem, you can use a technique called “decoupling” to separate the layers of objects.
In Java, the final keyword (also known as a modifier) is used to mark a variable, method, or class as immutable, meaning its value or behavior cannot be modified once it has been initialized.
A Java record is a new feature introduced in Java 14 that allows developers to create a class that is primarily used to store data. A record is essentially a concise way to define a class that consists mainly of state (fields) and accessors (getters).
JDK 17, introduces several new features and improvements, including enhanced random number generators, new encoding-specific methods for the String class, and default classes for Java ciphers. It also removes the experimental AOT and JIT compilers, and introduces support for Sealed Classes and Records. These changes provide developers with more flexibility and control, making it easier to write efficient and secure Java applications.
This article discusses the use of Java Optional to introduce optional values instead of null. We will deep dive into understanding why developers prefer the Optional class to clearly communicate an optional value as opposed to a vague null representation of a variable.
In Java, often times the ability to return a string representing the specified integer is a common task. This article illustrates several mechanisms to convert int to a string in Java. In the opposite scenario, the means to resolve an integer representing the value of the specified String. The returned value is an Integer object that is the equivalent integer value of the argument string.
Converting double to a String value in Java has been a typical task to do for software development. This article discusses the various ways on how to convert a double to a string in Java. While there are advantages in representing a double to its String object representation, the opposite task of converting a String object to a double can also be addressed. This document examines the reasons why conversions of double in Java are beneficial for beginners who are learning to develop in java.
The following page will illustrate how to get started with the maven build system in your java projects. Use this guide as a reference when using Maven for the very first time.
The following page will illustrate how to get started with the Java Programming Language. In addition, this document provides an overview of how to install java and the environment variables you will need to set. A hands-on approach illustrates how to compile and run your first Hello World java code.
The following page will be an excellent guide with getting started with the gradle build system in your Java™ projects. Use this guide as a reference when using Gradle as a build system for the very first time.