Passing argument from command line to java code

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Passing argument from command line to java code

lidia
Hi

I'm trying to pass an argument from command line to a java class.  I followed this post: http://gradle.1045684.n5.nabble.com/Gradle-application-plugin-question-td5539555.html but the code does not work for me (perhaps it is not meant for JavaExec?).  Here is what I tried:

task listTests(type:JavaExec){
    main = "util.TestGroupScanner"
    classpath = sourceSets.util.runtimeClasspath
    // this works...
    args 'demo'
    /*
    // this does not work!
    if (project.hasProperty("group")){
        args group
    }
    */
}

The output from the above hard coded args value is:

C:\ws\svn\sqe\sandbox\selenium2forbg\testgradle>g listTests
:compileUtilJava UP-TO-DATE
:processUtilResources UP-TO-DATE
:utilClasses UP-TO-DATE
:listTests
Received argument: demo

BUILD SUCCESSFUL

Total time: 13.422 secs


However, once I change the code to use the hasProperty section I get a NullPointerException:

C:\ws\svn\sqe\sandbox\selenium2forbg\testgradle>g listTests -Pgroup=demo -s

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\ws\svn\sqe\sandbox\selenium2forbg\testgradle\build.gradle' line:
25

* What went wrong:
A problem occurred evaluating root project 'testgradle'.
> java.lang.NullPointerException (no error message)

* Try:
Run with --info or --debug option to get more log output.

* Exception is:
org.gradle.api.GradleScriptException: A problem occurred evaluating root project
 'testgradle'.
        at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptR
unnerImpl.run(DefaultScriptRunnerFactory.java:54)
        at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl.
apply(DefaultScriptPluginFactory.java:127)
        at org.gradle.configuration.BuildScriptProcessor.evaluate(BuildScriptPro
cessor.java:38)

I'm not sure what is wrong...  I'm attaching a simple test project, if anyone has an idea, please let me know testgradle.zip.

Thanks
Lidia
Reply | Threaded
Open this post in threaded view
|

Re: Passing argument from command line to java code

lidia
Forgot to mention, I'm using the following version of gradle:

C:\ws\svn\sqe\sandbox\selenium2forbg\testgradle>g -v

------------------------------------------------------------
Gradle 1.0-rc-3
------------------------------------------------------------

Gradle build time: Sunday, April 29, 2012 11:51:52 PM UTC
Groovy: 1.8.6
Ant: Apache Ant(TM) version 1.8.2 compiled on December 20 2010
Ivy: 2.2.0
JVM: 1.6.0_26 (Sun Microsystems Inc. 20.1-b02)
OS: Windows XP 5.1 x86
Reply | Threaded
Open this post in threaded view
|

Re: Passing argument from command line to java code

Robert Fischer
In reply to this post by lidia
Is "group" null?

~~ Robert.


On Wed, Jun 27, 2012 at 8:17 PM, lidia <[hidden email]> wrote:

> Hi
>
> I'm trying to pass an argument from command line to a java class.  I
> followed this post:
> http://gradle.1045684.n5.nabble.com/Gradle-application-plugin-question-td5539555.html
> but the code does not work for me (perhaps it is not meant for JavaExec?).
> Here is what I tried:
>
> task listTests(type:JavaExec){
>    main = "util.TestGroupScanner"
>    classpath = sourceSets.util.runtimeClasspath
>    // this works...
>    args 'demo'
>    /*
>    // this does not work!
>    if (project.hasProperty("group")){
>        args group
>    }
>    */
> }
>
> The output from the above hard coded args value is:
>
> C:\ws\svn\sqe\sandbox\selenium2forbg\testgradle>g listTests
> :compileUtilJava UP-TO-DATE
> :processUtilResources UP-TO-DATE
> :utilClasses UP-TO-DATE
> :listTests
> Received argument: demo
>
> BUILD SUCCESSFUL
>
> Total time: 13.422 secs
>
>
> However, once I change the code to use the hasProperty section I get a
> NullPointerException:
>
> C:\ws\svn\sqe\sandbox\selenium2forbg\testgradle>g listTests -Pgroup=demo -s
>
> FAILURE: Build failed with an exception.
>
> * Where:
> Build file 'C:\ws\svn\sqe\sandbox\selenium2forbg\testgradle\build.gradle'
> line:
> 25
>
> * What went wrong:
> A problem occurred evaluating root project 'testgradle'.
>> java.lang.NullPointerException (no error message)
>
> * Try:
> Run with --info or --debug option to get more log output.
>
> * Exception is:
> org.gradle.api.GradleScriptException: A problem occurred evaluating root
> project
>  'testgradle'.
>        at
> org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptR
> unnerImpl.run(DefaultScriptRunnerFactory.java:54)
>        at
> org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl.
> apply(DefaultScriptPluginFactory.java:127)
>        at
> org.gradle.configuration.BuildScriptProcessor.evaluate(BuildScriptPro
> cessor.java:38)
>
> I'm not sure what is wrong...  I'm attaching a simple test project, if
> anyone has an idea, please let me know
> http://gradle.1045684.n5.nabble.com/file/n5709919/testgradle.zip
> testgradle.zip .
>
> Thanks
> Lidia
>
> --
> View this message in context: http://gradle.1045684.n5.nabble.com/Passing-argument-from-command-line-to-java-code-tp5709919.html
> Sent from the gradle-user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Passing argument from command line to java code

lidia
It shouldn't be.  I would expect that this line of code:

args group

would only be executed if group was passed on command line, in which case it is not null.  I changed the code to the following but the result is exactly the same:

group = hasProperty('group') ? group : 'nosuchgroup'

task listTests(type:JavaExec){
    main = "util.TestGroupScanner"
    classpath = sourceSets.util.runtimeClasspath
    args group
}

I get NullPoniterException on line 21 and that's:

    args group


Lidia


Robert Fischer wrote
Is "group" null?

~~ Robert.
Reply | Threaded
Open this post in threaded view
|

Re: Passing argument from command line to java code

lidia
In reply to this post by lidia
The issue was that I was using predefined project property names, such as group (list in table 13.1 at http://gradle.org/docs/current/userguide/writing_build_scripts.html).  Once changed "group" to "testngGroup", it worked fine.