/
PackagePluginMojo.java
334 lines (304 loc) · 13.5 KB
/
PackagePluginMojo.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*******************************************************************************
* Copyright (c) 2008, 2021 Sonatype Inc. and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Sonatype Inc. - initial API and implementation
* Christoph Läubrich - Automatically translate maven-pom information to osgi Bundle-Header #177
*******************************************************************************/
package org.eclipse.tycho.packaging;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.jar.Attributes;
import java.util.jar.Attributes.Name;
import java.util.jar.Manifest;
import java.util.stream.Collectors;
import org.apache.maven.archiver.MavenArchiveConfiguration;
import org.apache.maven.archiver.MavenArchiver;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.model.License;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProjectHelper;
import org.codehaus.plexus.archiver.Archiver;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.archiver.jar.JarArchiver;
import org.codehaus.plexus.archiver.jar.ManifestException;
import org.codehaus.plexus.archiver.util.DefaultFileSet;
import org.codehaus.plexus.component.annotations.Requirement;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.core.TychoConstants;
import org.eclipse.tycho.core.osgitools.DefaultReactorProject;
import org.eclipse.tycho.core.osgitools.project.BuildOutputJar;
import org.eclipse.tycho.core.osgitools.project.EclipsePluginProject;
import org.eclipse.tycho.core.shared.BuildProperties;
import org.eclipse.tycho.packaging.sourceref.SourceReferenceComputer;
import org.eclipse.tycho.packaging.sourceref.SourceReferencesProvider;
import org.osgi.framework.Constants;
/**
* Creates a jar-based plugin and attaches it as an artifact
*/
@Mojo(name = "package-plugin", threadSafe = true)
public class PackagePluginMojo extends AbstractTychoPackagingMojo {
/**
* The output directory of the jar file
*
* By default this is the Maven "target/" directory.
*/
@Parameter(property = "project.build.directory", required = true)
protected File buildDirectory;
protected EclipsePluginProject pdeProject;
/**
* The Jar archiver.
*/
@Component(role = Archiver.class, hint = "jar")
private JarArchiver jarArchiver;
/**
* Additional files to be included in the bundle jar. This can be used when
* <tt>bin.includes</tt> in build.properties is not flexible enough , e.g. for generated files.
* If conflicting, additional files win over <tt>bin.includes</tt><br/>
* Example:<br/>
*
* <pre>
* <additionalFileSets>
* <fileSet>
* <directory>${project.build.directory}/mytool-gen/</directory>
* <includes>
* <include>**/*</include>
* </includes>
* </fileSet>
* </additionalFileSets>
* </pre>
*
*/
@Parameter
private DefaultFileSet[] additionalFileSets;
/**
* Name of the generated JAR.
*/
@Parameter(property = "project.build.finalName", alias = "jarName", required = true)
protected String finalName;
/**
* The <a href="http://maven.apache.org/shared/maven-archiver/">maven archiver</a> to use. One
* of the archiver properties is the <code>addMavenDescriptor</code> flag, which indicates
* whether the generated archive will contain the pom.xml and pom.properties file. If no archive
* configuration is specified, the default value is <code>true</code>. If the maven descriptor
* should not be added to the artifact, use the following configuration:
*
* <pre>
* <plugin>
* <groupId>org.eclipse.tycho</groupId>
* <artifactId>tycho-packaging-plugin</artifactId>
* <version>${tycho-version}</version>
* <configuration>
* <archive>
* <addMavenDescriptor>false</addMavenDescriptor>
* </archive>
* </configuration>
* </plugin>
* </pre>
*/
@Parameter
private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
/**
* Whether to generate an
* <a href="https://wiki.eclipse.org/PDE/UI/SourceReferences">Eclipse-SourceReferences</a>
* MANIFEST header. When using this parameter, property ${tycho.scmUrl} must be set and be a
* valid <a href="http://maven.apache.org/scm/scm-url-format.html">maven SCM URL</a>.
*
* Example configuration:
*
* <pre>
* <sourceReferences>
* <generate>true</generate>
* </sourceReferences>
* </pre>
*
* Note that a {@link SourceReferencesProvider} component must be registered for the SCM type
* being used. You may also override the generated value by configuring:
*
* <pre>
* <sourceReferences>
* <generate>true</generate>
* <customValue>scm:myscm:customSourceReferenceValue</customValue>
* </sourceReferences>
* </pre>
*/
@Parameter
private SourceReferences sourceReferences = new SourceReferences();
/**
* Whether to derive OSGi-Headers from the maven-pom configuration, currently the following
* header are supported
* <ul>
* <li>{@link Constants#BUNDLE_LICENSE} is generated from maven <licenses> configuration</li>
* </ul>
*/
@Parameter(defaultValue = "true")
private boolean deriveHeaderFromProject = true;
@Component
private SourceReferenceComputer soureReferenceComputer;
@Requirement
private MavenProjectHelper projectHelper;
@Override
public void execute() throws MojoExecutionException {
ReactorProject reactorProject = DefaultReactorProject.adapt(project);
pdeProject = (EclipsePluginProject) reactorProject.getContextValue(TychoConstants.CTX_ECLIPSE_PLUGIN_PROJECT);
createSubJars();
File pluginFile = createPluginJar();
project.getArtifact().setFile(pluginFile);
}
private void createSubJars() throws MojoExecutionException {
for (BuildOutputJar jar : pdeProject.getOutputJars()) {
if (!jar.isDirClasspathEntry()) {
makeJar(jar);
}
}
}
private File makeJar(BuildOutputJar jar) throws MojoExecutionException {
String jarName = jar.getName();
BuildProperties buildProperties = pdeProject.getBuildProperties();
String customManifest = buildProperties.getJarToManifestMap().get(jarName);
try {
File jarFile = new File(project.getBasedir(), jarName);
JarArchiver archiver = new JarArchiver();
archiver.setDestFile(jarFile);
archiver.addDirectory(jar.getOutputDirectory());
if (customManifest != null) {
for (File sourceFolder : jar.getSourceFolders()) {
File manifestFile = new File(sourceFolder, customManifest);
if (manifestFile.isFile()) {
archiver.setManifest(manifestFile);
break;
}
}
}
archiver.createArchive();
return jarFile;
} catch (Exception e) {
throw new MojoExecutionException("Could not create jar " + jarName, e);
}
}
private File createPluginJar() throws MojoExecutionException {
try {
MavenArchiver archiver = new MavenArchiver();
archiver.setArchiver(jarArchiver);
File pluginFile = new File(buildDirectory, finalName + ".jar");
if (pluginFile.exists()) {
pluginFile.delete();
}
BuildProperties buildProperties = pdeProject.getBuildProperties();
List<String> binIncludesList = buildProperties.getBinIncludes();
List<String> binExcludesList = buildProperties.getBinExcludes();
// 1. additional filesets should win over bin.includes, so we add them first
if (additionalFileSets != null) {
for (DefaultFileSet fileSet : additionalFileSets) {
if (fileSet.getDirectory() != null && fileSet.getDirectory().isDirectory()) {
archiver.getArchiver().addFileSet(fileSet);
}
}
}
List<String> binIncludesIgnoredForValidation = new ArrayList<>();
// 2. handle dir classpath entries and "."
for (BuildOutputJar outputJar : pdeProject.getOutputJarMap().values()) {
String jarName = outputJar.getName();
if (binIncludesList.contains(jarName) && outputJar.isDirClasspathEntry()) {
binIncludesIgnoredForValidation.add(jarName);
String prefix = ".".equals(jarName) ? "" : jarName;
archiver.getArchiver().addDirectory(outputJar.getOutputDirectory(), prefix);
}
}
// 3. handle nested jars and included resources
checkBinIncludesExist(buildProperties, binIncludesIgnoredForValidation.toArray(new String[0]));
archiver.getArchiver().addFileSet(getFileSet(project.getBasedir(), binIncludesList, binExcludesList));
File manifest = new File(project.getBuild().getDirectory(), "MANIFEST.MF");
updateManifest(manifest);
archive.setManifestFile(manifest);
archiver.setOutputFile(pluginFile);
if (!archive.isForced()) {
// optimized archive creation not supported for now because of build qualifier mismatch issues
// see TYCHO-502
getLog().warn("ignoring unsupported archive forced = false parameter.");
archive.setForced(true);
}
archiver.createArchive(session, project, archive);
return pluginFile;
} catch (IOException | ArchiverException | ManifestException | DependencyResolutionRequiredException e) {
throw new MojoExecutionException("Error assembling JAR", e);
}
}
private void updateManifest(File output) throws IOException, MojoExecutionException {
writeManifest(output, getManifest());
}
protected void writeManifest(File output, Manifest mf) throws IOException {
File parentFile = output.getParentFile();
if (!parentFile.mkdirs() && !parentFile.exists()) {
throw new IOException("creating target directory " + parentFile.getAbsolutePath() + " failed");
}
try (BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(output))) {
mf.write(os);
}
}
protected Manifest getManifest() throws IOException, MojoExecutionException {
InputStream is = new FileInputStream(new File(project.getBasedir(), "META-INF/MANIFEST.MF"));
Manifest mf;
try {
mf = new Manifest(is);
} finally {
is.close();
}
Attributes attributes = mf.getMainAttributes();
if (attributes.getValue(Name.MANIFEST_VERSION) == null) {
attributes.put(Name.MANIFEST_VERSION, "1.0");
}
ReactorProject reactorProject = DefaultReactorProject.adapt(project);
attributes.putValue("Bundle-Version", reactorProject.getExpandedVersion());
soureReferenceComputer.addSourceReferenceHeader(mf, sourceReferences, project);
if (deriveHeaderFromProject) {
computeIfHeaderNotPresent(attributes, Constants.BUNDLE_LICENSE, () -> {
List<License> licenses = project.getLicenses();
return licenses.stream().map(license -> {
String name = license.getName();
if (name != null && !name.isBlank()) {
StringBuilder licenseHeader = new StringBuilder(name);
appendHeaderAttribute(licenseHeader, "link", license.getUrl());
return licenseHeader;
}
return null;
}).filter(Objects::nonNull).map(String::valueOf).collect(Collectors.joining(","));
});
}
return mf;
}
private static void appendHeaderAttribute(StringBuilder header, String attribute, String value) {
if (value != null && !value.isBlank()) {
header.append(";");
header.append(attribute);
header.append("=\"");
header.append(value);
header.append("\"");
}
}
private static void computeIfHeaderNotPresent(Attributes attributes, String hv, Supplier<String> headerComputer) {
if (attributes.getValue(hv) == null) {
String header = headerComputer.get();
if (header != null && !header.isBlank()) {
attributes.putValue(hv, header);
}
}
}
}