英文:
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:
<someTag>${userSettings.some.property}</someTag>
答案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
<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>
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:
...
<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>
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论