0
votes

I have a gradle script that runs after the SpringBoot jar file is generated:

task runScript (dependsOn: 'bootJar', type: JavaExec) {
    main = 'postpackage'
    classpath = sourceSets.main.runtimeClasspath
}

So far, the gradle script just prints a message:

println "hello world from groovy version ${GroovySystem.version}"

This works fine in my build.

gradle runScript

Task :runScript hello world from groovy version 2.4.15

What I want is something like:

println "hello world generated jar file name is ${jarFileName}"

What I want to do is pass in the SpringBoot generated jar name, or the name of the jar in build/libs/my-service-0.1.1.jar or whatever it is.

So it would print:

hello world generated jar file name is my-service-0.1.1.jar

How can I do that?

Here is what I tried:

postpackage.groovy:

println "hello world from groovy version ${GroovySystem.version}"

println "hello world from groovy version $bootJar.archiveName"

build.gradle:

task runScript (dependsOn: 'bootJar', type: JavaExec) {
    main = 'postpackage'
    classpath = sourceSets.main.runtimeClasspath
}

Here's the error:

Task :runScript FAILED hello world from groovy version 2.4.15 Exception in thread "main" groovy.lang.MissingPropertyException: No such property: bootJar for class: postpackage at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:66)

2
you can configure bootJar task like bootJar{ archiveName = project.property('theFileName') } and then use commandline parameter : ./gradlew -PtheFileName="the-target-name.jar" - M.Ricciuti
I added exactly what I am looking for - mikeb
then try println "hello world generated jar file name is $bootJar.archiveName" - M.Ricciuti
Exception in thread "main" groovy.lang.MissingPropertyException: No such property: bootJar for class: postpackage - mikeb
See the edits - I used $bootJar.archiveName and ${$bootJar.archiveName} - mikeb

2 Answers

1
votes

You should be able to reference the bootjar as "jar".

Example of your print statement:

println "hello world from groovy version ${jar.archiveName}"
0
votes

Answer:

Pass the argument via the build.gradle like this:

task runScript (dependsOn: 'bootJar', type: JavaExec) {
    main = 'postpackage'
    classpath = sourceSets.main.runtimeClasspath
    args "${bootJar.archiveName}"
}

Reference it in the script like this:

println "hello world from groovy version ${GroovySystem.version}"

println "hello world from groovy version ${args[0]}"

Works just fine:

:bootJar UP-TO-DATE :runScript hello world from groovy version 2.4.15

hello world from groovy version my-service-0.1.1.jar

BUILD SUCCESSFUL in 2s
5 actionable tasks: 1 executed, 4 up-to-date
12:36:00 PM: Task execution finished 'runScript'.