有没有办法捕获maven中的用户输入并将其分配给maven属性?

use*_*374 13 plugins input command-prompt maven-plugin maven

  1. 有没有办法暂停maven执行流程以提供命令提示符,以便用户可以输入文本.
  2. 然后我希望提供的文本存储在maven属性中.
  3. 如果用户输入可以被掩盖,那将是一个奖励.

这对于避免在pom中存储密码非常有用.

非常感谢

小智 12

您可以使用maven-antrun-plugin捕获用户输入.以下示例显示如何向当前用户询问新项目版本.

    <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>
Run Code Online (Sandbox Code Playgroud)

您可以通过以下方式运行此功能:

mvn -N -P change-version
Run Code Online (Sandbox Code Playgroud)

一些解释:

  • 如果要获取密码,请参阅http://www.dcepler.net/post.cfm/hiding-password-input-in-ant (2认同)

mab*_*aba 8

如果你在pom中添加一个属性,如下所示:

<properties>
    <db.password></db.password>
</properties>
Run Code Online (Sandbox Code Playgroud)

并在你的pom中使用它,如下所示:

<someTag>${db.password}</someTag>
Run Code Online (Sandbox Code Playgroud)

然后,您可以从命令行设置属性:

$ mvn -Ddb.password="DonaldDuck" install
Run Code Online (Sandbox Code Playgroud)

但它不像命令提示符那样是交互式的.