提交 ac7b10b2 编写于 作者: K kohsuke

adding fingerprint support for maven.


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@2008 71c3de6d-444a-0410-be80-ed276b4c234a
上级 1837fc4a
......@@ -2,6 +2,7 @@ package hudson.maven;
import hudson.model.Descriptor;
import hudson.maven.reporters.MavenArtifactArchiver;
import hudson.maven.reporters.MavenFingerprinter;
import java.util.List;
......@@ -13,7 +14,8 @@ public final class MavenReporters {
/**
* List of all installed {@link MavenReporter}s.
*/
public static final List<MavenReporterDescriptor> LIST = Descriptor.<MavenReporterDescriptor>toList(
MavenArtifactArchiver.DescriptorImpl.DESCRIPTOR
public static final List<MavenReporterDescriptor> LIST = Descriptor.toList(
MavenArtifactArchiver.DescriptorImpl.DESCRIPTOR,
MavenFingerprinter.DescriptorImpl.DESCRIPTOR
);
}
package hudson.maven.reporters;
import hudson.maven.MavenReporter;
import hudson.maven.MavenBuildProxy;
import hudson.maven.MavenReporterDescriptor;
import hudson.maven.MavenModule;
import hudson.maven.MojoInfo;
import hudson.maven.MavenBuild;
import hudson.maven.MavenBuildProxy.BuildCallable;
import hudson.model.BuildListener;
import hudson.model.FingerprintMap;
import hudson.model.Hudson;
import hudson.FilePath;
import hudson.tasks.Fingerprinter.FingerprintAction;
import org.kohsuke.stapler.StaplerRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.artifact.Artifact;
import java.io.IOException;
import java.io.File;
import java.util.Set;
import java.util.HashSet;
import java.util.Collection;
import java.util.Map;
import java.util.HashMap;
/**
* Records fingerprints of the builds to keep track of dependencies.
*
* @author Kohsuke Kawaguchi
*/
public class MavenFingerprinter extends MavenReporter {
/**
* Files whose fingerprints were already recorded.
*/
private transient Set<File> files;
/**
* Recorded fingerprints.
*/
private transient Map<String,String> record;
public boolean preBuild(MavenBuildProxy build, MavenProject pom, BuildListener listener) throws InterruptedException, IOException {
files = new HashSet<File>();
record = new HashMap<String,String>();
return true;
}
public boolean postExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, BuildListener listener) throws InterruptedException, IOException {
// really nice if we can do this in preExecute,
// but dependency resolution only happens after preExecute.
record(build,false,pom.getArtifacts());
// try to pick up artifacts as soon as they are found.
record(build,true,pom.getArtifact());
record(build,true,pom.getAttachedArtifacts());
return true;
}
private void record(MavenBuildProxy build, boolean produced, Collection<Artifact> artifacts) throws IOException, InterruptedException {
for (Artifact a : artifacts)
record(build,produced,a);
}
/**
* Records the fingerprint of the given {@link Artifact}.
*
* <p>
* This method contains the logic to avoid doubly recording the fingerprint
* of the same file.
*/
private void record(MavenBuildProxy build, final boolean produced, Artifact a) throws IOException, InterruptedException {
File f = a.getFile();
if(f==null || !files.add(f))
return;
// new file
final String digest = new FilePath(f).digest();
final String name = a.getGroupId()+':'+f.getName();
record.put(name,digest);
build.execute(new BuildCallable<Void,IOException>() {
public Void call(MavenBuild build) throws IOException, InterruptedException {
FingerprintMap map = Hudson.getInstance().getFingerprintMap();
map.getOrCreate(produced?build:null, name, digest);
return null;
}
});
}
public boolean postBuild(MavenBuildProxy build, MavenProject pom, BuildListener listener) throws InterruptedException, IOException {
if(!record.isEmpty()) {
build.execute(new BuildCallable<Void,IOException>() {
public Void call(MavenBuild build) throws IOException, InterruptedException {
build.getActions().add(new FingerprintAction(build,record));
return null;
}
});
}
return true;
}
public DescriptorImpl getDescriptor() {
return DescriptorImpl.DESCRIPTOR;
}
public static final class DescriptorImpl extends MavenReporterDescriptor {
public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl();
private DescriptorImpl() {
super(MavenFingerprinter.class);
}
public String getDisplayName() {
return "Record fingerprints";
}
public MavenFingerprinter newInstance(StaplerRequest req) throws FormException {
return new MavenFingerprinter();
}
public MavenReporter newAutoInstance(MavenModule module) {
return new MavenFingerprinter();
}
}
}
......@@ -13,6 +13,8 @@ import hudson.model.FingerprintMap;
import hudson.model.Hudson;
import hudson.model.Project;
import hudson.model.Result;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.remoting.VirtualChannel;
import hudson.util.IOException2;
import org.apache.tools.ant.DirectoryScanner;
......@@ -185,13 +187,16 @@ public class Fingerprinter extends Publisher implements Serializable {
* Action for displaying fingerprints.
*/
public static final class FingerprintAction implements Action {
private final Build build;
private final AbstractBuild build;
/**
* From file name to the digest.
*/
private final Map<String,String> record;
private transient WeakReference<Map<String,Fingerprint>> ref;
public FingerprintAction(Build build, Map<String, String> record) {
public FingerprintAction(AbstractBuild build, Map<String, String> record) {
this.build = build;
this.record = record;
}
......@@ -208,7 +213,7 @@ public class Fingerprinter extends Publisher implements Serializable {
return "fingerprints";
}
public Build getBuild() {
public AbstractBuild getBuild() {
return build;
}
......@@ -242,8 +247,8 @@ public class Fingerprinter extends Publisher implements Serializable {
* Gets the dependency to other builds in a map.
* Returns build numbers instead of {@link Build}, since log records may be gone.
*/
public Map<Project,Integer> getDependencies() {
Map<Project,Integer> r = new HashMap<Project,Integer>();
public Map<AbstractProject,Integer> getDependencies() {
Map<AbstractProject,Integer> r = new HashMap<AbstractProject,Integer>();
for (Fingerprint fp : getFingerprints().values()) {
BuildPtr bp = fp.getOriginal();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册