I have a Java library https://github.com/skycavemc/skycavelib which I want to use in a Kotlin project. This is the build.gradle where I have the library as dependency:
import java.util.Properties
import java.io.FileInputStream
plugins {
kotlin("jvm") version "1.7.10"
}
group = "de.skycave"
version = "1.0.0"
val localProperties = Properties()
localProperties.load(FileInputStream(rootProject.file("local.properties")))
repositories {
mavenCentral()
maven { url = uri("https://repo.papermc.io/repository/maven-public/") }
maven { url = uri("https://jitpack.io") }
maven {
url = uri("https://maven.pkg.github.com/skycavemc/skycavelib")
credentials {
username = localProperties.getProperty("gpr.user")
password = localProperties.getProperty("gpr.key")
}
}
}
dependencies {
implementation(kotlin("stdlib"))
compileOnly("io.papermc.paper:paper-api:1.19.2-R0.1-SNAPSHOT")
implementation("de.skycave:skycavelib:1.0.2")
}
java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
}
I also made sure I have a file named local.properties in my project where I set gpr.user and gpr.key correctly. The authentication works and the library is downloaded and indexed. IntelliJ also shows the library under "External Libraries".
When I try to use a class from that library, IntelliJ's autocompletion suggests the correct import. But after importing it, IntelliJ says "Unresolved reference" in the line of the import and the line where I use that class.
However, the gradle build still succeeds. Also, I only experience this issue when I import something from that library in a Kotlin class. In a Java class, IntelliJ can resolve the reference. This problem does not only happen in one specific project, but in all projects where I try to import something from that library, which means it's probably not an issue with the project configuration. The other Java library I use (paper-api) works fine when importing in both Kotlin and Java files. Invalidating caches and reloading all gradle projects has not solved the issue.
I suppose there is something misconfigured in my library https://github.com/skycavemc/skycavelib. Does someone have an idea what could have went wrong there? This is my build.gradle for the library I am trying to import from:
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.7.10'
id 'com.github.johnrengelman.shadow' version '7.1.2'
id 'maven-publish'
}
group = 'de.skycave'
version = '1.0.2'
def localProperties = new Properties()
localProperties.load(new FileInputStream(rootProject.file("local.properties")))
repositories {
mavenCentral()
maven {
name = 'papermc-repo'
url = 'https://repo.papermc.io/repository/maven-public/'
}
maven {
name = 'sonatype'
url = 'https://oss.sonatype.org/content/groups/public/'
}
maven {
name = 'jitpack'
url = 'https://jitpack.io'
}
}
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.7.10'
compileOnly 'io.papermc.paper:paper-api:1.19.2-R0.1-SNAPSHOT'
implementation 'org.mongodb:mongodb-driver-sync:4.7.1'
implementation 'com.google.code.gson:gson:2.9.1'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
implementation group: 'commons-io', name: 'commons-io', version: '2.11.0'
implementation 'com.github.heuerleon:mcguiapi:v1.3.5'
}
def targetJavaVersion = 17
java {
def javaVersion = JavaVersion.toVersion(targetJavaVersion)
sourceCompatibility = javaVersion
targetCompatibility = javaVersion
if (JavaVersion.current() < javaVersion) {
toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion)
}
manifest {
attributes 'Main-Class': "de.skycave.skycavelib.SkyCaveLib"
}
}
tasks.withType(JavaCompile).configureEach {
if (targetJavaVersion >= 10 || JavaVersion.current().isJava10Compatible()) {
//noinspection GroovyAssignabilityCheck, GroovyAccessibility
options.release = targetJavaVersion
}
}
processResources {
def props = [version: version]
inputs.properties props
filteringCharset 'UTF-8'
filesMatching('plugin.yml') {
expand props
}
}
build {
dependsOn(shadowJar)
}
shadowJar {
archiveFileName.set("${project.name}-${project.version}.jar")
}
publishing {
repositories {
maven {
name = "GitHubPackages"
url = "https://maven.pkg.github.com/skycavemc/skycavelib"
credentials {
username = localProperties.getProperty("gpr.user") ?: System.getenv("GITHUB_ACTOR")
password = localProperties.getProperty("gpr.token") ?: System.getenv("ACCESS_TOKEN")
}
}
}
publications {
library(MavenPublication) {
from components.java
}
}
}
Solution
I did some more research and found a hint that shadowed jars might not work well. I removed this part
build {
dependsOn(shadowJar)
}
from the build.gradle of my library and published it to the package repository. Everything works fine since then, seems like that was the issue.
Related
I have a Jave 8, SpringBoot, webService which has been running fine on Eclipse 2020-06 for months. I build using Gradle. In an attempt to get the latest version of an updated jar file from Artifactory I did :
gradlew clean
gradlew eclipse
F5 (Refresh)
This indeed pulled the required jar file but my program now failed to compile. The error is:
The import org.springframework.beans.factory.annotation cannot be resolved
And all my #Autowired commands now error.
What i've tried :
gradlew cleanEclipse eclipse
close project, close eclipse, reopen eclipse, reopen project
gradle -> Refresh Gradle project
Same a 2. preceded by reboot pc. When I do this I get:
After I did:
gradlew cleanEclipse eclipse
The following jar dissapeared from my project:
spring-beans.5.3.0.RC1.jar
This is what is causing the problem.
Is this incorrect in build.gradle:
implementation "org.springframework:spring-beans:5.+"
How do I get this jar back?
Any help appreciated.
My build.gradle file is:
plugins {
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'java'
}
repositories {
maven {
url "https://repo-int.xxxxxx.io/artifactory/cc-central"
}
maven {
url "https://code.lds.org/nexus/content/groups/main-repo"
}
maven {
url "${chemaxonRepositoryUrl}/libs-release"
credentials {
username = chemaxonRepositoryUser
password = chemaxonRepositoryPassword
}
}
}
ext {
archive_basename = "dcrws"
project_description = "Chemistry Workbench DcrWs"
springStarterVersion = '2.2.2.RELEASE'
// Jenkins/Environment Information
build_tag = System.getenv('BUILD_TAG') ?: "none"
build_by = System.getenv('NODE_NAME') ?: "none"
build_date = System.getenv('BUILD_TIMESTAMP') ?: "none"
build_system = System.getenv('JENKINS_HOME') ?: "none"
build_url = System.getenv('BUILD_URL') ?: "none"
build_user = System.getenv('USER') ?: "none"
}
// standard plugins
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'io.spring.dependency-management'
def git_info = hasProperty('git_info') ? git_info : 'https://git.git/repo,00000000'
def full_version = hasProperty('app_version') ? app_version : '0.0.0'
group = 'com.clarivate.singularity.chemworkbench'
version = full_version
archivesBaseName = ext.archive_basename
sourceCompatibility = 1.8
targetCompatibility = 1.8
configurations {
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
}
// if master branch, use latest version of chemcur:cwbcommon, else use 0.0+ version
ext.cwbcommon_version = '1.+'
if ( full_version =~ /^0./) { ext.cwbcommon_version = '0.0+' }
ext.pwbcommon_version = '1.+'
if ( full_version =~ /^0./) { ext.pwbcommon_version = '0.0+' }
dependencies {
implementation "org.springframework.boot:spring-boot-starter-actuator:$springStarterVersion"
implementation "org.springframework.boot:spring-boot-starter-web:$springStarterVersion"
implementation "org.springframework.boot:spring-boot-starter-cache:$springStarterVersion"
implementation "org.springframework.boot:spring-boot-starter-data-jdbc:$springStarterVersion"
implementation "org.springframework.boot:spring-boot-starter-data-jpa:$springStarterVersion"
implementation "org.springframework.boot:spring-boot-starter-tomcat:$springStarterVersion"
implementation 'com.fasterxml.jackson.core:jackson-core:2.+'
implementation "org.springframework:spring-beans:5.+"
implementation "org.springframework:spring-core:5.+"
implementation "javax.servlet:javax.servlet-api:4.+"
implementation "org.apache.commons:commons-lang3:3.9+"
implementation "commons-dbutils:commons-dbutils:1.7+"
implementation "com.oracle:ojdbc6:11.2.0.1.0"
compile 'com.chemaxon:calculations:20.+'
// homegrown libs
compile ("chemcur:cwbcommon:${cwbcommon_version}") {changing = true}
compile ("chemcur:pwbcommon:${pwbcommon_version}") {changing = true}
developmentOnly "org.springframework.boot:spring-boot-devtools:$springStarterVersion"
}
springBoot {
buildInfo {
properties {
artifact = archive_basename
version = full_version
group = group
name = project_description
additional = [
'Implementation-Title': project_description,
'Implementation-Version': full_version,
"Implementation-SCM": git_info,
"Jenkins-Build-Tag": build_tag,
"Build-By": build_by,
"Build-Date": build_date,
"Build-JDK": System.getProperty('java.version'),
"Build-Gradle-Version": project.gradle.gradleVersion,
]
}
}
}
I am trying to run the test for java applications (with module system enabled) using Gradle and getting the following error.
java.lang.IllegalAccessError: class org.junit.platform.launcher.core.LauncherFactory (in unnamed module #0x7cd3a5) cannot access class org.junit.platform.commons.util.Preconditions (in module org.junit.platform.commons) because module org.junit.platform.commons does not export org.junit.platform.commons.util to unnamed module #0x7cd3a5
Error says module org.junit.platform.commons does not export org.junit.platform.commons.util
This is how my build file looks like:
import org.springframework.boot.gradle.plugin.SpringBootPlugin
plugins {
id 'java'
id 'org.springframework.boot' version '2.3.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id "org.beryx.jlink" version "2.21.0"
id "org.javamodularity.moduleplugin" version "1.6.0"
}
repositories {
gradlePluginPortal()
jcenter()
}
sourceCompatibility = JavaVersion.VERSION_14
targetCompatibility = JavaVersion.VERSION_14
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
jar {
enabled = true;
}
application{
mainModule='com.sudhir.registration'
}
test {
useJUnitPlatform()
}
configurations {
springFactoriesHolder { transitive = false }
}
dependencyManagement {
imports {
mavenBom SpringBootPlugin.BOM_COORDINATES
}
}
dependencies {
springFactoriesHolder 'org.springframework.boot:spring-boot-actuator-autoconfigure'
springFactoriesHolder 'org.springframework.boot:spring-boot-autoconfigure'
springFactoriesHolder 'org.springframework.boot:spring-boot'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
exclude group: 'com.vaadin.external.google', module: 'android-json'
}
}
mainClassName = "com.sudhir.registration.ApiApplication"
prepareMergedJarsDir.doLast {
// extract and merge META-INF/spring.factories from springFactoriesHolder
def factories = configurations.springFactoriesHolder.files.collect {
def props = new Properties()
props.load(zipTree(it).matching { include 'META-INF/spring.factories' }.singleFile.newInputStream())
props
}
def mergedProps = new Properties()
factories.each { props ->
props.each { key, value ->
def oldVal = mergedProps[key]
mergedProps[key] = oldVal ? "$oldVal,$value" : value
}
}
def content = mergedProps.collect { key, value ->
def v = (value as String).replace(',', ',\\\n')
"$key=$v"
}.join('\n\n')
mkdir("$jlinkBasePath/META-INF")
new File("$jlinkBasePath/META-INF/spring.factories").text = content
// insert META-INF/spring.factories into the main jar
ant.zip(update: "true", destfile: jar.archivePath, keepcompression: true) {
fileset(dir: "$jlinkBasePath", includes: 'META-INF/**')
}
}
jlink {
imageZip = file("$buildDir/image-zip/registration-service-image.zip")
options = ['--strip-java-debug-attributes', '--compress', '2', '--no-header-files', '--no-man-pages']
forceMerge 'jaxb-api', 'byte-buddy', 'classgraph'
mergedModule {
uses 'ch.qos.logback.classic.spi.Configurator'
excludeRequires 'com.fasterxml.jackson.module.paramnames'
excludeProvides implementation: 'com.sun.xml.bind.v2.ContextFactory'
excludeProvides servicePattern: 'javax.enterprise.inject.*'
excludeProvides service: 'org.apache.logging.log4j.spi.Provider'
excludeProvides servicePattern: 'reactor.blockhound.integration.*'
}
launcher {
name = 'run'
jvmArgs = [
'--add-reads', 'registration.service.merged.module=com.sudhir.registration',
'-cp', 'config/',
]
}
}
tasks.jlink.doLast {
copy {
from "src/main/resources"
into "$imageDir.asFile/bin/config"
}
copy {
from "$buildDir/classes/java/main/com/sudhir/registration"
into "$imageDir.asFile/bin/config/com/sudhir/registration/for-spring-classpath-scanner"
}
}
However, I am able to run specific tests in IntelliJ. I think it's because Intellij is using classpath instead of module path.
I am using gradle-module-system plugin to build, test and run. sample code can be found here.
Can someone please assist me how to deal with this issue. I am very new to using java module system.
Not quite sure on the reason of this problem. These threads may help to understand it: this, this, this or this
But this is what helped me in a similar case:
test {
moduleOptions {
runOnClasspath = true
}
useJUnitPlatform()
}
(Adding moduleOptions setting to the test task)
i have two nexus repositories: one for releases, other for snapshots. i have code in publish task to define which repo to pick according to doc:
repositories {
repositories {
maven {
credentials {
username "$nexusUser"
password "$nexusPassword"
}
def releasesRepoUrl = "https://xxx/repository/factoring-maven-release/"
def snapshotsRepoUrl = "https://xxx/repository/factoring-maven-snapshots/"
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
}
}
publications {
create("default", MavenPublication.class) {
from(components["java"])
}
}
}
}
and subprojects included by this code :
rootProject.name = 'xxx-integration-lib'
include 'xxx-service1-dto'
include 'xxx-service2-dto'
subprojects build.gradle:
group = "xxx"
version = "0.0.6-SNAPSHOT"
but this doesnt work since subproject version is always unspecified.
tried:
making new allproject task to return version
using project.property('propName') - but this seems like a workaround, not a solution
any thoughts?
I am working on multimodule projects with gradle and we are not setting a version in submodules.All we do is setting it in base project in gradle.properties file.
group = "xxx"
version = "0.0.6-SNAPSHOT"
then you can use it:
url = project.version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
the only worked way for me was a workaround:
placing subproject version management in root build.gradle like this
project(':subproject1') {
version '0.0.6-SNAPSHOT'
}
project(':subproject2') {
version '0.0.12-SNAPSHOT'
}
project(':subproject14) {
version '0.0.5-SNAPSHOT'
}
and then project.version property is being injecting correctyl
I have a Spring Boot Java project that builds using Gradle (v6.2.2).
build.gradle.kts
plugins {
base
java
id("org.springframework.boot") apply false
}
val gradleVersionProperty: String by project
val javaVersion: String by project
val springBootVersion: String by project
val springCloudStarterParentBomVersion: String by project
if (JavaVersion.current() != JavaVersion.VERSION_11) {
throw GradleException("This build must be run with JDK 11")
} else {
println("Building with JDK " + JavaVersion.current())
}
tasks.withType<Wrapper> {
gradleVersion = gradleVersionProperty
distributionType = Wrapper.DistributionType.ALL
}
allprojects {
group = "com.meanwhile.in.hell"
version = "$version"
// Repos used in dependencyManagement section of settings.gradle
repositories {
mavenLocal()
mavenCentral()
maven("https://repo.spring.io/snapshot")
maven("https://repo.spring.io/milestone")
}
}
subprojects {
if (!project.name.startsWith("platform")) {
apply {
plugin("java-library")
}
java.sourceCompatibility = JavaVersion.VERSION_11
java.targetCompatibility = JavaVersion.VERSION_11
// Change the default test logging settings
tasks.withType<Test>() {
useJUnitPlatform()
testLogging {
events = setOf(
org.gradle.api.tasks.testing.logging.TestLogEvent.FAILED,
org.gradle.api.tasks.testing.logging.TestLogEvent.PASSED,
org.gradle.api.tasks.testing.logging.TestLogEvent.SKIPPED
)
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
}
enableAssertions = false
ignoreFailures = gradle.startParameter.isContinueOnFailure
maxParallelForks =
(Runtime.getRuntime().availableProcessors() / 2).takeIf { it > 0 } ?: 1
}
dependencies {
"api"(enforcedPlatform("org.springframework.boot:spring-boot-dependencies:$springBootVersion"))
"api"(enforcedPlatform("org.springframework.cloud:spring-cloud-dependencies:$springCloudStarterParentBomVersion"))
"implementation"(enforcedPlatform(project(":platform-dependencies")))
"testCompile"("org.springframework.boot:spring-boot-starter-test")
}
}
}
However, I would like to add support for Spring Boot Kotlin sub-projects within it. I have used a very simple sample project from a Kotlin-only project I have that builds fine within it. Without any changes to my root build.gradle.kts file, my current build error is:
* What went wrong:
Execution failed for task ':kotlin-sample-project:bootJar'.
> Main class name has not been configured and it could not be resolved
I have not configured the main class for any of the Java sub-projects and neither have I in my Kotlin-only other project.
My build.gradle.kts in kotlin-sample-project is very simple:
plugins {
id("org.springframework.boot")
}
dependencies {
implementation("org.springframework.cloud:spring-cloud-starter-gateway")
}
And my main class looks like:
src/main/kotlin/sample/KotlinSampleApplication.kts
package com.meanwhile.in.hell.kotlinsample
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
#SpringBootApplication
open class KotlinSampleApplication
fun main(args: Array<String>) {
runApplication<KotlinSampleApplication>(*args)
}
I have tried to add the kotlin plugin, but the build fails instantly not knowing what it is.
plugins {
base
java
kotlin
}
Error:
Line 9: kotlin
^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public val <T : Any> Class<TypeVariable(T)>.kotlin: KClass<TypeVariable(T)> defined in kotlin.jvm
I have tried to add the kotlin plugin, but the build fails instantly not knowing what it is.
It should be:
build.gradle.kts:
plugins {
kotlin("jvm").version("1.3.72")
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
Bot Kotlin JVM plugin should be applied and Kotlin stdlib be present on compile classpath.
I'm fairly new to Gradle (and Java 9, to be honest), and I'm trying to use Gradle to build a simple library project that is a mix of Java 9 and Kotlin. More in detail, there is an interface in Java and an implementation in Kotlin; I'd do everything in Kotlin, but the modules-info.java is java anyway, so I decided to do things this way.
I'm building on IntelliJ Idea, with the 1.2.0 kotlin plugin and gradle 4.3.1 defined externally.
Filesystem schema is:
+ src
+ main
+ java
+ some.package
- Roundabout.java [an interface]
- module-info.java
+ kotlin
+ some.package.impl
- RoundaboutImpl.kt [implementing the interface]
module-info.java is:
module some.package {
requires kotlin.stdlib;
exports some.package;
}
and build.gradle is:
buildscript {
ext.kotlin_version = '1.2.0'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
group 'some.package'
version '1.0-PRE_ALPHA'
apply plugin: 'java-library'
apply plugin: 'kotlin'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
sourceCompatibility = 9
compileJava {
dependsOn(':compileKotlin')
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath,
]
classpath = files()
}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib', version: "$kotlin_version"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
Notice that I had to specify a module path on the java compile task, or the compilation fails with:
error: module not found: kotlin.stdlib
requires kotlin.stdlib;
Anyway, now this build fails with this error, and I can't figure out how to solve it:
error: package some.package.impl does not exist
import some.package.impl.RoundaboutImpl;
error: cannot find symbol
return new RoundaboutImpl<>(queueSize, parallelism, worker, threadPool);
I think that the Kotlin part of the compilation is going ok, then the java part fails because it doesn't "see" the kotlin side, so to speak.
I think I should tell it somehow to to load the already compiled kotlin classes in the classpath; but (first) how do I do this in gradle? and (second) is it even possible? I think you can't mix module path and class path in Java 9.
How can I solve this? I think it is a pretty common situation, as every java9-style module will be a mixed-language module (because of module-info.java), so I think I'm missing something really basic here.
Thanks in advance!
Solved! It was sufficient to set the kotlin compilation dir to the same dir as Java:
compileKotlin.destinationDir = compileJava.destinationDir
It works now, both with the sources in the same tree or in different trees; but with a quirk: the jar task produces a jar with all the entries duplicated. I'll work on fix this, next.
Thanks to everyone!
I am using the following gradle script where I put the module-info.java under src/module. It gets automatically included in the jar (without duplicates):
if (JavaVersion.current() >= JavaVersion.VERSION_1_9) {
subprojects {
def srcModule = "src/module"
def moduleInfo = file("${project.projectDir}/$srcModule/module-info.java")
if (moduleInfo.exists()) {
sourceSets {
module {
java {
srcDirs = [srcModule]
compileClasspath = main.compileClasspath
sourceCompatibility = '9'
targetCompatibility = '9'
}
}
main {
kotlin { srcDirs += [srcModule] }
}
}
compileModuleJava.configure {
dependsOn compileKotlin
destinationDir = compileKotlin.destinationDir
doFirst {
options.compilerArgs = ['--module-path', classpath.asPath,]
classpath = files()
}
}
jar.dependsOn compileModuleJava
}
}
}
I won't update it any longer, have a look at https://github.com/robstoll/atrium/blob/master/build.gradle
to see the current version in use.
The accepted answer did not work for me (atleast not the way it was presented), but this is what worked:
plugins {
id "org.jetbrains.kotlin.jvm" version "1.3.50"
}
compileKotlin {
doFirst {
destinationDir = compileJava.destinationDir
}
}
jar {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
Doing it the way the accepted answer suggests led to me getting this error:
Directory '/path/to/project/build/classes/kotlin/main' specified for
property 'compileKotlinOutputClasses' does not exist.
Gradle version: 5.6
I ran into the same problem and the existing answers fixed only part of the issue for me, so I searched over all internet and ended up with a working solution. I don't know exactly why this works, but I decided to share my build.gradle.kts file here to help other people to find they way. This file is a combination of many pieces that I found on the internet.
I'm using Java 16, Kotlin 1.5.31 and Gradle 7.1.
The file tree is:
+ project
- build.gradle.kts
+ src
+ main
+ java
- module-info.java
+ my
+ package
- SomeClasses.java
+ kotlin
+ my
+ package
- MoreClasses.kt
module-info.java
module name.of.your.javamodule {
requires kotlin.stdlib;
requires kotlinx.coroutines.core.jvm;
requires org.jetbrains.annotations;
exports my.pacakge;
}
build.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
application
kotlin("jvm") version "1.5.31"
id("org.jetbrains.kotlin.plugin.serialization") version "1.5.31"
}
val kotlinVersion = "1.5.31"
group = "your.group.id"
version = "0.0.1-SNAPSHOT"
application {
mainClass.set("full.name.of.your.MainClass")
mainModule.set("name.of.your.javamodule") // Same defined in module-info.java
executableDir = "run"
}
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8", kotlinVersion))
implementation("com.michael-bull.kotlin-inline-logger:kotlin-inline-logger:1.0.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2-native-mt")
implementation("org.jetbrains:annotations:22.0.0")
testImplementation(kotlin("test", kotlinVersion))
}
java {
sourceCompatibility = JavaVersion.VERSION_16
targetCompatibility = JavaVersion.VERSION_16
}
tasks {
run.configure {
dependsOn(jar)
doFirst {
jvmArgs = listOf(
"--module-path", classpath.asPath
)
classpath = files()
}
}
compileJava {
dependsOn(compileKotlin)
doFirst {
options.compilerArgs = listOf(
"--module-path", classpath.asPath
)
}
}
compileKotlin {
destinationDirectory.set(compileJava.get().destinationDirectory)
}
jar {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
}
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = "16"
}
}
On gradle 7.4 with kotlin DSL, I need to:
move the module-info.java to src/main/java
create any java file inside each package to export under src/main/java, at least empty package-info.java
In build.gradle.kts:
val compileKotlin: KotlinCompile by tasks
val compileJava: JavaCompile by tasks
compileKotlin.destinationDirectory.set(compileJava.destinationDirectory)
Also discussed here:
https://github.com/gradle/gradle/issues/17271