Maven exec bash 脚本并将输出保存为属性

标签 maven maven-plugin

我想知道是否存在运行 bash 脚本并将其结果保存到属性中的 Maven 插件。

我的实际用例是获取 git 源版本。我在网上找到了一个插件,但它看起来没有经过很好的测试,我突然想到,我只需要一个像这篇文章标题中的插件一样简单的插件。插件看起来像:

<plugin>maven-run-script-plugin>
    <phase>process-resources</phase> <!-- not sure where most intelligent -->
    <configuration>
        <script>"git rev-parse HEAD"</script> <!-- must run from build directory -->
        <targetProperty>"properties.gitVersion"</targetProperty>
    </configuration>
</plugin>

当然有必要确保在需要属性之前发生这种情况,在我的情况下,我想使用此属性来处理源文件。

最佳答案

我想你可以利用 gmaven此任务的插件:

<plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
                <properties>
                    <script>git rev-parse HEAD</script>
                </properties>
                <source>
                    def command = project.properties.script
                    def process = command.execute()
                    process.waitFor()

                    project.properties.gitVersion = process.in.text
                </source>
            </configuration>
        </execution>
    </executions>
</plugin>

执行此脚本后,您应该可以引用 ${gitVersion}属性(property)。

关于Maven exec bash 脚本并将输出保存为属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13887436/

相关文章:

android - 如何将 aar 库上传到 Nexus?

java - 使用 mvn tomcat7 :run-war? 运行 Web 应用程序可能会导致 "Unable to install breakpoint"问题

java - maven可部署jar "java.lang.ClassNotFoundException: org.apache.hadoop.hive.jdbc.HiveDriver"错误

java - Gatling Maven 插件和 Maven 3.3.3

tomcat6 - Maven命令: mvn tomcat:run

svn - maven 发布 :prepare FAILS due to 1. 7 svn 工作副本(预计为 1.6)

java - 使用 Apache Derby 作为嵌入式数据库的 Spring-boot 错误

maven - maven surfire argLine 是否覆盖 ./mvn/jvm.config?

java - 使用 maven selenium 插件启动浏览器 session 时无法删除文件

html - 使用 Maven 插件将 asciidoc 转换为 html5 时如何启用(自定义)警告图标