Pass command line arguments to gradle - gradle

To start Java program, I can pass arguments like:
java Main arg1 arg2 arg3
What are the good ways to do that in gradle command line:
gradle startProgram arg1 arg2 arg3
And this is in build.gradle:
task startProgram(dependsOn: 'classes', type: JavaExec) {
main = 'Main'
classpath = sourceSets.main.runtimeClasspath
systemProperties = System.properties
}

Best way is to use java system properties (-D switch) but these are more 'global'. Instead You can use simple properties (-P switch) and get the passed values using instance of Project class.

Related

How to pass command line arguments to a task?

How do I pass command line arguments to a nimble task? For example, say I have the task
task mytask, "my task":
echo &"my task {args}"
When I run
nimble mytask --foo --bar
I would like nimble to output
mytask --foo --bar
or something like that.
from nimble documentation (i.e. github's README.md):
Tasks support two kinds of flags: nimble <compflags> task <runflags>. Compile flags are those specified before the task name and are forwarded to the Nim compiler that runs the .nimble task. This enables setting --define:xxx values that can be checked with when defined(xxx) in the task, and other compiler flags that are applicable in Nimscript mode. Run flags are those after the task name and are available as command-line arguments to the task. They can be accessed per usual from commandLineParams: seq[string].
commandLineParams is available in std/os. For your example:
import std / [os, strformat]
task mytask, "my task":
echo &"my task {commandLineParams()}"
Update:
Setting up a new nimble project with the above code added and running:
nimble mytask --foo --bar
you will actually find that it prints a nim sequence with ALL arguments and not only the runtime flags. For example on Windows and anonymizing specific folder names:
my task #["e", "--hints:off", "--verbosity:0", "--colors:on", "XXX\\nimblecache-0\\test_nimble_2483249703\\test_nimble.nims", "XXY\\test_nimble\\test_nimble.nimble", "XXZ\\nimble_23136.out", "mytask", "--foo", "--bar"]
So in order to get only --foo and --bar you need to select arguments after mytask
Note: we probably should fix nimble docs about that.
Passing just the right arguments:
proc forward_args(task_name: string): seq[string] =
let args = command_line_params()
let arg_start = args.find(task_name) + 1
return args[arg_start..^1]
task dev, "Start the project in dev mode":
const params = forward_args("dev").join(" ")
exec &"nimble run {bin[0]} {params} --silent"

Kotlin - System.getProperty cannot resolve command line argument

I have some code to load application.properties dynamically:
fun loadDefaultProperties(): Properties {
val configPath = System.getProperty("spring.config.location")
val resource = FileSystemResource(configPath)
return PropertiesLoaderUtils.loadProperties(resource)
}
But when I run the command...
java -jar my.jar -Dspring.config.location=my/location/application.properties
...System.getProperty("spring.config.location") returns null, and therefore, I get an IllegalArgumentException because the path is null.
Why am I unable to read the argument from the command line?
You're passing them in the wrong sequence. Pass them like:
java "-Dspring.config.location=my/location/application.properties" -jar my.jar
Otherwise they are program arguments. I've just tested it, and on MacOS, both the above as well as
java -Dspring.config.location=my/location/application.properties -jar my.jar
(without quotes) work.
Don't you need quotes?
java -jar my.jar -Dspring.config.location="my/location/application.properties"

How to pass task parameter into dependsOn method in Gradle

I have a task which copies reports:
task copyReports(type: Copy) {
dependsOn = ['cleanTest', 'test -i']
from 'build/reports/tests/test/classes'
into 'reports'
println 'FINISHED COPYING FILES'
}
This task depends on cleanTest and test. I want to run test task with the parameter -i to see output of the tests. How to pass this parameter into dependsOn method? At the moment I get the next error:
Task with path 'test -i' not found in root project
I also tried without single quotes. Like this:
dependsOn = [cleanTest, test -i]
Then I get the next error:
Could not get unknown property 'i' for task ':copyReports' of type org.gradle.api.tasks.Copy.
Any ideas?

Gradle JavaExec StandardOutput to include Java command

Is it possible to add in the java command to the standardOutput stream on a JavaExec command in Gradle?
Ie
task importSitesDef(dependsOn: init, type: JavaExec) {
main = 'com.x'
classpath = configurations.runE
standardOutput = new FileOutputStream(standardLog, true)
}
Will log the output but I want to see
java com.x -cp ... in the file too before the input.
This is due to using the same output stream/file for multiple tasks and its very hard to tell where the output from one task finished before another one starts.
I know this is an old question but all I did to get the Java command used was to get the output of the JavaExec commandLine method and join it's elements, like so:-
commandLine.collect().join(' ')
Then output it.

Override property in build.gradle from the command line

In build.gradle we can define variables like:
def libVersion='someVersion'
We can override properties in command line with -PlibVersion=otherVersion
Unfortunately it seems this command line option does not affect local variables defined in build.gradle. Is there a way to override these from command line? Please note that for some reasons i do not want to create settings.gradle nor gradle.properties files.
Here's an example:
ext.greeting = project.hasProperty('greeting') ? project.getProperty('greeting') : 'hello'
task greet << {
println greeting
}
If you run gradle greet, it will print hello.
If you run gradle -Pgreeting=welcome greet, it will print welcome.
I was trying to override a property from command line which I had defined in build.gradle but it never worked. But when I pass the property without defining in build.gradle it's working. For example:
If I use
def myProp='foo'
println "${myProp}"
and run from the cmd
gradlew -PmyProp=Hello build
Never worked. I'm always getting foo only.
But if I remove
def myProp='foo'
It's working and taking all the inputs whatever I passed from cmd.
There is another way, just define the property in gradle.properties then you can override easily from command line.
I recently experienced this with Gradle 7.3 so thought to share.

Resources