gucludd
(Baha)
September 11, 2015, 8:21pm
1
I need to pass some properties from custom plugin class into task class that extends DefaultTask.
public class TestCustomPlugin implements Plugin < Project > {
@Override
public void apply(Project target) {
System.out.println(“Upload task is on custom plug in…”);
String property1 = "testProp1";
String property2 = "testProp2";
Map<String,String> taskInfoMap = new LinkedHashMap<String,String>();
taskInfoMap.put("type", "DefaultTask");
taskInfoMap.put("description", "Uploads Jacoco dumps to tts client");
target.task(new LinkedHashMap<String,String>(), "testiqUpload");
}
}
public class UploadTask extends DefaultTask {
@TaskAction
public void testiqUpload() {
//Object o1 = this.property("testProp1");
//Object o2 = this.property("testProp2");
//System.out.println(o.toString());
System.out.println("Executing task: testiqUpload");
getDumpsAndUpload();
}
}
Could you please help me to figure out passing parameter into task class?
Thanks!
Rene
(René Groeschke)
September 11, 2015, 8:58pm
2
Not sure I understand your problem completely. The easiest way to instantiate a custom task would be
target.tasks.create("name", CustomTask.class){
//configure your instantiated task
}
gucludd
(Baha)
September 11, 2015, 9:09pm
3
My issue is that I need to pass parameters from TestCustomPlugin class into UploadTask. Both classes are mechanism of custom plugin.
Rene
(René Groeschke)
September 11, 2015, 9:24pm
4
Where are those parameters come from? You should model the CustomTask to have properties settable by the plugin which created it.
class TestCustomPlugin implements Plugin {
@Override
public void apply(Project target) {
System.out.println("Upload task is on custom plug in...");
String property1 = "testProp1";
String property2 = "testProp2";
TestCustomTask customTask = target.tasks.create("customTask", TestCustomTask.class)
customTask.property1 = property1
customTask.property2 = property2
}
}
class TestCustomPlugin extends DefaultTask {
String property1
String property2
@TaskAction void doWork(){
println property1
println property2
}
}
1 Like
gucludd
(Baha)
September 11, 2015, 10:33pm
5
Thanks a lot. It really helps. Any chance of java correspondence of the following statements:
TestCustomTask customTask = target.tasks.create(“customTask”, TestCustomTask.class)
customTask.property1 = property1
customTask.property2 = property2
I tried to apply that into Java:
public class TestCustomPlugin implements Plugin < Project > {
@Override
public void apply(Project target) {
System.out.println(“Upload task is on custom plug in…”);
String property1 = "testProp1";
String property2 = "testProp2";
UploadTask task = (UploadTask) target.task("testiqUpload");
task.setProperty1(property1);
task.setProperty2(property2);
}
}
public class UploadTask extends DefaultTask {
String property1
String property2
public setProperty1 (String prop1)
{
this.property1= prop1;
}
public setProperty2 (String prop2)
{
this.property2= prop2;
}
@TaskAction
public void testiqUpload() {
//Object o1 = this.property("testProp1");
//Object o2 = this.property("testProp2");
//System.out.println(o.toString());
System.out.println("Executing task: testiqUpload");
getDumpsAndUpload();
}
}
As a result of this, I hit the following exception:
A problem occurred evaluating root project ‘resources’.
Failed to apply plugin [id ‘org.qa.tts’]
org.gradle.api.DefaultTask_Decorated cannot be cast to org.qa.tts.client.gradle.UploadTask
There is no difference between Groovy and Java for creating task. To create a task with a specified type is done by TaskContainer
's <T extends Task> T create(String, Class<T>)
method .
@Rene ’s groovy code can be written in Java like…
UploadTask task = target.getTasks()
.create("testiqUpload", UploadTask.class);
task.setProperty1(property1);
task.setProperty2(property2);
In Groovy, setter/getter access can be written as property access.
So the Java code
public class Foo {
private String name;
public String getName() {return name;}
public void setName(String name) {this.name = name;}
}
Foo foo = new Foo();
foo.setName("bar");
assertThat(foo.getName(), is("bar"));
can be translated into Groovy like
Foo foo = new Foo()
foo.name = 'bar'
assert foo.name == 'bar'
1 Like
gucludd
(Baha)
September 12, 2015, 3:19pm
7
Thanks a lot @Shinya_Mochida . I tried your (and so Rene’s) suggestion on Java and my issue was resolved.
ZillaG
(Chris F)
November 21, 2019, 6:25pm
8