1

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.

Cydrick Trudel
  • 9,957
  • 8
  • 41
  • 63

2 Answers2

4

The dependency substitution must not live within a task action. it must be triggered way before. when executing a task it is usually too late for a dependency substitution. Your first snippet is misleading as it is not executed within a task but during the configuration phase, even though it is done within a task configuration.

bhantol
  • 9,368
  • 7
  • 44
  • 81
Rene Groeschke
  • 27,999
  • 10
  • 69
  • 78
  • Thank you. That's what I figured out. So if I'm right, Gradle executes everything in build.gradle first, than does the configuration phase on tasks and then the execution phase? – Cydrick Trudel Sep 18 '15 at 11:23
3

For the sake of completion, it is indeed impossible to do dependency substitution in a task as stated by Rene Groeschke. I ended up adding a class in my plugin that does the dependency substitution :

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 has to 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();
Cydrick Trudel
  • 9,957
  • 8
  • 41
  • 63