简体   繁体   English

如何在maven pom.xml上变量化shell脚本输出来使用

[英]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 ? 我想知道有没有办法让maven执行一个shell脚本来卷曲资源并使响应可用,就像maven可以引用的环境变量或全局变量一样,还是可以用Groovy来实现?

So the idea when I do a maven build I want to execute this shell script. 所以当我做maven构建时我想要执行这个shell脚本。 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. 脚本本身将卷曲到一些资源URI并输出响应(我们可能必须等待它返回),并且maven或者可能Groovy可以以某种方式获得卷曲响应并使用它来设置一些配置。

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) 您期望的卷曲响应(或输出或您的脚本)应具有name = value格式(即属性文件)
  • 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 必须在执行Properties Maven插件之前在POM中声明Exec Maven插件执行,并且它们必须连接到相同的Maven阶段或连接到两个连续的Maven阶段才能提供所需的流程
  • 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 必须将属性Maven插件执行附加到Maven的早期阶段(初始化或验证),以使配置可用于其他Maven阶段和插件

For a complete list of Maven phases, official documentation here 有关Maven阶段的完整列表,请参阅此处的官方文档

Update : below an example of flow, just tested and work perfectly (on Windows machine): 更新 :下面是一个流程示例,刚刚测试并且工作正常(在Windows机器上):

<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 . 基本上,附加到validate阶段的exec插件将在构建开始时执行,写入config.properties文件(通过echo命令)内容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. 然后附加到initialize阶段的属性插件将读取该config.properties文件并加载要用作构建的一部分的属性。

Then, as an example, the jar plugin will use that property as part of its configuration (the <finalName>${jar.name}</finalName> part). 然后,作为示例,jar插件将使用该属性作为其配置的一部分( <finalName>${jar.name}</finalName>部分)。

Running mvn clean package , you will find the from-exec.jar file in the target folder. 运行mvn clean package ,您将在目标文件夹中找到from-exec.jar文件。

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). 更新 :上面是如何从脚本动态加载一组属性的示例,然后可以将这些属性注入Maven构建(因此用作POM配置的一部分)。

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 ). 但是,如果您需要将此动态配置加载到您的应用程序中,您甚至可以跳过第二步(Propeprties Maven插件)并从代码中的config.properties文件加载属性,只要该文件是该文件的一部分即可。应用程序类路径(如上例所示,放在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. 由于属性文件的创建发生在早期Maven阶段( validateinitialize ),因此test (对于您的测试)和package (对于您的最终人工制品)阶段可以根据需要使用它。

In Java, you would use the java.util.Properties class, in Groovy you can follow the explanation here , from another question in stackoverflow. 在Java中,您将使用java.util.Properties类,在Groovy中,您可以按照此处的说明,从stackoverflow中的另一个问题开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM