How do you pass a property to a command line gradle task? - gradle

I'm new to Gradle and having issues passing a property to a command line task. When I attempt to execute this task by executing the command ./gradlew buildDocker -Pgcpproject=my-cool-project, I receive the following error:
Could not get unknown property 'gcpproject' for task ':buildDocker' of
type org.gradle.api.tasks.Exec.
This is the task that as I wrote it:
task buildDocker(type: Exec) {
commandLine 'sh', "./scripts/buildDockerImage.sh", "${gcpproject}"
}
Can anyone explain to me why this is happening? Thank you for reading.

According to the Documentation you should be able to use
task buildDocker(type: Exec) {
commandLine 'sh', "./scripts/buildDockerImage.sh", gcpproject
}

Related

Issue with Exec task in Gradle

I am trying to create Exec task using Gradle as shown below.
task clean(type:Exec) {
doFirst {
println 'Cleaning the existing class files ...'
}
workingDir './bin'
commandLine 'del', 'app\\*.class'
}
When I execute it, I am getting the below error.
Task :clean FAILED
Cleaning the existing class files ...
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ':clean'.
A problem occurred starting process 'command 'del''
Also, I try to compile java files using Exec in Gradle with command lines but somehow, the javac command is getting invoked and hence PATH variable declared in the system is working but the CLASSPATH set in the system is not getting used due to which I am getting class not found exception as shown below.
task build_2(type:Exec, dependsOn: [clean]) {
doFirst {
println 'Compiling ...'
}
workingDir './src'
commandLine 'javac', 'app/MySQLTester.java'
}
Note that whatever I enter in the commandLine of the Exec task works perfectly in Command Prompt from the workingDir I have mentioned in the Gradle task for both del and javac.
Kindly note that I am experimenting with Gradle Exec and hence not using actual Java gradle plugins.

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.

How to get the exact copy of original Run task in Gradle?

Does anybody know what the simplest way to register several run tasks that represent exact copy of original one with changed app’s arguments ? Or may be how to supply run task with an optional argument that would represent app’s arguments.
Basically, I want my build to contain some pre-defined options how to run an application and don’t want to declare new JavaExec that requires its manual configuring while I have already had ready-to-go run task supplied by default.
gradle run --args='--mode=middle' ---> gradle run || gradle runDefault || gradle run default
gradle run --args='--mode=greed' ---> gradle runGreed || gradle run greed
gradle run --args='--mode=lavish' ---> gradle runLavish || gradle run lavish
As for now I came up only with the option that suggests implementing my own JavaExec_Custom task class with supplied arguments property. And it seems to be too complex and boilerplating as for my goal.
You can create tasks that modify the configuration of the actual run task in a doFirst or doLast closure:
// Groovy-DSL
task runGreed {
doFirst {
run.args '--mode=greed'
}
finalizedBy run
}
// Kotlin-DSL
register("runGreed") {
doFirst {
run.get().args = listOf("--mode=greed")
}
finalizedBy(run)
}
You can use a property for the args
task run(type: JavaExec) {
args property('run.args').split(' ')
...
}
Usage
gradle run -Prun.args='foo bar baz'

Gradle tasks dependency

Q: Can one task be dependent on another task with specific argument? If the answer is yes then how to accomplish that?
I opened Gradle for myself recently, and just got curious about next:
Let's say I'd like to have one of the build's subfolders to be copied each time I execute the copy task.
task copy (type: Copy) {
dependsOn build
from '[fromName]'
into '[intoName]'
}
In my case, I'd like to copy folder build/reports/profile, but the thing is that this folder is created by execution of the build --profile task.
So, is there any chance to accomplish the following:
task copy (...) {
dependsOn build --profile
from ...
into ...
}
Btw, plugin project-report doesn't create this folder, to be dependent on its task.
Note: It's not as important to have it done, I'm just interested if it is possible at all.
You can't pass arguments with dependsOn to a task directly but you can create a custom Exec task and pass arguments via the command line interpreter (cmd) using the gradle wrapper like this:
task buildWithArgument(type: Exec) {
commandLine 'cmd', '/c', 'gradlew.bat build --profile'
}
task copy (type: Copy) {
dependsOn buildWithArgument
from '[fromName]'
into '[intoName]'
}

How to create a gradle task that will execute bootRun with a specific profile?

I essentially want to create a task in gradle that executes the command
gradle bootRun -Dspring.profiles.active=test
This command does exactly what I want it to do if executed from the command line but I have had no luck trying to use type:Exec on a task and also no luck passing in System properties
I don't really want to make this into an external command that the user needs to know about to run. I would like it to show up under tasks/other.
My closest attempt so far:
task bootRunTest() {
executable "gradle"
args "-Dspring.profiles.active=test bootRun"
}
The task I was trying to create wound up being this:
task bootRunTest(type: org.springframework.boot.gradle.run.BootRunTask, dependsOn: 'build') {
group = 'Application'
doFirst() {
main = project.mainClassName
classpath = sourceSets.main.runtimeClasspath
systemProperty 'spring.profiles.active', 'test'
}
}
Here is how you set the properties for the task you wish to run, in this case bootRun
add inside of Build.gradle
bootRun {
systemProperty "spring.profiles.active", "test,qa,ect"
}
Then from the command line
gradle bootRun
You can also do it by setting the OS variable, SPRING_PROFILES_ACTIVE, to the specific profile.
For eg:
SPRING_PROFILES_ACTIVE=dev gradle clean bootRun

Resources