我有一个 Maven 项目,想要使用 mvn clean deploy
来构建它,以便将构建的 Artifact 部署到 Nexus 存储库中。
该存储库的访问数据(用户名和密码)存储在 Jenkins 凭据中。
我想在 Jenkins 中调用 mvn deploy
,以便从 Jenkins 读取该 Nexus 存储库的凭据(不在 settings.xml
中硬编码)。
如果我无法访问 Jenkins 服务器上的 settings.xml
,我该怎么办?
更新 1:
我在“配置文件管理”(JENKINS_URL/configfiles/index
) 中创建了一个条目,其中包含以下数据:
类型:Maven settings.xml
全部替换:是
服务器 ID:myServer
凭据:Nexus 存储库的凭据
内容:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>myServer</id>
<username>foo</username>
<password>bar</password>
</server>
</servers>
</settings>
myServer
也用在我想要构建的 Artifact 的 pom.xml
中:
<distributionManagement>
<repository>
<id>myServer</id>
<url>http://nexus.mycompany.com</url>
</repository>
</distributionManagement>
在作业的配置中,我包含了这些设置,如下所示。 Nexus settings.xml
是“配置文件管理”中的配置。
但它不起作用——当构建尝试将 Artifact 部署到 Nexus 时,我收到“Forbidden”错误。
更新 2: 当我使用与 Jenkins 中相同的凭据(存储在本地 settings.xml
中)在本地运行 mvn -X deploy
时),我看到以下输出:
[DEBUG] Failed to decrypt password for server XXX release repository: org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException: java.io.FileNotFoundException: /XXXXXXXXXXXXXXX/.m2/settings-security.xml (No such file or directory)
org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException: org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException: java.io.FileNotFoundException: /XXXXXXXXXXXXXXX/.m2/settings-security.xml (No such file or directory)
at org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher.decrypt(DefaultSecDispatcher.java:121)
at org.apache.maven.settings.crypto.DefaultSettingsDecrypter.decrypt(DefaultSettingsDecrypter.java:107)
at org.apache.maven.settings.crypto.DefaultSettingsDecrypter.decrypt(DefaultSettingsDecrypter.java:63)
at org.apache.maven.internal.aether.DefaultRepositorySystemSessionFactory.newRepositorySession(DefaultRepositorySystemSessionFactory.java:165)
但是,settings.xml
中的密码根本没有加密:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository/>
<interactiveMode/>
<offline/>
<pluginGroups/>
<servers>
<server>
<id>NEXUS_REPOSITORY_SNAPSHOTS</id>
<username>user</username>
<password>password</password>
</server>
</servers>
<mirrors/>
<proxies/>
<profiles/>
<activeProfiles/>
</settings>
我发现了一个关于类似错误的报告 here .
最佳答案
您可以使用 Jenkins config-file-provider 插件(链接: https://plugins.jenkins.io/config-file-provider/ )创建一个或多个 Maven settings.xml 文件。
NOTE: Really this helps also if you need differents settings.xml for different projects.
然后在 Jenkins 项目的 Maven 部署步骤中,您可以选择指向定义的 settings.xml 文件之一(而不是指向常规 Jenkins 服务器/.m2/settings.xml )
关于java - 运行 `mvn deploy` 时如何使用 Jenkins 凭据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60303506/