Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 394
peter Avatar asked Oct 31 '22 14:10

peter


1 Answers

Here is a suggested approach:

  • Use the Exec Maven Plugin for launching your script or commands
  • Use the Properties Maven Plugin to load the configuration

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.

like image 166
A_Di-Matteo Avatar answered Nov 09 '22 12:11

A_Di-Matteo