I want to write a script for following task in gradle. In IDE I run the task following way. When I want to add a custom task in the build.gradle file for the same. I wrote following script but it is giving me no such property: Classes for class: org.gradle.api.Project error . How to write the task I have in the screenshot using a custom script in gradle? Thank you.
task h2Continuous(type: Classes) {
args "--continuous"
}
There is not a Task type Classes. classes (lowercase) is a ad hoc declared task of the Java plugin, but not a unique Task type. It is an ad hoc task which wraps (via dependsOn) compileJava and processResources tasks. compileJava is a unique task type: JavaCompile.
If you are truly concerned with only java compilation, you could type your task as a JavaCompile task. There are multiple ways you could add in the processResources, if you really wanted your task to emulate the Java plugin's classes task.
Related
I'm still new to Gradle and I'm struggling to understand how task configurations for plugin tasks work. Consider the Jar task from the Java plugin: if I overwrite the doFirst and doLast methods why does the plugin's original set of actions still occur but if I change the description of the Jar task then my new description is used? The contents of my build script are:
plugins{
id 'java'
}
jar {
description "The new description"
doFirst {
println "this happened first"
}
doLast {
println "This happened last"
}
}
I ask this more to solidify my understanding of how gradle works and not necessarily because I would like to do this yet.
A Gradle script is just a Groovy script (with some extras) that operates on the Gradle API written in Java. You can checkout the API documentation and most of your questions will be solved:
Consider the Jar task from the Java plugin
Let's be pedantic on this one: There is no Jar task from the Java plugin, there is a task named jar of type Jar. It is very important to distinguish between task types and task instances in this context. Now let's check out the documentation of the Jar task type in Gradle. As you can see, it inherits from the type DefaultTask. DefaultTask defines all those properties and methods that are common for Gradle tasks:
doFirst(Closure action) - Adds the given closure to the beginning of this task's
action list.
doLast(Closure action) - Adds the given closure to the end of this task's action list.
As you can see, whenever you use doFirst or doLast, it will only append (or prepend) task actions to the list of existing task actions, nothing will be overriden.
Thanks to Groovy, assigning a value to the property description using = will actually call the method setDescription:
setDescription(String description) - Sets a description for this task.
This is a classical property setter known from the Java world. It will simply set the new value and the old value will be overridden.
import org.apache.tools.ant.filters.ReplaceTokens
task genScript(type:Copy){
copy{
from "../../scripts/script.txt"
into projectDir
filter ReplaceTokens, tokens: [baseName: jar.baseName, version: jar.version, prefix: 'x']
}
}
jar.doLast{
tasks.genScript.execute()
}
genScript executes fine if I just click on it and run. But when I do ..\gradlew clean jar, it gives me the following error:
Could not find method execute() for arguments [] on task ':myModule:genScript' of type org.gradle.api.tasks.Copy.
How to fix it?
I am using Gradle 6.0.1.
You can't programatically execute tasks from other tasks in newer versions of Gradle. Instead, you are supposed to declare task dependencies and Gradle will ensure they get executed in the correct order.
The quick fix is just to make jar depend on your task like this:
jar.dependsOn('genScript')
Alternatively, you could move your logic into the doLast block in the jar task.
There is a command that lists all tasks (gradle tasks) accessible at project level which I am using very often when while working with Gradle. Is there a way to see properties available inside each of those tasks? I want to be able to do something like that:
gradle properties compileJava #this should list all properties available in **compileJava** task
One thing you can do is gradle help --task compileJava.
This will indicate which projects do define the compileJava task and also tell you what is the type of the task.
With this type, you can consult the DSL documentation to get information about the available properties.
There is no tool part of the Gradle CLI (at this time - Gradle 6.0.1) that will give you this in your terminal.
Many properties are not exposed via gradle help --task XXX. Use gradle model to get the list of all available tasks: it prints the class baking the task. Get the source code for the class.
Alternative way is via logging (run gradle bump):
tasks.register("dump") {
doLast{
logger.lifecycle("type: {}", bootJar.properties);
}
}
I am using the gradle application plugin.
I noticed that:
distZip task get's run before the assemble task.
And I can see
from the docs (link above), and confirmed during my gradle run, that
the distZip tasks depends on: jar, startScripts tasks
So execution looks something like this:
> Task :my-app:processResources
> Task :my-app:compileJava
> Task :my-app:classes
> Task :my-app:jar
> Task :my-app:startScripts
> Task :my-app:distTar
> Task :my-app:distZip
> Task :my-app:assemble
Looking through the code in ApplicationPlugin.java and/or DistributionPlugin.java, I expected to see the task dependencies defined.
e.g. something like this:
distZipTask.dependsOn(jarTask)
distZipTask.dependsOn(createScriptsTask)
assembleTask.dependsOn(distZipTask)
...but I couldn't find anything like this in the java code.
Would very much appreciate it if someone could point me in the direction of where to look to find out how these tasks are 'linked'.
Gradle can determine implicit task dependencies from task inputs. If (the output of) a task is used as input of another task, Gradle can derive that there must be a dependency on the task. This functionality is called incremental build support, as it also supports checking for changes in the inputs or outputs to skip tasks if the inputs did not change.
Regarding your example, the task dependencies are defined implicitly in line 197 and line 208 of the file ApplicationPlugin.java:
libChildSpec.from(jar);
...
binChildSpec.from(startScripts);
The arguments in these lines are the respective tasks (to be exact: providers of the tasks). The from method can often be used to define sources of a file operation (e.g. copying, zipping ...). So we define the results (outputs) of the tasks jar and startScripts as a source of some operation (CopySpec). The resulting CopySpec objects are then passed to both tasks distZip and distTar. When a task tries to resolve the defined sources it finds the tasks jar and startScripts and defines the dependencies on them on its own.
Please note that the mentioned task assemble is a so-called lifecycle task. It does not run any action but can be used to organize tasks into build phases (just like build and check).
It's easy to write a custom plugin and include it in the build script
apply from: "utilities.gradle"
For test purpose this file is in the same directory as the build.gradle
Calling a task defined in utilities.gradle from build.gradle works without any hassle.
In utilities gradle is also a plugin defined - configuring it from build.gradle just works.
But if I define a custom task in utilities.gradle calling it is no problem but if I want to use that custom taks in build.gradle it says
> Could not find property 'GreetingTask' on root project 'TestGradle'.
utilities.gradle:
task hello(type: GreetingTask)
class GreetingTask extends DefaultTask {
#TaskAction
def greet() {
println 'hello from GreetingTask'
}
}
build.gradle
task hellox(type: GreetingTask)
Ok... I read the documentation here: http://www.gradle.org/docs/current/userguide/custom_tasks.html
It says the custom task is not visible outside...
But then... how to share custom tasks with the team without making a Jar for everything.
What I want is to place the utilities.gradle on a network drive and share it with the other.
pls help
There is a special $rootDir/buildSrc directory which is its own build. All classes that this build produces are available to all build scripts in the "main" build. The buildSrc build has a default build.gradle, but you can add your own. By default, Java classes are expected under src/main/java, and Groovy classes under src/main/groovy. You can read more about buildSrc in the Gradle User Guide.
To share classes across multiple builds, a separate plugin project that publishes a Jar is the way to go.