Calling ant from gradle plugin with null File attribute results in C:/path/null


(mrichar2) #1

I have a gradle task passing in a java File to an ant task where the java File is null. Somewhere along the line it is being translated into C://null and thus causing errors later on. I am using Gradle 2.12.

Example:

Given two variables that both the ant task and the gradle extension track: myFile and myFileStr
Before the ant task is called:

  • myFile is null
  • myFileStr is null
    Inside the ant task printing out those variables:
  • myFile is C:\gradle-plugin-test\null
  • myFileStr is null

I would expect myFile to be null.

It is being passed in like this:

        ant.taskdef(name: 'mytask', classname: 'com.myorg.myplugin.ant.MyTask',
        classpath: project.configurations.compile.plus(project.buildscript.configurations.classpath).asPath);
        // call the ant task
        println "MyTask called with: " + extension.myFile + " myFileStr: " + extension.myFileStr;
        ant.mytask(
                myFile: extension.myFile,
                myFileStr: extension.myFileStr
        ) { }

Code to reproduce (consume the plugin from another project and run the ‘mytask’ task):

// src/main/java/com/myorg/myplugin/ant/MyTask.java
package com.myorg.myplugin.ant;

import java.io.File;

import org.apache.tools.ant.Task;

public class MyTask extends Task {
	private File myFile = null;
	private String myFileStr = null;
	
	public void setMyFile(File file) {
		myFile = file;
	}
	
	public void setMyFileStr(String str) {
		myFileStr = str;
	}
	
	@Override
	public void execute()
	{
		System.out.println("execute() myFile: " + myFile + " myFileStr: " + myFileStr);
	}
}

// src/main/groovy/com/myorg/myplugin/MyPlugin.groovy
package com.myorg.myplugin;

import org.gradle.api.Plugin;
import org.gradle.api.Project;

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

public class MyExtension
{
    def myFile;
    def myFileStr;
}

public class MyPlugin implements Plugin<Project>
{

    void apply(Project project)
    {
    	project.task('mytask', type: MyTask) {
            description = "description"
            outputs.upToDateWhen { false }
        }

        MyExtension extension = new MyExtension();
        project.getExtensions().add('myplugin', extension);
    }
}

public class MyTask extends DefaultTask {
	public MyTask() {
	}

	@TaskAction
    def doTask()
    {
		Project project = getProject();
	    MyExtension extension = (MyExtension) project.getExtensions().findByName('myplugin');
    
        ant.taskdef(name: 'mytask', classname: 'com.myorg.myplugin.ant.MyTask',
        classpath: project.configurations.compile.plus(project.buildscript.configurations.classpath).asPath);

        // call the ant task
        println "MyTask called with: " + extension.myFile + " myFileStr: " + extension.myFileStr;
        ant.mytask(
                myFile: extension.myFile,
                myFileStr: extension.myFileStr
        ) { }
    }
}

// src/main/resources/META-INF/gradle-plugins/myplugin.properties
implementation-class=com.myorg.myplugin.MyPlugin