|
1 | 1 | /*******************************************************************************
|
2 |
| - * Copyright (c) 2008, 2011 Sonatype Inc. and others. |
| 2 | + * Copyright (c) 2008, 2021 Sonatype Inc. and others. |
3 | 3 | * This program and the accompanying materials
|
4 | 4 | * are made available under the terms of the Eclipse Public License 2.0
|
5 | 5 | * which accompanies this distribution, and is available at
|
|
9 | 9 | *
|
10 | 10 | * Contributors:
|
11 | 11 | * Sonatype Inc. - initial API and implementation
|
| 12 | + * Christoph Läubrich - Automatically translate maven-pom information to osgi Bundle-Header #177 |
12 | 13 | *******************************************************************************/
|
13 | 14 | package org.eclipse.tycho.packaging;
|
14 | 15 |
|
|
20 | 21 | import java.io.InputStream;
|
21 | 22 | import java.util.ArrayList;
|
22 | 23 | import java.util.List;
|
| 24 | +import java.util.Objects; |
| 25 | +import java.util.function.Supplier; |
23 | 26 | import java.util.jar.Attributes;
|
24 | 27 | import java.util.jar.Attributes.Name;
|
25 | 28 | import java.util.jar.Manifest;
|
| 29 | +import java.util.stream.Collectors; |
26 | 30 |
|
27 | 31 | import org.apache.maven.archiver.MavenArchiveConfiguration;
|
28 | 32 | import org.apache.maven.archiver.MavenArchiver;
|
29 | 33 | import org.apache.maven.artifact.DependencyResolutionRequiredException;
|
| 34 | +import org.apache.maven.model.License; |
30 | 35 | import org.apache.maven.plugin.MojoExecutionException;
|
31 | 36 | import org.apache.maven.plugins.annotations.Component;
|
32 | 37 | import org.apache.maven.plugins.annotations.Mojo;
|
|
46 | 51 | import org.eclipse.tycho.core.shared.BuildProperties;
|
47 | 52 | import org.eclipse.tycho.packaging.sourceref.SourceReferenceComputer;
|
48 | 53 | import org.eclipse.tycho.packaging.sourceref.SourceReferencesProvider;
|
| 54 | +import org.osgi.framework.Constants; |
49 | 55 |
|
50 | 56 | /**
|
51 | 57 | * Creates a jar-based plugin and attaches it as an artifact
|
@@ -146,6 +152,16 @@ public class PackagePluginMojo extends AbstractTychoPackagingMojo {
|
146 | 152 | @Parameter
|
147 | 153 | private SourceReferences sourceReferences = new SourceReferences();
|
148 | 154 |
|
| 155 | + /** |
| 156 | + * Whether to derive OSGi-Headers from the maven-pom configuration, currently the following |
| 157 | + * header are supported |
| 158 | + * <ul> |
| 159 | + * <li>{@link Constants#BUNDLE_LICENSE} is generated from maven <licenses> configuration</li> |
| 160 | + * </ul> |
| 161 | + */ |
| 162 | + @Parameter(defaultValue = "true") |
| 163 | + private boolean deriveHeaderFromProject = true; |
| 164 | + |
149 | 165 | @Component
|
150 | 166 | private SourceReferenceComputer soureReferenceComputer;
|
151 | 167 |
|
@@ -276,11 +292,43 @@ protected Manifest getManifest() throws IOException, MojoExecutionException {
|
276 | 292 | if (attributes.getValue(Name.MANIFEST_VERSION) == null) {
|
277 | 293 | attributes.put(Name.MANIFEST_VERSION, "1.0");
|
278 | 294 | }
|
279 |
| - |
280 | 295 | ReactorProject reactorProject = DefaultReactorProject.adapt(project);
|
281 | 296 | attributes.putValue("Bundle-Version", reactorProject.getExpandedVersion());
|
282 | 297 | soureReferenceComputer.addSourceReferenceHeader(mf, sourceReferences, project);
|
| 298 | + if (deriveHeaderFromProject) { |
| 299 | + computeIfHeaderNotPresent(attributes, Constants.BUNDLE_LICENSE, () -> { |
| 300 | + List<License> licenses = project.getLicenses(); |
| 301 | + return licenses.stream().map(license -> { |
| 302 | + String name = license.getName(); |
| 303 | + if (name != null && !name.isBlank()) { |
| 304 | + StringBuilder licenseHeader = new StringBuilder(name); |
| 305 | + appendHeaderAttribute(licenseHeader, "link", license.getUrl()); |
| 306 | + return licenseHeader; |
| 307 | + } |
| 308 | + return null; |
| 309 | + }).filter(Objects::nonNull).map(String::valueOf).collect(Collectors.joining(",")); |
| 310 | + }); |
| 311 | + } |
283 | 312 | return mf;
|
284 | 313 | }
|
285 | 314 |
|
| 315 | + private static void appendHeaderAttribute(StringBuilder header, String attribute, String value) { |
| 316 | + if (value != null && !value.isBlank()) { |
| 317 | + header.append(";"); |
| 318 | + header.append(attribute); |
| 319 | + header.append("=\""); |
| 320 | + header.append(value); |
| 321 | + header.append("\""); |
| 322 | + } |
| 323 | + } |
| 324 | + |
| 325 | + private static void computeIfHeaderNotPresent(Attributes attributes, String hv, Supplier<String> headerComputer) { |
| 326 | + if (attributes.getValue(hv) == null) { |
| 327 | + String header = headerComputer.get(); |
| 328 | + if (header != null && !header.isBlank()) { |
| 329 | + attributes.putValue(hv, header); |
| 330 | + } |
| 331 | + } |
| 332 | + } |
| 333 | + |
286 | 334 | }
|
0 commit comments