How to configure Maven to use Java 8

In pom.xml, defined this maven.compiler.source properties to configure Maven to use Java 8 to compile the project.

Sometimes when you may need to compile a certain project to a different version than what you are currently using. The javac can accept such command using -source and -target. The Compiler Plugin can also be configured to provide these options during compilation.

For example, if you want to use the Java 8 language features (-source 1.8) and also want the compiled classes to be compatible with JVM 1.8 (-target 1.8), you can either add the two following properties, which are the default property names for the plugin parameters:

<project>
  [...]
  <properties>
    <maven .compiler.source="">1.8</maven>
    <maven .compiler.target="">1.8</maven>
  </properties>
  [...]
</project>

or configure the plugin directly:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupid>org.apache.maven.plugins</groupid>
        <artifactid>maven-compiler-plugin</artifactid>
        <version>3.6.1</version>
        <configuration>
          <source></source>1.8
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

Merely setting the target option does not guarantee that your code actually runs on a JRE with the specified version. The pitfall is unintended usage of APIs that only exist in later JREs which would make your code fail at runtime with a linkage error. To avoid this issue, you can either configure the compiler’s boot classpath to match the target JRE


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java – LongBinaryOperator Interface
  2. Java 8 Convert Array to Stream
  3. Java – IntConsumer Interface
  4. Java – LongToDoubleFunction Interface
  5. Java – ToIntBiFunction Interface
  6. Java 8 Sort Map by Value
  7. Java – DoubleSupplier Interface
  8. Java – BinaryOperator Interface
  9. Java – DoubleFunction Interface
  10. Java – DoubleConsumer Interface
  11. Java – LongToIntFunction Interface
  12. Java – BiConsumer Interface
  13. Java – DoubleToLongFunction Interface
  14. Java – LongSupplier Interface
  15. Java – BaseStream Interface

Be the first to comment

Leave a Reply