Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #142 from aheritier/JENKINS-9786
JENKINS-9786 : Jenkins can read repositories definitions from Maven POMs
  • Loading branch information
ndeloof committed May 31, 2011
2 parents a0fb3a8 + fe30ad8 commit 1a7001b
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 114 deletions.
3 changes: 3 additions & 0 deletions changelog.html
Expand Up @@ -65,6 +65,9 @@
<li class=rfe>
Improve the post deployment job to make a clear error if you disabled artifacts archives
(<a href="http://issues.jenkins-ci.org/browse/JENKINS-9791">issue 9791</a>)
<li class=rfe>
Post-build deploy task for Maven jobs : Repositories definitions can now be read from the POMs.
(<a href="http://issues.jenkins-ci.org/browse/JENKINS-9786">issue 9786</a>)
</ul>
</div><!--=TRUNK-END=-->

Expand Down
97 changes: 46 additions & 51 deletions maven-plugin/src/main/java/hudson/maven/RedeployPublisher.java
Expand Up @@ -28,31 +28,15 @@
import hudson.Launcher;
import hudson.Util;
import hudson.maven.reporters.MavenAbstractArtifactRecord;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Hudson;
import hudson.model.Node;
import hudson.model.Result;
import hudson.model.TaskListener;
import hudson.maven.reporters.MavenArtifactRecord;
import hudson.model.*;
import hudson.remoting.Callable;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Maven.MavenInstallation;
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;
import hudson.util.FormValidation;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.deployer.ArtifactDeploymentException;
Expand All @@ -65,9 +49,13 @@
import org.apache.maven.repository.Proxy;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.Map.Entry;

/**
* {@link Publisher} for {@link MavenModuleSetBuild} to deploy artifacts
* after a build is fully succeeded.
Expand All @@ -90,6 +78,7 @@ public class RedeployPublisher extends Recorder {
/**
* For backward compatibility
*/
@Deprecated
public RedeployPublisher(String id, String url, boolean uniqueVersion) {
this(id, url, uniqueVersion, false);
}
Expand All @@ -105,47 +94,52 @@ public RedeployPublisher(String id, String url, boolean uniqueVersion, boolean e
this.evenIfUnstable = evenIfUnstable;
}

public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
if(build.getResult().isWorseThan(getTreshold()))
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
if (build.getResult().isWorseThan(getTreshold()))
return true; // build failed. Don't publish

if (url==null) {
listener.getLogger().println("No Repository URL is specified.");
List<MavenAbstractArtifactRecord> mavenAbstractArtifactRecords = getActions(build, listener);
if (mavenAbstractArtifactRecords == null || mavenAbstractArtifactRecords.isEmpty()) {
listener.getLogger().println("[ERROR] No artifacts are recorded. Is this a Maven project?");
build.setResult(Result.FAILURE);
return true;
}

List<MavenAbstractArtifactRecord> mars = getActions( build, listener );
if(mars==null || mars.isEmpty()) {
listener.getLogger().println("No artifacts are recorded. Is this a Maven project?");
build.setResult(Result.FAILURE);
return true;
}

if(build instanceof MavenModuleSetBuild && ((MavenModuleSetBuild)build).getParent().isArchivingDisabled()){
listener.getLogger().println("[ERROR] You cannot use the \"Deploy artifacts to Maven repository\" feature if you " +
"disabled " +
"automatic artifact archiving");
"disabled automatic artifact archiving");
build.setResult(Result.FAILURE);
return true;
}

listener.getLogger().println("Deploying artifacts to "+url);
long startupTime = Calendar.getInstance().getTimeInMillis();

try {

//MavenEmbedder embedder = MavenUtil.createEmbedder(listener,build);
MavenEmbedder embedder = createEmbedder(listener,build);
MavenEmbedder embedder = createEmbedder(listener, build);
ArtifactRepositoryLayout layout =
(ArtifactRepositoryLayout) embedder.lookup( ArtifactRepositoryLayout.ROLE,"default");
(ArtifactRepositoryLayout) embedder.lookup(ArtifactRepositoryLayout.ROLE, "default");
ArtifactRepositoryFactory factory =
(ArtifactRepositoryFactory) embedder.lookup(ArtifactRepositoryFactory.ROLE);

final ArtifactRepository repository = factory.createDeploymentArtifactRepository(
id, url, layout, uniqueVersion);
WrappedArtifactRepository repo = new WrappedArtifactRepository(repository,uniqueVersion);
for (MavenAbstractArtifactRecord mar : mars)
mar.deploy(embedder,repo,listener);

(ArtifactRepositoryFactory) embedder.lookup(ArtifactRepositoryFactory.ROLE);
ArtifactRepository artifactRepository = null;
if (url != null) {
// By default we try to get the repository definition from the job configuration
artifactRepository = getDeploymentRepository(factory, layout, id, url);
}
for (MavenAbstractArtifactRecord mavenAbstractArtifactRecord : mavenAbstractArtifactRecords) {
if (artifactRepository == null && mavenAbstractArtifactRecord instanceof MavenArtifactRecord) {
// If no repository definition is set on the job level we try to take it from the POM
MavenArtifactRecord mavenArtifactRecord = (MavenArtifactRecord) mavenAbstractArtifactRecord;
artifactRepository = getDeploymentRepository(factory, layout, mavenArtifactRecord.repositoryId, mavenArtifactRecord.repositoryUrl);
}
if (artifactRepository == null) {
listener.getLogger().println("[ERROR] No Repository settings defined in the job configuration or distributionManagement of the module.");
build.setResult(Result.FAILURE);
return true;
}
mavenAbstractArtifactRecord.deploy(embedder, artifactRepository, listener);
}
listener.getLogger().println("[INFO] Deployment done in " + Util.getTimeSpanString(Calendar.getInstance().getTimeInMillis() - startupTime));
return true;
} catch (MavenEmbedderException e) {
e.printStackTrace(listener.error(e.getMessage()));
Expand All @@ -156,9 +150,17 @@ public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListene
}
// failed
build.setResult(Result.FAILURE);
listener.getLogger().println("[INFO] Deployment failed after " + Util.getTimeSpanString(Calendar.getInstance().getTimeInMillis() - startupTime));
return true;
}

private ArtifactRepository getDeploymentRepository(ArtifactRepositoryFactory factory, ArtifactRepositoryLayout layout, String repositoryId, String repositoryUrl) throws ComponentLookupException {
if (repositoryUrl == null) return null;
final ArtifactRepository repository = factory.createDeploymentArtifactRepository(
repositoryId, repositoryUrl, layout, uniqueVersion);
return new WrappedArtifactRepository(repository, uniqueVersion);
}

/**
*
* copy from MavenUtil but here we have to ignore localRepo path and setting as thoses paths comes
Expand Down Expand Up @@ -312,13 +314,6 @@ public boolean showEvenIfUnstableOption() {
return true;
}

public FormValidation doCheckUrl(@QueryParameter String url) {
String fixedUrl = hudson.Util.fixEmptyAndTrim(url);
if (fixedUrl==null)
return FormValidation.error(Messages.RedeployPublisher_RepositoryURL_Mandatory());

return FormValidation.ok();
}
}

//---------------------------------------------
Expand Down
Expand Up @@ -23,29 +23,24 @@
*/
package hudson.maven.reporters;

import hudson.maven.MavenBuildProxy;
import hudson.maven.MavenModule;
import hudson.maven.MavenReporter;
import hudson.maven.MavenReporterDescriptor;
import hudson.maven.MojoInfo;
import hudson.maven.MavenBuild;
import hudson.model.BuildListener;
import hudson.util.InvocationInterceptor;
import hudson.FilePath;
import hudson.Extension;
import hudson.FilePath;
import hudson.Util;
import hudson.maven.*;
import hudson.model.BuildListener;
import hudson.util.InvocationInterceptor;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.project.MavenProject;

import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationHandler;

/**
* Archives artifacts of the build.
Expand Down Expand Up @@ -99,41 +94,47 @@ public boolean postBuild(MavenBuildProxy build, MavenProject pom, final BuildLis
// artifacts that are known to Maven.
Set<File> mavenArtifacts = new HashSet<File>();

if(pom.getFile()!=null) {// goals like 'clean' runs without loading POM, apparently.
if (pom.getFile() != null) {// goals like 'clean' runs without loading POM, apparently.
// record POM
final MavenArtifact pomArtifact = new MavenArtifact(
pom.getGroupId(), pom.getArtifactId(), pom.getVersion(), null, "pom", pom.getFile().getName(), Util.getDigestOf(new FileInputStream(pom.getFile())));
pom.getGroupId(), pom.getArtifactId(), pom.getVersion(), null, "pom", pom.getFile().getName(), Util.getDigestOf(new FileInputStream(pom.getFile())));

final String repositoryUrl = pom.getDistributionManagementArtifactRepository() == null ? null : Util.fixEmptyAndTrim(pom.getDistributionManagementArtifactRepository().getUrl());
final String repositoryId = pom.getDistributionManagementArtifactRepository() == null ? null : Util.fixEmptyAndTrim(pom.getDistributionManagementArtifactRepository().getId());

mavenArtifacts.add(pom.getFile());
pomArtifact.archive(build,pom.getFile(),listener);
pomArtifact.archive(build, pom.getFile(), listener);

// record main artifact (if packaging is POM, this doesn't exist)
final MavenArtifact mainArtifact = MavenArtifact.create(pom.getArtifact());
if(mainArtifact!=null) {
if (mainArtifact != null) {
File f = pom.getArtifact().getFile();
mavenArtifacts.add(f);
mainArtifact.archive(build, f,listener);
mainArtifact.archive(build, f, listener);
}

// record attached artifacts
final List<MavenArtifact> attachedArtifacts = new ArrayList<MavenArtifact>();
for( Artifact a : (List<Artifact>)pom.getAttachedArtifacts() ) {
for (Artifact a : (List<Artifact>) pom.getAttachedArtifacts()) {
MavenArtifact ma = MavenArtifact.create(a);
if(ma!=null) {
if (ma != null) {
mavenArtifacts.add(a.getFile());
ma.archive(build,a.getFile(),listener);
ma.archive(build, a.getFile(), listener);
attachedArtifacts.add(ma);
}
}

// record the action
build.executeAsync(new MavenBuildProxy.BuildCallable<Void,IOException>() {
build.executeAsync(new MavenBuildProxy.BuildCallable<Void, IOException>() {
public Void call(MavenBuild build) throws IOException, InterruptedException {
// if a build forks lifecycles, this method can be called multiple times
List<MavenArtifactRecord> old = Util.filter(build.getActions(), MavenArtifactRecord.class);
if (!old.isEmpty())
build.getActions().removeAll(old);

MavenArtifactRecord mar = new MavenArtifactRecord(build,pomArtifact,mainArtifact,attachedArtifacts);
MavenArtifactRecord mar = new MavenArtifactRecord(build, pomArtifact, mainArtifact, attachedArtifacts,
repositoryUrl,
repositoryId);
build.addAction(mar);

mar.recordFingerprints();
Expand Down

0 comments on commit 1a7001b

Please sign in to comment.