/
Gradle.java
306 lines (263 loc) · 10.2 KB
/
Gradle.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package hudson.plugins.gradle;
import hudson.*;
import hudson.model.*;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import hudson.tools.ToolInstallation;
import hudson.util.ArgumentListBuilder;
import net.sf.json.JSONObject;
import org.jenkinsci.lib.dryrun.DryRun;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @author Gregory Boissinot
*/
public class Gradle extends Builder implements DryRun {
private final String description;
private final String switches;
private final String tasks;
private final String rootBuildScriptDir;
private final String buildFile;
private final String gradleName;
private final boolean useWrapper;
@DataBoundConstructor
public Gradle(String description, String switches, String tasks, String rootBuildScriptDir, String buildFile,
String gradleName, boolean useWrapper) {
this.description = description;
this.switches = switches;
this.tasks = tasks;
this.gradleName = gradleName;
this.rootBuildScriptDir = rootBuildScriptDir;
this.buildFile = buildFile;
this.useWrapper = !useWrapper;
}
@SuppressWarnings("unused")
public String getSwitches() {
return switches;
}
@SuppressWarnings("unused")
public String getBuildFile() {
return buildFile;
}
@SuppressWarnings("unused")
public String getGradleName() {
return gradleName;
}
@SuppressWarnings("unused")
public String getTasks() {
return tasks;
}
@SuppressWarnings("unused")
public String getDescription() {
return description;
}
@SuppressWarnings("unused")
public boolean isUseWrapper() {
return useWrapper;
}
@SuppressWarnings("unused")
public String getRootBuildScriptDir() {
return rootBuildScriptDir;
}
public GradleInstallation getGradle() {
for (GradleInstallation i : getDescriptor().getInstallations()) {
if (gradleName != null && i.getName().equals(gradleName)) {
return i;
}
}
return null;
}
@Override
public boolean performDryRun(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
return performTask(true, build, launcher, listener);
}
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
return performTask(false, build, launcher, listener);
}
private boolean performTask(boolean dryRun, AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
EnvVars env = build.getEnvironment(listener);
//Switches
String extraSwitches = env.get("GRADLE_EXT_SWITCHES");
String normalizedSwitches;
if (extraSwitches != null) {
normalizedSwitches = switches + " " + extraSwitches;
} else {
normalizedSwitches = switches;
}
normalizedSwitches = normalizedSwitches.replaceAll("[\t\r\n]+", " ");
normalizedSwitches = Util.replaceMacro(normalizedSwitches, env);
normalizedSwitches = Util.replaceMacro(normalizedSwitches, build.getBuildVariables());
//Add dry-run switch if needed
if (dryRun) {
normalizedSwitches = normalizedSwitches + " --dry-run";
}
//Tasks
String extraTasks = env.get("GRADLE_EXT_TASKS");
String normalizedTasks;
if (extraTasks != null) {
normalizedTasks = tasks + " " + extraTasks;
} else {
normalizedTasks = tasks;
}
normalizedTasks = normalizedTasks.replaceAll("[\t\r\n]+", " ");
normalizedTasks = Util.replaceMacro(normalizedTasks, env);
normalizedTasks = Util.replaceMacro(normalizedTasks, build.getBuildVariables());
//Build arguments
ArgumentListBuilder args = new ArgumentListBuilder();
GradleInstallation ai = getGradle();
if (ai == null) {
if (useWrapper) {
String execName = (Functions.isWindows()) ? GradleInstallation.WINDOWS_GRADLE_WRAPPER_COMMAND : GradleInstallation.UNIX_GRADLE_WRAPPER_COMMAND;
FilePath gradleWrapperFile = new FilePath(build.getModuleRoot(), execName);
args.add(gradleWrapperFile.getRemote());
} else {
args.add(launcher.isUnix() ? GradleInstallation.UNIX_GRADLE_COMMAND : GradleInstallation.WINDOWS_GRADLE_COMMAND);
}
} else {
ai = ai.forNode(Computer.currentComputer().getNode(), listener);
ai = ai.forEnvironment(env);
String exe;
if (useWrapper) {
exe = ai.getWrapperExecutable(launcher, build);
} else {
exe = ai.getExecutable(launcher);
}
if (exe == null) {
listener.fatalError("ERROR");
return false;
}
args.add(exe);
}
args.addKeyValuePairs("-D", fixParameters(build.getBuildVariables()));
args.addTokenized(normalizedSwitches);
args.addTokenized(normalizedTasks);
if (buildFile != null && buildFile.trim().length() != 0) {
String buildFileNormalized = Util.replaceMacro(buildFile.trim(), env);
args.add("-b");
args.add(buildFileNormalized);
}
if (ai != null) {
env.put("GRADLE_HOME", ai.getHome());
}
if (!launcher.isUnix()) {
// on Windows, executing batch file can't return the correct error code,
// so we need to wrap it into cmd.exe.
// double %% is needed because we want ERRORLEVEL to be expanded after
// batch file executed, not before. This alone shows how broken Windows is...
args.prepend("cmd.exe", "/C");
args.add("&&", "exit", "%%ERRORLEVEL%%");
}
FilePath rootLauncher;
if (rootBuildScriptDir != null && rootBuildScriptDir.trim().length() != 0) {
String rootBuildScriptNormalized = rootBuildScriptDir.trim().replaceAll("[\t\r\n]+", " ");
rootBuildScriptNormalized = Util.replaceMacro(rootBuildScriptNormalized.trim(), env);
rootBuildScriptNormalized = Util.replaceMacro(rootBuildScriptNormalized, build.getBuildVariableResolver());
rootLauncher = new FilePath(build.getModuleRoot(), rootBuildScriptNormalized);
} else {
rootLauncher = build.getWorkspace();
}
//Not call from an Executor
if (rootLauncher == null) {
rootLauncher = build.getProject().getSomeWorkspace();
}
try {
GradleConsoleAnnotator gca = new GradleConsoleAnnotator(
listener.getLogger(), build.getCharset());
int r;
try {
r = launcher.launch().cmds(args).envs(env).stdout(gca)
.pwd(rootLauncher).join();
} finally {
gca.forceEol();
}
boolean success = r == 0;
// if the build is successful then set it as success otherwise as a failure.
build.setResult(Result.SUCCESS);
if (!success) {
build.setResult(Result.FAILURE);
}
return success;
} catch (IOException e) {
Util.displayIOException(e, listener);
e.printStackTrace(listener.fatalError("command execution failed"));
build.setResult(Result.FAILURE);
return false;
}
}
private Map<String, String> fixParameters(Map<String, String> parmas) {
Map<String, String> result = new HashMap<String, String>();
for (Map.Entry<String, String> entry : parmas.entrySet()) {
String value = entry.getValue();
if (isValue2Escape(value)) {
result.put(entry.getKey(), "\"" + value + "\"");
} else {
result.put(entry.getKey(), value);
}
}
return result;
}
private boolean isValue2Escape(String value) {
if (value == null) {
return false;
}
if (value.trim().length() == 0) {
return false;
}
return value.contains("<") || value.contains(">");
}
@Override
public DescriptorImpl getDescriptor() {
return (DescriptorImpl) super.getDescriptor();
}
@Extension
public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {
@CopyOnWrite
private volatile GradleInstallation[] installations = new GradleInstallation[0];
public DescriptorImpl() {
load();
}
protected DescriptorImpl(Class<? extends Gradle> clazz) {
super(clazz);
}
/**
* Obtains the {@link GradleInstallation.DescriptorImpl} instance.
*/
public GradleInstallation.DescriptorImpl getToolDescriptor() {
return ToolInstallation.all().get(GradleInstallation.DescriptorImpl.class);
}
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}
protected void convert(Map<String, Object> oldPropertyBag) {
if (oldPropertyBag.containsKey("installations")) {
installations = (GradleInstallation[]) oldPropertyBag.get("installations");
}
}
@Override
public String getHelpFile() {
return "/plugin/gradle/help.html";
}
@Override
public String getDisplayName() {
return Messages.step_displayName();
}
public GradleInstallation[] getInstallations() {
return installations;
}
public void setInstallations(GradleInstallation... installations) {
this.installations = installations;
save();
}
@Override
public Gradle newInstance(StaplerRequest request, JSONObject formData) throws FormException {
return (Gradle) request.bindJSON(clazz, formData);
}
}
}