Is it possible to create spring boot maven project with java 12? - java

I am trying to create web app using spring boot, maven and java 12. I started the project with spring-boot-starter-parent version 2.1.5.RELEASE, java 12 and maven-compiler-plugin version 3.8.1. I tried to run ./mvnw package but i got this error:
Fatal error compiling: invalid target release: 12
After that i wrote on the internet that maven compiler plugin uses old dependency for asm and i should specify new version of it. So i selected the latest version of asm which is 7.1 and i added configuration tag with properties release 12 and compilerArgs --enable-preview for java 12. The result was:
Fatal error compiling: invalid flag: --release
The next thing that i tried was to use spring boot starter parent version 2.2.0.M3 and to specify the repository to find milestone versions of spring from http://repo.spring.io/milestone. Unfortunately i got the same result. Any ideas?

Trying with previous versions to spring boot 2.2.0.MX it also failed for me, but I have managed to make it work with 2.2.0.M6 including some plugins and tweaking them to adapt with the preview features like:
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<arguments>--enable-preview</arguments>
<jvmArguments>--enable-preview</jvmArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
</plugins>
This is now working with Java 13.

Related

Unable to build Java project targeting JDK 11, Invalid target release 11

I have JDK 11 installed along with Maven 3.6.2 and am using error prone to compile my Java maven projects. With this configuration:
pom.xml:
<properties>
<java.version>11</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<compilerId>javac-with-errorprone</compilerId>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
<useIncrementalCompilation>false</useIncrementalCompilation>
<compilerArgs>
<arg>-Xep:ParameterName:OFF</arg>
</compilerArgs>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-javac-errorprone</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_core</artifactId>
<version>${google.error.prone.version}</version>
</dependency>
</dependencies>
</plugin>
I get this error:
CompilerException: InvocationTargetException: invalid target release: 11
I tried following this information:
Unable to compile simple Java 10 / Java 11 project with Maven
However, upgrading ASM did not change anything.
As per the maven documentation:
The Compiler Plugin is used to compile the sources of your project.
Since 3.0, the default compiler is javax.tools.JavaCompiler (if you
are using java 1.6) and is used to compile Java sources. If you want
to force the plugin using javac, you must configure the plugin option
forceJavacCompilerUse.
Also note that at present the default source setting is 1.6 and the
default target setting is 1.6, independently of the JDK you run Maven
with. You are highly encouraged to change these defaults by setting
source and target as described in Setting the -source and -target of
the Java Compiler.
After changing the source and target to 11 as follows:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
the above mentioned error of invalid java 11 version didn't appear
I put in properties this: <java.version>1.11</java.version> and in maven compiler pugin of this way: <source>11</source> <target>11</target>.
It works for me.

maven-site plugins 3.3 java.lang.ClassNotFoundException: org.apache.maven.doxia.siterenderer.DocumentContent

Since this night, maven site 3.3 plugins stop to work.
Try to delete local repository, but no change.
Maven 3.3.9
java 1.8
No config or dependencies defined in pom for site plugins
[WARNING] Error injecting: org.apache.maven.report.projectinfo.CiManagementReport
java.lang.NoClassDefFoundError: org/apache/maven/doxia/siterenderer/DocumentContent
I had just started to get this issue also during builds. What worked for me was to specifically define the maven-site-plugin and the maven-project-info-reports-plugin along with the version numbers in the pom.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
This is caused by maven-project-info-reports-plugin updated to 3.0.0, and rely on doxia-site-renderer 1.8 (and have org.apache.maven.doxia.siterenderer.DocumentContent this class), but maven-site-plugin:3.3 rely on doxia-site-renderer:1.4 (and do not have org.apache.maven.doxia.siterenderer.DocumentContent)
We can specific maven-project-info-reports-plugin version in reporting part:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.9</version>
</plugin>
</plugins>
</reporting>
Or we can specify maven-site-plugin to the latest 3.7.1 like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
in build part of pom.
Version of the maven site plugin needs to be explicitly set in the build section too. Here is the example:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
<reportSets>
<reportSet>
<reports>
<report>index</report>
<report>licenses</report>
<report>dependency-info</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
<build>
<plugins>
<!-- Part of Maven - specified version explicitly for compatibility
with the maven-project-info-reports-plugin 3.0.0-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
</plugins>
</build>
Maven 3 doesn't support Doxia anymore.
Use
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.2</version>
</plugin>
Reference: https://maven.apache.org/plugins/maven-site-plugin/maven-3.html
You really need to add more information (I didn't downvote BTW).
IIRC; if you don't specify a version for a plugin bound to lifecycle phases, you'll get the latest.
Try:
Upgrading to the latest version of maven - 3.5.4 ATOW
Running mvn help:effective-pom and checking which versions are
actually being resolved - if you have an old log from CI or wherever
to compare with..
Explicity setting the maven-site-plugin version
in pluginManagement section
Adding a dependency to maven-site-plugin (see below)
org/apache/maven/doxia/siterenderer/DocumentContent can be found in doxia-site-renderer:
<dependency>
<groupId>org.apache.maven.doxia</groupId>
<artifactId>doxia-site-renderer</artifactId>
<version>1.8.1</version>
</dependency>
I suspect explicitly setting the version of maven-site-plugin to whatever you used to use (incidentally) will work.
Edit: Was chasing a similar issue in maven plugin build testing, explicitly setting maven-site-plugin version (3.7.1 ATOW) in integration pom used by maven-invoker-plugin has worked for me.
The following versions in pom.xml fixed the problem for me:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.9</version>
</plugin>
I also hit this error on some of my build jobs today. The fix suggested above, adding a concrete dependency for the maven-site-plugin does work and fixes this issue.
However, what it highlighted for me was the fact I was even running the mvn site goal, which I didn't even know we were running and we don't really require.
My fix was to therefore remove the site goal from my mvn arg, as although the site it creates is actually quite useful, I never knew we were creating it, we never published it anywhere and were actually deleting it every build anyway.
I tried to follow Changhua's advice, and define maven-project-info-reports-plugin to version 3.0.0, and maven-site-plugin to 3.7.1 in my pom file, but found that the maven site still pulled in version 3.3 of the maven-site-plugin, regardless of how I set it.
I finally realized that my problem had to do with our project structure. We have a parent pom, in which we were defining the maven-site-plugin dependency, which was then inherited by the children poms. However, the build pom file was separate, and didn't define maven-site-plugin at all, which allowed maven to pull in the 3.3 version on its own. I added the maven-site-plugin dependency (version 3.7.1) to the build pom file, so that it now exists in both the build pom file and the parent pom file, and now the build is correctly using version 3.7.1, and is passing again.

Eclipse Kepler is unable to set the Java jdk 1.8 when maven project is updated

Installed java version is 1.8, while selecting this version in pom.xml and updating the maven project,it automatically jumps from 1.8 to 1.4, due to which I am unable to have Lambda expression specific code.
I am using eclipse kepler.
Any idea what is happening here ?
Add something similar to this to your pom. You might have to tweak the plug in version.
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
Apologies I'm on mobile and the editor sucks for code formatting.
Edit: As noted in the accepted answer you also need to enable Java 8 support in Kepler.
With the help of Hogler's comment above I am able to reswolve this issue
Just did this :
https://wiki.eclipse.org/JDT/Eclipse_Java_8_Support_For_Kepler

How do I tell Spring Boot which main class to use for the executable jar?

Execution default of goal
org.springframework.boot:spring-boot-maven-plugin:1.0.1.RELEASE:repackage
failed:
Unable to find a single main class from the following candidates
My project has more than one class with a main method. How do I tell the Spring Boot Maven plugin which of the classes it should use as the main class?
Add your start class in your pom:
<properties>
<!-- The main class to start by executing java -jar -->
<start-class>com.mycorp.starter.HelloWorldApplication</start-class>
</properties>
or
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.mycorp.starter.HelloWorldApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
For those using Gradle (instead of Maven) :
springBoot {
mainClass = "com.example.Main"
}
If you do NOT use the spring-boot-starter-parent pom, then from the Spring documentation:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.1.3.RELEASE</version>
<configuration>
<mainClass>my.package.MyStartClass</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
For those using Gradle (instead of Maven), referencing Spring Boot Gradle Plugin Reference Guide (as of Gradle 7.6 and Spring Boot 3.0.0):
The main class can also be configured explicitly using the task’s mainClass property:
tasks.named("bootJar") {
mainClass = 'com.example.ExampleApplication'
}
Alternatively, the main class name can be configured project-wide using the mainClass property of the Spring Boot DSL:
springBoot {
mainClass = 'com.example.ExampleApplication'
}
If you're using spring-boot-starter-parent in your pom, you simply add the following to your pom:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Then do your mvn package.
See this Spring docs page.
A very important aspect here is to mention that the directory structure has to be src/main/java/nameofyourpackage
I tried the following code in pom.xml and it worked for me
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>myPackage.HelloWorld</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<fork>true</fork>
<executable>D:\jdk1.8\bin\javaw.exe</executable>
</configuration>
</plugin>
</plugins>
Since Spring Boot 1.5, you can complete ignore the error-prone string literal in pom or build.gradle. The repackaging tool (via maven or gradle plugin) will pick the one annotated with #SpringBootApplication for you. (Refer to this issue for detail: https://github.com/spring-projects/spring-boot/issues/6496 )
I had renamed my project and it was still finding the old Application class on the build path. I removed it in the 'build' folder and all was fine.
Have seen this issue with Java 1.9 and SpringBoot 1.5.x, when main-class is not specified explicitly.
With Java 1.8, it is able to find main-class without explicit property and 'mvn package' works fine.
If you are using Grade, it is possible to apply the 'application' plugin rather than the 'java' plugin. This allows specifying the main class as below without using any Spring Boot Gradle plugin tasks.
plugins {
id 'org.springframework.boot' version '2.3.3.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'application'
}
application {
mainClassName = 'com.example.ExampleApplication'
}
As a nice benefit, one is able to run the application using gradle run with the classpath automatically configured by Gradle. The plugin also packages the application as a TAR and/or ZIP including operating system specific start scripts.
For Kotlin with Gradle:
springBoot {
mainClass.set("com.example.MainKt")
}

How can I switch the bytecode target level in IntelliJ using Maven 3 profiles

I am using maven profiles to switch between two "setups" in Intelli J. How can I switch the bytecode target level? That is: I want to change the following setting in compiler.xml
<bytecodeTargetLevel>
<module name="finmath-lib" target="1.6" />
</bytecodeTargetLevel>
Note: I tried to perform this via the following part in the respective Maven 3 profile
<profile>
<id>java-8</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</profile>
but this does not work!
Note: The pom.xml etc. belongs to the finmath lib project, and if you are interested, it can be found at www.finmath.net
I am using IntelliJ IDEA 14.1.3 and it works with following config ...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
Hope this is helpful. Thanks!
The problem is that you want to change it to 1.8.
I've tried to switch IntelliJ's compiler version between 1.7 and 1.6 or 1.5 using Maven profile successfully.
However when you want to change it to 1.8, the config will be ignored.
I don't know whether this is maven's problem or Intellj's.
It seems that the JDK 1.8 is not well supported now, which is understandable.
You can open the Maven Projects tool window in IntelliJ and select the profile you want to use from the list.
In IntelliJ 13, File > Project Structure, set the Project language level to 8.0.

Categories

Resources