简体   繁体   English

在Gradle插件中获取项目源目录

[英]Get a project source dir in a Gradle plugin

First of all sorry if I've missed this in the documentation somewhere. 首先,很抱歉,如果我在某个地方的文档中错过了此内容。

I'm trying to write a Gradle plugin in Java. 我正在尝试用Java编写Gradle插件。

More specifically I need to do this: 更具体地说,我需要这样做:

public class MyPlugin implements Plugin<Project>{

    @Override
    public void apply(Project project) {
        project.getSourceDir();
    }
}

In other words, how can I, if possible, get the directory where the sources of the project my plugin is run over reside? 换句话说,如果可能的话,如何获得我的插件所运行的项目源所在的目录?

Am I correct in my assumption that when the apply method gets called Gradle will already know where the sources are and this location can not change further on? 我的假设是正确的,即当apply方法调用Gradle时将已经知道源在哪里,并且此位置无法继续更改?

Thanks. 谢谢。

Yes because the plugin is applied to a Project per your parameter. 是的,因为该插件根据您的参数应用于项目。 If you "apply" it to two projects then it creates two instances of the plugin and pass your project objects to the apply method. 如果您将其“应用”到两个项目,则它将创建该插件的两个实例,并将您的项目对象传递给apply方法。

Look through the Gradle user guide. 查看Gradle用户指南。

A project has a list of "sourceSet" objects, which you get to with "project.sourceSets". 一个项目有一个“ sourceSet”对象的列表,可通过“ project.sourceSets”访问。 Each sourceSet has a set of properties. 每个sourceSet都有一组属性。

It's good to browse through The DSL reference to see what properties are available. 最好浏览DSL参考以了解可用的属性。 Look for "SourceSet". 查找“ SourceSet”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM