5

我想知道是否存在运行 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>

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

4

1 回答 1

4

我认为您可以使用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}属性。

于 2012-12-15T08:31:35.333 回答