How to variablize shell script output on maven pom.xml to use

I am wondering is there a way to have maven to execute a shell script that curl a resource and make the response available like an environment variables or global variables that maven can reference to, or is it possible to do it with Groovy?

So the idea when I do a maven build I want to execute this shell script. The script itself will make curl to some resource URI and output the response (we possibly have to wait for it to get back), and maven or possibly Groovy can get that curl response in some way and use it for setting up some configuration.

Answers


Here is a suggested approach:

Requirements for this approach:

  • The curl response you expect (or the output or your script) should have a name=value format (that is, a properties file)
  • The Exec Maven Plugin execution must be declared in your POM before the Properties Maven Plugin execution and they must be attached to the same Maven phase or to two consecutive Maven phases in order to provide the desired flow
  • The Properties Maven Plugin execution must be attached to an early phase of Maven (initialize or validate) in order to make the configuration available to other Maven phases and plugins

For a complete list of Maven phases, official documentation here

Update: below an example of flow, just tested and work perfectly (on Windows machine):

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sample</groupId>
    <artifactId>generation</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.1</version>
                <executions>
                    <execution>
                        <id>retrieve-config</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executable>echo</executable>
                    <arguments>
                        <argument>jar.name=from-exec</argument>
                        <argument>></argument>
                        <argument>config.properties</argument>
                    </arguments>
                    <workingDirectory>${basedir}/src/main/resources/</workingDirectory>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.0-alpha-2</version>
                <executions>
                    <execution>
                        <id>read-properties</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                            <files>
                                <file>${basedir}/src/main/resources/config.properties</file>
                            </files>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <finalName>${jar.name}</finalName>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Basically, the exec plugin attached to the validate phase will be executed at the beginning of the build, writing to a config.properties file (via the echo command) the content jar.name=from-exec.

Then the properties plugin attached to the initialize phase will read that config.properties file and load the properties to be used as part of the build.

Then, as an example, the jar plugin will use that property as part of its configuration (the <finalName>${jar.name}</finalName> part).

Running mvn clean package, you will find the from-exec.jar file in the target folder.

Update: Above is an example of how to load dynamically from a script a set of properties which can then be injected into a Maven build (hence used as part of the POM configuration).

However, if you need to load this dynamic configuration into your application, you can even skip the second step (the Propeprties Maven Plugin) and load the properties from the config.properties file in your code, as long as the file is part of the application classpath (like in the example above, placed under src/main/resources).

Since the creation of the properties file happens on early Maven phase (validate or initialize), the test (for your tests) and package (for your final artefact) phases could then use it as required.

In Java, you would use the java.util.Properties class, in Groovy you can follow the explanation here, from another question in stackoverflow.


Need Your Help

SELECT DISTINCT or GROUP BY on field

sql postgresql group-by distinct greatest-n-per-group

I have a table on my DB that record every call made by employees all over the day.

Post.order method does not work with Sequel and Sinatra

sinatra sequel

I am trying to order my posts based on the created date ( newest to oldest ) so i do this :