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
Related
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.
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 publish an AAR to another part of my team that checks out only this AAR, not the source code.
I would like to provide them with the source code, seamlessly (with Ctrl + Click on class or Ctrl + B to get on the source code directly inside Android Studio).
As far as I understand, it's only possible with .jar archives, not .aar.
Here's the maven-publish configuration file. I can publish the archives (debug, "release" and sources) to the maven. I can get the debug dependency ("xxx-debug.aar"). I can download manually the sources ("xxx-sources.jar") and link them via "Choose sources..." in Android Studio. But the linking isn't automatic like any other decent library (Retrofit or OkHttp comes to mind) :
// MAVEN PUBLICATION
apply plugin: 'maven-publish'
task sourceJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier "sources"
}
publishing {
group = 'foo.bar.rootpackage'
version = "foobar1.0"
publications {
aar(MavenPublication) {
println("Maven publish: package name is ${aarName}")
groupId group
artifactId sdkModuleName
version version
artifact(sourceJar)
def debugAar = "$buildDir/outputs/aar/${aarName}-debug.aar"
if (new File(debugAar).exists()) {
artifact(debugAar) {
classifier 'debug'
extension 'aar'
}
}
def releaseAar = "$buildDir/outputs/aar/${aarName}-release.aar"
if (new File(releaseAar).exists()) {
artifact(releaseAar) {
extension 'aar'
}
}
//The publication doesn't know about our dependencies, so we have to manually add them to the pom
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
def compileTimeDependencies = configurations.api.allDependencies +
configurations.implementation.allDependencies +
configurations.releaseImplementation.allDependencies
appendDependencies(compileTimeDependencies, dependenciesNode)
}
}
}
repositories {
maven {
// PRIVATE MAVEN STUFF
}
}
}
ext {
appendDependencies = { Set<Dependency> compileTimeDependencies, dependenciesNode ->
compileTimeDependencies.each {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
if (!it.excludeRules.isEmpty()) {
def exclusionsNode = dependencyNode.appendNode('exclusions')
it.excludeRules.each { rule ->
def exclusionNode = exclusionsNode.appendNode('exclusion')
exclusionNode.appendNode('groupId', rule.group)
exclusionNode.appendNode('artifactId', rule.module ?: '*')
}
}
}
}
}
Inside the build.gradle of the other project needing this dependency, I implement the dependency like that :
debugApi("foo.bar.rootpackage:core:$foobar1.0:debug#aar") {
transitive = true
}
releaseApi("foo.bar.rootpackage:core:$foobar1.0#aar") {
transitive = true
}
Project builds, but no "automatic link" with the sources.
How to fix ?
Use android-maven-publish. Linking the sources package should happen in the generated POM.
I'm trying to produce a maven signed jar but if I receive this exception
groovy.lang.MissingPropertyException: Could not set unknown property 'keyId' for object of type org.gradle.plugins.signing.SigningExtension
This is my build.gradle
plugins {
id 'java-library'
id 'maven-publish'
id 'signing'
}
apply from: 'gradle.properties'
group 'com.foo'
version '1.0.0'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
// dependencies
}
task sourcesJar(type: Jar) {
archiveClassifier = 'sources'
from sourceSets.main.allJava
}
task javadocJar(type: Jar) {
archiveClassifier = 'javadoc'
from javadoc.destinationDir
}
publishing {
publications {
myLibrary(MavenPublication) {
from components.java
artifact sourcesJar
artifact javadocJar
}
}
repositories {
maven {
name = 'myRepo'
url = "file://${buildDir}/repo"
}
}
}
signing.keyId='MY_KEY'
signing.password='MY_SECRET'
signing.secretKeyRingFile=/NOT_TO_PUBLISH/secret-keys.gpg
signing {
sign publishing.publications.myLibrary
}
If I comment on the signing sections all works fine and my publications are generated.
Any helps would be appreciated
The documentation has them without quote marks:
signing.keyId=24875D73
signing.password=secret
signing.secretKeyRingFile=/Users/me/.gnupg/secring.gpg
I'm sure you have, but have you tried without quote marks?
The documentation above also contains an alternate way of setting the values, do you get a different response?
allprojects {
ext."signing.keyId" = id
ext."signing.secretKeyRingFile" = file
ext."signing.password" = password
}
Additionally, are you using an up to date version of Gradle (so it properly supports the plugin)?
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