From 8d73d2a435efa5005b50d32e14dfbb3cffebb2b3 Mon Sep 17 00:00:00 2001 From: John Nicholson Date: Tue, 13 Nov 2018 22:08:09 +0000 Subject: [PATCH 1/2] Add Pom File option to Artifact. This adds dependency information to Nexus artifacts. --- README.md | 2 ++ pom.xml | 2 +- .../sp/sd/nexusartifactuploader/Artifact.java | 13 ++++++++- .../ArtifactRepositoryManager.java | 13 +++++---- .../NexusArtifactUploader.java | 27 ++++++++++++++++--- .../sp/sd/nexusartifactuploader/Utils.java | 10 ++++--- .../dsl/ArtifactJobDslContext.java | 4 +++ .../NexusArtifactUploaderJobDslContext.java | 2 +- .../NexusArtifactUploaderJobDslExtension.java | 1 + .../steps/NexusArtifactUploaderStep.java | 27 ++++++++++++++++--- .../Artifact/config.jelly | 3 +++ .../Artifact/help-pomFile.html | 3 +++ .../nexusartifactuploader/ArtifactTest.java | 4 +-- 13 files changed, 91 insertions(+), 20 deletions(-) create mode 100644 src/main/resources/sp/sd/nexusartifactuploader/Artifact/help-pomFile.html diff --git a/README.md b/README.md index ec0b4bc..574ac99 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ Uploading snapshots is not supported by this plugin. type('jar') classifier('debug') file('nexus-artifact-uploader.jar') + pomFile('pom.xml') } artifact { artifactId('nexus-artifact-uploader') @@ -48,6 +49,7 @@ Uploading snapshots is not supported by this plugin. [artifactId: projectName, classifier: '', file: 'my-service-' + version + '.jar', + pomFile: 'pom.xml' type: 'jar'] ] ) diff --git a/pom.xml b/pom.xml index 1400bd7..bd6d456 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ sp.sd nexus-artifact-uploader - 2.11-SNAPSHOT + 2.12-SNAPSHOT hpi diff --git a/src/main/java/sp/sd/nexusartifactuploader/Artifact.java b/src/main/java/sp/sd/nexusartifactuploader/Artifact.java index 859597c..5fcd696 100644 --- a/src/main/java/sp/sd/nexusartifactuploader/Artifact.java +++ b/src/main/java/sp/sd/nexusartifactuploader/Artifact.java @@ -20,13 +20,15 @@ public class Artifact extends AbstractDescribableImpl implements Seria private final String type; private final String classifier; private final String file; + private final String pomFile; @DataBoundConstructor - public Artifact(String artifactId, String type, String classifier, String file) { + public Artifact(String artifactId, String type, String classifier, String file, String pomFile) { this.artifactId = artifactId; this.type = type; this.classifier = classifier; this.file = file != null ? file.trim() : null; + this.pomFile = pomFile != null ? pomFile.trim() : null; } public String getArtifactId() { @@ -45,6 +47,10 @@ public String getFile() { return file; } + public String getPomFile() { + return pomFile; + } + @Extension public static class DescriptorImpl extends Descriptor { @@ -77,5 +83,10 @@ public FormValidation doCheckFile(@QueryParameter String value) { } return FormValidation.ok(); } + + public FormValidation doCheckPomFile(@QueryParameter String value) { + return FormValidation.ok(); + } + } } diff --git a/src/main/java/sp/sd/nexusartifactuploader/ArtifactRepositoryManager.java b/src/main/java/sp/sd/nexusartifactuploader/ArtifactRepositoryManager.java index dcebde1..d6c17d7 100644 --- a/src/main/java/sp/sd/nexusartifactuploader/ArtifactRepositoryManager.java +++ b/src/main/java/sp/sd/nexusartifactuploader/ArtifactRepositoryManager.java @@ -52,14 +52,17 @@ private RemoteRepository makeRemoteRepository() { .setAuthentication(new Authentication(this.username, this.password)); } - public void upload(String groupId, String artifactId, String version, - File artifactFile, String type, String classifier) - throws Exception { + public void upload(String groupId, String artifactId, String version, File artifactFile, String type, String classifier, File pomFile) throws Exception { + RemoteRepository remoteRepository = makeRemoteRepository(); - Artifact artifact = new DefaultArtifact(groupId, artifactId, classifier, - type, version).setFile(artifactFile); + Artifact artifact = new DefaultArtifact(groupId, artifactId, classifier, type, version).setFile(artifactFile); DeployRequest deployRequest = new DeployRequest().addArtifact(artifact); + if (pomFile != null) { + Artifact pomArtifact = new DefaultArtifact(groupId, artifactId, classifier, "pom", version).setFile(pomFile); + deployRequest.addArtifact(pomArtifact); + } + deployRequest.setRepository(remoteRepository); final SettingsBuildingRequest request = new DefaultSettingsBuildingRequest() diff --git a/src/main/java/sp/sd/nexusartifactuploader/NexusArtifactUploader.java b/src/main/java/sp/sd/nexusartifactuploader/NexusArtifactUploader.java index 9837069..faabc26 100644 --- a/src/main/java/sp/sd/nexusartifactuploader/NexusArtifactUploader.java +++ b/src/main/java/sp/sd/nexusartifactuploader/NexusArtifactUploader.java @@ -147,6 +147,16 @@ public void perform(Run build, FilePath workspace, Launcher launcher, TaskListen listener.getLogger().println(artifactFilePath.getName() + " file doesn't exists"); throw new IOException(artifactFilePath.getName() + " file doesn't exists"); } else { + + FilePath artifactPomFilePath = null; + if (artifact.getPomFile() != null && artifact.getPomFile().trim().length() > 0) { + artifactPomFilePath = new FilePath(workspace, build.getEnvironment(listener).expand(artifact.getPomFile())); + if (!artifactPomFilePath.exists()) { + listener.getLogger().println(artifactPomFilePath.getName() + " pom file doesn't exists"); + throw new IOException(artifactPomFilePath.getName() + " pom file doesn't exists"); + } + } + result = artifactFilePath.act(new ArtifactFileCallable(listener, this.getUsername(envVars, project), this.getPassword(envVars, project), @@ -158,7 +168,8 @@ public void perform(Run build, FilePath workspace, Launcher launcher, TaskListen envVars.expand(artifact.getType()), envVars.expand(artifact.getClassifier()), protocol, - nexusVersion + nexusVersion, + artifactPomFilePath )); } if (!result) { @@ -181,11 +192,12 @@ private static final class ArtifactFileCallable extends MasterToSlaveFileCallabl private final String resolvedClassifier; private final String resolvedProtocol; private final String resolvedNexusVersion; + private final FilePath artifactPomFilePath; public ArtifactFileCallable(TaskListener Listener, String ResolvedNexusUser, String ResolvedNexusPassword, String ResolvedNexusUrl, String ResolvedGroupId, String ResolvedArtifactId, String ResolvedVersion, String ResolvedRepository, String ResolvedType, String ResolvedClassifier, - String ResolvedProtocol, String ResolvedNexusVersion) { + String ResolvedProtocol, String ResolvedNexusVersion, FilePath artifactPomFilePath) { this.listener = Listener; this.resolvedNexusUser = ResolvedNexusUser; this.resolvedNexusPassword = ResolvedNexusPassword; @@ -198,13 +210,20 @@ public ArtifactFileCallable(TaskListener Listener, String ResolvedNexusUser, Str this.resolvedClassifier = ResolvedClassifier; this.resolvedProtocol = ResolvedProtocol; this.resolvedNexusVersion = ResolvedNexusVersion; + this.artifactPomFilePath = artifactPomFilePath; } @Override public Boolean invoke(File artifactFile, VirtualChannel channel) throws IOException { + + File pomFile = null; + if (artifactPomFilePath != null && artifactPomFilePath.getRemote() != null) { + pomFile = new File(artifactPomFilePath.getRemote()); + } + return Utils.uploadArtifact(artifactFile, listener, resolvedNexusUser, resolvedNexusPassword, resolvedNexusUrl, resolvedGroupId, resolvedArtifactId, resolvedVersion, resolvedRepository, resolvedType, resolvedClassifier, - resolvedProtocol, resolvedNexusVersion); + resolvedProtocol, resolvedNexusVersion, pomFile); } @Override @@ -226,7 +245,7 @@ public boolean isApplicable(Class aClass) { } public String getDisplayName() { - return "Nexus artifact uploader"; + return "Nexus Artifact Uploader"; } public FormValidation doCheckNexusUrl(@QueryParameter String value) { diff --git a/src/main/java/sp/sd/nexusartifactuploader/Utils.java b/src/main/java/sp/sd/nexusartifactuploader/Utils.java index aac5919..6634671 100644 --- a/src/main/java/sp/sd/nexusartifactuploader/Utils.java +++ b/src/main/java/sp/sd/nexusartifactuploader/Utils.java @@ -19,7 +19,7 @@ public static Boolean uploadArtifact(File artifactFile, TaskListener Listener, S String ResolvedGroupId, String ResolvedArtifactId, String ResolvedVersion, String ResolvedRepository, String ResolvedType, String ResolvedClassifier, String ResolvedProtocol, - String ResolvedNexusVersion) throws IOException { + String ResolvedNexusVersion, File pomFile) throws IOException { Boolean result = false; if (Strings.isNullOrEmpty(ResolvedNexusUrl)) { Listener.getLogger().println("Url of the Nexus is empty. Please enter Nexus Url."); @@ -33,6 +33,9 @@ public static Boolean uploadArtifact(File artifactFile, TaskListener Listener, S Listener.getLogger().println("Type: " + ResolvedType); Listener.getLogger().println("Version: " + ResolvedVersion); Listener.getLogger().println("File: " + artifactFile.getName()); + if (pomFile != null) { + Listener.getLogger().println("Pom file: " + pomFile.getName()); + } Listener.getLogger().println("Repository:" + ResolvedRepository); String repositoryPath = "/content/repositories/"; if (ResolvedNexusVersion.contentEquals("nexus3")) { @@ -41,8 +44,9 @@ public static Boolean uploadArtifact(File artifactFile, TaskListener Listener, S ArtifactRepositoryManager artifactRepositoryManager = new ArtifactRepositoryManager(ResolvedProtocol + "://" + ResolvedNexusUrl + repositoryPath + ResolvedRepository, ResolvedNexusUser, ResolvedNexusPassword, ResolvedRepository, Listener); - artifactRepositoryManager.upload(ResolvedGroupId, ResolvedArtifactId, ResolvedVersion, - artifactFile, ResolvedType, ResolvedClassifier); + + artifactRepositoryManager.upload(ResolvedGroupId, ResolvedArtifactId, ResolvedVersion, artifactFile, ResolvedType, ResolvedClassifier, pomFile); + Listener.getLogger().println("Uploading artifact " + artifactFile.getName() + " completed."); result = true; } catch (Exception e) { diff --git a/src/main/java/sp/sd/nexusartifactuploader/dsl/ArtifactJobDslContext.java b/src/main/java/sp/sd/nexusartifactuploader/dsl/ArtifactJobDslContext.java index dbc26b0..52984c4 100644 --- a/src/main/java/sp/sd/nexusartifactuploader/dsl/ArtifactJobDslContext.java +++ b/src/main/java/sp/sd/nexusartifactuploader/dsl/ArtifactJobDslContext.java @@ -11,6 +11,7 @@ public class ArtifactJobDslContext implements Context { String type; String classifier; String file; + String pomFile; void artifactId(String artifactId) { this.artifactId = artifactId; @@ -28,4 +29,7 @@ void file(String file) { this.file = file; } + public String pomFile() { + return pomFile; + } } diff --git a/src/main/java/sp/sd/nexusartifactuploader/dsl/NexusArtifactUploaderJobDslContext.java b/src/main/java/sp/sd/nexusartifactuploader/dsl/NexusArtifactUploaderJobDslContext.java index 1ee12eb..792ef02 100644 --- a/src/main/java/sp/sd/nexusartifactuploader/dsl/NexusArtifactUploaderJobDslContext.java +++ b/src/main/java/sp/sd/nexusartifactuploader/dsl/NexusArtifactUploaderJobDslContext.java @@ -55,7 +55,7 @@ void credentialsId(String credentialsId) { void artifact(@DslContext(ArtifactJobDslContext.class) Closure artifactClosure) { ArtifactJobDslContext context = new ArtifactJobDslContext(); executeInContext(artifactClosure, context); - Artifact artifact = new Artifact(context.artifactId, context.type, context.classifier, context.file); + Artifact artifact = new Artifact(context.artifactId, context.type, context.classifier, context.file, context.pomFile); artifactList.add(artifact); } } diff --git a/src/main/java/sp/sd/nexusartifactuploader/dsl/NexusArtifactUploaderJobDslExtension.java b/src/main/java/sp/sd/nexusartifactuploader/dsl/NexusArtifactUploaderJobDslExtension.java index c320fd6..9f8261d 100644 --- a/src/main/java/sp/sd/nexusartifactuploader/dsl/NexusArtifactUploaderJobDslExtension.java +++ b/src/main/java/sp/sd/nexusartifactuploader/dsl/NexusArtifactUploaderJobDslExtension.java @@ -30,6 +30,7 @@ type('jar') classifier('debug') file('nexus-artifact-uploader.jar') + pomFile('pom.xml') } artifact { artifactId('nexus-artifact-uploader') diff --git a/src/main/java/sp/sd/nexusartifactuploader/steps/NexusArtifactUploaderStep.java b/src/main/java/sp/sd/nexusartifactuploader/steps/NexusArtifactUploaderStep.java index 8e0ae87..6fa73e0 100644 --- a/src/main/java/sp/sd/nexusartifactuploader/steps/NexusArtifactUploaderStep.java +++ b/src/main/java/sp/sd/nexusartifactuploader/steps/NexusArtifactUploaderStep.java @@ -236,11 +236,22 @@ protected Boolean run() throws Exception { Item project = build.getParent(); EnvVars envVars = build.getEnvironment(listener); for (Artifact artifact : step.artifacts) { + FilePath artifactFilePath = new FilePath(ws, build.getEnvironment(listener).expand(artifact.getFile())); if (!artifactFilePath.exists()) { listener.getLogger().println(artifactFilePath.getName() + " file doesn't exists"); throw new IOException(artifactFilePath.getName() + " file doesn't exists"); } else { + + FilePath artifactPomFilePath = null; + if (artifact.getPomFile() != null && artifact.getPomFile().trim().length() > 0) { + artifactPomFilePath = new FilePath(ws, build.getEnvironment(listener).expand(artifact.getPomFile())); + if (!artifactPomFilePath.exists()) { + listener.getLogger().println(artifactPomFilePath.getName() + " pom file doesn't exists"); + throw new IOException(artifactPomFilePath.getName() + " pom file doesn't exists"); + } + } + result = artifactFilePath.act(new ArtifactFileCallable(listener, step.getUsername(envVars, project), step.getPassword(envVars, project), @@ -252,7 +263,8 @@ protected Boolean run() throws Exception { envVars.expand(artifact.getType()), envVars.expand(artifact.getClassifier()), step.getProtocol(), - step.getNexusVersion() + step.getNexusVersion(), + artifactPomFilePath )); } if (!result) { @@ -279,12 +291,14 @@ private static final class ArtifactFileCallable extends MasterToSlaveFileCallabl private final String resolvedClassifier; private final String resolvedProtocol; private final String resolvedNexusVersion; + private final FilePath artifactPomFilePath; public ArtifactFileCallable(TaskListener Listener, String ResolvedNexusUser, String ResolvedNexusPassword, String ResolvedNexusUrl, String ResolvedGroupId, String ResolvedArtifactId, String ResolvedVersion, String ResolvedRepository, String ResolvedType, String ResolvedClassifier, - String ResolvedProtocol, String ResolvedNexusVersion) { + String ResolvedProtocol, String ResolvedNexusVersion, + FilePath artifactPomFilePath) { this.listener = Listener; this.resolvedNexusUser = ResolvedNexusUser; this.resolvedNexusPassword = ResolvedNexusPassword; @@ -297,13 +311,20 @@ public ArtifactFileCallable(TaskListener Listener, String ResolvedNexusUser, Str this.resolvedClassifier = ResolvedClassifier; this.resolvedProtocol = ResolvedProtocol; this.resolvedNexusVersion = ResolvedNexusVersion; + this.artifactPomFilePath = artifactPomFilePath; } @Override public Boolean invoke(File artifactFile, VirtualChannel channel) throws IOException { + + File pomFile = null; + if (artifactPomFilePath.getRemote() != null) { + pomFile = new File(artifactPomFilePath.getRemote()); + } + return Utils.uploadArtifact(artifactFile, listener, resolvedNexusUser, resolvedNexusPassword, resolvedNexusUrl, resolvedGroupId, resolvedArtifactId, resolvedVersion, resolvedRepository, - resolvedType, resolvedClassifier, resolvedProtocol, resolvedNexusVersion); + resolvedType, resolvedClassifier, resolvedProtocol, resolvedNexusVersion, pomFile); } @Override diff --git a/src/main/resources/sp/sd/nexusartifactuploader/Artifact/config.jelly b/src/main/resources/sp/sd/nexusartifactuploader/Artifact/config.jelly index 92333df..4326b40 100644 --- a/src/main/resources/sp/sd/nexusartifactuploader/Artifact/config.jelly +++ b/src/main/resources/sp/sd/nexusartifactuploader/Artifact/config.jelly @@ -14,6 +14,9 @@ + + +
diff --git a/src/main/resources/sp/sd/nexusartifactuploader/Artifact/help-pomFile.html b/src/main/resources/sp/sd/nexusartifactuploader/Artifact/help-pomFile.html new file mode 100644 index 0000000..e4f527e --- /dev/null +++ b/src/main/resources/sp/sd/nexusartifactuploader/Artifact/help-pomFile.html @@ -0,0 +1,3 @@ +
File path in the workspace. ex:${WORKSPACE}/<module>/pom.xml. Adding the pom file uploads the pom.xml to Nexus thereby given dependency information when the artifact is used. + In multi-module maven projects, if variables are used for version numbers then you should create a separate artifact. This artifact's ArtifactId should match the artifactId in the root pom.xml. + The Type set to pom and the File (not Pom File) pointing to the root pom.xml
\ No newline at end of file diff --git a/src/test/java/sp/sd/nexusartifactuploader/ArtifactTest.java b/src/test/java/sp/sd/nexusartifactuploader/ArtifactTest.java index 9fd93f2..ade3fef 100644 --- a/src/test/java/sp/sd/nexusartifactuploader/ArtifactTest.java +++ b/src/test/java/sp/sd/nexusartifactuploader/ArtifactTest.java @@ -15,7 +15,7 @@ public class ArtifactTest { @WithoutJenkins public void testDefaults() { Artifact artifact = new Artifact("nexus-artifact-uploader", "jpi", "debug", - "target/nexus-artifact-uploader.jpi"); + "target/nexus-artifact-uploader.jpi", "pom.xml"); assertEquals("nexus-artifact-uploader", artifact.getArtifactId()); assertEquals("jpi", artifact.getType()); assertEquals("debug", artifact.getClassifier()); @@ -26,7 +26,7 @@ public void testDefaults() { @WithoutJenkins public void testFileNameTrimming() { Artifact artifact = new Artifact("nexus-artifact-uploader", "jpi", "debug", - "target/nexus-artifact-uploader.jpi "); + "target/nexus-artifact-uploader.jpi ", "pom.xml"); assertEquals("target/nexus-artifact-uploader.jpi", artifact.getFile()); } } \ No newline at end of file From ea0858820ff05eb3f57bb407ccce07679783b324 Mon Sep 17 00:00:00 2001 From: John Nicholson Date: Tue, 13 Nov 2018 22:32:42 +0000 Subject: [PATCH 2/2] Fix to find bugs --- .../sd/nexusartifactuploader/dsl/ArtifactJobDslContext.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/sp/sd/nexusartifactuploader/dsl/ArtifactJobDslContext.java b/src/main/java/sp/sd/nexusartifactuploader/dsl/ArtifactJobDslContext.java index 52984c4..55ff245 100644 --- a/src/main/java/sp/sd/nexusartifactuploader/dsl/ArtifactJobDslContext.java +++ b/src/main/java/sp/sd/nexusartifactuploader/dsl/ArtifactJobDslContext.java @@ -29,7 +29,7 @@ void file(String file) { this.file = file; } - public String pomFile() { - return pomFile; + void pomFile(String pomFile) { + this.pomFile = pomFile; } }