Run go command as a gradle task - go

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.

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.

How to run a task before build in 2021 gradle Kotlin syntax in an Android project

I read a lot of answers to multiple questions like this, but they are all very old and use arcane/obsolete syntax in groovy and/or are not suitable for Android projects.
I have a task.
tasks.register("asd") {
doFirst {
exec {
How do I run it when build starts in an Android app/build.gradle.kts file?
I have printed both tasks and project.tasks names and I have tried
tasks.named("build").dependsOn(":asd")
tasks.named("app:build").dependsOn(":asd")
tasks.named(":app:build").dependsOn(":asd")
project.tasks.named("build").dependsOn(":asd")
project.tasks.named("app:build").dependsOn(":asd")
project.tasks.named(":app:build").dependsOn(":asd")
It either fails with Task <name> not found in project or it does nothing.
I tried with doFirst, doLast and neither (directly exec) and still nothing.
So I found the right way to go about it
android {
project.tasks.preBuild.dependsOn("asd")
}
tasks.register("asd") {
doFirst {
exec {
No need for the : when referring to the task.
The build and preBuild tasks are accessible from (project.)tasks.
doFirst was necessary otherwise it would loop endlessly.
tasks.register allowed the task to run when it needed and not immediately, which happens if we use create

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') .

gradle custom task that depends on build task without testing

I am using gradle 6.0.1
I am trying to write my on task, but I want first the the build task is executed but without tests.
I tried (from build.gradle):
task startEnv(type: GradleBuild) {
tasks = ['build']
doLast {
// START ENV CODE
}
}
However, I don't manage to find a way to call build without running tests, as I would run
gradle build -x test
Is it possible to achieve this functionality?
Another option I can use, is to check inside my startEnv task whether build already exists and run this task only if build exists - Is there a way to query whether build exists? (this is a multi module projects, so I am not sure it is enough to check whether build directory exists on the root project).
I followed the comments and tried the solution mentioned at Skip a task when running another task
I added to build.gradle:
gradle.taskGraph.whenReady { graph ->
if (graph.hasTask(startEnv)) {
println("DEBUG1")
test.enabled = false
}
}
task startEnv(type: GradleBuild) {
tasks = ['build']
doLast {
// START ENV CODE
}
}
But when I run ./gradlew startEnv - it still fails with some tests that in current phase I know they should fail.
I can see the DEBUG1 print when I execute this command but the build fails with tests that are failing.
Thank you,

How can I call an sbt task from Gradle?

How do I call a task on an sbt project from a Gradle build? I would like to call an existing sbt task, rather than port and duplicate the task's code into my Gradle build.
Ideally, I would like to be able to do this without needing to have sbt installed on the machine I'm running from.
As a simple yet concrete example, assume I have the following project structure:
parent_directory
gradle_project
build.gradle
[...]
sbt_project
build.sbt
[...]
and the following build.sbt file:
val helloTask = TaskKey[Unit]("hello", "Print hello")
helloTask := println("Hello world!")
I would like to call the "hello" sbt task defined in build.sbt from a "helloSbt" Gradle task defined in build.gradle.
The sbt-extras project provides a stand-alone script called sbt which can be directly used to run sbt without having it on the machine first. This can be called via an "Exec" task in the Gradle project, specifying the sbt task to run as a program argument.
First, copy the sbt file from sbt-extras/sbt into the local project. Assuming the sbt script has been copied into project as gradle_project/sbt-extras/sbt, the following Gradle task will execute the "hello" task in build.sbt:
task helloSbt(type: Exec) {
workingDir new File(project.projectDir.parentFile, 'sbt_project')
executable new File(project.projectDir, 'sbt-extras/sbt')
args 'hello'
}

Resources