配置Jenkins向Artifactory发布Maven制品的后续步骤及POM修改咨询
Hey there! Let's break down exactly what you need to finish setting up your Jenkins build to publish artifacts to Artifactory, including whether you need to tweak your POM.xml.
1. First, Lock Down Artifactory Credentials in Jenkins
You’ve added the repo URL, but Jenkins needs permission to push artifacts to Artifactory. Here’s how to set that up:
- Head to Manage Jenkins > Manage Credentials (the "System" domain usually works best for shared credentials).
- Add a new Username with password credential: use your Artifactory username and either your password or API key (API keys are more secure, so prefer that if you can). Give it a memorable ID like
artifactory-deploy-credsso you can reference it later. - Go back to your Maven build task, find the Artifactory plugin configuration section, and link this credential to your already-configured Artifactory server URL.
2. Configure the Jenkins Build Step to Publish
Don’t use the regular "Invoke top-level Maven targets" step—instead, use the Invoke Artifactory Maven 3 step that comes with the plugin. Here’s what to set:
- In the "Goals and options" field, enter your build + publish command:
clean install deploy(thedeploygoal is critical here—it tells Maven to push artifacts to the repository). - Switch to the Artifactory Deployment tab:
- Check the box for "Deploy artifacts to Artifactory".
- Select your Target Repository: pick your snapshot repo if you’re publishing SNAPSHOT versions, or your release repo for stable builds.
- Optional but recommended: Set a Build Name and Build Number (you can use Jenkins variables like
${JOB_NAME}and${BUILD_NUMBER}) so Artifactory tracks which build each artifact came from.
3. Yes, You Do Need to Modify Your POM.xml
Maven needs to know where to send artifacts, so add these sections to your POM:
Distribution Management (Required for Publishing)
This tells Maven exactly which Artifactory repos to deploy to:
<distributionManagement> <repository> <id>artifactory-release-repo</id> <url>https://your-artifactory-url/libs-release-local</url> </repository> <snapshotRepository> <id>artifactory-snapshot-repo</id> <url>https://your-artifactory-url/libs-snapshot-local</url> </snapshotRepository> </distributionManagement>
Pro tip: The
<id>values here need to match the ID of the credential you created in Jenkins (or the server ID in your Maven settings.xml if you use that instead).
Repositories (For Pulling Dependencies)
If your project relies on private artifacts stored in Artifactory, add this to pull them during the build:
<repositories> <repository> <id>artifactory-release-repo</id> <url>https://your-artifactory-url/libs-release</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>false</enabled></snapshots> </repository> <repository> <id>artifactory-snapshot-repo</id> <url>https://your-artifactory-url/libs-snapshot</url> <releases><enabled>false</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </repository> </repositories>
4. Optional: Use Maven Settings.xml Instead (If You Prefer)
If you don’t want to hardcode repo URLs in your POM (good for open-source projects or shared POMs), you can configure a Maven settings.xml in Jenkins:
- Go to Manage Jenkins > Global Tool Configuration, find your Maven installation, and add a custom settings.xml.
- Inside the settings, add
<servers>(matching your Artifactory credentials) and<mirrors>pointing to Artifactory. This way, your POM doesn’t need thedistributionManagementsection—Maven will use the settings instead.
5. Test It Out!
Trigger a manual build in Jenkins and watch the console output. Look for lines like "Deploying artifact to Artifactory" to confirm it’s working. Then log into Artifactory, navigate to your target repo, and check that your artifacts (JARs, WARs, POMs) are there. You should also see the build listed in Artifactory’s Builds section if you configured the build name/number.
内容的提问来源于stack exchange,提问作者MK dev

