【问题标题】:Is there a way to capture user input in maven and assign it to a maven property?有没有办法在 maven 中捕获用户输入并将其分配给 maven 属性?
【发布时间】:2016-06-02 23:45:47
【问题描述】:
  1. 有没有办法暂停 Maven 执行流程以提供命令提示符,以便用户输入文本。
  2. 然后我希望将提供的文本存储在 Maven 属性中。
  3. 如果用户输入可以被屏蔽,那将是一个奖励。

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

非常感谢

【问题讨论】:

  • 为了什么目的?通常是没有办法的。但作为示例,您可以查看 Maven-release-Plugin 或 maven-pgp-plugin。
  • maven-scm-plugin 和 tomcat6-maven-plugin 也可以从 settings.xml 文件中读取(加密)密码。检查插件的常见问题解答和/或示例部分,因为插件之间的配置可能不同。

标签: maven plugins input maven-plugin command-prompt


【解决方案1】:

您可以使用 ma​​ven-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>

您可以通过调用来运行此功能:

mvn -N -P change-version

一些解释:

【讨论】:

【解决方案2】:

如果你像这样在你的 pom 中添加一个属性:

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

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

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

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

$ mvn -Ddb.password="DonaldDuck" install

但它不像命令提示符那样具有交互性。

【讨论】:

  • 我认为这是一种更好的方法,而且更“Maven 方式”
  • @Anonymous 这是可以管理的。此外,OP 的问题中没有提到操作系统(或外壳)。
  • 一个进程的参数通常对其他进程可见,有时对用户可见
猜你喜欢
  • 2020-03-22
  • 2016-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-18
  • 2023-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多