问题
I have a multi-module maven project running in Jenkins. I would like to deploy the final artifact (an RPM from an assembly build) to the Nexus server. I see no reason to deploy intermediate artifacts (hence no "mvn clean deploy") since this will produce extra junk on the server that I don't need. We're trying to set up a continuous delivery pipeline, so we're not deploying SNAPSHOT versions - ever. The various plugins for Jenkins seem focused on deploying all of the artifacts. How can I just deploy the one I choose?
EDIT:
After much consideration, I'd be willing to deploy all the artifacts to nexus if the deploy happens after all of the build has completed. If I was going that route, I'd want to use the "Deploy artifacts to maven repository" post build action. This tool seems broken though because it's missing the functionality of specifying the username/password. I know this can be specified in the settings.xml, but apparently only the one in ".m2". It doesn't seem to be looking at the settings.xml I specified for this build.
This also seems like it's broken for redeploying artifacts. There is some verbage in "Jenkins the Definitive Guide" for this, but it talks about a "local" settings.xml. All my builds are happening on Jenkins slaves, so this isn't an option and doesn't even really make sense with the Jenkins architecture.
回答1:
If you need still generic way.
Use Execute shell option and use the mvn deploy command manually, You can pass your version and others things such as groupID etc as parameters in job, If you maintain separate job to build and upload this will work out.
Ex:
export M2_HOME=/PATH/TO/softwares/apache-maven-3.0.4 PATH=$M2_HOME/bin:$JAVA_HOME/bin:$PATH export PATH
mvn -v
mvn deploy:deploy-file -Durl=http://someorg:8081/nexus/content/repositories/t1.snapshot/ -DrepositoryId=t1.snapshot -DartifactId=artifactID -DgroupId=groupID -Dpackaging=zip -Dfile=${WORKSPACE}/filename.zip -Dversion=1.0-TEST-SNAPSHOT -s "/path/to/.m2/settings.xml"
回答2:
You can make use of 'Deploy artifacts to Maven Repository' under 'Post-Build Actions'. Take look at this answer
回答3:
Here's the ugly hack I came up with. I'll gladly give someone else the "correct" answer for this if anyone has a better idea:
I realized that I need to deploy both the parent pom.xml and the assembly. I did this in two separate post build steps.
First, I chose "Invoke top-level Maven targets" with a Maven Version of "Maven" (I think this uses Jenkins version of maven. I don't want to put a different version on the system). I used Goals of:
-s svn-admin/settings.xml -N deploy
That deploys just the parent pom to nexus with my specified settings.xml.
The REALLY big hack happens when I want to deploy the rpm. I tried a "deploy-file" target, but without a variable I could expand to the version number, I couldn't specify the exact file and wildcards don't expand. Instead I did an "Execute shell" option and used curl I found here:
env
UPLOAD_FILE=assembly/target/ips-${POM_VERSION}-x.x86_64.rpm
DESTINATION=http://mvnrepo01/nexus/content/repositories/releases/com/bla/ips/assembly/${POM_VERSION}/assembly-${POM_VERSION}.rpm
sha1sum ${UPLOAD_FILE} | awk -F" " '{print $1}' | curl -v -u admin:password --upload-file - ${DESTINATION}.sha1
md5sum ${UPLOAD_FILE} | awk -F" " '{print $1}' | curl -v -u admin:password --upload-file - ${DESTINATION}.md5
curl -v -u admin:password --upload-file ${UPLOAD_FILE} ${DESTINATION}
UPLOAD_FILE=assembly/pom.xml
DESTINATION=http://mvnrepo01/nexus/content/repositories/releases/com/bla/ips/assembly/${POM_VERSION}/assembly-${POM_VERSION}.pom
sha1sum ${UPLOAD_FILE} | awk -F" " '{print $1}' | curl -v -u admin:password --upload-file - ${DESTINATION}.sha1
md5sum ${UPLOAD_FILE} | awk -F" " '{print $1}' | curl -v -u admin:password --upload-file - ${DESTINATION}.md5
curl -v -u admin:password --upload-file ${UPLOAD_FILE} ${DESTINATION}
Like i said, this is an ugly hack. I'm pretty sure there are metadata files that aren't getting updated, but the rpm, it's pom, and their checksums are getting uploaded.
回答4:
You can use Nexus Jenkins Plugin to deploy a specific artifact from Jenkins into Nexus: https://support.sonatype.com/hc/en-us/articles/227256688-How-do-I-configure-the-Nexus-Jenkins-Plugin
Example of Jenkins pipeline:
stage('Publish') {
def pom = readMavenPom file: 'pom.xml'
nexusPublisher nexusInstanceId: 'your-nexus-instance-id', \
nexusRepositoryId: 'your-nexus-repository-id', \
packages: [[$class: 'MavenPackage', \
mavenAssetList: [[classifier: '', extension: '', filePath: "target/${pom.artifactId}-${pom.version}.${pom.packaging}"]], \
mavenCoordinate: [artifactId: "${pom.artifactId}", \
groupId: "${pom.groupId}", \
packaging: "${pom.packaging}", \
version: "${pom.version}"]]]
}
In this case Nexus Jenkins Plugin will deploy only your target/${pom.artifactId}-${pom.version}.${pom.packaging}
and pom.xml
files to Nexus repository.
来源:https://stackoverflow.com/questions/24069350/how-can-you-deploy-a-specific-artifact-from-jenkins-into-nexus