How to run a single task in in Eclipse with gradle buildship? - eclipse

Currently I have in my gradle.build file the tasks:
task helloA {
println 'hello A!'
}
task helloB {
println 'hello B!'
}
However executing task helloA via Gradle Tasks/other/helloA prints on the console:
hello A!
hello B!
:helloA UP-TO-DATE
BUILD SUCCESSFUL
However I would expect it to just print: 'hello A!' instead of
hello A!
hello B!
together.
How do I change that? I read through the gradle docs, but coudln't find something about that apart from some extensive workaround.

No task is executed at all in your buildfile, they are just configured.
Be aware that there are three phases in a Gradle run. Initialization, configuration and execution. In initialization phase, you define which projects are part of the build and where they are on disc (basically settings.gradle execution). In configuration phase all tasks are configured. In execution phase the tasks that need to be executed (explicitly called tasks or default tasks, depended upon directly or transitively by explicitly called or default tasks).
Try this and you will see:
task helloA {
println 'configure A!'
doLast {
println 'execute A!'
}
}
task helloB {
println 'configure B!'
doLast {
println 'execute B!'
}
}

Related

How to run a custom Task before SBT's `publish` task in an AutoPlugin?

I have a task called prePublishCheck which ensures that the Git working tree isn't dirty before publishing a JAR to Artifactory, which is supposed to run before the publish task in an AutoPlugin.
How can I get SBT to run this task before publish?
The following code has no effect with at least sbt 1.1.5:
// Has no effect on `publish`
prePublishCheck := { throw new Exception("test") },
publish := (prePublishCheck before publish).value
// Has no effect on `publish`
prePublishCheck := { throw new Exception("test") },
prePublishCheck := (prePublishCheck before publish).value
These similar answers are obsolete:
Run custom task automatically before/after standard task

How can I add a task after do_deploy()?

I have written a recipe where I want to execute a task after do_deploy():
[...]
inherit deploy
[...]
do_deploy () {
echo "do_deploy() has been called."
}
addtask deploy after do_compile
do_after_deploy () {
echo "do_after_deploy() has been called."
}
addtask after_deploy after do_deploy
When I build the recipe the do_deploy() task is executed. However, the after_deploy() task is not.
When I manually execute the task with bitbake my_recipe -c after_deploy the instructions in the task are executed.
What is the reason for this? Is do_deploy() the very last task and BitBake doesn't let me add tasks after it?
do_deploy() gets executed by default because base.bbclass happens to make do_build (the default task) depend on do_deploy.
You should be able to make your new task run by default with
addtask after_deploy after do_deploy before do_build

How to make sbt run another consecutive command regardless of previous command's result?

My tests are slow. Real slow. Like I can get another cup of coffee and reading some articles while waiting for them to finished slow. So I added this task to build.sbt just to alert me when my testing is finished.
lazy val alertMe = taskKey[Unit]("Alert me when testing is completed.")
alertMe in Test := {
"say \"testing is completed\""!
}
Noted that I use say command on OS X. I then used this task like this.
;test ;alertMe
Voila! This works great.... only for successful testing. In case that any test case failed, test task return result as error, and alertMe is not invoked.
This behavior is pretty understandable. but I want my task, alert me, to run regardless of test task result. How can I do this ?
Maybe you can add test task in alertMe task, like:
lazy val alertMe = taskKey[Unit]("Alert me when testing is completed.")
alertMe := {
Command.process("test", state.value)
"say \"testing is completed\""!
}
usage: sbt alertme, it will run the test task and the shell command.
Command.process will execute the test task and without causing current task fail. so the commands always will be executed.

gradle get value from extension (pass as input to task)

I have following code in my plugin:
#Override
void apply(Project project) {
project.extensions.create(EXTENSION,TestExtension)
project.task("task1") << {
println "Task 1"
println(project.mmm.test)
def extension = project.extensions.findByName(EXTENSION)
println(extension.test)
}
project.task("task2",type: TestTask) {
println "Task 2 "
def extension = project.extensions.findByName(EXTENSION)
// conventionMapping.test = {extension.test}
// println(project.extensions.findByName(EXTENSION).test)
// test = "test"
}
}
In task 1 extension.test return correct value. However in task2 extension.test always return null. What I am doing wrong? Is there a better way to pass some of the extensions values as input for task? I am using gradle 1.12 with jdk 1.8 on Mac. Best Regards
Edit :correct version:
project.task("task2", type: TestTask) {
project.afterEvaluate {
def extension = project.extensions.findByName(EXTENSION)
println(project.extensions.findByName(EXTENSION).test)
test = project.extensions.findByName(EXTENSION).test
}
}
task1 prints the value at execution time (notice the <<), task2 at configuration time (before the rest of the build script after the apply plugin: ... has been evaluated). This explains why the println for task1 works as expected, and the println for task2 doesn't.
However, configuring a task at execution time is too late. Instead, a plugin needs to defer reading user-provided values until the end of the configuration phase (after build scripts have been evaluated, but before any task has been executed). There are several techniques for doing so. One of the simpler ones is to wrap any such read access with project.afterEvaluate { ... }.
Updated answer as some time has passed and Gradle evolved its concepts and its syntax in the meantime.
To use up-to-date and optimally configured task you should use following syntax:
tasks.register("task1") {
doLast {
def extension = project.extensions.findByName(EXTENSION)
println(project.extensions.findByName(EXTENSION).test)
test = project.extensions.findByName(EXTENSION).test
}
}
Task Action:
A task has both configuration and actions. When using the doLast, you
are simply using a shortcut to define an action. Code defined in the
configuration section of your task will get executed during the
configuration phase of the build regardless of what task was targeted.
Deprecated << Operator
<< was deprecated in 4.x and removed in 5.0. See task action (doLast) on how to evaluate task logic at execution phase when all extensions and configurations should be evaluated.
Task Configuration Avoidance
To avoid the cost of creating a task if this won't be executed on your invoked Gradle command TaskContainer.register(String) method.
Avoid afterEvaluate
afterEvaluate should in most cases be avoided, see #sterling's comments from the link. You have now the possibility to evaluate task action part in execution phase, and additionally you can also rely on Task Inputs/Outputs with the combination of Lazy Configuration.

Filtering available Gradle tasks by task group

With the command gradle tasks one can get a report of all available tasks. Is there any way to add a parameter to this command and filter tasks by their task group.
I would like to issue a command like gradle tasks group:Demo to filter all tasks and retrieve a list of only those tasks that belong to the task group called Demo.
From v5.1, you can do this: gradle tasks --group=<group-name>
Gradle docs.
You can do so by adding the following task to your build script:
task showOnlyMyTasks << {
tasks.each {
task -> if (task.group == 'My task group name') {
println(task.name)
}
}
}
And then run:gradle showOnlyMyTasks
If you need only the list, you can use gradle -q
Old answer: There is no such feature. Feel free to suggest new features at http://forums.gradle.org.
Now available since Gradle 5.1, see this answer: https://stackoverflow.com/a/54341658/4433326