Is it possible to wrap the call ":myproject:assemble -a" into a task?
I want to provide a one click solution for assembling a single project in a multi project environment. Is that possible?
You can simpy create a custom task and set it's dependsOn property as follows:
assembleMyProject(dependsOn: ':myproject:assemble') {
}
Related
I am trying to create a custom Gradle task which invokes an existing Gradle task with parameters which are specific to my project. Here is how I invoke the task from the command line:
./gradlew downloadJson \
-Pendpoint=http://example.com/foo \
-Pdestination=src/main/com/example/foo.json
I would like to create a downloadFoo task which I can invoke without specifying the parameters explicitely.
tasks.register("downloadFoo" /* type needed? */) {
// What goes here?
}
Related
Create Gradle Tasks that executes other gradle task with parameters
How to pass Custom gradle Task as parameter to another Custom gradle Task
There's no real concept of tasks wrapping other tasks in Gradle...
What you can do in this situation is to just create a new task of type ApolloDownloadSchemaTask, and then set the properties:
import com.apollographql.apollo.gradle.internal.ApolloDownloadSchemaTask
tasks.register("downloadFoo", ApolloDownloadSchemaTask) { task ->
description("Downloads foo.")
group("Apollo")
task.schemaFilePath.set("src/main/com/example/foo.json")
task.endpointUrl.set("http://example.com/foo")
}
Try this form:
tasks.register("downloadBackendSchema", com.apollographql.apollo.gradle.internal.ApolloDownloadSchemaTask) { task ->
description("Downloads Apollo Schema")
group("Apollo")
task.schemaRelativeToProject.set("src/main/java/com/project/backend/grqphql/schema.json")
task.endpoint.set("http://your-project.com/graphql")
}
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 have the following scenario where I wish to disable JOOQ Schema generation unless explicitly called for:
gradle.taskGraph.beforeTask {
task ->
if (!rootProject.hasProperty("generate") && task.name.equals("generateSampleJooqSchemaSource")) {
task.enabled = false
}
}
Now I can always manually generate the schema when passing "generate" as a parameter as follows:
$ gradle build -Pgenerate
Whereas a normal
$ gradle build
would not regenerate all my schema classes.
However, I do not like this approach very much. You have to remember the parameter name, and also prepend it with -P which does not look as clean.
I would ideally like to be able to make a custom task as follows:
task generate {
tasks[build].executeWithParameter("generate")
}
So that it would also show up as an actual task.
How can I accomplish this?
You can create a task that dependsOn build task and disable jooq task from it. This requires the task declaration to be put after jooq task declaration.
task generate (dependsOn: build) {
tasks['generateSampleJooqSchemaSource'].enabled = false
}
to set project property from task
task generate (dependsOn: build) {
project.ext.generate = "true"
}
I'm working on a project that uses EJB2s. The created EJB Jars require additional processing by the application server before they're bundled in the war/ear and deployed.
I have created a custom task that works to do the additional processing if I invoke it explicitly (gradle ejbDeploy), but am having trouble fitting it into the gradle multi-project lifecyle. I need to somehow add it to the build graph to execute automatically after the jar task.
My first attempt was to add it to jar with
jar.doLast{
ejbDeploy.execute()
}
which seems to work for arbitrary code blocks, but not for tasks
What's the recommended solution for this? I see three approaches:
Hook into the build graph and add it explicitly after the jar
task.
Set it up somehow in jar.doLast{}
Set it up as a prerequisite for the WAR task execution
Is there a recommended approach?
Thanks!
I would go for approach #3 and set it up as a dependency of the war task, e.g.:
war {
it.dependsOn ejbDeploy
...
}
I'm new to Gradle, but I would say the answer really depends on what you're trying to accomplish.
If you want to task to execute when someone runs the command gradle jar, then approach #3 won't be sufficient.
Here's what I did for something similar
classes {
doLast {
buildValdrConstraints.execute()
}
}
task buildValdrConstraints(type: JavaExec) {
main = 'com.github.valdr.cli.ValdrBeanValidation'
classpath = sourceSets.main.runtimeClasspath
args '-cf',valdrResourcePath + '/valdr-bean-validation.json'
}
Add the following, and then ejbDeploy will be executed right after jar, but before war
jar.finalizedBy ejbDeploy
See Gradle Docs. 18.11. Finalizer tasks
I build my project using Gradle 1.0 and I want to use the application plugin to create an application distribution package.
Instead of the generated start scripts I want to provide my own.
How can I skip the CreateStartScripts task?
It appears to be hard-coded, so I am guessing there is no easy way to override it. Maybe hacking the read-only startScripts.outputs property will work.
Either way, I would recommend you just create your own archive task. Here is one that does pretty much exactly what distZip does, but without the startScripts (borrowed from ApplicationPlugin.groovy).
task myDist(type: Zip){
def baseDir = { archiveName - ".zip" }
into(baseDir){
from(project.file("src/dist"))
into("lib") {
from(jar)
from(project.configurations.runtime)
}
}
}