如何在 maven pom.xml 上可变化 shell 脚本输出以供使用

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

我想知道有没有办法让 maven 执行 shell 脚本来卷曲资源并使响应像 maven 可以引用的环境变量或全局变量一样可用,或者是否有可能Groovy?

因此,当我进行 maven 构建时,我想执行这个 shell 脚本。脚本本身会对某些资源 URI 进行 curl 并输出响应(我们可能必须等待它返回),maven 或 Groovy 可以通过某种方式获取该 curl 响应并将其用于设置一些配置。

这是一个建议的方法:

此方法的要求:

  • 您期望的 curl 响应(或输出或您的脚本)应该具有名称=值格式(即属性文件)
  • Exec Maven 插件执行必须在 Properties Maven 插件执行之前在您的 POM 中声明,并且它们必须附加到同一个 Maven 阶段或两个连续的 Maven 阶段以提供所需的流程
  • Properties Maven 插件执行必须附加到 Maven 的早期阶段(初始化或验证),以使配置可用于其他 Maven 阶段和插件

Maven 阶段的完整列表,官方文档here

更新:下面是一个流程示例,刚刚测试并完美运行(在 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>

基本上,附加到 validate 阶段的 exec 插件将在构建开始时执行,写入 config.properties 文件(通过 echo 命令)内容 jar.name=from-exec.

然后附加到 initialize 阶段的属性插件将读取该 config.properties 文件并加载要用作构建的一部分的属性。

然后,作为示例,jar 插件将使用 属性 作为其配置的一部分(<finalName>${jar.name}</finalName> 部分)。

运行mvn clean package,您会在目标文件夹中找到from-exec.jar文件。

Update:上面是一个示例,说明如何从脚本动态加载一组属性,然后可以将这些属性注入到 Maven 构建中(因此用作 POM 的一部分配置)。

然而,如果您需要将此动态配置加载到您的应用程序中,您甚至可以跳过第二步(Propeprties Maven 插件)并从代码中的 config.properties 文件加载属性,只要因为该文件是应用程序 class 路径的一部分(如上例所示,位于 src/main/resources 下)。

由于属性文件的创建发生在早期的 Maven 阶段(validateinitialize),test(用于您的测试)和 package(用于您的最终人工制品)阶段可以根据需要使用它。

在Java中,你会使用java.util.Properties class,在Groovy中你可以按照Whosebug中另一个问题的解释here