Following is the error message while running build command in gradle. I am migrating from tomcat 7 to Tomcat 9.
_jspx_imports_classes = new java.util.HashSet<>();
^^^^^^^^^^^^^^^^^
'<>' operator is not allowed for source level below 1.7
I am using JDK 1.8 and gradle for build the code.
Today i fixed my issue
subprojects {
apply plugin: 'com.parag.gradle.buildinfo'
if(! assemblyProjects.contains(it)) {
// setup source and target compatibility for jsp and add default dependencies
plugins.withId('com.parag.gradle') {
tasks.withType(<taskType>) {
sourceCompatibility = 1.8
targetCompatibility = 1.8
jspPackage = "org.apache.jsp.${project.name}"
}
dependencies {
compile 'org.apache.tomcat:tomcat-jsp-api'
}
}
dependencies {
compile 'org.apache.tomcat:tomcat-el-api'
// compile 'javax.servlet:javax.servlet-api'
compile 'org.apache.tomcat:tomcat-servlet-api'
}
}
}
Can I possibly make Gradle compile one part of my app for Java version 1.8 and the other for 1.7?
My situation is I'm writing a library, part of which will be used in a project where the version is 1.7.
I realize now that I could have broken logic in my thoughts, but the question still stands. Or, if possible, suggest something completely different.
EDIT: And if possible suggest any relevant terms, because I can't even think of a google query now.
Suppose you have a multi project build with the following directory structure:
root
build.gradle.kts
sub-project
build.gradle.kts
src/main/java
java-1.7-project
build.gradle.kts
-src/main/java
Root project build file:
plugins {
java
}
allprojects {
group = "com.company.example"
version = "0.0.1"
apply {
plugin("java")
}
repositories {
mavenCentral()
}
}
configure(subprojects.filter { it.name != "java-1.7" }) {
java.sourceCompatibility = org.gradle.api.JavaVersion.VERSION_11
}
Java-1.7-project build file:
configure<JavaPluginExtension> {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}
I have created Spring Boot project with
SDK 11. Java Version 11.0.3
Kotlin as language
Gradle
I'm following this Tutorial:
https://scotch.io/#grahamcox82/how-to-build-a-simple-rest-api-with-kotlin-and-spring-boot
I'm trying to
import java.time.Instant
in my Kotlin data class
And have an error
Unresolved reference: java
build.gradle.kts file:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.1.6.RELEASE"
id("io.spring.dependency-management") version "1.0.7.RELEASE"
kotlin("jvm") version "1.2.71"
kotlin("plugin.spring") version "1.2.71"
}
group = "com.smight"
version = "0.0.1"
java.sourceCompatibility = JavaVersion.VERSION_1_8
val developmentOnly by configurations.creating
configurations {
runtimeClasspath {
extendsFrom(developmentOnly)
}
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
developmentOnly("org.springframework.boot:spring-boot-devtools")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("io.projectreactor:reactor-test")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
Maybe I should install java library? How can I check this?
Can anyone help please?
According to some research, this error can appear in this conditions :
You created a Kotlin2Js project instead of Kotlin JVM (source), try to recreate your project by selecting the right project type
or
You are using a Kotlin version that does not support JDK 11 (source), install JDK 8 instead and reconfigure your JAVA_HOME environment variable
It may as well be an error in your build.gradle file, copy/paste it in your question if the solutions above doesn't work
To get a more specific error you should first clean the autogenerated files
$ ./gradlew clean
In a modularized Spring project using Kotlin DSL the unresolved reference error could occur because the submodules are bootable.
build.gradle.kts (Project)
...
subprojects {
...
tasks.getByName<BootJar>("bootJar") {
enabled = false
}
tasks.getByName<Jar>("jar") {
enabled = true
}
}
GL
The problem was that JDK was not correct found from IntelliJ
I solved the problem so:
File -> Project Structure -> SDKs -> "+"
Find the path to your SDK where it is installed
New Project
Copy/Paste
Rebuild
So Java 9 is there, soon to be followed by Java 10. Time we should make our libraries ready for use in Java 9 projects. I did it in the following way:
provide a module-info.java
added the (experimental) jigsaw plugin in build.gradle
Manually made changes according to the guide on the gradle site instead of using the jigsaw plugin.
So far, both approaches work fine, and I can use the generated Jar in Java 9 projects.
The problem is, the resulting Jar is not compatible with Java 8 although I used no Java 9 features except the module-info.java. When I set targetCompatibility = 8, an error message tells me to also set sourceCompatibility = 8 accordingly. Which then rejects the module-info.java for which I should set sourceCompatibility = 9.
How can this be solved?
I removed the jigsaw plugin again, and tried this, but am stuck:
set sourceCompatibility = 8 and targetCompatibility = 8
create a new source set moduleInfo that contains the single file module-info.java
set sourceCompatibility = 9 and targetCompatibility = 9 for the new sourceset
Now compilation works, and Gradle uses Java 9 when it tries to compile the module-info.java. However, modules (in this case log4j) are missing, and I get this error:
:compileJava UP-TO-DATE
:processResources NO-SOURCE
:classes UP-TO-DATE
:jar UP-TO-DATE
:sourcesJar UP-TO-DATE
:assemble UP-TO-DATE
:spotbugsMain UP-TO-DATE
:compileModuleInfoJava
classpath:
compilerArgs: [--module-path, , --add-modules, ALL-SYSTEM]
D:\git\utility\src\module-info\java\module-info.java:14: error: module not found: org.apache.logging.log4j
requires org.apache.logging.log4j;
^
warning: using incubating module(s): jdk.incubator.httpclient
1 error
1 warning
:compileModuleInfoJava FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileModuleInfoJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
5 actionable tasks: 1 executed, 4 up-to-date
This is the build.gradle used (Gradle version is 4.5.1):
plugins {
id "com.github.spotbugs" version "1.6.0"
}
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'java-library'
apply plugin: 'com.github.spotbugs'
sourceCompatibility = 8
targetCompatibility = 8
group = 'com.dua3.utility'
repositories {
mavenLocal()
jcenter()
}
dependencies {
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.10.0'
testRuntime group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.10.0'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
ext.moduleName = 'com.dua3.utility'
sourceSets {
moduleInfo {
java {
srcDir 'src/module-info/java'
}
}
}
compileModuleInfoJava {
sourceCompatibility = 9
targetCompatibility = 9
inputs.property("moduleName", moduleName)
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath,
'--add-modules', 'ALL-SYSTEM'
]
classpath = files()
System.out.println("classpath: "+classpath.asPath)
System.out.println("compilerArgs: "+options.compilerArgs)
}
}
tasks.withType(com.github.spotbugs.SpotBugsTask) {
reports {
xml.enabled false
html.enabled true
}
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
// fails with jigsaw: archives javadocJar
}
defaultTasks 'build', 'publishToMavenLocal', 'install'
And this is module-info.java:
module com.dua3.utility {
exports com.dua3.utility;
exports com.dua3.utility.io;
exports com.dua3.utility.jfx;
exports com.dua3.utility.swing;
exports com.dua3.utility.lang;
exports com.dua3.utility.math;
exports com.dua3.utility.text;
requires javafx.controls;
requires javafx.web;
requires java.xml;
requires java.desktop;
requires org.apache.logging.log4j;
}
OK, I finally got it working. In case anyone else wants to know how to do it, this is what I have done:
set the Java version to 8, so that the library will be usable by Java 8 applications:
sourceCompatibility = 8
targetCompatibility = 8
configure the module name
ext.moduleName = com.dua3.utility
add a new sourceset consisting only of module-info.java:
sourceSets {
moduleInfo {
java {
srcDir 'src/module-info/java'
}
}
}
set compatibility to Java 9 for the moduleInfo, sourceSet, configure modules, and set the output directory:
compileModuleInfoJava {
sourceCompatibility = 9
targetCompatibility = 9
inputs.property("moduleName", moduleName)
doFirst {
classpath += sourceSets.main.compileClasspath
options.compilerArgs = [
'--module-path', classpath.asPath,
'--add-modules', 'ALL-SYSTEM,org.apache.logging.log4j',
'-d', sourceSets.main.output.classesDirs.asPath
]
}
}
configure the jar task to include moduleInfo:
jar
{
from sourceSets.main.output
from sourceSets.moduleInfo.output
}
In case you are using the SpotBugs plugin, you also have to configure the sourceSet explicitly because it will otherwise fail when it tries to process the ModuleInfo sourceSet.
I finally ended up with this version of build.gradle:
plugins {
id "com.github.spotbugs" version "1.6.0"
}
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'java-library'
apply plugin: 'com.github.spotbugs'
sourceCompatibility = 8
targetCompatibility = 8
group = 'com.dua3.utility'
repositories {
mavenLocal()
jcenter()
}
dependencies {
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.10.0'
testRuntime group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.10.0'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
ext.moduleName = 'com.dua3.utility'
sourceSets {
moduleInfo {
java {
srcDir 'src/module-info/java'
}
}
}
compileModuleInfoJava {
sourceCompatibility = 9
targetCompatibility = 9
inputs.property("moduleName", moduleName)
doFirst {
classpath += sourceSets.main.compileClasspath
options.compilerArgs = [
'--module-path', classpath.asPath,
'--add-modules', 'ALL-SYSTEM',
'-d', sourceSets.main.output.classesDirs.asPath
]
}
}
jar
{
from sourceSets.main.output
from sourceSets.moduleInfo.output
}
spotbugs {
sourceSets = [sourceSets.main]
}
tasks.withType(com.github.spotbugs.SpotBugsTask) {
reports {
xml.enabled false
html.enabled true
}
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
defaultTasks 'build', 'publishToMavenLocal', 'install'
The question is over a year old, but in case anyone stumbles here, this functionality is now supported by Gradle Modules Plugin since version 1.5.0.
With this plugin, you don't have to create a custom source set, and you only need to call modularity.mixedJavaRelease method.
Here's a sample of how to apply the plugin to one's main build.gradle:
plugins {
// your remaining plugins here
id 'org.javamodularity.moduleplugin' version '1.5.0' apply false
}
subprojects {
// your remaining subproject configuration here
apply plugin: 'org.javamodularity.moduleplugin'
modularity.mixedJavaRelease 8 // sets "--release 8" for main code, and "--release 9" for "module-info.java"
// test.moduleOptions.runOnClasspath = true // optional (if you want your tests to still run on classpath)
}
I have developed a Gradle plugin for this: https://github.com/Glavo/module-info-compiler
I have tried Gradle Modules Plugin, but there are still some troublesome problems, so I developed this plugin, a compiler specifically used to compile module-info.java.
It is not implemented by calling javac. It is a complete compiler that can run above Java 8. It recognizes the syntax of module-info.java and generates the corresponding module-info.class file according to it.
It only checks the grammar, and does not actually check those packages, classes or modules, so it can work without configuration of any module path.
This Gradle plugin has processed everything for you. For a Java 8 project containing module-info.java, you only need to do this:
plugins {
id("java")
id("org.glavo.compile-module-info-plugin") version "2.0"
}
tasks.compileJava {
options.release.set(8)
}
This answer copy the answer written by myself under another question (https://stackoverflow.com/a/72074642/7659948).
I would like to set the required Java version (e.g. 7 or 8) in my Gradle build file without having to specify the actual path to a local JDK installation.
Is this possible?
This feature was just added to Gradle 6.7 as Java toolchains:
// build.gradle.kts
plugins {
id("java-library") // or id("application")
}
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(11))
}
}
With this in place Gradle will automatically download & use the appropriate JDK (using AdoptOpenJDK by default) for the specified Java version.
TLDR;
Thanks #franklin-yu "targetCompatibility = '1.7' -> your user can compile with 8 and run with 7."
See Gradle, "sourceCompatibility" vs "targetCompatibility"?
targetCompatibility = '1.7' does the trick for e.g. Java 7
Use sourceCompatibility = '1.7' for the language level
You can try this:
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
tasks.withType<JavaCompile> {
options.compilerArgs.addAll(arrayOf("--release", "8"))
}
This will also give JDK compliance to you. You can also see the following related issues:
Gradle: [Java 9] Add convenience method for -release compiler argument
Eclipse Plug-ins for Gradle: JDK API compatibility should match the sourceCompatibility option.
In the build.gradle file, add the following two lines:
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
The targetCompatibility defines the generated JVM bytecode version (this is the version that users of your application need). The sourceCompatibility defines which source code constructs are allowed (e.g. you need Java 1.8 or higher to use lambda expressions in source code).
Source
Based on the answer of CletusW. In windows 10, it seems that the new installed Java will not be chosen automatically, so I check it manually.
apply plugin: 'java'
java.toolchain.languageVersion = JavaLanguageVersion.of(15) // auto install
// check JDK version
if (!System.getProperty("java.home").contains(java.toolchain.languageVersion.get().toString())) {
def msg = ('JDK in this project: ' + System.getProperty('java.home') + '\n' +
'In this project, you should use JDK-' + java.toolchain.languageVersion.get())
throw new GradleException(msg)
}