code moved from build.gradle to groovy failing - gradle

I have some functionality of git commits in a task of type exec in build.gradle,
task getCommits(type: Exec){
// Some code goes here
.
.
commandLine "git", "log", "${previousVersionString}..${releaseVersion}"
}
above code works fine in build.gradle
As a result of clean up activity I'm taking this code out of build.gradle and putting it in a groovy class but it says can not resolve commandLine.
I'm very much new to gradle and groovy,
Can anybody suggest where I'm going wrong

commandLine is Gradle method, that cannot be called outside
To execute command line in any groovy script:
def proc = ['git', 'log', "${previousVersionString}..${releaseVersion}"].execute()
println (proc.err.text ?: proc.text)

Related

How to make ./gradlew buld execute other terminal commands

I am building a gradle project which has a javacc parser file (i.e a file with .jj extension)
Therefore, to execute this .jj file we need to run 3 commands in the terminal as
javacc filename.jj
javac *.java
java parsername
However, I want to know how to edit the build.gradle, so that whenever the user enters ./gradlew build all the above mentioned commands would be automatically executed.
Have you consider declaring your own task to do so ?
task executeCMD(type:Exec) {
workingDir '.'
commandLine 'cmd', 'echo', 'Hello world!'
doLast {
println "Executed!"
}
}
Am not sure how to link with the build command , maybe this can be helpful build.dependsOn project(':ProjectName').task('build') .

Run go command as a gradle task

I am using gradle as the build tool for a terraform project. I do have unit tests written in go for the project under the ..test/... folder . The way I am running test locally is just on the commandline go test ..test/..' which will run all tests under the test folder. I want to integrate this in the build , so that every build will run this command 'go test ..test/..', How do I achieve this in gradle. Can a custom task be utilized to run a go command?
I am trying to do something like the following
task testExec(type: Exec) {
workingDir "${buildDir}/test"
commandLine 'go','test'
} doLast {
println "Test Executed!"
}
But I get the error
> A problem occurred starting process 'command 'go''
For what its worth , I tried other commands and get the same erorr for ex
task testExec(type: Exec) {
workingDir "${buildDir}/test"
commandLine 'echo','${}buildDir'
} doLast {
println "Test Executed!"
}
gives similar error
> A problem occurred starting process 'command 'echo''
You can use the gradle plugin. First you can follow the starting guide and add the plugin:
plugins {
id 'com.github.blindpirate.gogradle' version '0.11.4'
}
golang {
packagePath = 'github.com/your/package' // go import path of project to be built, NOT local file system path!
}
Then you can run the following command to execute all go files that follow the file name convention <name>_test.go:
gradlew goTest
Otherwise you can also create a complete custom task or a custom task with the plugin.
EDIT:
I found the cause of your error. The variable buildDir refers to the build folder in your project: <project_folder>/build. The problem now is that the folder test does not exists and the exception is thrown. Instead, you can use the variable projectDir.

Does a "wrapper" entry in a Gradle build script take the place of the Gradle wrapper (gradlew)?

Does use of the gradle version in the wrapper task eliminate the need to use gradlew?
wrapper {
gradleVersion = '4.10.2'
}
task foo {
println "...bar"
}
The wrapper seems to be automatically executed as part of the "Configure project" phase and, if so, seems to be a "cleaner" way to manage this (i.e., less files to deal with, embedded in the actual gradle build script, etc.).
New to Gradle, just trying to figure things out...
Add...
I added the "foo" task to clarify my point of confusion.
1 - I am executing the script using "gradle foo"
2 - In the process, I note that the "wrapper" task is automatically invoked
My point-of-confusion is that it seemed that including the wrapper entry would force the right version of Gradle to be used (i.e., why else would the wrapper task be automatically invoked). On inspection, it appears that including the version in the wrapper task is another way to get the version into Gradle's properties file (vice providing it on the command line when generating the wrapper).
Live-and-learn, thanks for dealing with this question.
I am executing the script using "gradle foo" 2 - In the process, I note that the "wrapper" task is automatically invoked
No, it's not. If it was, then the following script would print "hello":
wrapper {
gradleVersion = '4.10.2'
doLast {
println 'hello'
}
}
task foo {
println "...bar"
}
What you see there is the configuration of the wrapper task, which would be invoked if you executed gradle wrapper, and would install (or upgrade) the wrapper for the version 4.10.2 of gradle in the project.
This task configuration is not needed anymore. As indicated in the documentation, you can invoke the wrapper task and specify the version directly on the command line with gradle wrapper --gradle-version 4.10.2.
Note that, in your script, the line println "...bar" is executed when the task foo is configured, not when it's executed. Whatever tak you execute, you will always see bar... printed in the console.

How to run gradle script from gradle

How can I run another gradle script from gradle. I have multiple gradle scripts to run tests under <root_project>/test directory. The <root_project>/build.gradle is invoked with the name of the test gradle file to be run. For example
gradle run_test -PtestFile=foobar
This should run <root_project>/test/foobar.gradle (default tasks of it)
What I'm currently doing is invoking project.exec in the run_test task. This works but now I need to pass the project properties from the gradle process running run_test to the one which runs foobar.gradle
How can I do this ? Is there a more gradle-integrated way to run a gradle script from another, passing all the required info to the child gradle script ?
I do something similar where a "construct" task in one gradle hands off to a "perform" task in another gradle file in a dir it has created (called the artefact), and it passes through all the project properties through too.
Here's the relevant code for the handoff:
def gradleTask = "yourTaskToEventuallyRun"
def artefactBuild = project.tasks.create([name: "artefactBuild_$gradleTask", type: GradleBuild])
artefactBuild.buildFile = project.file("${artefactDir}/build.gradle")
artefactBuild.tasks = [gradleTask]
// inject all parameters passed to this build through to artefact build
def artefactProjectProperties = artefactBuild.startParameter.projectProperties
def currentProjectProperties = project.gradle.startParameter.projectProperties
artefactProjectProperties << currentProjectProperties
// you can add more here
// artefactProjectProperties << [someKey: someValue]
artefactBuild.execute()
If you want to specify another gradle script, you can use:
gradle --build-file anotherBuild.gradle taskName
or
gradle -b anotherBuild.gradle taskName

Gradle multi project, scope task

I have a multi project in the following way:
rootProject
Subproject1
Subproject2
task whatever << {
println "WHATEVER"
}
I want to be able to configure task 'whatever' once and execute it from any scope (root or subproject) and be executed only once!
This means:
If I run /gradle whatever, I should get: "WHATEVER"
If I run /subproject1/gradle whatever, I should get: "WHATEVER"
In summary, I don't what to execute the same task several tasks according to the number of projects.
I haven't been able to get such a simple result. Please let me know if you can offer any help! Thanks!
gradle whatever searches for whatever in the current subproject and below. Instead, use gradle :whatever and declare the task in the root project.

Resources