Custom plugin with allprojects


(Alan Lee) #1

I have created a plugin and a task.

in the build.gradle, I want to set the root of the task to be the project path itself.

allprojects{

apply “plugin”

test{

root = project.projectDir.getCanonicalPath()+"/src";

}

}

In the plugin, it will read the option from test.

def run() {

logger.debug("[XDK] Start the prepare task");

//read the parameter in the test

if(project.test.root != null){

root = project.test.root;

}

}

I have checked at setting the task test option, it should be correct. But when i run the task for all projects, the root is always the last project path. But I want to have different configuration based on the project itself path.

In fact, if i apply the task, the task is shared ? or unique for each project?

Thanks


(Peter Niederwieser) #2

Plugin implementations have to defer all access to build script properties until after the build script has been evaluated. There are several techniques for doing so. One of them is to wrap the code with ‘project.afterEvaluate { … }’.


(Alan Lee) #3

I tried but it doesn’t work as expected.

Am i correct?

allprojects{

apply “plugin”

project.afterEvaluate {

test{

root = project.projectDir.getCanonicalPath()+"/src";

}

} }

One more question about applying task via the plugin in allprojects, the task is shared among different projects ? so only 1 configuration is possible?

or each project will has that task instance so i can configure among different project?

Thanks a lot


(Peter Niederwieser) #4

The plugin has to read the property in ‘afterEvaluate’, not the build script set it in ‘afterEvaluate’.

Each project has its own tasks and plugins.


(Alan Lee) #5

I have tried but it doesn’t work.

I have changed the plugin.groovy.

if i added afterEvaluate, the path is always the last one instead of the current one. If there is no afterEvaluate, the path is correct

Now my build.gradle

allprojects{

apply “plugin”

test{

root = project.projectDir.getCanonicalPath()+"/src";

}

}

In the plugin.groovy

project.afterEvaluate{

//in this one, the log show the last setting in allprojects

if(project.test.root != null){

project.test.root = project.test.root;

}

}

//in this one, the log is the right project path

if(project.test.root != null){

project.test.root = project.test.root;

}

In the task.groovy

def run() {

//read the parameter

//it is still the last one setting

if(project.test.root != null){

root = project.test.root;

}

}


(Alan Lee) #6

I have research on this

http://code4reference.com/2012/08/gradle-custom-plugin-part-2/

I have tried this tutorial and I can set the extension properties for each project.

But now i want to set the extension / configuration for each task without a further object

So the task test is both task and configuration(extension). Is it possible to do so?

test{

root = project.projectDir.getCanonicalPath()+"/src";

}


(Alan Lee) #7

I have figured out why…

my careless mistake…

i have set it to be static…