1 package hudson.maven.reporters; 2 3 import hudson.FilePath; 4 import hudson.maven.MavenBuild; 5 import hudson.maven.MavenBuildProxy; 6 import hudson.maven.MavenBuildProxy.BuildCallable; 7 import hudson.maven.MavenModule; 8 import hudson.maven.MavenReporter; 9 import hudson.maven.MavenReporterDescriptor; 10 import hudson.model.BuildListener; 11 import hudson.model.FingerprintMap; 12 import hudson.model.Hudson; 13 import org.apache.maven.artifact.Artifact; 14 import org.apache.maven.project.MavenProject; 15 16 import java.io.File ; 17 import java.io.IOException ; 18 import java.util.HashSet ; 19 import java.util.Set ; 20 21 26 public class MavenArtifactArchiver extends MavenReporter { 27 public boolean postBuild(MavenBuildProxy build, MavenProject pom, BuildListener listener) throws InterruptedException , IOException { 28 final Set <FilePath> archivedFiles = new HashSet <FilePath>(); 29 30 record(build,pom.getArtifact(),listener,archivedFiles); 31 for( Object a : pom.getAttachedArtifacts() ) 32 record(build,(Artifact)a,listener,archivedFiles); 33 34 build.execute(new BuildCallable<Void ,IOException >() { 36 public Void call(MavenBuild build) throws IOException , InterruptedException { 37 FingerprintMap map = Hudson.getInstance().getFingerprintMap(); 38 for (FilePath file : archivedFiles) 39 map.getOrCreate(build, file.getName(), file.digest()); 40 return null; 41 } 42 }); 43 44 return true; 45 } 46 47 50 private void record(MavenBuildProxy build, Artifact a, BuildListener listener, Set <FilePath> archivedFiles) throws IOException , InterruptedException { 51 File file = a.getFile(); 52 if(file==null) 53 return; if(!file.exists() || file.isDirectory()) 55 return; 57 listener.getLogger().println("Archiving "+ file); 58 59 FilePath target = build.getArtifactsDir() 60 .child(a.getGroupId()) 61 .child(a.getArtifactId()) 62 .child(a.getVersion()) 63 .child(a.getArtifactId() + '-' + a.getVersion() + (a.getClassifier() != null ? '-' + a.getClassifier() : "") + '.' + a.getType()); 64 65 new FilePath(file).copyTo(target); 66 67 archivedFiles.add(target); 68 } 69 70 public DescriptorImpl getDescriptor() { 71 return DescriptorImpl.DESCRIPTOR; 72 } 73 74 public static final class DescriptorImpl extends MavenReporterDescriptor { 75 public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl(); 76 77 private DescriptorImpl() { 78 super(MavenArtifactArchiver.class); 79 } 80 81 public String getDisplayName() { 82 return "Archive the artifacts"; 83 } 84 85 public MavenReporter newAutoInstance(MavenModule module) { 86 return new MavenArtifactArchiver(); 87 } 88 } 89 90 private static final long serialVersionUID = 1L; 91 } 92 | Popular Tags |