Hello,
I am trying to write a Task that downloads an artifact stored in a repository. This artifact is just a zip file but the version gets determined in a function. The zip is not required for the build itself but later for the setup creation.
The idea is that other teams can include this plugin and pass in their version number which will be used to do a mapping of their version number to the zip-dependency I want to download.
As of Gradle 9.0 calling Task.getProject()
will be deprecated. The warnings got me to: Upgrading your build from Gradle 7.x to 8.0
There are some solutions but none fit my problem.
package org.example;
import org.gradle.api.DefaultTask;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.TaskAction;
public abstract class DownloadTask extends DefaultTask {
@Input
abstract Property<String> getProjectVersion();
@TaskAction
void action() {
var zipVersion = Common.determineVersion(getProjectVersion().get());
var dependency = getProject().getDependencies().create("com.example:zipArtifact:" + zipVersion);
var configuration = getProject().getConfigurations().detachedConfiguration(dependency);
configuration.resolve().forEach(file -> {
System.out.println("RESOLVED FILE: " + file);
});
}
}
This is my current approach which works, but there are deprecation warnings because I use Task.getProject().
I tried several ways to inject Dependencies and Configuration but so far I have not found a way to do that. Or is there any other way to achieve my goal to dynamically load another dependency?
I have two other ideas to solve this issue:
- let the plugin update the version in
libs.versions.toml
. Each project needs to run this plugin themselves before to update their dependencies. - expose
Common.determineVersion(String projectVersion)
.
Exposing works fine:
import org.gradle.api.Project;
import org.gradle.api.Plugin;
public class CommonPlugin implements Plugin<Project> {
public void apply(Project project) {
// nothing to do
}
public static String determineVersion(String productVersion) {
return "1.2.3.4";
}
}
With that each team could define a dependency block:
dependencies {
implementation "com.example:zipArtifact:" + CommonPlugin.determineVersion(project.version)
}
But I am not sure if that is the best solution, I’d prefer the dynamic dependency resolution to copy them in the right folder were they will be needed.