[groovy-user] Can we exec maven commands from Groovy files?

19 views
Skip to first unread message

Murthy Perla

unread,
Feb 28, 2008, 12:43:51 PM2/28/08
to us...@groovy.codehaus.org
Hi,
       We can execute ant commands from Groovy files with the help of
"AntBuilder". Do we have any method by which we can execute maven
commands from Groovy files?

I have some working maven scripts, but I want to execute them from my
Groovy files based on the some conditions. With different inputs I
need to execute maven commands with different parameters & values.

       Do we have any specific method for achieving this or Can we
achieve this by any other process?
       I am using Groovy 1.5.4 & Maven2

Thanks,
Murthy.

Luke Daley

unread,
Feb 28, 2008, 5:52:20 PM2/28/08
to us...@groovy.codehaus.org

On 29/02/2008, at 3:43 AM, Murthy Perla wrote:

> We can execute ant commands from Groovy files with the help of
> "AntBuilder". Do we have any method by which we can execute maven
> commands from Groovy files?

Your going to have to launch maven as an external process. http://
groovy.codehaus.org/groovy-jdk/java/lang/String[].html makes this
pretty easy.

def process = (["mvn", "clean"] as String[]).execute()
process.in.eachLine { line ->
println line
}
process.waitFor()

> I have some working maven scripts

Don't let anyone the Maven team hear you say Maven "scripts". That's
blasphemy to them.

LD.

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

http://xircles.codehaus.org/manage_email


Erik Husby

unread,
Feb 28, 2008, 5:57:10 PM2/28/08
to us...@groovy.codehaus.org
Yes. Simply use the Antbuilder.exec method to exec Maven.


On Feb 28, 2008, at 12:43 PM, "Murthy Perla" <murth...@gmail.com>
wrote:

Erik Husby

Murthy Perla

unread,
Feb 29, 2008, 8:11:32 AM2/29/08
to us...@groovy.codehaus.org
 
   Thanks to all. I am sorry for my language. I will take care of that in my next mails.
 
   I  tried "****".execute() for executing the maven commands. When I used it from groovy console, its working as expected & I need not write any p.waitFor(). But when I do the same from my actual groovy files & executing the groovy file from Eclipse, The process of execution  of maven commands is starting & its going to waiting state indefinitely. I opened task manager & explicitly stopped the Java.exe associated with this process, then it's taking control back & its continuing further.
 
   Is there any thing that I am missing in this? My Env: Eclipse, Windows XP, Groovy 1.5.4
   How can I solve this behaviour?
 
-Murthy.
 
--
Thanks and Regards,
Murthy P D N S.

Jürgen Hermann

unread,
Feb 29, 2008, 8:41:16 AM2/29/08
to us...@groovy.codehaus.org
You need to consume stdout AND stderr for any nontrivial subprocesses (maven is nontrivial), else the pipes get clogged and the subprocess blocks. See the Process GDK docs and the Nabble archives.

Erik Husby

unread,
Feb 29, 2008, 11:36:12 AM2/29/08
to us...@groovy.codehaus.org
Here is an example of part of a Groovy script that I use to run Maven.

targets.update = [     "Multiproject Update, no build", { params ->

        Ant.exec( executable:"maven"){
            arg(value:"--nobanner")
            arg(value:"--quiet")
            params.each { p -> arg(value:p) }
            arg(value:"-p")
            arg(value:"projectMP.xml")
            arg(value:"-Dgoal=build:start")
            arg(value:"multiproject:goal")
        }
    }]


Its part of a script that I use to run Maven under different situations. I use Ant.exec because it knows how to manage the input and output so I don't have to. Using String.execute() requires you to manage the input and output streams.


---

Erik Husby

Senior Software Engineer I

Broad Institute of MIT and Harvard

Rm. 2139, 320 Charles St, Cambridge, MA 02141-2023

mobile: 781.354.6669, office: 617.258.9227

email: mhu...@broad.mit.edu AIM: ErikAtBroad



Murthy Perla

unread,
Feb 29, 2008, 11:43:21 AM2/29/08
to us...@groovy.codehaus.org
Thank you. That worked.

When I am working from a groovy console consumeProcessOutput(StringBuffer, StringBuffer ) is working as expected. But when I tried to use the same in eclipse its giving the below error.
Here is the working code that worked on groovy console but failed when used eclipse:

def p =  mycmd.execute();

StringBuffer output = new StringBuffer();
StringBuffer error = new StringBuffer();
p.consumeProcessOutput(output, error);

Error:
groovy.lang.MissingMethodException: No signature of method: java.lang.ProcessImpl.consumeProcessOutput() is applicable for argument types: (java.lang.StringBuffer, java.lang.StringBuffer) values: {, }
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:169)
    at com.supplyscape.qa.demo.MyTest.test1(MyTest.groovy:56)

     Am I missing any thing in this context?

Thank you,
Murthy.

Paul King

unread,
Feb 29, 2008, 3:37:03 PM2/29/08
to us...@groovy.codehaus.org

The consumeProcessOutput(StringBuffer, StringBuffer) is only in 1.5.4.
Possible your eclipse environment is pointing to an older version.
As a test you could try consumeProcessOutput with no args. It has been
there since 1.0 but then you have no way to get the results. OK if
you are firing of tasks that e.g. produce files.

Paul

> <mailto:mhu...@broad.mit.edu>> wrote:
> >
> > Yes. Simply use the Antbuilder.exec method to exec Maven.
> >
> >
> > On Feb 28, 2008, at 12:43 PM, "Murthy Perla"

> > <murth...@gmail.com <mailto:murth...@gmail.com>>

Murthy Perla

unread,
Feb 29, 2008, 5:48:44 PM2/29/08
to us...@groovy.codehaus.org

     Thank you. Its correct. The eclipse groovy plugin is 1.5.1.

Thank you,
Murthy.

Murthy Perla

unread,
Mar 3, 2008, 11:46:34 AM3/3/08
to us...@groovy.codehaus.org
Hi,
         Thank you for the code provided. When I tried to run the piece of code from Groovy Console, Its giving the below error.
         I tried to use 'dir' for executing the maven command in specific directory. I dont know it accepts that or not. [tried to run with out 'dir' but still it failed]
 
Groovy Code:  def ant = new AntBuilder()
                     ant.exec(dir:".", executable:"maven"){}
 
Exception thrown: : Execute failed: java.io.IOException: CreateProcess: maven error=2
        
         I have system path set for maven. I tried using the -p option & still its the same error.
 
         To my knowledge when it says error=2, I guess its looking for some file & its not getting that.
         Am I missing any thing? Please help me out.
 
Thanks,
Murthy.

On 2/29/08, Erik Husby <mhu...@broad.mit.edu> wrote:

Erik Husby

unread,
Mar 3, 2008, 1:08:52 PM3/3/08
to us...@groovy.codehaus.org
You need to consult the Ant documentation on the exec task for all its options.

You also need to insure that "maven" is in your path -- how you do that depends on your OS.

Murthy Perla

unread,
Mar 3, 2008, 3:24:21 PM3/3/08
to us...@groovy.codehaus.org

       Thanks for your inputs. I could do that with the following code.


def ant = new AntBuilder()
ant.exec(dir:".", executable:"cmd"){
arg(value: "/c")
arg(value:"maven")

arg(value:"--nobanner")
arg(value:"--quiet")
}

Thanks,
Murthy.
Reply all
Reply to author
Forward
0 new messages