How to determine what gradle command was running - gradle

I want to measure time of execution for different gradle command so I have to know what exactly command was run.
For example when I execute from terminal
./gradlew clean
If I execute
./gradlew assembleDebug
desired result is "assembleDebug"
I know I can get list of tasks with project.tasks, but it will give me only list of tasks which my command will execute, without command that started them all.
Is there any option to determine that "first" command?

I figured out how that should work
project.gradle.startParameter.taskNames
gives list of task names. For example if I run multiple commands like
./gradlew app:clean app:cleanBuildCache
it will contain both commands.
Is is possible to cast it for string directly
project.gradle.startParameter.taskNames.toString()
So I will have
[app:clean, app:cleanBuildCache]
With single command like
./gradlew app:clean
I receive
[app:clean]
and that was what I searched for

Related

Choice paramter in jenkins as an output from shell script (pipeline)

I'm looking for a way to use an output from shell as a jenkins parameter but in pipeline, don't want to use any of UI plugins.
For example I want to use output from command
ls /home
as a choice parameter (so I would be able to choose users from the list), is it possible to do something like that?
It must be done in pipeline, I'm not looking for some plugins which allow you to do something like that, but you need to do all in UI, if plugin support pipelines then it's also ok.
For a pipeline to run, its parameters need to be completely defined before the pipeline is started.
For the pipeline to parse the output of ls /home, it needs to run and allocate a node and a workspace, which can only happen after the pipeline is started.
So you are in a kind of a chicken-and-egg problem, where you need to run some code before you start a pipeline, but you can't run pipeline before you run that code.
So your issue boils down to "How can I run some arbitrary Groovy script before I start my pipeline?"
There are two options for this:
ActiveChoice plugin allows you to define a parameter that returns a script. Jenkins will then run the script (don't forget to approve it) in order to show you your "Build with parameters" page. Debugging this is notoriously hard, but this can go to great lengths.
Alternatively, you may want to run a scripted pipeline before you run the Declarative (main) one, as outlined e.g. in this answer. This may look a bit like this:
def my_choices_list = []
node('master') {
stage('prepare choices') {
// read the folder contents
def my_choices = sh script: "ls -l /home", returnStdout:true
// make a list out of it - I haven't tested this!
my_choices_list = my_choices.trim().split("\n")
}
}
pipeline {
parameters {
choiceParam('OPTION', my_choices_list)

Put output of a command in a variable GoCD

I try to create a variable with the version of a project in a GoCD job.
My project is built with maven. The version of the project is set on the pom.xml file (present on the git repository).
Maven allow to retrieve the version of the project with command :
mvn help:evaluate -Dexpression=project.version
So I tried to put the output of this command in a linux variable :
my_version=${mvn help:evaluate -Dexpression=project.version}
And then use it to push the project on a docker repository :
docker push my_project:$my_version
The problem is that GoCD seems to have his own interpreter (see https://docs.gocd.org/current/faq/dev_use_current_revision_in_build.html ).To use a variable, I must do something like this :
sh -c docker push my_project:$my_version
But I can't find a way to set a linux variable.
Is it something that is ok to do with GoCD ? What is the right way to do this ?
GoCD doesn't maintain a single shell session for the entire job. So even if you use sh export MY_VERSION=... and try to use that in the next task within the same job it will not work as expected.
To solve this problem the best way is to wrap the entire set of commands you want to perform within a shell script and execute that shell script as a task. Given the entire shell script would be executed within a single shell instance anything variable you define can be used.

Capture output of tests in gradle

How do I capture the standard output and standard error from all test tasks in gradle? For exec tasks, I can just say standardOutput = new ByteAr... and it works - but test tasks don't seem to have a standard out.
What I'm looking to achieve is simple. (Actually, I want this for ALL gradle tasks, but test tasks are most important).
If the task succeeds...
I get no output at all
If the task fails
I get the most detailed output possible.
Simple as that. How do I achieve this?

Run maven tests in a DOS batch file

I am new to scripting. Basically I want to run maven tests and then execute other commands once they're finished (e.g. Write a message to a file, or email the results to myself, etc. For simplicity, let's say I just want to write DONE to "C:/results" file in this case). This is the script that I have:
mvn test
echo "DONE" > C:/results
The problem is, the second line (echo) never executes, because first one (mvn test) never seems to finish, even though I can see from the output that running the test finished.
How can I change the script to execute the rest of the commands once execution of first line (mvn test) finishes?
If you are on windows you have to be aware that mvn itself is .bat file which means you have to do the following: call mvn test
You need to do the call... cause the mvn.bat is running and ended at the end of mvn.bat. This is the way it is in Windows.

Transform standard output from Gradle's Exec task on the fly?

Is it possible to transform the standard output logs that a Gradle Exec task produces, on the fly?
We have a task executing a command line tool that takes about a minute to run which logs a lot. I would like to filter out just a few of those lines and log them to show progress without cluttering the build log.
You could set your own OutputStream implementation with Exec.setStandardOutput. Or you could execute a shell command that runs the tool and filters its output.

Resources