maven - 将exec-maven-plugin的输出分配给变量

标签 maven exec-maven-plugin

我想使用exec-maven-plugin来获取git'revision',所以我正在使用以下配置:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <executions>
        <execution>
            <id>gitVersion</id>
            <phase>validate</phase>
            <goals>
                <goal>exec</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <executable>git</executable>
        <workingDirectory>./</workingDirectory>
        <arguments>
            <argument>rev-list</argument>
            <argument>master</argument>
            <argument>--count</argument>
        </arguments>
    </configuration>
</plugin>

但我遇到了一个问题-如何将输出分配给其他插件/生命周期中可用的任何变量?

(我能够使用gmaven-plugin并执行groovy脚本来完成它,但是我发现它有点过分/不太优雅)

编辑:
供引用,常规的工作解决方案:
<plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <version>1.5</version>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
                <providerSelection>2.0</providerSelection>
                <properties>
                    <script>git rev-list master --count</script>
                </properties>
                <source>
                    def command = project.properties.script
                    def process = command.execute()
                    process.waitFor()

                    def describe = process.in.text.trim()
                    println "setting revision to: " + describe

                    project.properties.setProperty('gitVersion',describe)
                </source>
            </configuration>
        </execution>
    </executions>
</plugin>

最佳答案

为了清晰起见,使用groovy解决方案:

<plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <version>1.5</version>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
                <providerSelection>2.0</providerSelection>
                <properties>
                    <script>git rev-list master --count</script>
                </properties>
                <source>
                    def command = project.properties.script
                    def process = command.execute()
                    process.waitFor()

                    def describe = process.in.text.trim()
                    println "setting revision to: " + describe

                    project.properties.setProperty('gitVersion',describe)
                </source>
            </configuration>
        </execution>
    </executions>
</plugin>

关于maven - 将exec-maven-plugin的输出分配给变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17963222/

相关文章:

java - 源值 1.5 的错误已过时,将在未来版本中删除

spring - 将 Spring 项目作为 pom.xml 中的依赖项添加到 Spring Boot 项目

maven - 如何在单个 pom.xml 中使用 maven 执行多个命令提示符命令

java - 有没有一种简单的方法可以将日志记录从依赖项插入到类中?

java - NetBeans 8.1 无法启动 WildFly 10 服务器

java - 提取运行时maven信息

java - exec-maven-plugin 找不到或加载主类但输出在命令行上运行正常

java - 如何在 Maven 构建过程中调用 ruby​​ 脚本?

maven - 使用 Maven exec 插件启动 Windows 批处理脚本会阻止构建,即使该脚本使用 "start"

java - 使用 Exec Maven 插件 fork Java,而不使用 `exec` 目标