gradlegradle-plugin

Gradle plugin download dependency programmatically


curently I am writing a gradle plugin and I need to add and download a maven dependency programmatically in a given task.

I evaluated DependencyHandler and ArtifactResolutionQuery but I can't figure out where and how to add a Dependency and resolve it in mavenCentral repository

Similar coding for Maven does look rather easy

Artifact artifact = artifactFactory.createArtifactWithClassifier(groupId, artifactId, version, type, classifier);
      
artifactResolver.resolve(artifact, remoteRepositories, localRepository);

So I guess/hope there is a similar easy way in Gradle and I am just not seeing it

Regards Mathias

Update 1:

So here is some of the stuff I tried, wildly c&p from different tries. it is worth saying that the dependency I want to download has the classifier ZIP, so normal in my build.gradle I write

  compile 'group:artifact:version@zip

to get the file

ComponentIdentifier componentIdentifier = new DefaultModuleComponentIdentifier("com.sap.cloud",
                "neo-java-web-sdk", "3.39.10");

System.out.println("CompIdentifier = " + componentIdentifier.getDisplayName());
//getProject().getDependencies().add("compile", componentIdentifier.getDisplayName());


Configuration configuration = getProject().getConfigurations().getByName("compile");
org.gradle.api.artifacts.Dependency dep2 = new DefaultExternalModuleDependency("com.sap.cloud", "neo-java-web-sdk", "3.39.10");

boolean depList = configuration.getDependencies().add(dep2);
//
configuration.forEach(file -> {
    getProject().getLogger().lifecycle("Found project dependency @ " + file.getAbsolutePath());
});
Set<File> files =  configuration.resolve();
for (File file2 : files) {
    System.out.println("Files: " + file2.getName());
}

DependencyHandler dep =  getProject().getDependencies();
ComponentModuleMetadataHandler modules = dep.getModules();

ArtifactResolutionQuery a = getProject().getDependencies().createArtifactResolutionQuery()
   
 .forComponents(componentIdentifier).withArtifacts(MavenModule.class, SourcesArtifact.class);
ArtifactResolutionResult r = a.execute();
Set<ComponentArtifactsResult> set = r.getResolvedComponents();
Set<ComponentResult> c = r.getComponents();

Solution

  • I think the simplest way to download a dependency programmatically in a Gradle plugin is the same as doing it in a build script. Just create a new configuration, add your dependency and resolve the configuration. Watch the example below how this works in Java (the preferred language for Gradle plugins):

    Configuration config = project.getConfigurations().create("download");
    config.setTransitive(false); // if required
    project.getDependencies().add(config.getName(), "com.sap.cloud:neo-java-web-sdk:3.39.10@zip");
    File file = config.getSingleFile();
    

    For this example, the name of the configuration ("download") can be any string not already used as configuration name (like compile or runtime). Since the configuration will be resolved afterwards, you must use another name whenever you reuse this code snippet (or if you call it multiple times).