本文介绍了如何在Gradle中的buildSrc下的自定义任务中使用第三方依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已按照本指南使用Gradle创建自定义任务. http://www.ysofters.com/2015/02/26/how-to-create-gradle-project-with-custom-task-classes-in-groovy/我还查看了gradle文档. https://docs.gradle.org/current/userguide/custom_tasks.html

I have followed this guide for creating a custom task with Gradle.http://www.ysofters.com/2015/02/26/how-to-create-gradle-project-with-custom-task-classes-in-groovy/I also had a look at the gradle doc. https://docs.gradle.org/current/userguide/custom_tasks.html

非常清楚,我可以获取示例来编译和使用任务,因此到目前为止一切都很好.

It's very clear, and I can get the examples to compile and use the tasks, so everythings fine so far.

但是,示例仅显示gradle api文件的导入,即

However, examples only show the import of gradle api-files, i.e.

import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction

我想添加更多的依赖项,即

I want to add some more dependencies i.e

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean
import org.springframework.core.io.FileSystemResource

但是我可以这样做-gradle会抱怨并说无法解决课程.."

but I can do that - gradle will complain and say "unable to resolve class .."

我也尝试在build.gradle的buildscript-> dependencies闭包下设置spring-core和apache-commons,但这没有任何区别.

I tried to also set up spring-core and apache-commons under the buildscript->dependencies closure in build.gradle but it didn't make any difference i.e.

buildscript {
 dependencies {

        classpath group: 'org.springframework', name: 'spring-core', version: springVersion
        classpath group: 'org.apache.commons', name: 'commons-lang3', version: '3.4'
      .....

那么,我想念的是什么-如何在自定义任务类中使用第三方依赖项?

So, what am I missing - how can I use third party dependencies in my custom task class?

推荐答案

,您需要将build.gradle文件放入您的buildSrc目录中.您可以从gradle项目本身签出buildSrc/build.gradle文件作为示例: https://github.com/gradle/gradle/blob/v4.1.0/buildSrc/build.gradle

you need to put a build.gradle file into your buildSrc directory. You can checkout the buildSrc/build.gradle file from the gradle project itself as an example: https://github.com/gradle/gradle/blob/v4.1.0/buildSrc/build.gradle

这篇关于如何在Gradle中的buildSrc下的自定义任务中使用第三方依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 18:39