Gradle command line startup error - windows

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.

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.

Gradle 7.2 Tar task baseName deprecated but replacement archiveBaseName is rejected

I have started using Gradle 7.2 to build a project that produces a compressed Tar archive. Gradle is issuing a deprecation warning that baseName is deprecated and should be replaced with archiveBaseName. But it rejects archiveBaseName.
A very much simplified example using a trivial Gradle build script, with their associated execution outputs are given below. I did run 'gradle --stop' to ensure a prior version's daemon wasn't actually executing; a GRADLE_HOME environment variable is pointing to the correct Gradle folder (not sure it's needed, but it is set.)
I tried using "archiveBaseName" both without and with "=" ("archiveBaseName 'Test'" and "archiveBaseName='Test'").
The docs seem to suggest archiveBaseName should already be available, so I don't think it's just a heads up of things to come.
Thank you!
Gradle file:
task dist(type: Tar) {
baseName 'Test'
into ('.') { from('.') }
}
Execution:
gradle build --warning-mode=all
c:\jdev\newpaas\xxx>gradle build --warning-mode all
> Configure project :
The AbstractArchiveTask.baseName property has been deprecated. This is scheduled to be removed in Gradle 8.0. Please use the archiveBaseName property instead. See https://docs.gradle.org/7.2/dsl/org.gradle.api.tasks.bundling.AbstractArchiveTask.html#org.gradle.api.tasks.bundling.AbstractArchiveTask:baseName for more details.
at build_2qa2gx0itzunotwyc4ndf9v86$_run_closure1.doCall(C:\jdev\newpaas\xxx\build.gradle:2)
(Run with --stacktrace to get the full stack trace of this deprecation warning.)
> Task :buildEnvironment
------------------------------------------------------------
Root project 'TestProject'
------------------------------------------------------------
classpath
No dependencies
A web-based, searchable dependency report is available by adding the --scan option.
BUILD SUCCESSFUL in 983ms
1 actionable task: 1 executed
Gradle file:
task dist(type: Tar) {
archiveBaseName 'Test'
into ('.') { from('.') }
}
Execution:
c:\jdev\newpaas\xxx>gradle build --warning-mode all
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\jdev\newpaas\xxx\build.gradle' line: 2
* What went wrong:
A problem occurred evaluating root project 'TestProject'.
> Could not find method archiveBaseName() for arguments [Test] on task ':dist' of type org.gradle.api.tasks.bundling.Tar.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
Gradle file:
task dist(type: Tar) {
archiveBaseName='Test'
into ('.') { from('.') }
}
Execution:
c:\jdev\newpaas\xxx>gradle dist
> Task :dist FAILED
FAILURE: Build failed with an exception.
* What went wrong:
A problem was found with the configuration of task ':dist' (type 'Tar').
- Type 'org.gradle.api.tasks.bundling.Tar' property 'archiveFile' doesn't have a configured value.
Reason: This property isn't marked as optional and no value has been configured.
Possible solutions:
1. Assign a value to 'archiveFile'.
2. Mark property 'archiveFile' as optional.
Please refer to https://docs.gradle.org/7.2/userguide/validation_problems.html#value_not_set for more details about this problem.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
1 actionable task: 1 executed
I stumbled across this syntax and it seems to be working for me:
archiveFileName = "${archiveBaseName}-${archiveVersion}.${archiveExtension}"

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.

Gradle doesn't catch exception

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

-a option in gradle

The following is from the gradle docs
The Task Tree context menu provides the following options:
Execute ignoring dependencies. This does not require dependent
projects to be rebuilt (same as the -a option).
It's not clear what is the -a option? I tried to execute gradle hl -a for the following build script:
task helloapi {
description = "Hello api"
}
task hl (dependsOn: 'helloapi'){
println "Hl"
description = "Hl"
}
helloapi << {
println "Hello api"
}
But I got
Hl
:api:helloapi
Hello api
:api:hl
I thought the only way to exclude tasks depending on the target task is to use -x option. BTW, when I tryied to ran it in gradle --gui I got the following error:
Executing command: "api:hl-a"
Hl
FAILURE: Build failed with an exception.
* What went wrong:
Project 'api' not found in project ':api'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 6.751 secs
Is it a bug?
-a option ignores project dependencies - not task dependencies. Imagine that there's a complicated multimodule project. There was a change only in a single module (a task was changed) and You'd like to check if this task works well, when -a option is specified other module will not be rebuilt, just the changed one.

Resources