本文介绍了用Java定义Gradle插件属性扩展的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Java中创建一个具有属性扩展的Gradle插件(不是惯例,因为这显然是,)。为了记录,我正在Linux机器上使用Gradle 1.6(Ubuntu 12.04)。



我已经知道应该完成这个工作了在插件类定义中。这是添加扩展的一种方式。创建一个包含你的属性的扩展类:

  public class MyPluginExtensions {

File sourceDir;
文件outputDir;

public MyPluginExtensions(项目项目){
this.project = project;
sourceDir = new File(project.getProjectDir(),src);
outputDir = new File(project.getBuildDir(),out);
}

}

现在将这些扩展添加到项目中在主插件类中:

  public class MyPlugin implements Plugin< Project> {

@Override
public void apply(Project project){

Map< String,Object> taskInfo = new HashMap< String,Object>();
taskInfo.put(type,MyPluginTask.class);
taskInfo.put(description,生成等等。);
taskInfo.put(group,Blah);
任务myPluginTask = project.task(taskInfo,myPluginTask);

//定义约定并将它们附加到任务
MyPluginExtensions extensions = new MyPluginExtensions(project);
myPluginTask.getExtensions()。add(
sourceDir,
extensions.sourceDir);
myPluginTask.getExtensions()。add(
outputDir,
extensions.outputDir);
}
}

然而,这种方法似乎并不正确。一个新的项目属性显示在 project.ext 命名空间中。我希望能够解决插件扩展如下:

 在我的build.gradle中:
myPluginTask.sourceDir = file('the / main / src')
myPluginTask.outputDir = file('the / output')

然而,当我将这些东西放在使用我的插件的gradle脚本中并尝试设置此属性时,gradle告诉我我无法设置它:

  *出错:
评估脚本时发生问题。
>有一个注册名为'sourceDir'的扩展名。你不应该通过属性设置器重新分配它。

那么,在基于Java的Gradle插件中为任务添加属性扩展的正确方法是什么?



编辑:



基于其他SO帖子,我的扩展对象一炮打响:

  //第一次尝试:
//myPluginTask.getExtensions()。add sourceDir,extensions.sourceDir);
//myPluginTask.getExtensions().add(\"outputDir\",extensions.outputDir);

//第二次尝试
myPluginTask.getExtensions()。add(myPluginTask,extensions);

这似乎起作用。然而,Gradle现在抱怨我已经添加了一个动态属性:

 弃用的动态属性:sourceDirontask :myPluginTask',值:/ Users / jfer ...。 

所以,再次,添加插件扩展属性的正确方法是什么?



编辑2



所以,再来一个镜头,项目对象并使用create方法:

  //第一次尝试:
//myPluginTask.getExtensions()。添加(sourceDir,extensions.sourceDir);
//myPluginTask.getExtensions().add(\"outputDir\",extensions.outputDir);

//第二次尝试
// myPluginTask.getExtensions()。add(myPluginTask,extensions);

//第三次尝试
project.getExtensions()。create(myPluginTask,MyPluginExtensions.class,project);

然而,由于以下几个原因,这个失败:


  1. 创建具有相同名称的属性扩展名(myPluginTask),因为任务会在任务名称和扩展名称之间创建冲突,导致任务从gradle的角度消失(并倾斜错误,例如没有这样的属性:dependsOn for class ... MyPluginExtensions)。
  2. 如果我提供的名称不与任务名称相冲突(例如,myPluginPropExt) ,create()方法可以工作,但不会像预期的那样在其自己的名称空间中添加扩展名(例如, project.myPluginPropExt.propertyName ),而是将其添加到项目名称空间中例如, project.propertyName ),这是不正确的,并且会导致Gradle抛出一堆弃用的动态属性警告。

  3. 解决方案

    所以这里是我的问题的解决方案:

      public class MyPlugin实现Plugin< Project> {

    @Override
    public void apply(Project project){

    Map< String,Object> taskInfo = new HashMap< String,Object>();
    taskInfo.put(type,MyPluginTask.class);
    taskInfo.put(description,生成等等。);
    taskInfo.put(group,Blah);
    任务myPluginTask = project.task(taskInfo,myPluginTask);

    //定义约定并将它们附加到任务
    MyPluginExtensions extensions = new MyPluginExtensions(project);

    //神奇的扩展代码:
    project.getExtensions()。add(myPluginName,extensions);


    $ / code $ / pre

    现在我可以为其中一个扩展名设置一个值属性在我的 gradle.build 文件中是这样的(并且我不会收到有关添加不推荐使用的动态属性的警告):

      myPluginName.sourceDir = file('/ main / src')

    最后的诀窍是在我的Plugin任务中获得这个值:

    pre $ public $ MyPluginTask extends DefaultTask {
    @TaskAction
    public void action(){
    MyPluginExtensions extensions =(MyPluginExtensions)getProject()
    .getExtensions()。findByName(myPluginName);

    System.out.println(sourceDir value:+ extensions.sourceDir);


    $ / code $ / pre
    $ b $ p

    这个方法可行,但这个解决方案让我感到恼火的是我希望能够将扩展属性放入与我在基于groovy的插件中看到的任务相同的命名空间中(例如, myPluginTask.sourceDir ),但是这个显然不支持或者不行。



    与此同时,希望这可以帮助别人。


    I'm trying to create a Gradle plugin in Java that has property extensions (not conventions, as this is apparently the old, wrong way). For the record, I'm working with Gradle 1.6 on a Linux machine (Ubuntu 12.04).

    I've gotten as far as figuring out that the this should be done in the Plugin class definition. Here is A way of adding an extension. Create an extension class that contains your properties:

    public class MyPluginExtensions {
    
        File sourceDir;
        File outputDir;
    
        public MyPluginExtensions(Project project) {
            this.project = project;
            sourceDir = new File(project.getProjectDir(), "src");
            outputDir = new File(project.getBuildDir(), "out");
        }
    
    }
    

    Now add these extensions to the project in the main plugin class:

    public class MyPlugin implements Plugin<Project> {
    
        @Override
        public void apply(Project project) {
    
            Map<String,Object> taskInfo = new HashMap<String,Object>();
            taskInfo.put("type", MyPluginTask.class);
            taskInfo.put("description", "Generates blah from blah.");
            taskInfo.put("group", "Blah");
            Task myPluginTask = project.task(taskInfo, "myPluginTask");
    
            // Define conventions and attach them to tasks
            MyPluginExtensions extensions = new MyPluginExtensions(project);
            myPluginTask.getExtensions().add(
                    "sourceDir", 
                    extensions.sourceDir);
            myPluginTask.getExtensions().add(
                    "outputDir", 
                    extensions.outputDir);
        }
    }
    

    This approach, however, doesn't seem to be correct. A new project property is shows up in the project.ext namespace. I expect to be able to address the plugin extensions as:

    in my build.gradle:
    myPluginTask.sourceDir = file('the/main/src')
    myPluginTask.outputDir = file('the/output')
    

    However, when I put such things in a gradle script that uses my plugin and try to set this property, gradle tells me I can't set it:

    * What went wrong:
    A problem occurred evaluating script.
    > There's an extension registered with name 'sourceDir'. You should not reassign it via a property setter.
    

    So, what's the right way to add property extensions for a task in a Java-based Gradle plugin?

    EDIT:

    Based on some other SO posts, I tried just adding my extensions object in one shot:

    // first attempt:
    //myPluginTask.getExtensions().add("sourceDir", extensions.sourceDir);
    //myPluginTask.getExtensions().add("outputDir",extensions.outputDir);
    
    // second attempt
    myPluginTask.getExtensions().add("myPluginTask", extensions);
    

    This appears to work. However, Gradle is now complaining that I've added a dynamic property:

    Deprecated dynamic property: "sourceDir" on "task ':myPluginTask'", value: "/Users/jfer...".
    

    So, again, what's the right way to add a plugin extension property?

    EDIT 2

    So, taking yet another shot at this, I'm adding the extension to the project object and using the create method instead:

    // first attempt:
    //myPluginTask.getExtensions().add("sourceDir", extensions.sourceDir);
    //myPluginTask.getExtensions().add("outputDir",extensions.outputDir);
    
    // second attempt
    // myPluginTask.getExtensions().add("myPluginTask", extensions);
    
    // third attempt
    project.getExtensions().create("myPluginTask", MyPluginExtensions.class, project);
    

    However, this fails for a couple of reasons:

    1. Creating a properties extension with the same name ("myPluginTask") as the task creates a collision between the task name and extension name, causing the task to disappear from gradle's perspective (and throw oblique errors, such as "No such property: dependsOn for class ...MyPluginExtensions").
    2. If I provide a name that does not collide with a task name (e.g., "myPluginPropExt"), the create() method works, but DOES NOT add the extension in its own namespace as expected (e.g., project.myPluginPropExt.propertyName and instead adds it in the project namespace (e.g., project.propertyName) which is not correct and causes Gradle to throw a bunch of "deprecated dynamic property" warnings.
    解决方案

    So here is a solution to my problem:

    public class MyPlugin implements Plugin<Project> {
    
        @Override
        public void apply(Project project) {
    
            Map<String,Object> taskInfo = new HashMap<String,Object>();
            taskInfo.put("type", MyPluginTask.class);
            taskInfo.put("description", "Generates blah from blah.");
            taskInfo.put("group", "Blah");
            Task myPluginTask = project.task(taskInfo, "myPluginTask");
    
            // Define conventions and attach them to tasks
            MyPluginExtensions extensions = new MyPluginExtensions(project);
    
            // the magic extension code:
            project.getExtensions().add("myPluginName", extensions);
        }
    }
    

    Now I can set a value for one of the extension properties in my gradle.build file like so (and I don't get a warning about adding deprecated dynamic properties):

     myPluginName.sourceDir = file('the/main/src')
    

    The final trick is to get this value in my Plugin's task:

    public class MyPluginTask extends DefaultTask {
        @TaskAction
        public void action() {
            MyPluginExtensions extensions = (MyPluginExtensions) getProject()
                    .getExtensions().findByName("myPluginName");
    
            System.out.println("sourceDir value: " + extensions.sourceDir);
        }
    }
    

    This works, but what annoys me about this solution is that I want to be able to put the extension properties in the same namespace as the task (e.g., myPluginTask.sourceDir) which I have seen in groovy-based plugins, but this apparently is not supported or just doesn't work.

    In the meantime, hope this helps someone else.

    这篇关于用Java定义Gradle插件属性扩展的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 14:10