Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Pom File option to Artifact. This adds dependency information to… #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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']
]
)
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>sp.sd</groupId>
<artifactId>nexus-artifact-uploader</artifactId>
<version>2.11-SNAPSHOT</version>
<version>2.12-SNAPSHOT</version>
<packaging>hpi</packaging>

<properties>
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/sp/sd/nexusartifactuploader/Artifact.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ public class Artifact extends AbstractDescribableImpl<Artifact> 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() {
Expand All @@ -45,6 +47,10 @@ public String getFile() {
return file;
}

public String getPomFile() {
return pomFile;
}

@Extension
public static class DescriptorImpl extends Descriptor<Artifact> {

Expand Down Expand Up @@ -77,5 +83,10 @@ public FormValidation doCheckFile(@QueryParameter String value) {
}
return FormValidation.ok();
}

public FormValidation doCheckPomFile(@QueryParameter String value) {
return FormValidation.ok();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -226,7 +245,7 @@ public boolean isApplicable(Class<? extends AbstractProject> aClass) {
}

public String getDisplayName() {
return "Nexus artifact uploader";
return "Nexus Artifact Uploader";
}

public FormValidation doCheckNexusUrl(@QueryParameter String value) {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/sp/sd/nexusartifactuploader/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand All @@ -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")) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class ArtifactJobDslContext implements Context {
String type;
String classifier;
String file;
String pomFile;

void artifactId(String artifactId) {
this.artifactId = artifactId;
Expand All @@ -28,4 +29,7 @@ void file(String file) {
this.file = file;
}

void pomFile(String pomFile) {
this.pomFile = pomFile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
type('jar')
classifier('debug')
file('nexus-artifact-uploader.jar')
pomFile('pom.xml')
}
artifact {
artifactId('nexus-artifact-uploader')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<f:entry title="File" field="file">
<f:textbox/>
</f:entry>
<f:entry title="Pom File" field="pomFile">
<f:textbox/>
</f:entry>
<f:entry>
<div align="right">
<f:repeatableDeleteButton/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>File path in the workspace. ex:<i>${WORKSPACE}/&lt;module&gt;/pom.xml</i>. 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 <code>ArtifactId</code> should match the artifactId in the root pom.xml.
The <code>Type</code> set to <i>pom</i> and the <code>File</code> (not Pom File) pointing to the root pom.xml</div>
4 changes: 2 additions & 2 deletions src/test/java/sp/sd/nexusartifactuploader/ArtifactTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
}
}