Gradle: how can I call 'def' from an imported script?

I am currently modulating our gradle assembly to have a libs / commons.gradle file containing a lot of global stuff. I need this because various branches of software are being developed in parallel, and we would like to avoid distributing every change to the script file among all branches.

So, I created this lib file and used "apply from" to download it:

apply: 'gradle / glib / commons.gradle'

Inside commons.gradle, I define the svnrevision function:

...

def svnRevision = { ISVNOptions options = SVNWCUtil.createDefaultOptions(true); SVNClientManager clientManager = SVNClientManager.newInstance(options); SVNStatusClient statusClient = clientManager.getStatusClient(); SVNStatus status = statusClient.doStatus(projectDir, false); SVNRevision revision = status.getCommittedRevision(); return revision.getNumber().toString(); } 

...

I call a function from mine, including build.gradle:

...

 task writeVersionProperties { File f = new File(project.webAppDirName+'/WEB-INF/version.properties'); if (f.exists()) { f.delete(); } f = new File(project.webAppDirName+'/WEB-INF/version.properties'); FileOutputStream os = new FileOutputStream(f); os.write(("version="+svnRevision()).getBytes()); os.flush(); os.close(); } 

...

But I finish:

...

 FAILURE: Build failed with an exception. * Where: Build $PATH_TO/build20.gradle * What went wrong: A problem occurred evaluating root project 'DEV_7.X.X_GRADLEZATION'. > Could not find method svnRevision() for arguments [] on root project 'DEV_7.X.X_GRADLEZATION'. 

...

So my query is : how can I name the subfunction in gradle that is defined in the included script?

Any help appreciated!

+5
source share
1 answer

From http://www.gradle.org/docs/current/userguide/writing_build_scripts.html :

13.4.1. Local variables

Local variables are declared with the def keyword. They are only visible in the area in which they were declared. Local variables are signs of the Groovy core language.

13.4.2. Additional properties

All extended objects in the Gradle domain model can contain additional user-defined ones. This includes, but is not limited to, projects, tasks, and sets of sources. You can add additional properties, read and set through the property property of the owner object. Alternatively, an ext block can be used to add multiple properties at once.

If you declare it as:

 ext.svnRevision = { ... } 

and don't change the call, I expect it to work.

+3
source

Source: https://habr.com/ru/post/1201713/


All Articles