【问题标题】:How to set Spring Boot maven plugin pom proxy values on command line如何在命令行上设置 Spring Boot maven 插件 pom 代理值
【发布时间】:2022-01-20 18:07:25
【问题描述】:

我正在尝试使用spring-boot-maven-plugin:2.5.6:build-image 来构建应用程序映像。我的pom.xml 中有一个用于构建映像的个人资料,

<profile>
    <id>build-image</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>build-image</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <docker>
                        <publishRegistry>
                            <username>${docker.publish.username}</username>
                            <password>${docker.publish.password}</password>
                            <url>https://registry.example.com</url>
                        </publishRegistry>
                    </docker>
                    <publish>true</publish>
                    <image>
                        <env>
                            <HTTP_PROXY>https://proxy.example.com:8080</HTTP_PROXY>
                            <HTTPS_PROXY>https://proxy.example.com:8443</HTTPS_PROXY>
                            <NO_PROXY>localhost,127.0.0.1,example.com</NO_PROXY>
                        </env>
                    </image>
                </configuration>
            </plugin>
        </plugins>
    </build>
</profile>

我正在使用这样的命令来构建图像,

mvn clean deploy \
  -P build-image \
  -Dspring-boot.build-image.imageName=example/image:latest

我的问题是如何在命令行上传递其他spring-boot.build-image 属性?我可以设置图像名称,但无法弄清楚其他属性。特别是代理配置。我尝试过类似的变体,

mvn clean deploy \
  -P build-image \
  -Dspring-boot.build-image.imageName=example/image:latest \
  -Dspring-boot.build-image.image.env.HTTP_PROXY="https://proxy.example.com:8080" \
  -Dspring-boot.build-image.image.env.HTTPS_PROXY="https://proxy.example.com:8443" \
  -Dspring-boot.build-image.image.env.NO_PROXY="localhost,127.0.0.1,example.com"

我似乎不知道如何通过-D 设置这些代理变量。有人知道要在命令行上设置的属性吗?

我已经找到了定义这些属性的class,它在javadoc 中列出了env,但我没有看到设置env 属性的实际方法。这是那个类的sn-p,

/**
 * Image configuration, with {@code builder}, {@code runImage}, {@code name},
 * {@code env}, {@code cleanCache}, {@code verboseLogging}, {@code pullPolicy}, and
 * {@code publish} options.
 * @since 2.3.0
 */
@Parameter
private Image image;

/**
 * Alias for {@link Image#name} to support configuration via command-line property.
 * @since 2.3.0
 */
@Parameter(property = "spring-boot.build-image.imageName", readonly = true)
String imageName;

谢谢

【问题讨论】:

    标签: java spring spring-boot maven proxy


    【解决方案1】:

    您可以尝试在配置中使用属性:

    <image>
      <env>
        <HTTP_PROXY>${image.http_proxy}</HTTP_PROXY>
        <HTTPS_PROXY>${image.https_proxy}</HTTPS_PROXY>
        <NO_PROXY>${image.no_proxy}</NO_PROXY>
      </env>
    </image>
    

    然后使用-DpropertyName 传递它们,例如

    mvn clean deploy \
      -P build-image \
      -Dspring-boot.build-image.imageName=example/image:latest
      -Dimage.http_proxy=...
    

    默认值可以在 pom.xml 的 properties 元素中定义

    <properties>
      <image.http_proxy>http://some-proxy</image.http_proxy>
    </properties>
    

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 2010-10-19
      • 2011-06-07
      • 2019-08-30
      • 2022-06-10
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      • 2014-11-05
      相关资源
      最近更新 更多