Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?)

like image 740
felipecao Avatar asked Oct 16 '13 21:10

felipecao


People also ask

Where do I run Maven commands?

Running a Maven build via the command line To build a Maven project via the command line, you use the mvn command from the command line. The command must be executed in the directory which contains the relevant pom file. You pass the build life cycle, phase or goal as parameter to this command.


1 Answers

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.

like image 131
prunge Avatar answered Sep 19 '22 16:09

prunge