MavenFingerprinter.java 5.8 KB
Newer Older
K
kohsuke 已提交
1 2
package hudson.maven.reporters;

3 4
import hudson.FilePath;
import hudson.maven.MavenBuild;
K
kohsuke 已提交
5
import hudson.maven.MavenBuildProxy;
6
import hudson.maven.MavenBuildProxy.BuildCallable;
K
kohsuke 已提交
7
import hudson.maven.MavenModule;
8 9
import hudson.maven.MavenReporter;
import hudson.maven.MavenReporterDescriptor;
K
kohsuke 已提交
10
import hudson.maven.MojoInfo;
11
import hudson.maven.MavenModuleSetBuild;
K
kohsuke 已提交
12 13 14 15 16
import hudson.model.BuildListener;
import hudson.model.FingerprintMap;
import hudson.model.Hudson;
import hudson.tasks.Fingerprinter.FingerprintAction;
import org.apache.maven.artifact.Artifact;
17
import org.apache.maven.project.MavenProject;
K
kohsuke 已提交
18 19

import java.io.File;
20
import java.io.IOException;
K
kohsuke 已提交
21 22
import java.util.Collection;
import java.util.HashMap;
23 24
import java.util.HashSet;
import java.util.Map;
25
import java.util.Map.Entry;
26
import java.util.Set;
27
import java.util.List;
K
kohsuke 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40

/**
 * 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;
    /**
41
     * Fingerprints for files that were used.
K
kohsuke 已提交
42
     */
43 44 45 46 47
    private transient Map<String,String> used;
    /**
     * Fingerprints for files that were produced.
     */
    private transient Map<String,String> produced;
K
kohsuke 已提交
48

K
bug fix  
kohsuke 已提交
49
    public boolean preBuild(MavenBuildProxy build, MavenProject pom, BuildListener listener) throws InterruptedException, IOException {
K
kohsuke 已提交
50
        files = new HashSet<File>();
51 52
        used = new HashMap<String,String>();
        produced = new HashMap<String,String>();
K
kohsuke 已提交
53 54 55
        return true;
    }

56 57 58
    /**
     * Mojos perform different dependency resolution, so we need to check this for each mojo.
     */
59
    public boolean postExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, BuildListener listener, Throwable error) throws InterruptedException, IOException {
60 61 62
        record(pom.getArtifacts(),used);
        record(pom.getArtifact(),produced);
        record(pom.getAttachedArtifacts(),produced);
63
        record(pom.getGroupId(),pom.getFile(),produced);
K
kohsuke 已提交
64

65 66
        return true;
    }
K
kohsuke 已提交
67

68 69 70
    /**
     * Sends the collected fingerprints over to the master and record them.
     */
K
bug fix  
kohsuke 已提交
71
    public boolean postBuild(MavenBuildProxy build, MavenProject pom, BuildListener listener) throws InterruptedException, IOException {
72
        build.executeAsync(new BuildCallable<Void,IOException>() {
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
            // record is transient, so needs to make a copy first
            private final Map<String,String> u = used;
            private final Map<String,String> p = produced;

            public Void call(MavenBuild build) throws IOException, InterruptedException {
                FingerprintMap map = Hudson.getInstance().getFingerprintMap();

                for (Entry<String, String> e : p.entrySet())
                    map.getOrCreate(build, e.getKey(), e.getValue()).add(build);
                for (Entry<String, String> e : u.entrySet())
                    map.getOrCreate(null, e.getKey(), e.getValue()).add(build);

                Map<String,String> all = new HashMap<String, String>(u);
                all.putAll(p);

                // add action
                build.getActions().add(new FingerprintAction(build,all));
                return null;
            }
        });
K
kohsuke 已提交
93 94 95
        return true;
    }

K
bug fix  
kohsuke 已提交
96
    private void record(Collection<Artifact> artifacts, Map<String,String> record) throws IOException, InterruptedException {
K
kohsuke 已提交
97
        for (Artifact a : artifacts)
K
bug fix  
kohsuke 已提交
98
            record(a,record);
K
kohsuke 已提交
99 100 101 102 103 104 105 106 107
    }

    /**
     * Records the fingerprint of the given {@link Artifact}.
     *
     * <p>
     * This method contains the logic to avoid doubly recording the fingerprint
     * of the same file.
     */
K
bug fix  
kohsuke 已提交
108
    private void record(Artifact a, Map<String,String> record) throws IOException, InterruptedException {
K
kohsuke 已提交
109
        File f = a.getFile();
K
bug fix  
kohsuke 已提交
110 111
        if(files==null)
            throw new InternalError();
112 113 114 115
        record(a.getGroupId(), f, record);
    }

    private void record(String groupId, File f, Map<String, String> record) throws IOException, InterruptedException {
116
        if(f==null || !f.exists() || f.isDirectory() || !files.add(f))
K
bug fix  
kohsuke 已提交
117
            return;
K
kohsuke 已提交
118 119

        // new file
K
bug fix  
kohsuke 已提交
120
        String digest = new FilePath(f).digest();
121
        record.put(groupId+':'+f.getName(),digest);
K
kohsuke 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135
    }

    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() {
K
i18n  
kohsuke 已提交
136
            return Messages.MavenFingerprinter_DisplayName();
K
kohsuke 已提交
137 138 139 140 141 142
        }

        public MavenReporter newAutoInstance(MavenModule module) {
            return new MavenFingerprinter();
        }
    }
143

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    /**
     * Creates {@link FingerprintAction} for {@link MavenModuleSetBuild}
     * by aggregating all fingerprints from module builds.
     */
    public static void aggregate(MavenModuleSetBuild mmsb) throws IOException {
        Map<String,String> records = new HashMap<String, String>();
        for (List<MavenBuild> builds : mmsb.getModuleBuilds().values()) {
            for (MavenBuild build : builds) {
                FingerprintAction fa = build.getAction(FingerprintAction.class);
                if(fa!=null)
                    records.putAll(fa.getRecords());
            }
        }
        if(!records.isEmpty()) {
            FingerprintMap map = Hudson.getInstance().getFingerprintMap();
            for (Entry<String, String> e : records.entrySet())
                map.getOrCreate(null, e.getKey(), e.getValue()).add(mmsb);
            mmsb.addAction(new FingerprintAction(mmsb,records));
        }
    }

165
    private static final long serialVersionUID = 1L;
K
kohsuke 已提交
166
}