Jenkins Build Maven Project with Java 8 and Java 7 - java

I have 2 applications, Apps A uses Java 8 and Apps B uses Java 7.
Furthermore, my company using Jenkins and Java 7.
I need to build Apps A without change the original environment.
Is there any method that I can choose between Java 7 and Java 8 to build an application with Jenkins?
or
Can I choose java version when maven build?
Thank you

Jenkins can manage multiple versions of the Java JDK which can be selected when creating each build job:
https://www.safaribooksonline.com/library/view/jenkins-the-definitive/9781449311155/ch04s05.html

With maven you can specify different build profiles, the profiles can be automatically selected depending on wich JDK you are executing maven targets with. This is an example for that, here activated using jdk 1.4:
<profiles>
<profile>
<activation>
<jdk>1.4</jdk>
</activation>
...
</profile>
</profiles>
Read more about profiles here: http://maven.apache.org/guides/introduction/introduction-to-profiles.html
You can also specify witch buildversion to use with a build plugin like this:
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.4</source>
<target>1.4</target>
</configuration>
</plugin>
</plugins>
[...]
Read more about source and target versions here: https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
By combining these two possible configurations in you pom I think you should be able to set maven to automatically creating the target java version for your liking.
Profiles can use many activation methods to determine what to do so further options are available.

Related

Method introduced in java 9 is compiled using compiler target java 8

I'm experiencing an odd behavior in maven as well as in eclipse itself.
Even though i configured my project to be compiled in Java 1.8, I can compile and run (eclipse) a piece of code that was introduced in Java 9
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
The code line in question:
LocalTime.ofInstant(cal.toInstant(), cal.getTimeZone().toZoneId());
I'm using Oracle's JDK 11 locally for compiling and running in eclipse without any errors. When i package it into a docker container using openjdk:8-jdk-alpine it will boot up, but throw the following Exception when I call the method:
java.lang.NoSuchMethodError: java.time.LocalTime.ofInstant(Ljava/time/Instant;Ljava/time/ZoneId;)Ljava/time/LocalTime
How can I avoid and identify these situations before they go to testing? Am I doing something wrong or is it a bug in the build system or in JDK11?
thanks in advance
The source option specifies that the source code must be compatible with Java 8, the target option that the classes should be compatible with Java 8. However, you will still compile with the Java 11 class library if you build with Java 11 and then you can get errors like the one you have.
There are two good solutions. One is to use the Maven toolchains plugin and build with Java 8. Then you can have multiple Java versions installed and Maven will use the configured one on a per-project basis.
The other is to use the new release and testRelease options. They will build with API classes from the given release. Just add <release>1.8</release>.
If you are using JDK 11, configure your maven pom.xml like that:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
</plugins>
</build>

What's the difference between `spring-boot-maven-plugin` and `maven-compiler-plugin`?

I do not know the difference between spring-boot-maven-plugin and maven-compiler-plugin.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
Is that mean Spring Boot Maven Plugin's feature include maven-compiler-plugin?
I just use Spring Boot Maven Plugin is ok, do not need add 2 plugins??
"Spring Boot Maven Plugin provides Spring Boot support in Maven, letting you package executable jar or war archives and run an application “in-place”."
"Maven Compiler Plugin is used to compile the sources of your project."
maven-compiler-plugin has two goals. Both are already bound to their proper phases within the Maven Lifecycle and are therefore, automatically executed during their respective phases.
compiler:compile is bound to the compile phase and is used to compile the main source files.
compiler:testCompile is bound to the test-compile phase and is used to compile the test source files.
To understand more about maven build lifecycle - http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference
maven-compiler-plugin usages -
To compile source code using -source and -target javac Options
To compile source code using a different JDK
To compile source code using Memory Allocation Enhancement
To Pass Compiler Arguments
Most commonly used to define source and target versions.
Sometimes you may want 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. maven-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.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
[...]
</project>
or configure the plugin directly:
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
Technically we can use both spring-boot-maven-plugin and maven-compiler-plugin in combination if the requirement is to create an executable jar as well as make sure source and target code have a specific version (which is accomplished by including maven-compiler-plugin).
In my case, i didn't use in combination but when my java project is a spring boot app that needs to run as a micro-service etc then we need an executable jar as build output so used spring boot maven plugin (only) but my other java project that consists of spring beans or components and is going to be used as a spring enabled library in other external apps but not required to run on its own but had to make sure source and target versions are specified then normal "mvn package" generated jar should work. For that maven compiler plugin (only) should do the job.

How to make Maven Javadoc Plugin work with any Java version

I'm using Maven Javadoc Plugin like this
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
I have multiple JDK versions installed and want to be able to build a project with any of them. The above configuration works fine with Java 8, but it fails with Java 11 with the following error
Failed to execute goal
org.apache.maven.plugins:maven-javadoc-plugin:3.1.1:jar
(attach-javadocs) on project ...: MavenReportException: Error while
generating Javadoc: Unable to find javadoc command: The environment
variable JAVA_HOME is not correctly set.
The obvious solution is to set JAVA_HOME. But as I mentioned, I have multiple versions of JDK, so reconfiguring JAVA_HOME every time I want to use another version wouldn't be convenient at all.
Another solution (from Unable to find javadoc command - maven) is to add the following configuration to the plugin:
<configuration>
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
</configuration>
This makes it work with Java 11, but breaks Java 8 because the javadoc location is ${java.home}/../bin/javadoc in that case.
Ideally I always want to use javadoc from the directory where the java executable that Maven uses is, but I haven't found a way to do it with Maven Javadoc Plugin.
Maven profiles may be able to help.
Add something like this to your POM.
<profiles>
<profile>
<id>jdk-8-config</id>
<activation>
<jdk>1.8</jdk>
</activation>
<properties>
<javadocExecutable>${java.home}/../bin/javadoc</javadocExecutable>
</properties>
</profile>
<profile>
<id>jdk-11-config</id>
<activation>
<jdk>11</jdk>
</activation>
<properties>
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
</properties>
</profile>
</profiles>
JDK ranges are also possible, read more on those in the linked doc.
Maven will pick up the version of Java being used to run itself, activate the correct profile, and define the property correct.
A caveat - typically we build with JAVA_HOME set, or via Jenkins which can be configured to define JAVA_HOME per job. This approach works well in those cases.
You could also investigate Maven toolchains. I have no experience with those, other than reading they help make it easier to define tool locations on various machines.

Specifying Java version in maven - differences between properties and compiler plugin

I'm not very experienced with Maven and while experimenting with multi-module project I started wondering how can I specify Java version for all my child modules in parent Maven pom. Until today I was using just:
<properties>
<java.version>1.8</java.version>
</properties>
...but when researching I found that you can also specify Java version in Maven compiler plugin, like that:
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
And then wrap this into plugin management tag to enable child poms usage of this. So the first question is this:
What are the differences beetwen setting Java version in properties and in Maven compiler plugin?
I couldn't find clear answer but in process of researching I found that you can also specify Java version in this way:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
...which suggest that compiler plugin is there even if I don't explicit declare it. Running mvn package outputs with
maven-compiler-plugin:3.1:compile (default-compile) # testproj ---
...and some other plugins that I didn't declare.
So are those plugins default, hidden part of Maven pom? Are there any differences between setting source/target in properties and in Maven plugin configuration element?
Some other questions are - which way should be used (and when if they are not equal)? Which one is best for multi-module project and what happens if Java version specified in pom is different than version pointed in JAVA_HOME?
How to specify the JDK version?
Use any of three ways: (1) Spring Boot feature, or use Maven compiler plugin with either (2) source & target or (3) with release.
Spring Boot
<java.version> is not referenced in the Maven documentation.
It is a Spring Boot specificity.
It allows to set the source and the target java version with the same version such as this one to specify java 1.8 for both :
1.8
Feel free to use it if you use Spring Boot.
maven-compiler-plugin with source & target
Using maven-compiler-plugin or maven.compiler.source/maven.compiler.target properties are equivalent.
That is indeed :
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
is equivalent to :
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
according to the Maven documentation of the compiler plugin
since the <source> and the <target> elements in the compiler configuration use the properties maven.compiler.source and maven.compiler.target if they are defined.
source
The -source argument for the Java compiler.
NOTE: Since 3.8.0 the default value has changed from 1.5 to 1.6. Since 3.9.0 the default value has changed from 1.6 to 1.7
Default value is: 1.7.
User property is: maven.compiler.source.
target
The -target argument for the Java compiler.
NOTE: Since 3.8.0 the default value has changed from 1.5 to 1.6. Since 3.9.0 the default value has changed from 1.6 to 1.7
Default value is: 1.6.
User property is: maven.compiler.target.
About the default values for source and target, note that
since the 3.8.0 of the maven compiler, the default values have changed from 1.5 to 1.6.
maven-compiler-plugin with release instead of source & target
The maven-compiler-plugin 3.6 and later versions provide a new way :
org.apache.maven.plugins
maven-compiler-plugin
3.8.0
9
You could also declare just :
<properties>
<maven.compiler.release>9</maven.compiler.release>
</properties>
But at this time it will not work as the maven-compiler-plugin default version you use doesn't rely on a recent enough version.
The Maven release argument conveys release : a new JVM standard option that we could pass from Java 9 :
Compiles against the public, supported and documented API for a
specific VM version.
This way provides a standard way to specify the same version for the source, the target and the bootstrap JVM options.
Note that specifying the bootstrap is a good practice for cross compilations and it will not hurt if you don't make cross compilations either.
Which is the best way to specify the JDK version?
The first way (<java.version>) is allowed only if you use Spring Boot.
For Java 8 and below :
About the two other ways : valuing the maven.compiler.source/maven.compiler.target properties or using the maven-compiler-plugin, you can use one or the other. It changes nothing in the facts since finally the two solutions rely on the same properties and the same mechanism : the maven core compiler plugin.
Well, if you don't need to specify other properties or behavior than Java versions in the compiler plugin, using this way makes more sense as this is more concise:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
From Java 9 :
The release argument (third point) is a way to strongly consider if you want to use the same version for the source and the target.
What happens if the version differs between the JDK in JAVA_HOME and which one specified in the pom.xml?
It is not a problem if the JDK referenced by the JAVA_HOME is compatible with the version specified in the pom but to ensure a better cross-compilation compatibility think about adding the bootstrap JVM option with as value the path of the rt.jar of the target version.
An important thing to consider is that the source and the target version in the Maven configuration should not be superior to the JDK version referenced by the JAVA_HOME.
A older version of the JDK cannot compile with a more recent version since it doesn't know its specification.
To get information about the source, target and release supported versions according to the used JDK, please refer to java compilation : source, target and release supported versions.
How handle the case of JDK referenced by the JAVA_HOME is not compatible with the java target and/or source versions specified in the pom?
For example, if your JAVA_HOME refers to a JDK 1.7 and you specify a JDK 1.8 as source and target in the compiler configuration of your pom.xml, it will be a problem because as explained, the JDK 1.7 doesn't know how to compile with.
From its point of view, it is an unknown JDK version since it was released after it.
In this case, you should configure the Maven compiler plugin to specify the JDK in this way :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerVersion>1.8</compilerVersion>
<fork>true</fork>
<executable>D:\jdk1.8\bin\javac</executable>
</configuration>
</plugin>
You could have more details in examples with maven compiler plugin.
It is not asked but cases where that may be more complicated is when you specify source but not target. It may use a different version in target according to the source version. Rules are particular : you can read about them in the Cross-Compilation Options part.
Why the compiler plugin is traced in the output at the execution of the Maven package goal even if you don't specify it in the pom.xml?
To compile your code and more generally to perform all tasks required for a maven goal, Maven needs tools. So, it uses core Maven plugins (you recognize a core Maven plugin by its groupId : org.apache.maven.plugins) to do the required tasks : compiler plugin for compiling classes, test plugin for executing tests, and so for... So, even if you don't declare these plugins, they are bound to the execution of the Maven lifecycle.
At the root dir of your Maven project, you can run the command : mvn help:effective-pom to get the final pom effectively used. You could see among other information, attached plugins by Maven (specified or not in your pom.xml), with the used version, their configuration and the executed goals for each phase of the lifecycle.
In the output of the mvn help:effective-pom command, you could see the declaration of these core plugins in the <build><plugins> element, for example :
...
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>default-clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>default-testResources</id>
<phase>process-test-resources</phase>
<goals>
<goal>testResources</goal>
</goals>
</execution>
<execution>
<id>default-resources</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
...
You can have more information about it in the introduction of the Maven lifeycle in the Maven documentation.
Nevertheless, you can declare these plugins when you want to configure them with other values as default values (for example, you did it when you declared the maven-compiler plugin in your pom.xml to adjust the JDK version to use) or when you want to add some plugin executions not used by default in the Maven lifecycle.
None of the solutions above worked for me straight away. So I followed these steps:
Add in pom.xml:
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
Go to Project Properties > Java Build Path, then remove the JRE
System Library pointing to JRE1.5.
Force updated the project.
The below steps work for me like charm! so thought to share with everyone.
These are the lines i added in the pom.xml file to work with a basic project. I am using Java 12 (you can replace yours 11, 10, 1.8 etc).
<properties>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>12</release>
</configuration>
</plugin>
</plugins>
</build>
After changing the pom file please reload your project so that IDE can download/fetch the plugin to the project. (For IntelijIDEA: Right-click on pom.xml -> Go to maven -> Reload project).
please make sure to configure the desire version in your IDE as well.
if you are using IntelliJ idea maven build.
Consider the alternative:
<properties>
<javac.src.version>1.8</javac.src.version>
<javac.target.version>1.8</javac.target.version>
</properties>
It should be the same thing of maven.compiler.source/maven.compiler.target but the above solution works for me, otherwise the second one gets the parent specification (I have a matrioska of .pom)
For NetBeans IDE, changing project properties - (Jersey Service) - Categories > Sources >
Selected 'Source/Binary Format' as 1.8.

stop IntelliJ IDEA to switch java language level every time the pom is reloaded (or change the default project language level)

Using IntelliJ 12, I have a java project and I use maven with a pom.xml.
My project is using java8, but it seems the default project language level has been set to 6 while importing the project.
I can change the language level to 8.0 (F4 -> Modules -> Language level) however every time I edit my pom.xml the project level is switched back to "use project language level", and I have to edit this settings again and again.
Is there something I need to add to the pom.xml to set the default language level to 8.0?
As per Mark's comment, here is how to do it:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
A shorter version of vikingsteve's answer is:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
I'm upgrading a project from JDK 8 to JDK 10+. I had the compiler properties specified correctly as follows:
<properties>
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>
</properties>
However the Idea project would keep resetting the language level to 8.
Eventually I figured out that Idea's Maven import process was using JDK 8 to import the project which limited the language level to <= 8.
To fix I updated the 'JDK for importer' property under Settings -> Build, Execution, Deployment -> Build Tools -> Maven -> Importing to use JDK 11.
I think this has to do with a conceptual conflict between the Maven compiler plugin and IntelliJ idea. Apparently the newer versions of the compiler plugin have a default level of 1.5 (see http://maven.apache.org/plugins/maven-compiler-plugin/). So if the compiler plugin is used at all in a project, and the compiler level is not explicitly set in the pom.xml, whenever the POM is re-processed the level will revert to the default.
So there is a conceptual conflict which is ignored by Intellij IDEA. The IDE still allows one to set the project and module settings, but provides no warning or feedback that this setting is controlled by pom.xml. Solutions would either be to explicitly allow overriding the POM compiler plugin setting (perhaps not wise because what then happens when you use maven on the command line), or to deactivate the controls in the IDE when this setting from the POM is in effect.
The solution at the present time is to set the desired compiler level in the compiler plugin in the pom, the re-import, rather than trying to set it in module settings.
There are two ways of doing this, add either one of them in your pom.xml file:
First- Add Properties
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
second- Add Plugin
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
Let me know if it helps.
None of the solutions helped in my case. I didn’t need to specify any Java version in my pom.xml.
I needed to open the <project-name>.iml file and change the JDK version there.
Original:
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
<!-- ... ^ -->
<!-- ... | -->
Updated:
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<!-- ... ^ -->
<!-- ... | -->
This makes no sense at all. At no point have I specified a JDK version for Java 1.5.
What solved the issue for me was a list of updates:
In Project Structure > modules: changes the language level Java version to 8
In Project Structure > Project: Java version should be 1.8
In pom file, same changes specified in the responses above
In Settings > Java compiler > changed the bytecode versions to 8
In Settings > maven > importing > JDK for importer should be 1.8
I struggled a lot with this problem, due to building microservices with Dropwizard. Eventually I found out that I had my build properties in the wrong pom file (The main service's pom.xml).
So, even though the other packages are more like libraries, I were not able to use the Java 8 syntax.
When I refactored the build plugin into the "global" .pom.xml" file, all child containers were able to use the new syntax.
May help someone having issues with multi-container projects
thank you it works.
be careful not to make the same mistake I did.
if you have profiles, add the plugin in the right profile.
<profiles>
<profile>
<id>foo</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>bar</id>
.......
</profile>
<profiles>
For me the solution of updating the POM (plugins plus properties) to the required Java compiler version (1_7 in my case) worked.
However as the .iml file for each project was generated with original pom (with default compiler version of 1_5 as explained by someone above) has a JDK version of 1_5, this still overrides the pom version.
I deleted the .idea folder manually and imported the module into IntelliJ with a reimport from the updated pom. When I reimported the Module from updated POM,I could see that the iml files had the updated JDK version (1_7 in my case) .
There was one additional step I had to follow, in addition to setting the maven build properties, adding the maven-compiler-plugin, and modifying the Java version in the .iml file. (each documented already in the other answers). You also have to set the compiler version in the settings.
Problem: Expected Java version: 11 but Stuck at version: 8
I tried almost all the answers, still I was stuck with language_level 8 and nowhere in my project or in the Intellij IDE I found any trace that could relate to Java version 8.
Then I explored the pom.xml of the parent with no success but in the pom.xml of the grand-parent, I found that the release version is 8.
Solution:
Adding the following 3 lines in the properties made the difference.
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.release>11</maven.compiler.release>
</properties>

Categories

Resources