访问 pom.xml 中的 Spring Boot Maven 插件中的 Maven settings.xml。

huangapple go评论41阅读模式
英文:

Access Maven settings.xml in pom.xml for Spring Boot Maven Plugin

问题

我想访问包含在我的pom中的settings.xml中的用户特定设置(密码、用户名等),因为我想在Spring Boot Maven插件中使用发布功能(将创建的Docker镜像推送到我们的私有Docker仓库)。

有办法实现这一目标吗?

当然,我不想将任何用户特定的密码保存在pom.xml中。

Maven文档表示可以访问settings.xml(例如:https://maven.apache.org/guides/getting-started/index.html#how-do-i-filter-resource-files),但它没有解释如何做到这一点。

我期望在我的pom中有类似以下的内容:

<someTag>${userSettings.some.property}</someTag>

英文:

I would like to access the user-specific settings (passwords, usernames etc. for a shared private repository) that are contained within the settings.xml in my pom.

I need these settings for the Spring Boot Maven Plugin, because I want to use the publish feature there (pushing a created docker image to our private docker repository).

Is there a way to achieve this?

Of course I don't want to save any user-specific passwords inside the pom.xml.

The maven documentation states that it is possible to access the settings.xml (e.g. here: https://maven.apache.org/guides/getting-started/index.html#how-do-i-filter-resource-files), but it does not explain how to to that.

I expecting something like this in my pom:

&lt;someTag&gt;${userSettings.some.property}&lt;/someTag&gt;

答案1

得分: 1

假设我们有以下 spring-boot-maven-plugin 的配置:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <image>
            <name>docker.example.com/library/${project.artifactId}</name>
            <publish>true</publish>
        </image>
        <docker>
            <publishRegistry>
                <username>user</username>
                <password>secret</password>
                <url>https://docker.example.com/v1/</url>
                <email>user@example.com</email>
            </publishRegistry>
        </docker>
    </configuration>
</plugin>

而我们不希望将凭据存储在 pom.xml 中,因为这对所有有访问版本控制系统权限的人都可见。Maven 的方法是在 ~/.m2/settings.xml 中定义一个带有凭据的 profile,然后在 pom.xml 中使用相应的占位符替换凭据,如下所示:

~/.m2/settings.xml

...
<profiles>
...    
    <profile>
        <id>registry-example.com</id>
        <properties>
            <registry.username>user</registry.username>
            <registry.password>secret</registry.password>
            <registry.url>https://docker.example.com/v1/</registry.url>
            <registry.email>user@example.com</registry.email>
        </properties>
    </profile>
...    
</profiles>
...

pom.xml

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <image>
            <name>docker.example.com/library/${project.artifactId}</name>
            <publish>true</publish>
        </image>
        <docker>
            <publishRegistry>
                <username>${registry.username}</username>
                <password>${registry.password}</password>
                <url>${registry.url}</url>
                <email>${registry.email}</email>
            </publishRegistry>
        </docker>
    </configuration>
</plugin>

现在,为了告诉 maven 考虑我们创建的 profile,需要使用 -P 标志运行 maven,指定我们的 profile 的 ID:

mvn spring-boot:build-image -Pregistry-example.com
英文:

Suppose we have following configuration for spring-boot-maven-plugin

&lt;plugin&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
    &lt;configuration&gt;
        &lt;image&gt;
            &lt;name&gt;docker.example.com/library/${project.artifactId}&lt;/name&gt;
            &lt;publish&gt;true&lt;/publish&gt;
        &lt;/image&gt;
        &lt;docker&gt;
            &lt;publishRegistry&gt;
                &lt;username&gt;user&lt;/username&gt;
                &lt;password&gt;secret&lt;/password&gt;
                &lt;url&gt;https://docker.example.com/v1/&lt;/url&gt;
                &lt;email&gt;user@example.com&lt;/email&gt;
            &lt;/publishRegistry&gt;
        &lt;/docker&gt;
    &lt;/configuration&gt;
&lt;/plugin&gt;

and do not want to store our credentials in pom.xml since it is available to everyone who has access to version control system. Maven way is to define profile in ~/.m2/setting.xml with our credentials and and replace credentials in pom.xml with corresponding placeholders, like:

~/.m2/settings.xml:

...
&lt;profiles&gt;
...    
    &lt;profile&gt;
        &lt;id&gt;registry-example.com&lt;/id&gt;
        &lt;properties&gt;
            &lt;registry.username&gt;user&lt;/registry.username&gt;
            &lt;registry.password&gt;secret&lt;/registry.password&gt;
            &lt;registry.url&gt;https://docker.example.com/v1/&lt;/registry.url&gt;
            &lt;registry.email&gt;user@example.com&lt;/registry.email&gt;
        &lt;/properties&gt;
    &lt;/profile&gt;
...    
&lt;/profiles&gt;
...

pom.xml:

&lt;plugin&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
    &lt;configuration&gt;
        &lt;image&gt;
            &lt;name&gt;docker.example.com/library/${project.artifactId}&lt;/name&gt;
            &lt;publish&gt;true&lt;/publish&gt;
        &lt;/image&gt;
        &lt;docker&gt;
            &lt;publishRegistry&gt;
                &lt;username&gt;${registry.username}&lt;/username&gt;
                &lt;password&gt;${registry.password}&lt;/password&gt;
                &lt;url&gt;${registry.url}&lt;/url&gt;
                &lt;email&gt;${registry.email}&lt;/email&gt;
            &lt;/publishRegistry&gt;
        &lt;/docker&gt;
    &lt;/configuration&gt;
&lt;/plugin&gt;

Now, in order to tell maven to take into account the profile we have created we need to run maven with -P flag specifying id of our profile:

mvn spring-boot:build-image -Pregistry-example.com

huangapple
  • 本文由 发表于 2023年2月6日 16:26:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定