Gradle not able to access system environmental properties - gradle

I have this script in build.gradle and i am unable to access tomcat home from system environmental variables. (FYI - null value) I verified i have environmental variable as TOMCAT_HOME = "C:\tomcatC:\apache-tomcat-8.0.21"
Any ideas what i might be doing wrong ?
build.mustRunAfter clean
task deploy(dependsOn: ['clean', 'build', 'deployWar']) << {
println '*********************'
println 'hcadmin.war installed'
println '*********************'
}
task deployWar(type: Copy) {
from war
into System.getProperty("TOMCAT_HOME") + "//webapps"
}

You're basically mixing up java system properties with environment variables:
To get environment variables:
System.getenv()["TOMCAT_HOME"] // Java-style
System.env.'TOMCAT_HOME' // Groovy-style
To get java system properties variables (eg with 'java.home' property):
System.getProperty("java.home") // Java-style
System.getProperties()["java.home"] // Java-style
System.properties.'java.home' // Groovy-style

Try Groovy way System.env.'TOMCAT_HOME'

Related

JAVAFX environmental variable return null in build.gradle.kts

I am using following build.gradle.kts for JavaFX app
plugins {
kotlin("jvm") version "1.4.30"
id("application")
id("org.openjfx.javafxplugin") version "0.0.9"
}
repositories {
mavenCentral()
}
javafx {
version = "11"
modules("javafx.controls", "javafx.fxml")
sdk = System.getenv("JAVAFX")
if (sdk == null || sdk2.isBlank()) {
throw InvalidUserDataException("JAVAFX environment variable is not set. JAVAFX = $sdk")
}
application {
mainClass.set("example.Main")
applicationName = "Main"
applicationDefaultJvmArgs = listOf(
"--module-path=${sdk}${File.separator}lib",
"--add-modules=javafx.controls,javafx.fxml" )
println("applicationDefaultJvmArgs:$applicationDefaultJvmArgs")
}
}
dependencies {
implementation(kotlin("stdlib"))
}
I have set environmental variable in .bashrc like below
export JAVAFX=$HOME/path/to/JavaFX/SDK
when I execute echo $JAVAFX I get the JavaFX SDK path but still I am getting null in build.gradle.kts
Tried restarting IntelliJ idea too, still the same.
Out-of-the-box, Gradle will spawn a Java process in the background. Typically this is the Gradle Daemon, but there may be more.
So to ensure new environment variables are seen, it's best to kill any Java process that are running in the background. Depending on your system, open up task manager and kill and lingering Java process.

How to apply javaagent to gretty plugin based on gradle command line?

The question is specific, but it's more of a general 'how to do this in gradle' question.
I have a demo java web app that I can run using the gretty plugin. I would like to selectively control whether a javaagent is applied to the jvmArgs of the gretty process based on a command line flag. The agent jar location is known by getting its path from a dummy configuration:
configurations {
agent
}
dependencies {
...
agent group: 'com.foo', name: 'foo-agent', version: '1.0'
}
I know I can access the jar file location using something like:
project.configurations.agent.find { it.name.startsWith("foo-agent") }
How can I selectively apply that to the gretty jvmArgs configuration based on a command line property such as
gradle -PenableAgent
I ended up solving this by creating a task and simply calling it before I run the war:
task agent {
doFirst {
def agentJar = project.configurations.agent.find { it.name.startsWith("foo-agent") }
gretty.jvmArgs << "-javaagent:" + agentJar
}
}
Then I can simply call:
gradle agent appRunWar
In my project I use Spring Instrument as java agent so this was my solution.
You can make appRun task dependent on agent task then no additional gradle run parameter needed.
dependencies {
...
agent 'org.springframework:spring-instrument:4.2.4.RELEASE'
}
configurations {
dev
agent
}
gretty {
...
contextPath = '/'
jvmArgs=[]
springBoot = true
...
}
task agent {
doFirst {
def agentJar = project.configurations.agent.find{it.name.contains("spring-instrument") }
gretty.jvmArgs << "-javaagent:" + agentJar
}
}
project.afterEvaluate {
tasks.appRun.dependsOn agent
}

How to use Gradle properties in Groovy project

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

Gradle start scripts env

In gradle run task I have info about lib path:
run {
systemProperty "java.library.path", "lib/native"
}
Is it possible to add the same variable to some gradle task that will include variable to the bin scripts, for now I have to put them manualy but I would like to automate this with gradle:
CLASSPATH=$APP_HOME/lib/***.jar:$APP_HOME/lib/***.jar: ...
>>> LD_LIBRARY_PATH=$APP_HOME/lib
You can use some text, such as MY_APP_HOME, to define java.library.path in JVM arguments:
applicationDefaultJvmArgs = ['-Djava.library.path=MY_APP_HOMElib/native']
And then substitute it by start scripts APP_HOME variable in each script:
startScripts {
doLast {
unixScript.text = unixScript.text.replace('MY_APP_HOME', '\$APP_HOME/')
windowsScript.text = windowsScript.text.replace('MY_APP_HOME', '%APP_HOME%\\')
}
}

How to use a parameter in gradle copy task in the destination folder?

Given the following task in gradle, the dev would start a new module by:
gradle initModule -PmoduleName=derp
task initModule(type: Copy) {
description "Initialize an empty module based on the template. Usage: gradle initModule -P moduleName=derp"
onlyIf { project.hasProperty("moduleName") }
from "$rootDir/modules/template"
into "$rootDir/modules/$moduleName"
}
I am unable to run gradle since moduleName is not defined during evaluation, although I was hoping that "onlyIf" would do so.
To solve this I assign it to a locally defined variable in a guard block:
def modName = ""
if (!project.hasProperty("moduleName")) {
logger.error("Invalid moduleName : It can't be")
} else {
modName = moduleName
}
and finally use the new variable to survive the configuration phase.
Is this the right way to do this? It just doesn't feel right.
Additionally if I was using a rule to make this a tad more elegant:
tasks.addRule("Pattern: initModule_<mod name>") { String taskName ->
if (taskName.startsWith("initModule_")) {
def params = taskName.split('_');
task(taskName) {
doLast {
project.ext.moduleName = params.tail().head()
tasks.initModule.execute()
}
}
}
}
The moduleName is not passed around (even if I change it to finalizedBy).
Any help is appreciated.
As you already figured out the property is not available during the configuration phase.
But can postpone the look-up to the execution phase by using
into "$rootDir/modules/" + project.property("moduleName")
instead of
into "$rootDir/modules/$moduleName"

Resources