How to make ./gradlew buld execute other terminal commands - gradle

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

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 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'
}

code moved from build.gradle to groovy failing

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)

How do I turn off in console gradle build message like 'BUILD SUCCESSFUL'?

I want to export project version to environment
by tag='gradle printVersion'`
printVersion is gradle task
task exportVersion {
println project.version
}
it print 0.0.1-SNAPSHOT to console, and my $tag env is set to 0.0.1-SNAPSHOT:exportVersionUP-TO-DATEBUILD SUCCESSFUL
How do I make the gradle to include :exportVersion, BUILD SUCCESSFUL to console?
First of all you have your task misconfigured. This way version will be printed every time gradle is run. To avoid it you should add an action: << or doLast. To suppress gradle output, use -q switch:
>cat build.gradle
task exportVersion << {
println project.version
}
>gradle -q exportVersion
unspecified
P.S. #DaveyDaveDave is right, it shouldn't be handled that way.

Resources