I'm having some trouble with Gradle plugins. I'm trying to do dependency substitution inside a plugin, and the result is different than when I'm doing the substitution in the build.gradle
file.
I have Project1 which is dependent on Project2. In Project2, I have a class named AClass
which I'm using in Project1.
I then want to substitute the module org.example:Project2:1.0
with the 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 fine. However, if I try to put the following code in a 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 displays error the following error:
/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 reasons.
I run Gradle using the following tasks (where updateDependency
is the name of the task associated with the dependency substitution):
gradle clean updateDependency build
I suspect that it has something to do with the order that Gradle applies the code, but I have no idea how to fix it.