Gradle - Dependency Replacement does not work in plugin

I have problems with Gradle plugins. I am trying to make a dependency replacement inside a plugin, and the result is different from when I perform the substitution in the build.gradle file.

I have Project1, which depends on Project2. In Project2, I have a class called AClass , which I use in Project1.

Then I want to replace the org.example:Project2:1.0 module with project :Project2 . So, in build.gradle , I put the following code:

 task updateDependency { configurations.all { resolutionStrategy.dependencySubstitution { substitute module("org.example:Project2:1.0") with project(":Project2") } } } 

which works great. However, if I try to put the following code in the plugin:

 public class UpdateDependency extends DefaultTask { @TaskAction public void executeTask() { project.configurations.all { resolutionStrategy.dependencySubstitution { substitute module("org.example:Project2:1.0") with project(":Project2") } } } } 

and call the task associated with the code, it gives an error message:

 /home/me/Workspace/Project1/src/Main.java: error: cannot find symbol new AClass() ^ symbol: class AClass location: class Main 1 error :compileJava FAILED 

Obviously, Project1 cannot find Project2 for some reason.

I start Gradle with the following tasks (where updateDependency is the name of the task related to replacing dependencies):

 gradle clean updateDependency build 

I suspect it has something to do with the order in which Gradle applies the code, but I don't know how to fix it.

+1
plugins dependencies build.gradle gradle
source share
2 answers

Substitution of dependencies should not be in task action. It must be started earlier. when performing a task, it is usually too late to replace dependencies. Your first snippet is misleading because it is not executed as part of the task, but at the configuration stage, even if it is performed in the task configuration.

+4
source share

To complete, it is indeed impossible to make a dependency replacement in the task, as Rene Grochet states. I ended up adding a class to my plugin that does dependency replacement:

 package com.example import org.gradle.api.Project import org.gradle.api.artifacts.ResolutionStrategy import org.gradle.api.artifacts.DependencySubstitution public class ResolveProject { public ResolveProject() { //Gets a static reference of the Project object from the class // that extends Plugin<Project> Project project = PluginEntryPoint.getProject() project.configurations.all { resolutionStrategy.dependencySubstitution { substitute module("org.example:Project2:1.0") with project(":Project2") } } } } 

And then the class should be called from the build.gradle file

 apply plugin: 'myplugin' buildscript { repositories { maven { url 'file:///path/to/my/maven/repo' } } depdendencies { classpath group: 'com.example', name: 'MyPlugin', version: '1.0' } } dependencies { //Adding the same dependency as the one in the ResolveProject class. compile "org.example:Project2:1.0" } // This needs to be after the dependencies new com.example.ResolveProject(); 
+1
source share

All Articles