Is there a way to capture user input in maven and assign it to a maven property?

流过昼夜 提交于 2019-12-17 19:19:20

问题


  1. Is there a way to pause the maven execution flow to provide a command prompt so user can input text.
  2. Then I would like the provided text to be stored in a maven properties.
  3. If the user input could be masked that would be a bonus.

This would be really useful to avoid storing passwords in pom.

Many Thanks


回答1:


You can catch a user input using maven-antrun-plugin. The following example show how ask current user the new project version.

    <profile>
        <id>change-version</id>
        <build>
            <defaultGoal>validate</defaultGoal>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.7</version>
                    <executions>
                        <execution>
                            <id>catch-new-version</id>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <phase>validate</phase>
                            <configuration>
                                <target>
                                    <!-- == catch new version in a prompt == -->
                                    <input
                                        message="Please enter the new SNAPSHOT version (current is '${project.version}'): "
                                        addproperty="new-user-version" />
                                </target>
                                <exportAntProperties>true</exportAntProperties>
                            </configuration>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.apache.ant</groupId>
                            <artifactId>ant</artifactId>
                            <version>1.8.4</version>
                        </dependency>
                    </dependencies>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>versions-maven-plugin</artifactId>
                    <version>1.3.1</version>
                    <executions>
                        <execution>
                            <id>set-new-version</id>
                            <goals>
                                <goal>set</goal>
                            </goals>
                            <phase>validate</phase>
                            <configuration>
                                <generateBackupPoms>false</generateBackupPoms>
                                <newVersion>${new-user-version}</newVersion>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

You can run this feature by calling :

mvn -N -P change-version

Some explanations :

  • The -N- option allow to not recurse into sub-projects.
  • Using org.apache.ant:ant:1.8.4 to avoid https://issues.apache.org/bugzilla/show_bug.cgi?id=51161
  • Using maven 3.0.4
  • Documentation: maven-antrun-plugin, Input Tag



回答2:


If you add a property in your pom like so:

<properties>
    <db.password></db.password>
</properties>

And use it in your pom somewhere like this:

<someTag>${db.password}</someTag>

Then you can set the property from command line:

$ mvn -Ddb.password="DonaldDuck" install

But it is not interactive like a command prompt.



来源:https://stackoverflow.com/questions/11340894/is-there-a-way-to-capture-user-input-in-maven-and-assign-it-to-a-maven-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!