开发者

Maven Plugin Development - how to make maven block until execute completes

开发者 https://www.devze.com 2023-04-04 07:02 出处:网络
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.

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 开发者_如何学JAVAknow 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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号