0

I've have some java code that starts a web server and listens for requests. I'd like to start the web server from a custom maven plugin goal.

This works fine (starts my web server and listens for requests):

public static void main(String[] args) {
  MyWebServer webserver = new MyWebServer();
  webserver.startServer();
}

I'd like to put this inside a custom maven plugin:

/**
 *
 * @goal start
 * 
 */
public class MyMojo
    extends AbstractMojo
{
    public void execute()
        throws MojoExecutionException
    {
        (new MyWebServer()).startServer();
    }
}

It almost works, except that Maven doesn't stop to let the web server respond to requests. It calls startServer and then appears to stops it and immediately continues on with other tasks.

Anyone know how to tell maven to wait/block until it receives CTRL-C from command line? I'd like it to behave similar to the way the jetty maven plugin works.

Thanks, Dave

Upgradingdave
  • 12,916
  • 10
  • 62
  • 72

1 Answers1

0

A bit of a hack, but simply adding a System.in.read() does the trick:

/**
 *
 * @goal start
 * 
 */
public class MyMojo
    extends AbstractMojo
{
    public void execute()
        throws MojoExecutionException
    {
        try{
        (new MyWebServer()).startServer();

        System.out.println("Press any key to stop the server");
        byte name[] = new byte[100];
        System.in.read(name);
    } catch (Exception e) {
        System.out.println(e);
    }
    }
}

I also found this question which might be useful for others working on maven plugins: Maven plugin executing another plugin

Community
  • 1
  • 1
Upgradingdave
  • 12,916
  • 10
  • 62
  • 72