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.
Related
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') .
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.
I am running a very simple Gradle task.
which run Yarn command - yarn_test which run Selenium tests.
I am using com.moowork.gradle:gradle-node-plugin:1.2.0 plugin in order to run Yarn commands
my task look like -
task run_tests(type: YarnTask) {
try {
args = ['test']
}catch (all){
println('Tests failed!')
}}
and in my packge.json i have:
"test": "mocha --timeout 25000 ./automation/test --reporter xunit-file"
even when I changed it to run as exec
executable "sh"
args "-c", "yarn test"
I got the same error, so it is not something with the plugin but, with Gradle try-catch
when even one test fail no meter why I want to catch the exception.
but for some reason it doesn't get caught.
when I run with --stacktrace this is what I get-
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Task :run_tests FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':run_tests'.
Process 'command '/DATA/build/workspace/build_build/.gradle/yarn/yarn-v1.3.2/bin/yarn'' finished with non-zero exit value 1
* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':run_tests'.
What am I missing?
All I want is to print some error, and finish the build.. but not with exception?!
Gradle 4.7, run on Ubuntu with Gradle wrapper.
Thanks
EDIT
even when i run just
sh: exit 1
I got same result so, it has to be something with gradle try-catch and not with yarn ormocha etc
The problem is that your try/catch is covering the configuration gradle build phase, not the execution phase. The full gradle build lifecycle is covered here, but I'll include the basic descriptions of each phase at the end of this answer.
It looks like YarnTask supports the ignoreExitValue property, so you should at least be able to do something like:
task run_tests(type: YarnTask) {
args = ['test']
ignoreExitValue = true
}
to just ignore the result; I think you can make it even a bit more sophisticated, like:
task run_tests(type: YarnTask) {
args = ['test']
ignoreExitValue = true
doLast {
if(result.getExitValue() != 0){
println "Tests failed!"
}
}
}
The doLast action will be executed after completion of all of the task's defined actions (e.g., after the tests have been run).
Phases of the Gradle Build Lifecycle, the very short version:
Initialization - Determine which projects/subprojects will be built
Configuration - Configures all the projects/tasks (executes the build scripts, which prepares the tasks)
Execution - Executes the tasks themselves
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
}
I started reading the gradle user guide and tried to reproduce what is done there.
So i created a file "build.gradle" and put it in here:
"c:\development\build.gradle".
That file includes the following text:
task compile << {
println 'compiling source'
}
task compileTest(dependsOn: compile) << {
println 'compiling unit tests'
}
task test(dependsOn: [compile, compileTest]) << {
println 'running unit tests'
}
task dist(dependsOn: [compile, test]) << {
println 'building the distribution'
}
Now I opened a Windows command windows and typed in this:
gradle -b C:\development\build.gradle dist test
This is what I got:
FAILURE: Build failed with an exception.
Where: Build file 'C:\development\build.gradle' line: 1
What went wrong: A problem occurred evaluating root project 'development'.
Could not find property 'compile' on root project 'development'.
Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
After some tries I found, that the file itself was been found, but I don't know why it is not executed.
What am I doing wrong?
I found the solution by myself.
Make sure, that your build file has the same encoding as your operating system.
In my case I created a UTF-8 file, but should have been an ISO-file.
After changing to the correct encoding, everything works fine.