Gradle warning: "The libsDir method has been deprecated." - gradle

I'm using Gradle 5.6.1 with a build.gradle file that publishes a distribution to Artifactory.
When I run the publish task, I receive a warning:
$ gradle publishMyPublicationToRemoteArtifactoryRepository
Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Using the suggested flag reveals The libsDir method has been deprecated.:
$ gradle publishMyPublicationToRemoteArtifactoryRepository --warning-mode all
> Configure project :
The libsDir method has been deprecated. This is scheduled to be removed in Gradle 6.0.
The relevant section of build.gradle is below:
distributions {
main {
baseName = archivesBaseName
contents {
from libsDir
}
}
}
How do I correct libsDir so that the warning is resolved?

Referencing the libsDir directly usually indicates bad design and often can be avoided with a different approach to the problem.
However, for those who are looking for a direct replacement and are using Gradle 6.0 or higher, it is libsDirectory.
The default value with java plugin is ${project.buildDir}/${project.libsDirName}, you may also use it as a replacement if that's what you want.
Please see https://docs.gradle.org/current/dsl/org.gradle.api.plugins.BasePluginConvention.html#org.gradle.api.plugins.BasePluginConvention:libsDir

There is a discussion on GitHub where the following alternative is suggested:
distributions {
main {
baseName = archivesBaseName
contents {
from "${buildDir}/${distsDirName}"
}
}
}

Related

WIthin nebula/gradle, how can I inject the version being released into the jar being published?

We have a tool that runs from the command line. One of the commands is -version.
Before we converted to the nebula release plugin, the version was in the gradle.properties file, and as part of the build we copied it from there to a src/main/resources/version.txt file, that was later read by the tool to output the version.
But now the version is never in a file that's checked into git. Instead, it is only known during the nebula release process.
We want to obtain that version during the nebula release process and inject it into the jar that nebula is about to publish. For example, it could be added to the manifest.
We've tried to figure out how to do this, but don't see any examples online, and nothing about it in the documentation.
Simply create a task that caches the version that is dynamically inferred by Nebula.
Since you originally copied/created src/main/resources/version.txt, we'll use that that model our task.
Assuming a simple/standard Java project, using the Kotlin DSL:
val cacheNebulaVersion by tasks.registering {
mustRunAfter(tasks.named("release"))
doLast {
val sourceSets = project.extensions.getByName("sourceSets") as SourceSetContainer
sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME).output.resourcesDir?.let {
// If there are not existing resources in your project then you must create
// the resources dir otherwise a FileNotFoundException will be thrown.
if (!it.exists()) {
it.mkdirs()
}
File(it, "version.txt").printWriter().use { out ->
out.println(project.version)
}
}
}
}
When I invoke ./gradlew clean build snapshot cacheNebulaVersion, the version produced by Nebula is cached/created at src/main/resources/version.txt in the build output. The task above does not bundle it with the jar.
Hopefully that gives you an idea what to do.

The DefaultSourceDirectorySet constructor has been deprecated. How to use the ObjectFactory service?

I recently updated to gradle version 5.0-rc-4, and when running ./gradlew assemble (or any other task) I now get the following message:
Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
When I use ./gradlew assemble --warning-mode all I get:
> Configure project :
The DefaultSourceDirectorySet constructor has been deprecated. This is scheduled to be removed in Gradle 6.0. Please use the ObjectFactory service to create instances of SourceDirectorySet instead.
But in the following build.gradle I don't see where I'm using any DefaultSourceDirectorySet, so what is this warning about, and what would I need to change to be compatible with Gradle 6.0?
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.10'
}
dependencies {
compile 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
}
repositories {
mavenCentral()
mavenLocal()
jcenter()
}
Related
I found create version.txt file in project dir via build.gradle task(gradle 5.0) but I don't have constructs like that so I don't know how it would apply.
I found this deprecation mentioned in the release notes at https://docs.gradle.org/5.0-milestone-1/release-notes.html but they say
In this release of Gradle, the ObjectFactory service, which is part of the public API, now includes a method to create SourceDirectorySet instances. Plugins can now use this method instead of the internal types.
but I don't see how.
I also found the SourceDirectorySet interface at https://docs.gradle.org/current/javadoc/org/gradle/api/file/SourceDirectorySet.html but I don't see how to use it.
Template repository: https://github.com/PHPirates/kotlin-template-project
Update 2019-01-23 Five minutes ago, kotlin 1.3.20 was released and also updated in the Gradle repository so this issue should be solved by updating the Kotlin Gradle plugin to 1.3.20.
Update 2019-01-11 The target version in Youtrack issue KT-26808 has just been updated to 1.3.20. You can view the latest released version in the Gradle repositories here, but at the moment there are still a lot of open issues for 1.3.20.
Update 2018-12-17 The deprecation warning is fixed in commit https://github.com/JetBrains/kotlin/commit/67e82a54e5ee529116e881953f93a4c8f216e33a, the Youtrack issue is closed. Now waiting for a release to roll out.
As #Javaru pointed out, this has already been reported (in september 2018) at Youtrack issue KT-26808.
Using information from Lance's comment in the link that Thomas David Baker pointed to:
Answer:
If you get this warning while you are not using DefaultSourceDirectorySet directly, this is probably coming from a Gradle plugin you use. You could check this using the --warning-mode all --stacktrace flags for the Gradle build, so like ./gradlew assemble --warning-mode all --stacktrace.
In this particular case it's the Kotlin Gradle Plugin, they use it at DefaultKotlinSourceSet.kt#L140-L155:
private val createDefaultSourceDirectorySet: (name: String?, resolver: FileResolver?) -> SourceDirectorySet = run {
val klass = DefaultSourceDirectorySet::class.java
val defaultConstructor = klass.constructorOrNull(String::class.java, FileResolver::class.java)
if (defaultConstructor != null && defaultConstructor.getAnnotation(java.lang.Deprecated::class.java) == null) {
// TODO: drop when gradle < 2.12 are obsolete
{ name, resolver -> defaultConstructor.newInstance(name, resolver) }
} else {
// (code omitted)
}
}
We can trust that they will resolve the issue in time, so don't worry about the warning.

is there any Gradle inbuilt feature equivalent to Maven's “copy-dependencies” without writing gradle task..?

looks like gradle still not mature when compared to maven.many handy features not available in gradle.
is there any Gradle inbuilt feature equivalent to Maven's “copy-dependencies” without writing gradle task..?
Please check installDist task of distribution plugin.
You have to write code like this:
distributions {
main {
baseName = 'someName'
contents {
from { 'src/readme' }
into { '../../' } // <----- change destination here
}
}
}
This plugin will create distribution installation, which will also copy all runtime dependencies.

Task and plugin conflict in Gradle (Failed to apply plugin [class 'org.gradle.langu...)

I tried to run a task from build.gradle using the following command:
gradle footype
However the build failed and displayed these two error messages that I want to fix:
> Configure project :
The Task.leftShift(Closure) method has been deprecated and is scheduled to
be removed in Gradle 5.0. Please use Task.doLast(Action) instead.
And this message as well:
* What went wrong:
An exception occurred applying plugin request [id: 'java']
> Failed to apply plugin [class
'org.gradle.language.base.plugins.LifecycleBasePlugin']
> Declaring custom 'assemble' task when using the standard Gradle
lifecycle plugins is not allowed.
Here is the code of the build.gradle file:
plugins{
id "com.gradle.build-scan" version "1.10.2"
id "org.arquillian.spacelift" version "1.0.0-alpha-17"
id "java"
}
group 'k'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
task fooType {
doLast {
def foo = "bar"
println "$foo + foo = ${foo + "foo"}"
}
}
I searched on other posts and they all seem to mention a task called clean() which doesn't appear in my code, so I'd like to know what's the problem.
Thank you muchly for reading this post.
When it comes to this message:
Configure project :
The Task.leftShift(Closure) method has been deprecated and is scheduled to
be removed in Gradle 5.0. Please use Task.doLast(Action) instead.
it is just a warning. It states that one (possibly more) plugin you applied uses << which will be removed in version 5.0 of gradle in favour of doLast. You are not using << directly in your script and that's ok. To eliminate this warning first of all you need to know which plugin uses it, then raise an issue on plugin's site asking for deprecated code elimination.
When it comes to the second message it's an error and in single build script nothing can be done about it. Two plugins java and org.arquillian.spacelift have declared a task with the same name (it's assemble) - this is a conflict. Maybe you can split you project into a multimodule?

Unable to resolve a plugin using the new plugin mechanism in Gradle

While trying to upgrade some of our scripts to Gradle 4.0.1 on of the plugins we are using is failing and I thought of fixing that plugin first. The plugin is a third party open source project.
So I have cloned the project and tried to compile it. However it fails with following message:
c:\source\gradle-xld-plugin>gradlew build
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\source\gradle-xld-plugin\build.gradle' line: 2
* What went wrong:
Plugin [id: 'com.gradle.plugin-publish', version: '0.9.7'] was not found in
any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- maven(https://artifactory/java-v) (Could not resolve plugin artifact 'com.gradle.plugin-publish:com.gradle.plugin-publish.gradle.plugin:0.9.7')
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --
debug option to get more log output.
BUILD FAILED in 0s
The build.gradle script for the plugin starts like this:
plugins {
id "com.gradle.plugin-publish" version "0.9.7"
id "com.github.hierynomus.license" version "0.11.0"
id 'nebula.nebula-release' version '4.0.1'
id "com.jfrog.bintray" version "1.7.3"
}
In addition to this the company policy dictates we have to go through an internal artifactory server, so following has been added to the settings.gradle file:
pluginManagement {
repositories {
maven {
url "https://artifactory/java-v"
}
}
}
The jar file exists at following location: https://artifactory/java-v/com/gradle/publish/plugin-publish-plugin/0.9.7/plugin-publish-plugin-0.9.7.jar
but when I look at the error message I am a little puzzled that it says that it cannot find com.gradle.plugin-publish:com.gradle.plugin-publish.gradle.plugin:0.9.7.
It seems to have suffixed the id with .gradle.plugin.
Does anyone know whether I am looking at the wrong location or how come it is suffixing the id with .gradle.plugin. And shouldn't it look at a location that has the GAV like this: com.gradle.plugin-publish:com.gradle.plugin-publish:0.9.7?
And does anyone know about how the resolution mechanism for the new plugin mechanism in Gradle works.
Thanks in advance
Edit
Thanks to Mateusz Chrzaszcz I was able to progress.
The only caveat I have with the solution is that it seems like a workaround rather than a solution. But it works!
In addition to his solution you had to resolve the plugins. I was able to hack my way to actually resolve the appropriate names.
In order to do so one has to do as follows:
In a webbrowser go for the plugin: id "com.github.hierynomus.license" version "0.11.0" go to following URL: https://plugins.gradle.org/api/gradle/4.0.1/plugin/use/com.github.hierynomus.license/0.11.0
The json returned contains the GAV needed in the useModule call. Use that
The following serves as an example:
resolutionStrategy {
eachPlugin {
if (requested.id.namespace == 'com.gradle' && requested.id.name == 'plugin-publish') {
useModule('com.gradle.publish:plugin-publish-plugin:0.9.7')
} else if(requested.id.namespace == 'com.github.hierynomus' && requested.id.name == 'license') {
useModule('nl.javadude.gradle.plugins:license-gradle-plugin:0.11.0')
}
}
}
Try to implement Plugin Resolution Rules.
According to gradle documentation:
Plugin resolution rules allow you to modify plugin requests made in plugins {} blocks, e.g. changing the requested version or explicitly specifying the implementation artifact coordinates.
To add resolution rules, use the resolutionStrategy {} inside the pluginManagement {} block
like that:
pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.namespace == 'com.gradle.plugin-publish') {
useModule('com.gradle.plugin-publish:0.9.7') //try a few combinations
}
}
}
repositories {
maven {
url 'https://artifactory/java-v'
}
}
}
Keep in mind this is incubating feature though.

Resources