Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven exec bash script and save output as property

I'm wondering if there exists a Maven plugin that runs a bash script and saves the results of it into a property.

My actual use case is to get the git source version. I found one plugin available online but it didn't look well tested, and it occurred to me that a plugin as simple as the one in the title of this post is all I need. Plugin would look something like:

<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>

Of course necessary to make sure this happens before the property will be needed, and in my case I want to use this property to process a source file.

like image 805
djechlin Avatar asked Oct 05 '22 16:10

djechlin


1 Answers

I think you could utilize gmaven plugin for this task:

<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>

After execution of this script you should be able to refer to ${gitVersion} property.

like image 121
Andrew Logvinov Avatar answered Oct 13 '22 00:10

Andrew Logvinov