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