Another thing to do especially since some devs keep steering people to
use commons-exec and then other devs say not to use it is to use the
command line execution functionality in plexus-utils.


1. Add the following dependency to your pom:

                <dependency>
                        <groupId>plexus</groupId>
                        <artifactId>plexus-utils</artifactId>
                        <version>1.0.3</version>
                </dependency>


2. Createa utility method like the following:


        /**
         * @see org.apache.maven.plugin.Mojo#execute()
         */
        public static int execute(Commandline cl, Log log) throws
MojoExecutionException
        {
       try
                {
                        if (log.isDebugEnabled())
                        {
                        log.debug("Command Line: " + cl);
                        }
                StreamConsumer consumer = new MavenLogConsumer(log);
                int exitCode = CommandLineUtils.executeCommandLine( cl, 
consumer,
                        consumer );
                return exitCode;
                }
                catch (CommandLineException e)
                {
                        throw new MojoExecutionException("command line error", 
e);
                }
        }

3. Create a StreamConsumer that will funnel output to your current
maven process:

public class MavenLogConsumer implements StreamConsumer
{
        private Log log;
        
        /**
         * @param log
         */
        public MavenLogConsumer(Log log)
        {
                this.log = log;
        }

        /**
         * @see 
org.codehaus.plexus.util.cli.StreamConsumer#consumeLine(java.lang.String)
         */
        public void consumeLine(String line)
        {
                log.info(line);
        }       
}

You should now be good to execute the jar command directly.

Wb


On 10/4/05, Orjan Austvold <[EMAIL PROTECTED]> wrote:
> Hi,
>
> The jar tool is located in the tools.jar archive. Try something like
>
> sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out,
> System.err, "jar");
> jarTool.run(new String[]{"cfm", "new-archive.jar", "myManifest.mf"});
>
>
> Ørjan
>
>
> Ashley Williams wrote:
> > Just to give a little more context I'm writing a plugin that simply
> > invokes "java -jar jaxb-xjc.jar <myschema>", hopefully specifying  which
> > java vm to use, eg Java 5 or Java 4. Just a simple one liner  plugin
> > really.
> >
> > On 3 Oct 2005, at 23:26, Ashley Williams wrote:
> >
> >> I'm writing a plugin that needs to call java -jar in the execute
> >> method. Is there an api I should use as I don't wish to use the  java
> >> Runtime class to do this. I've had a look at the compiler  plugin
> >> thinking that it might give me a clue (maybe by invoking  javac), but
> >> it just delegates to some CompilerManager class.
> >>
> >> Thanks
> >> AW
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: [EMAIL PROTECTED]
> >> For additional commands, e-mail: [EMAIL PROTECTED]
> >>
> >>
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
>
> --
> Ørjan Austvold - Senior Software Architect
> www.colibria.com - putting the presence into messaging
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to