Gradle Programming TODEPLETE: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * Gradle =Recipes=")
 
m (Ovidiu moved page Gradle Programming to Gradle Programming TODEPLETE without leaving a redirect)
 
(18 intermediate revisions by the same user not shown)
Line 2: Line 2:


* [[Gradle#Subjects|Gradle]]
* [[Gradle#Subjects|Gradle]]
* [[Gradle Concepts]]
=Groovy=
{{Internal|Groovy#Overview|Groovy}}
=Projects=
==Inter-Project "Communication"==
The [[Gradle Concepts#Project|Project]] is the main API to use to interact with Gradle, so a great deal of custom configuration can be achieved getting a hold of a project reference and reading state from it or invoking into it from the build.gradle files. The reference to the current project can be obtained by using getProject() in the corresponding build.gradle file, which is equivalent with invoking 'project'
<syntaxhighlight lang='groovy'>
println project
</syntaxhighlight>
A project reference can be used to get references to other Projects in the tree and thus navigate the hierarchy.
For all projects in the hierarchy, the reference to the root project can be obtained by invoking 'project.getRootProject()', or simply 'rootProject'.
<syntaxhighlight lang='groovy'>
println rootProject
</syntaxhighlight>
For a sub-project, a reference to the parent project can be obtained by invoking 'project.getParent()', or simply 'parent'.
<syntaxhighlight lang='groovy'>
println parent
</syntaxhighlight>
=Tasks=
{{Internal|Gradle Task|Gradle Task}}
==Obtaining a Task Reference==
<syntaxhighlight lang='groovy'>
Task task = project.getTasksByName("build", false).asList().get(0)
</syntaxhighlight>
<font color=darkgray>Re-evaluate this.
The reference of a specific task may be obtained with findByPath("..."), which returns <tt>null</tt> if the task does not exist, or getByPath("..."), which throws UnknownTaskException if the task does not exist.
<syntaxhighlight lang='groovy'>
rootProject.tasks.findByPath('startScripts').mainClassName = 'com.example.Main';
</syntaxhighlight>
If we need to change the value of several member variables, it is cleaner to keep the reference to the task in a [[Gradle_Variables_and_Properties#Local_Variables|local variable]] and use that reference to change state:</font>
<syntaxhighlight lang='groovy'>
def org.gradle.api.Task t = rootProject.tasks.getByPath('startScripts');
t.applicationName = 'something';
t.mainClassName = 'com.example.Main';
</syntaxhighlight>
=Imports=
A build.gradle file accepts an import declaration section, in case we need to use Java classes:
<syntaxhighlight lang='groovy'>
import java.nio.file.Files
...
task someTask {
  doFirst {
    Files.write(file.toPath(), new byte[0])
  }
}
</syntaxhighlight>


=Recipes=
=Recipes=
==Task that Prints the Execution Classpath==
{{Internal|Gradle_Operations#Print_the_Runtime_Classpath|Print the Runtime Classpath}}
==Obtain and Use Gradle Version in build.gradle==
{{Internal|Gradle_Object_Instance#Gradle_Version|Gradle Object - Gradle Version}}
==Stop a Gradle Build with an Error==
{{Internal|Stop a Gradle Build with an Error#Overview|Stop a Gradle Build with an Error}}
==Inject gradle.properties Version into an Artifact and Expose it to 'version' Command==
{{Internal|Inject gradle.properties Version into an Artifact and Expose it to version Command|Inject gradle.properties Version into an Artifact and Expose it to 'version' Command}}

Latest revision as of 00:09, 9 November 2020

Internal

Groovy

Groovy

Projects

Inter-Project "Communication"

The Project is the main API to use to interact with Gradle, so a great deal of custom configuration can be achieved getting a hold of a project reference and reading state from it or invoking into it from the build.gradle files. The reference to the current project can be obtained by using getProject() in the corresponding build.gradle file, which is equivalent with invoking 'project'

println project

A project reference can be used to get references to other Projects in the tree and thus navigate the hierarchy.

For all projects in the hierarchy, the reference to the root project can be obtained by invoking 'project.getRootProject()', or simply 'rootProject'.

println rootProject

For a sub-project, a reference to the parent project can be obtained by invoking 'project.getParent()', or simply 'parent'.

println parent

Tasks

Gradle Task

Obtaining a Task Reference

Task task = project.getTasksByName("build", false).asList().get(0)

Re-evaluate this.

The reference of a specific task may be obtained with findByPath("..."), which returns null if the task does not exist, or getByPath("..."), which throws UnknownTaskException if the task does not exist.

rootProject.tasks.findByPath('startScripts').mainClassName = 'com.example.Main';

If we need to change the value of several member variables, it is cleaner to keep the reference to the task in a local variable and use that reference to change state:

def org.gradle.api.Task t = rootProject.tasks.getByPath('startScripts');
t.applicationName = 'something';
t.mainClassName = 'com.example.Main';

Imports

A build.gradle file accepts an import declaration section, in case we need to use Java classes:

import java.nio.file.Files
...
task someTask {

  doFirst {

    Files.write(file.toPath(), new byte[0])
  }
}

Recipes

Task that Prints the Execution Classpath

Print the Runtime Classpath

Obtain and Use Gradle Version in build.gradle

Gradle Object - Gradle Version

Stop a Gradle Build with an Error

Stop a Gradle Build with an Error

Inject gradle.properties Version into an Artifact and Expose it to 'version' Command

Inject gradle.properties Version into an Artifact and Expose it to 'version' Command