How to use Gradle properties in Groovy project - gradle

Let's say I want to run Groovy code with Groovy plugin and pass properties to some groovy files. I know you can import properties from Maven build with project.properties. Just didn't figure out how to do it with Gradle build.

Try this:
task runScript(type: JavaExec) {
main 'Main'
systemProperty( 'my-prop', 'hello' )
classpath = sourceSets.main.runtimeClasspath
}
In the groovy script (Main.groovy):
println 'my-prop is ' + System.getProperty( 'my-prop' )
// all properties
println System.properties

You may try to define properties you need in gradle.properties file at the root of your project
myUserProperty=value1
systemProp.system=someValue
and then use it in build.gradle script
task printProperties << {
println myUserProperty
println System.properties['system']
}

Related

Is it possible to use #Grab inside a Gradle build.gradle?

Is it possible to use #Grab inside a Gradle build.gradle file?
My understanding is that Gradle build scripts are written in Groovy and Grape/#Grab is built into Groovy.
But if i attempt to add a #Grab annotation into a build.gradle file it doesn't work.
e.g. adding:
#Grab(group='org.springframework', module='spring-orm', version='3.2.5.RELEASE')
import org.springframework.jdbc.core.JdbcTemplate
it gives me the error
org.gradle.groovy.scripts.ScriptCompilationException: Could not compile build file.
Is this possible?
According to this post, the answer is no, we can't use #Grab.
However, there is an equivalent way to declare a dependency and add it to the classpath, as shown below (usage for the example: gradle go):
buildscript {
repositories {
jcenter()
}
dependencies {
classpath group: 'org.springframework', name: 'spring-orm', version: '3.2.5.RELEASE'
}
}
import org.springframework.jdbc.core.JdbcTemplate
task go() {
doLast {
println 'TRACER : ' + JdbcTemplate.class.getSimpleName()
}
}

How to execute custom gradle task for debugging from IDE (Netbeans)

We are using custom source set and hence the default gradle tasks "run" and "debug" cant be used from netBeans to launch.
Please let me know the steps to configure Netbeans such that when I clik "Debug Project", I need to execute custom gradle task :-
gradle -PmainClass=xxx.TestDelMe execute
Sample build.gradle with execute task :-
sourceSets {
integTest{
java.srcDir 'src/integTest/java'
resources.srcDir 'src/integTest/resources'
}
}
/**
* This task creates the jar for the compiled test code.
*/
task integTestJar (type: Jar) {
from sourceSets.integTest.output
appendix = 'integ-tests'
}
/*
gradle -PmainClass=xxx.TestDelMe execute
*/
task execute(type:JavaExec) {
main = mainClass
debug = true
dependsOn 'assemble', 'integTestJar'
classpath = sourceSets.integTest.runtimeClasspath + sourceSets.integTest.compileClasspath
}
Thanks,
Subra
Found below workaround :-
Enable debugging for JavaExec type task by using the flag "debug=true" (as below)
/*
gradle -PmainClass=xxx.TestDelMe execute
*/
task execute(type:JavaExec) {
main = mainClass
**debug = true**
dependsOn 'assemble', 'integTestJar'
classpath = sourceSets.integTest.runtimeClasspath + sourceSets.integTest.compileClasspath
}
Create a custom task for execute from
Launch the custom task by right clicking the project
Now the JVM will wait on port 5005 till netbeans attaches to it as below
Then setbreak point and enjoy debugging

How to run JBoss TattleTale from inside Gradle build

I am in love with JBoss TattleTale. Typically, in my Ant builds, I follow the docs to define the Tattletale tasks and then run them like so:
<taskdef name="report"
classname="org.jboss.tattletale.ant.ReportTask"
classpathref="tattletale.lib.path.id"/>
...
<tattletale:report source="${src.dir]" destination="${dest.dir}"/>
I am now converting my builds over to Gradle and am struggling to figure out how to get Tattletale running in Gradle. There doesn't appear to be a Gradle-Tattletale plugin, and I'm not experienced enough with Gradle to contribute one. But I also know that Gradle can run any Ant plugin and can also executing stuff from the system shell; I'm just not sure how to do this in Gradle because there aren't any docs on this (yet).
So I ask: How do I run the Tattletale ReportTask from inside a Gradle build?
Update
Here is what the Gradle/Ant docs show as an example:
task loadfile << {
def files = file('../antLoadfileResources').listFiles().sort()
files.each { File file ->
if (file.isFile()) {
ant.loadfile(srcFile: file, property: file.name)
println " *** $file.name ***"
println "${ant.properties[file.name]}"
}
}
}
However, no where in here do I see how/where to customize this for Tattletale and its ReportTask.
The following is adapted from https://github.com/roguePanda/tycho-gen/blob/master/build.gradle
It bypasses ant and directly invokes the Tattletale Java class.
It was changed to process a WAR, and mandates a newer javassist in order to handle Java 8 features such as lambdas.
configurations {
tattletale
}
configurations.tattletale {
resolutionStrategy {
force 'org.javassist:javassist:3.20.0-GA'
}
}
dependencies {
// other dependencies here...
tattletale "org.jboss.tattletale:tattletale:1.2.0.Beta2"
}
task createTattletaleProperties {
ext.props = [reports:"*", enableDot:"true"]
ext.destFile = new File(buildDir, "tattletale.properties")
inputs.properties props
outputs.file destFile
doLast {
def properties = new Properties()
properties.putAll(props)
destFile.withOutputStream { os ->
properties.store(os, null)
}
}
}
task tattletale(type: JavaExec, dependsOn: [createTattletaleProperties, war]) {
ext.outputDir = new File(buildDir, "reports/tattletale")
outputs.dir outputDir
inputs.files configurations.runtime.files
inputs.file war.archivePath
doFirst {
outputDir.mkdirs()
}
main = "org.jboss.tattletale.Main"
classpath = configurations.tattletale
systemProperties "jboss-tattletale.properties": createTattletaleProperties.destFile
args([configurations.runtime.files, war.archivePath].flatten().join("#"))
args outputDir
}
The previous answers either are incomplete or excessively complicated. What I did was use the ant task from gradle which works fine. Let's assume your tattletale jars are beneath rootDir/tools/...
ant.taskdef(name: "tattleTaleTask", classname: "org.jboss.tattletale.ant.ReportTask", classpath: "${rootDir}/tools/tattletale-1.1.2.Final/tattletale-ant.jar:${rootDir}/tools/tattletale-1.1.2.Final/tattletale.jar:${rootDir}/tools/tattletale-1.1.2.Final/javassist.jar")
sources = "./src:./src2:./etcetera"
ant.tattleTaleTask(
source: sources,
destination: "tattleTaleReport",
classloader: "org.jboss.tattletale.reporting.classloader.NoopClassLoaderStructure",
profiles: "java5, java6",
reports: "*",
excludes: "notthisjar.jar,notthisjareither.jar,etcetera.jar"
){
}
So the above code will generate the report beneath ./tattleTaleReport. It's that simple. The annoyance is that the source variable only accepts directories so if there are jars present in those directories you do not wish to scan you need to add them to the excludes parameter.

Gradle dependency does not work as my expectation

I have two sub projects subproject1 and subproject2. I'd like to add some classes from subproject2 to subproject1 and get the subproject1.jar. Below is my gradle file:
task copyClasses (dependsOn: [ ':subproject1:clean', ':subproject1:classes']) {
println "copyClasses "
doLast {
Task study = tasks.getByPath(':subproject1:jar')
study.doFirst {
copy {
println "copy ... "
println sourceSets.main.output.classesDir
println project(':subproject1').sourceSets.main.output.classesDir
from sourceSets.main.output.classesDir
into project(':subproject1').sourceSets.main.output.classesDir
}
}
}
}
task jarUpdated (dependsOn: [ clean, classes, copyClasses, ':subproject1:jar']) {
doLast {
println "jarUpdated"
}
}
But I got the build sequence as below:
$ gradle jarUpdated
copyClasses
:subproject1:compileJava
:subproject1:processResources UP-TO-DATE
:subproject1:classes
:subproject1:jar
:subproject2:compileJava
:subproject2:processResources UP-TO-DATE
:subproject2:classes
:subproject2:clean
:subproject1:clean
:subproject2:copyClasses
Calling Task.doFirst(Closure) after task execution has started has been deprecated and is scheduled to be removed in Gradle 2.0. Check the configuration of task ':subproject1:jar'.
:subproject2:jarUpdated
jarUpdated
BUILD SUCCESSFUL
My expectation is:
$ gradle jarUpdated
:subproject2:clean
:subproject2:compileJava
:subproject2:processResources UP-TO-DATE
:subproject2:classes
:subproject1:clean
:subproject1:compileJava
:subproject1:processResources UP-TO-DATE
:subproject2:copyClasses
copyClasses
copy ...
:subproject1:jar
:subproject2:jarUpdated
jarUpdated
BUILD SUCCESSFUL
Would you please suggest or point out what I missed? Thanks a lot!
The "easiest" way to do exactly what you asked for is probably something like this in your subproject1 build file.
jar {
from tasks.getByPath(':subproject2:compileJava')
}
However this is a very simplistic approach with a LOT of caveats, for example
Subproject 1 can not compile against subproject 2 classes
Any dependencies of Subproject 2 will not be included
etc
I would actually advise declaring subproject2 as a dependency of subproject1 and using one of the plugins that Peter suggested.

Gradle war ignores transitive dependencies when using 'configurations.runtime.asPath' in custom task

I'm facing behavior that I can't explain, using gradle 1.10 I have:
settings.gradle:
include('lib1', 'lib2', 'web')
build.gradle:
subprojects {
apply plugin: 'java'
}
project(':web') {
apply plugin: 'war'
dependencies {
compile project(':lib1')
}
task myTask(type: JavaExec, dependsOn: 'compileJava') {
main = "some.thirdparty.Class"
args "--searchPath", configurations.runtime.asPath
}
}
project(':lib1') {
dependencies {
compile project(':lib2')
}
}
project(':lib2') {
}
When I run gradle clean war I only have lib1.jar in war/build/libs/web.war/WEB-INF/lib.
To make WEB-INF/lib contain both lib1.jar and lib2.jar I have to:
move project('web') block to the end of the file
update configurations.runtime.asPath to configurations.runtime (but I need to provide class path as a path, so it is not a solution)
I read the build lifecycle description, tried to compare --debug outputs but that didn't help.
Why is this happening? And what would be a good solution to provide the module runtime class path as a path in JavaExec task please?
asPath resolves the configuration, but resolution will only work correctly if it happens at execution time rather than configuration time (in particular in the presence of project dependencies). Try to wrap the args line with doFirst { ... }.

Resources