Run executable jar with parameters from gradle - gradle

I wish to run executable jar from my gradle file. I have tied:
task runJar(dependsOn:jar) << {
javaexec {
main="-jar"; args "C:/Development/AndroidStudioProjects/AndroidDev/Test.jar"
}
}
But I get "Could not find property 'jar' on project ':MyProj"
I also tried:
task runScheduleReader << {
javaexec {
main = "MainClass"
classpath = "C:/Development/AndroidStudioProjects/AndroidDev/Test.jar"
args('1')
}
}
I am relatively new to groovy, can you please help me with that?
P.S... I put those function outside of android {}

In your first code snippet dependsOn in task declaration means, that task runJar should be executed only after the jar task. Exception you get, says, that your current project doesn't have such a task. So, if you really don't need to execute jar task just before, you can simply not declere this task dependency:
task runJar() << {
javaexec {
main="-jar"; args "C:/Development/AndroidStudioProjects/AndroidDev/Test.jar"
}
}
Though, this is a little bit strange case, when you have to execute some jar without relative path, this solution should work.
The second snippet should pass the jar as the argument too, but this time, it should be an arguments array, something like this:
task runScheduleReader() << {
javaexec {
main="-jar";
args = [
"C:/Development/AndroidStudioProjects/AndroidDev/Test.jar",
"1"
]
}
}

Related

Error using dependsOn to call task from another gradle file

I am working with a build.gradle file that has multiple ways to specify executions for a task - setup. To call a task from another gradle file - runtests.gradle, I created a task - testTask and added task dependency using dependsOn, but this implementation does not seem to work and giving out an error like :
Could not find property 'testTask' on root project 'GradleFile
My build file looks like this :
build.gradle
task setup(dependsOn: testTask) <<
{
println "In main execution"
}
// new task
task testTask(type: GradleBuild) {
if (getEnvironmentVariable('RUN_TEST').equalsIgnoreCase("true")) {
buildFile = "../Behave/runtests.gradle"
tasks = ['mainTask']
}
else {
println "Exiting runTests Task"
}
}
setup.doFirst {
println "In first execution"
}
setup.doLast {
println "In last execution"
}
D:\>gradle -q GradleFile/build.gradle setup
I am not looking to make much changes to existing tasks, so is there any other workaround I should try?
I have been through many links but could not find anything that suits this scenario. Looking for suggestions please.
Gradle is sensitive to the ordering of tasks in the build script if a task instance is given in the dependsOn. The task setup depends on task (instance) testTask which, at the moment the build script is compiled, doesn't exist yet. The most common options to solve the issue are:
Define task setup below testTask:
task testTask(type: GradleBuild) {
}
task setup(dependsOn: testTask) {
}
Use a relative path to the task, i.e. the task's name, in the dependsOn
task setup(dependsOn: 'testTask') {
}
task testTask(type: GradleBuild) {
}
Please find more details in Javadoc of Task.

Gradle task lines always getting executed

I have the following Gradle task:
task deploy(type: Exec) {
doFirst {
println 'Performing deployment'
}
final Map properties = project.getProperties()
if (!properties['tag']) {
throw new StopExecutionException("Need to pass a tag parameter")
}
commandLine "command", tag
}
If I run another task I get an error because properties['tag'] is undefined, I guess because Gradle is executing everything in the task except commandLine. Is there a better way to do this or a way to stop Gradle from executing parts of the task even when I'm running another task?
Using Gradle 6.6.1
I use this pattern:
// Groovy DSL
tasks.register("exec1", Exec) {
def tag = project.findProperty("tag") ?: ""
commandLine "echo", tag
doFirst {
if (!tag) {
throw new GradleException("Need to pass a tag parameter")
}
}
}
It adds the tag property if it exists.
If it does not exist, it adds an empty string but checks for this before it actually runs.
It would be great if the Exec task accepted providers as arguments so you could just give it providers.gradleProperty("tag"), but unfortunately it doesn't.

Creating a task that runs before all other tasks in gradle

I need to create an initialize task that will run before all other task when I execute it.
task A {
println "Task A"
}
task initializer {
println "initialized"
}
If I execute gradle -q A, the output will be:
>initialized
>Task A
Now if i'll add:
task B {
println "Task B"
}
Execute gradle -q B, and I get:
>initialized
>Task B
So it doesn't matter which task I execute, it always get "initialized" first.
You can make every Task who's name is NOT 'initializer' depend on the 'initializer' task. Eg:
task initializer {
doLast { println "initializer" }
}
task task1() {
doLast { println "task1" }
}
// make every other task depend on 'initializer'
// matching() and all() are "live" so any tasks declared after this line will also depend on 'initializer'
tasks.matching { it.name != 'initializer' }.all { Task task ->
task.dependsOn initializer
}
task task2() {
doLast { println "task2" }
}
Or you could add a BuildListener (or use one of the convenience methods eg: Gradle.buildStarted(...))
Seems like you aim execution phase, and you want a task precursing each task or just run as a first task in the execution phase?
If you want a task to always execute in every project before each other task after its being evaluated you can add a closure to he main build.gradle:
allprojects {
afterEvaluate {
for(def task in it.tasks)
if(task != rootProject.tasks.YourTask)
task.dependsOn rootProject.tasks.YourTask
}
}
or
tasks.matching {it != YourTask}.all {it.dependsOn YourTask}
You can also use the Task Execution Graph to define the lifecycle. There are few ways of achieving your goal, depending on your needs and a project structure.
The previously suggested solution with dependsOn works fine, but I don't like about it that it changes and clutters the task dependencies. The first solution coming to my mind is using Gradle Initialization Scripts. They are really cool. But, the usage is a bit tedious: currently there is no way to have a default project-local Gradle init script. You have to either explicitly specify the script(s) on command line, or place them in USER_HOME/GRADLE_HOME.
So another solution (already briefliy mentioned by #lance-java) which can be used to run some initialization, like a init task/script, is "build listeners". Depending on how early/late the initialization code should run, I use one of these two:
gradle.afterProject {
println '=== initialized in afterProject'
}
or
gradle.taskGraph.whenReady {
println '=== initialized in taskGraph.whenReady'
}
Here the docs of the Gradle interface and of BuildListener.
Note that some of the events occur very early, and you probably can't use them because of that, like e.g. beforeProject and buildStarted (explanations here and there).

Automatically pass unparsed arguments to JavaExec-type task in Gradle?

I'm trying to pass a lot of arguments to a JavaExec task in Gradle. Right now I'm doing something like:
task foo(type: JavaExec) {
if (project.hasProperty('prop1')) {
args += ["--prop1"]
}
if (project.hasProperty('foo2')) {
args += ["--foo3"]
}
...
if (project.hasProperty('flagn')) {
args += ["--flagn"]
}
}
And then when I execute this I do something like ./gradlew :foo -Pprop1 -Pfoo2 ... -Pflagn. This works but is really tedious since I need to literally pass the same string arguments from Gradle to JVM. Is there an easier way - like automatically pass all the unparsed arguments or something simpler?
You can access the project properties from the command line via the StartParameter object.
So, for your example, you could use:
task foo(type: JavaExec) {
args gradle.startParameter.projectProperties.keySet().collect { "--$it" }
}
However, this will add any command line project property to the JavaExec args. Maybe you could think about filtering for properties with a specific prefix.

How to execute JavaExec multiple times in a single task using Gradle?

I have a task that runs a simple JavaExec.
What I cant seem to get working is the ability to run the JavaExec multiple times while iterating a Filetree object (containing the files) each of while I want to pass into the main JavaExec class one by one. Unfortunately the compiler or code generation tool as it is doesnot accept a directory as an arg so I need to pass the file as an arg per loop. Here's what I have:
task generateClasses(type: JavaExec) {
description = 'Generates Json Classes...'
classpath configurations.all
main = "org.apache.gora.compiler.Compiler"
FileTree tree = fileTree(dir: 'src/main')
tree.include '**/*.json'
tree.each {File file ->
println file
args = [ "src/main/json/$file.name", "$buildDir/generated-src/src/main/java" ]
}
}
compileJava.source generateClasses.outputs.files, sourceSets.main.java
From the above it works and I get all files listed but the JavaExec is called just the once on the very last file read.
How do I address the above? Please help.
How about using the project.javaexec method? See the API Documentation or the
DSL ref.
task generateClasses {
description = 'Generate Json Classes'
fileTree(dir: 'src/main', include:'**/*.json').each { file ->
doLast {
javaexec {
classpath configurations.all
main = 'org.apache.gora.compiler.Compiler'
args = ["src/main/json/$file.name", "$buildDir/generated-src/src/main/java"]
}
}
}
}

Resources