【问题标题】:Save exec output to <properties></properties> section of pom.xml for use in later maven phase将 exec 输出保存到 pom.xml 的 <properties></properties> 部分,以便在以后的 maven 阶段使用
【发布时间】:2019-03-27 19:07:24
【问题描述】:

我有一个 maven java 项目,它 docker builds 一个 docker 映像,然后 docker saves 使用 io.fabric8.docker-maven-plugin 插件将该映像作为构建的工件。

当我构建 docker 映像时,我想获取大小并将其添加到元数据中,这样我们就可以看到未压缩之前的映像有多大。

io.fabric8.docker-maven-plugin 插件无法获取生成的 docker 图像的确切大小,所以我目前正在使用org.codehaus.mojo.exec-maven-plugin 插件来获取图像的大小,如下所示:

<plugin>
    <!-- generate metadata about docker image -->
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.1</version>
    <executions>
        <execution>
            <id>generate-docker-metadata</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>docker</executable>
                <commandlineArgs>image inspect ${project.docker.image}:${project.version} --format='{{.Size}}' >> ${project.basedir}/src/main/resources/META-INF/Docker-Image-Size</commandlineArgs>
                <workingDirectory>.</workingDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

目前我可以将输出放入Docker-Image-Size 文件中,但我需要一种方法将其作为属性提供,该属性可以在生成元数据文件的元数据部分中引用。

如何将此 exec 输出保存到 pom.xml 文件的 &lt;properties&gt;/&lt;properties&gt; 部分,以便稍后在不同阶段使用?

我需要使用属性文件,还是可以将其保存为${docker.image.size}

【问题讨论】:

  • 我认为这在 Maven 的一次运行中是不可能的。相反,您可以先运行目标或脚本来操作 pom,然后再启动 Maven 构建。
  • 问题是直到我用mvn 构建图像时我才知道大小。鉴于当我运行 mvn install 时它会构建映像,只有这样我才能将映像存在于主机上以用作 docker inspect 命令的输入。
  • AFAIK 您无法在构建过程中更改 POM。所以我想答案是你要求的行为是不可能的。
  • 将 docker 图像查询的结果写入目标中的临时文件,或直接写入元数据文件。

标签: java maven docker properties


【解决方案1】:

我无法直接编辑 pom.xml&lt;properties&gt;/&lt;properties&gt; 部分中已存在的任何属性。

然而,我想出的答案是使用org.codehaus.gmaven.gmaven-plugin 插件来运行shell 脚本,编辑输出,并将其保存到一个变量中,以后可以在@987654324 中使用@ 文件。请注意,除了org.codehaus.gmaven.gmaven-plugin 插件部分之外,他的变量实际上并未在文件中的任何位置定义。

pom.xml&lt;properties&gt;/&lt;properties&gt;部分放置一个空变量总是会使变量为空。

我像这样使用org.codehaus.gmaven.gmaven-plugin 插件:

<plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
                <properties>
                    <script>docker image inspect ${project.docker.image}:${project.version} --format='{{.Size}}</script>
                </properties>
                <source>
                    def command = project.properties.script
                    def process = command.execute()
                    process.waitFor()

                    def text = process.in.text.trim()
                    // Remove single quotes that surround number output
                    def number = text.substring(1, text.length()-1);
                    project.properties.dockerImageSize = number
                </source>
            </configuration>
        </execution>
    </executions>
</plugin>

然后元数据部分像这样使用变量:

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <version>2.4.0</version>
    <extensions>true</extensions>
    <executions>
        <execution>
            <id>bundle-manifest</id>
                <phase>process-classes</phase>
                <goals>
                    <goal>manifest</goal>
                </goals>
        </execution>
    </executions>
    <configuration>
        <instructions>
            <Bundle-Category>Thing</Bundle-Category>
            <Bundle-Activator>com.company.thing.impl.Activator</Bundle-Activator>
            <Bundle-Vendor>${company.vendor}</Bundle-Vendor>
            <Bundle-ContactAddress>${company.contactAddress}</Bundle-ContactAddress>
            <Bundle-Copyright>${company.copyright}</Bundle-Copyright>
            <Bundle-LicenseType>${company.licenseType}</Bundle-LicenseType>
            <Bundle-Description>${company.description}</Bundle-Description>
            <Bundle-DockerImageSize>${dockerImageSize}</Bundle-DockerImageSize>
            <Import-Package>
                com.company.thing.api*;version="[0.0.10,1.0.0)",
                *
            </Import-Package>
        </instructions>
    </configuration>
</plugin>

【讨论】:

    猜你喜欢
    • 2013-10-03
    • 2013-02-16
    • 2018-07-09
    • 2021-04-18
    • 2015-07-21
    • 2018-09-30
    • 2018-12-15
    • 2014-06-29
    • 2011-01-12
    相关资源
    最近更新 更多