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