File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\maven-plugin\src\main\java\hudson\maven\reporters\MavenArtifactArchiver.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.maven.reporters;
25
26 import hudson.Extension;
27 import hudson.FilePath;
28 import hudson.Util;
29 import hudson.maven.*;
30 import hudson.model.BuildListener;
31 import hudson.util.InvocationInterceptor;
32 import org.apache.maven.artifact.Artifact;
33 import org.apache.maven.project.MavenProject;
34
35 import java.io.File;
36 import java.io.FileInputStream;
37 import java.io.IOException;
38 import java.lang.reflect.InvocationHandler;
39 import java.lang.reflect.Method;
40 import java.util.ArrayList;
41 import java.util.HashSet;
42 import java.util.List;
43 import java.util.Set;
44
45 /**
46 * Archives artifacts of the build.
47 *
48 * <p>
49 * Archive will be created in two places. One is inside the build directory,
50 * to be served from Jenkins. The other is to the local repository of the master,
51 * so that artifacts can be shared in maven builds happening in other slaves.
52 *
53 * @author Kohsuke Kawaguchi
54 */
55 public class MavenArtifactArchiver extends MavenReporter {
56 /**
57 * Accumulates {@link File}s that are created from assembly plugins.
58 * Note that some of them might be attached.
59 */
60 private transient List<File> assemblies;
61
62 @Override
63 public boolean preBuild(MavenBuildProxy build, MavenProject pom, BuildListener listener) throws InterruptedException, IOException {
64 // System.out.println("Zeroing out at "+MavenArtifactArchiver.this);
65 assemblies = null;
66 return true;
67 }
68
69 @Override
70 public boolean preExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, BuildListener listener) throws InterruptedException, IOException {
71 if(mojo.is("org.apache.maven.plugins","maven-assembly-plugin","assembly")) {
72 if (assemblies==null) assemblies = new ArrayList<File>();
73
74 try {
75 // watch out for AssemblyArchiver.createArchive that returns a File object, pointing to the archives created by the assembly plugin.
76 mojo.intercept("assemblyArchiver",new InvocationInterceptor() {
77 public Object invoke(Object proxy, Method method, Object[] args, InvocationHandler delegate) throws Throwable {
78 Object ret = delegate.invoke(proxy, method, args);
79 if(method.getName().equals("createArchive") && method.getReturnType()==File.class) {
80 // System.out.println("Discovered "+ret+" at "+MavenArtifactArchiver.this);
81 File f = (File) ret;
82 if(!f.isDirectory())
83 assemblies.add(f);
84 }
85 return ret;
86 }
87 });
88 } catch (NoSuchFieldException e) {
89 listener.getLogger().println("[JENKINS] Failed to monitor the execution of the assembly plugin: "+e.getMessage());
90 }
91 }
92 return true;
93 }
94
95 public boolean postBuild(MavenBuildProxy build, MavenProject pom, final BuildListener listener) throws InterruptedException, IOException {
96 // artifacts that are known to Maven.
97 Set<File> mavenArtifacts = new HashSet<File>();
98
99 if (pom.getFile() != null) {// goals like 'clean' runs without loading POM, apparently.
100 // record POM
101 final MavenArtifact pomArtifact = new MavenArtifact(
102 pom.getGroupId(), pom.getArtifactId(), pom.getVersion(), null, "pom", pom.getFile().getName(), Util.getDigestOf(new FileInputStream(pom.getFile())));
103
104 final String repositoryUrl = pom.getDistributionManagementArtifactRepository() == null ? null : Util.fixEmptyAndTrim(pom.getDistributionManagementArtifactRepository().getUrl());
105 final String repositoryId = pom.getDistributionManagementArtifactRepository() == null ? null : Util.fixEmptyAndTrim(pom.getDistributionManagementArtifactRepository().getId());
106
107 mavenArtifacts.add(pom.getFile());
108 pomArtifact.archive(build, pom.getFile(), listener);
109
110 // record main artifact (if packaging is POM, this doesn't exist)
111 final MavenArtifact mainArtifact = MavenArtifact.create(pom.getArtifact());
112 if (mainArtifact != null) {
113 File f = pom.getArtifact().getFile();
114 mavenArtifacts.add(f);
115 mainArtifact.archive(build, f, listener);
116 }
117
118 // record attached artifacts
119 final List<MavenArtifact> attachedArtifacts = new ArrayList<MavenArtifact>();
120 for (Artifact a : pom.getAttachedArtifacts()) {
121 MavenArtifact ma = MavenArtifact.create(a);
122 if (ma != null) {
123 mavenArtifacts.add(a.getFile());
124 ma.archive(build, a.getFile(), listener);
125 attachedArtifacts.add(ma);
126 }
127 }
128
129 // record the action
130 build.executeAsync(new MavenBuildProxy.BuildCallable<Void, IOException>() {
131 private static final long serialVersionUID = -7955474564875700905L;
132
133 public Void call(MavenBuild build) throws IOException, InterruptedException {
134 // if a build forks lifecycles, this method can be called multiple times
135 List<MavenArtifactRecord> old = build.getActions(MavenArtifactRecord.class);
136 if (!old.isEmpty())
137 build.getActions().removeAll(old);
138
139 MavenArtifactRecord mar = new MavenArtifactRecord(build, pomArtifact, mainArtifact, attachedArtifacts,
140 repositoryUrl,
141 repositoryId);
142 build.addAction(mar);
143
144 // TODO kutzi: why are the fingerprints recorded here?
145 // I thought that is the job of MavenFingerprinter
146 mar.recordFingerprints();
147
148 return null;
149 }
150 });
151 }
152
153 // do we have any assembly artifacts?
154 // System.out.println("Considering "+assemblies+" at "+MavenArtifactArchiver.this);
155 // new Exception().fillInStackTrace().printStackTrace();
156 if (build.isArchivingDisabled()) {
157 listener.getLogger().println("[JENKINS] Archiving disabled");
158 } else if (assemblies!=null) {
159 for (File assembly : assemblies) {
160 if(mavenArtifacts.contains(assembly))
161 continue; // looks like this is already archived
162 FilePath target = build.getArtifactsDir().child(assembly.getName());
163 listener.getLogger().println("[JENKINS] Archiving "+ assembly+" to "+target);
164 new FilePath(assembly).copyTo(target);
165 // TODO: fingerprint
166 }
167 }
168
169 return true;
170 }
171
172 @Extension
173 public static final class DescriptorImpl extends MavenReporterDescriptor {
174 public String getDisplayName() {
175 return Messages.MavenArtifactArchiver_DisplayName();
176 }
177
178 public MavenReporter newAutoInstance(MavenModule module) {
179 return new MavenArtifactArchiver();
180 }
181 }
182
183 private static final long serialVersionUID = 1L;
184 }
185