Gradle - 依赖替代在插件中不起作用 [英] Gradle - Dependency Substitution not working in a plugin

查看:885
本文介绍了Gradle - 依赖替代在插件中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Gradle插件有些麻烦。我试图在插件中进行依赖替换,结果与在 build.gradle 文件中进行替换时不同。



我有Project1依赖于Project2。在Project2中,我在Project1中使用了一个名为 AClass 的类。



然后我想用项目 org.example:Project2:1.0 >:Project2的。所以,在 build.gradle 中,我放了以下代码:

 任务updateDependency {
configured.all {
resolutionStrategy.dependencySubstitution {
substitute module(org.example:Project2:1.0)with project(:Project2)
}
}
}

哪个工作正常。但是,如果我尝试将以下代码放在插件中:

  public class UpdateDependency extends DefaultTask {

@TaskAction
public void executeTask(){

project.configurations.all {
resolutionStrategy.dependencySubstitution {
substitute module(org.example:Project2: 1.0)与项目(:Project2)
}
}


}
}

并调用与代码相关联的任务,它会显示错误以下错误:

  /home/me/Workspace/Project1/src/Main.java:错误:找不到符号
new AClass()
^
符号:class AClass
位置:class Main
1错误
:compileJava失败

显然由于某些原因Project1无法找到Project2。



我使用以下任务运行Gradle(其中 updateDependen cy 是与依赖替换相关联的任务的名称):

  gradle clean updateDependency build 

我怀疑它与Gradle应用代码的顺序有关,但我不知道如何修复它。

解决方案

依赖替换不能生活在任务操作中。它必须先触发。执行任务时,依赖替换通常太晚了。您的第一个代码片段具有误导性,因为它不会在任务中执行,而是在配置阶段,即使它在任务配置中完成。


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.

解决方案

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.

这篇关于Gradle - 依赖替代在插件中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆