Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am currently modularizing our gradle build in order to have a libs/commons.gradle file containing a lot of global stuff. I need this because of various branches of the software beeing developed in parallel and we'd like to avoid to spread every scriptfile change among all branches.

So I created that lib file and used "apply from" to load it :

apply from: '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 am calling the function from my 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 end up in :

...

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 queston is : How can I call a subfunction in gradle, which is defined in an included script?

Any help appreciated!

like image 574
gorefest Avatar asked Sep 03 '14 10:09

gorefest


People also ask

What is def in Gradle?

Keyword def comes from Groovy and means that variable has local scope. Using ext. outDir means that you add property outDir to ExtraPropertiesExtension, think of it like project has a map of properties with name ext , and you put your property in this map for later access.

How do I run a script in Gradle?

From the main menu, select Run | Edit Configurations to open the run/debug configuration for your project. icon. In the list that opens, select Run Gradle task. In the Select Gradle Task dialog, specify the project and the task that you want to execute before launching the project.

What is the use of Buildscript in Gradle?

buildscript: This block is used to configure the repositories and dependencies for Gradle. dependencies: This block in buildscript is used to configure dependencies that the Gradle needs to build during the project.

What is subprojects in Gradle?

The subproject producer defines a task named buildInfo that generates a properties file containing build information e.g. the project version. You can then map the task provider to its output file and Gradle will automatically establish a task dependency.


1 Answers

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 scope where they have been declared. Local variables are a feature of the underlying Groovy language.

13.4.2. Extra properties

All enhanced objects in Gradle's domain model can hold extra user-defined properties. This includes, but is not limited to, projects, tasks, and source sets. Extra properties can be added, read and set via the owning object's ext property. 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 will work.

like image 179
cornuz Avatar answered Sep 28 '22 05:09

cornuz