forked from bndtools/bnd
/
Resolve.java
275 lines (256 loc) · 8.77 KB
/
Resolve.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
package aQute.bnd.gradle;
import static aQute.bnd.gradle.BndUtils.logReport;
import static aQute.bnd.gradle.BndUtils.unwrapFile;
import java.io.File;
import java.io.Writer;
import java.util.Objects;
import org.gradle.api.GradleException;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.plugins.JavaBasePlugin;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.OutputFile;
import org.osgi.service.resolver.ResolutionException;
import aQute.bnd.build.Project;
import aQute.bnd.build.Workspace;
import aQute.bnd.osgi.Constants;
import aQute.lib.io.IO;
import aQute.lib.utf8properties.UTF8Properties;
import biz.aQute.resolve.ResolveProcess;
/**
* Resolve task type for Gradle.
* <p>
* This task type can be used to resolve a bndrun file setting the `-runbundles`
* instruction.
* <p>
* Here is an example of using the Resolve task type:
*
* <pre>
* import aQute.bnd.gradle.Resolve
* def resolveTask = tasks.register("resolve", Resolve) {
* bndrun = file("my.bndrun")
* outputBndrun = layout.buildDirectory.file("my.bndrun")
* }
* </pre>
* <p>
* Properties:
* <ul>
* <li>bndrun - This is the bndrun file to be resolved. This property must be
* set.</li>
* <li>bundles - The bundles to added to a FileSetRepository for non-Bnd
* Workspace builds. The default is "sourceSets.main.runtimeClasspath" plus
* "configurations.archives.artifacts.files". This must not be used for Bnd
* Workspace builds.</li>
* <li>ignoreFailures - If true the task will not fail if the execution fails.
* The default is false.</li>
* <li>workingDirectory - This is the directory for the resolve process. The
* default for workingDirectory is temporaryDir.</li>
* <li>properties - Properties that are available for evaluation of the bnd
* instructions for non-Bnd Workspace builds. The default is the properties of
* the task and project objects. This must not be used for Bnd Workspace
* builds.</li>
* <li>failOnChanges - If true the task will fail if the resolve process results
* in a different value for -runbundles than the current value. The default is
* false.</li>
* <li>outputBndrun - This is the output file for the calculated -runbundles
* property. The default is the input bndrun file which means the input bndrun
* file will be updated in place.</li>
* <li>reportOptional - If true failure reports will include optional
* requirements. The default is true.</li>
* <li>writeOnChanges - If true the task will write changes to the value of the
* -runbundles property. The default is true.</li>
* </ul>
*/
public class Resolve extends AbstractBndrun {
private boolean failOnChanges = false;
private final RegularFileProperty outputBndrun;
private boolean reportOptional = true;
private boolean writeOnChanges = true;
/**
* Whether resolve changes should fail the task.
*
* @return <code>true</code> if a change to the current
* <code>-runbundles</code> value will fail the task. The default is
* <code>false</code>.
*/
@Input
public boolean isFailOnChanges() {
return failOnChanges;
}
/**
* Whether resolve changes should fail the task.
* <p>
* Alias for {@link #isFailOnChanges()}.
*
* @return <code>true</code> if a change to the current
* <code>-runbundles</code> value will fail the task. The default is
* <code>false</code>.
*/
@Internal
public boolean getFailOnChanges() {
return isFailOnChanges();
}
/**
* Set whether resolve changes should fail the task.
*
* @param failOnChanges If <code>true</code>, then a change to the current
* <code>-runbundles</code> value will fail the task. The default
* is <code>false</code>.
*/
public void setFailOnChanges(boolean failOnChanges) {
this.failOnChanges = failOnChanges;
}
/**
* Return the output file for the calculated <code>-runbundles</code>
* property.
* <p>
* By default, the input <code>bndrun</code> file is used as the output
* bndrun file. That is, the input bndrun file will be updated in place. If
* this property is set to a value other than the input bndrun file, the
* output bndrun file will <code>-include</code> the input bndrun file and
* can be thus be used by other tasks, such as TestOSGi as a resolved input
* bndrun file.
*
* @return The output file for the calculated <code>-runbundles</code>
* property.
*/
@OutputFile
public RegularFileProperty getOutputBndrun() {
return outputBndrun;
}
/**
* Whether to report optional requirements.
*
* @return <code>true</code> if optional requirements will be reported. The
* default is <code>false</code>.
*/
@Input
public boolean isReportOptional() {
return reportOptional;
}
/**
* Whether to report optional requirements.
* <p>
* Alias for {@link #isReportOptional()}.
*
* @return <code>true</code> if optional requirements will be reported. The
* default is <code>false</code>.
*/
@Internal
public boolean getReportOptional() {
return isReportOptional();
}
/**
* Set whether to report optional requirements.
*
* @param reportOptional If <code>true</code>, then optional requirements
* will be reported. The default is <code>false</code>.
*/
public void setReportOptional(boolean reportOptional) {
this.reportOptional = reportOptional;
}
/**
* Whether resolve changes should be written.
*
* @return <code>true</code> if a change to the current
* <code>-runbundles</code> value will be written to the output
* bndrun file. The default is <code>false</code>.
*/
@Input
public boolean isWriteOnChanges() {
return writeOnChanges;
}
/**
* Whether resolve changes should be written.
* <p>
* Alias for {@link #isWriteOnChanges()}.
*
* @return <code>true</code> if a change to the current
* <code>-runbundles</code> value will be written to the output
* bndrun file. The default is <code>false</code>.
*/
@Internal
public boolean getWriteOnChanges() {
return isWriteOnChanges();
}
/**
* Set whether resolve changes should be written.
*
* @param writeOnChanges If <code>true</code>, then a change to the current
* <code>-runbundles</code> value will be written to the output
* bndrun file. The default is <code>false</code>.
*/
public void setWriteOnChanges(boolean writeOnChanges) {
this.writeOnChanges = writeOnChanges;
}
/**
* Create a Resolve task.
*/
public Resolve() {
super();
setGroup(JavaBasePlugin.VERIFICATION_GROUP);
org.gradle.api.Project project = getProject();
outputBndrun = project.getObjects()
.fileProperty()
.convention(getBndrun());
}
/**
* Create the Bndrun object.
*
* @param workspace The workspace for the Bndrun.
* @param bndrunFile The bndrun file for the Bndrun.
* @return The Bndrun object.
* @throws Exception If the create action has an exception.
*/
@Override
protected biz.aQute.resolve.Bndrun createBndrun(Workspace workspace, File bndrunFile) throws Exception {
File outputBndrunFile = unwrapFile(getOutputBndrun());
if (!Objects.equals(outputBndrunFile, bndrunFile)) {
try (Writer writer = IO.writer(outputBndrunFile)) {
UTF8Properties props = new UTF8Properties();
props.setProperty(Constants.INCLUDE, String.format("\"%s\"", IO.absolutePath(bndrunFile)));
props.store(writer, null);
}
bndrunFile = outputBndrunFile;
}
return super.createBndrun(workspace, bndrunFile);
}
/**
* Resolve the Bndrun object.
*
* @param run The Bndrun object.
* @throws Exception If the worker action has an exception or the input is
* not a Bndrun object.
*/
@Override
protected void worker(Project run) throws Exception {
if (run instanceof biz.aQute.resolve.Bndrun) {
worker((biz.aQute.resolve.Bndrun) run);
return;
}
throw new GradleException(
"Resolving a project's bnd.bnd file is not supported by this task. This task can only resolve a bndrun file.");
}
/**
* Resolve the Bndrun object.
*
* @param run The Bndrun object.
* @throws Exception If the worker action has an exception.
*/
private void worker(biz.aQute.resolve.Bndrun run) throws Exception {
getLogger().info("Resolving runbundles required for {}", run.getPropertiesFile());
getLogger().debug("Run properties: {}", run.getProperties());
try {
String result = run.resolve(isFailOnChanges(), isWriteOnChanges());
getLogger().info("{}: {}", Constants.RUNBUNDLES, result);
} catch (ResolutionException e) {
getLogger().error(ResolveProcess.format(e, isReportOptional()));
throw new GradleException(String.format("%s resolution exception", run.getPropertiesFile()), e);
} finally {
logReport(run, getLogger());
}
if (!isIgnoreFailures() && !run.isOk()) {
throw new GradleException(String.format("%s resolution failure", run.getPropertiesFile()));
}
}
}