简体   繁体   中英

How do I prompt for user input in Maven?

I'm writing a Maven plugin which I'd like to prompt for a simple user input and decide whether to halt the plugin's execution.

I'd like to do something like this:

$> mvn myplugin:run

[MAVEN] would you like to continue? [default value: y] _

I've tried using maven-antrun-plugin as described here , but in this case Maven gets user input when I build my plugin. Instead, I'd like to retrieve input when user is running my plugin from within some other app that has declared my plugin (confusing?)

Use a Prompter component and have it injected in your plugin (assuming you are using Maven plugin annotations , if not use the equivalent javadoc tags ):

@Component
private Prompter prompter;

And to use it:

String name = prompter.prompt("Please enter your name");

Pull in this dependency in your plugin's POM:

<dependency>
    <groupId>org.codehaus.plexus</groupId>
    <artifactId>plexus-interactivity-api</artifactId>
    <version>1.0-alpha-6</version>
</dependency>

The prompter component is used by the release plugin for prompting the user for tags and versions and the archetype plugin as well.

Don't do this. If you need to supply data to a mojo, do it via configuration.

The behavior of your Maven build should be entirely predictable based on your POM and the goals & options supplied to the Maven command line. If you allow a user to feed in additional information during the build, your POM no longer completely describes your project.

Also, it would prevent any automated build server doing its job.

作为变体,不要像wool.in.silver写的那样破坏maven方式,你可以使用shell脚本来提示值,然后使用聚集的值作为参数调用maven

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM