Skip to content

Commit 0d9b22f

Browse files
authored
#177 Automatically translate maven-license information to osgi (#178)
Bundle-Header Signed-off-by: Christoph Läubrich <laeubi@laeubi-soft.de>
1 parent ce14ac0 commit 0d9b22f

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

tycho-packaging-plugin/src/main/java/org/eclipse/tycho/packaging/PackagePluginMojo.java

+50-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2008, 2011 Sonatype Inc. and others.
2+
* Copyright (c) 2008, 2021 Sonatype Inc. and others.
33
* This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License 2.0
55
* which accompanies this distribution, and is available at
@@ -9,6 +9,7 @@
99
*
1010
* Contributors:
1111
* Sonatype Inc. - initial API and implementation
12+
* Christoph Läubrich - Automatically translate maven-pom information to osgi Bundle-Header #177
1213
*******************************************************************************/
1314
package org.eclipse.tycho.packaging;
1415

@@ -20,13 +21,17 @@
2021
import java.io.InputStream;
2122
import java.util.ArrayList;
2223
import java.util.List;
24+
import java.util.Objects;
25+
import java.util.function.Supplier;
2326
import java.util.jar.Attributes;
2427
import java.util.jar.Attributes.Name;
2528
import java.util.jar.Manifest;
29+
import java.util.stream.Collectors;
2630

2731
import org.apache.maven.archiver.MavenArchiveConfiguration;
2832
import org.apache.maven.archiver.MavenArchiver;
2933
import org.apache.maven.artifact.DependencyResolutionRequiredException;
34+
import org.apache.maven.model.License;
3035
import org.apache.maven.plugin.MojoExecutionException;
3136
import org.apache.maven.plugins.annotations.Component;
3237
import org.apache.maven.plugins.annotations.Mojo;
@@ -46,6 +51,7 @@
4651
import org.eclipse.tycho.core.shared.BuildProperties;
4752
import org.eclipse.tycho.packaging.sourceref.SourceReferenceComputer;
4853
import org.eclipse.tycho.packaging.sourceref.SourceReferencesProvider;
54+
import org.osgi.framework.Constants;
4955

5056
/**
5157
* Creates a jar-based plugin and attaches it as an artifact
@@ -146,6 +152,16 @@ public class PackagePluginMojo extends AbstractTychoPackagingMojo {
146152
@Parameter
147153
private SourceReferences sourceReferences = new SourceReferences();
148154

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 &lt;licenses> configuration</li>
160+
* </ul>
161+
*/
162+
@Parameter(defaultValue = "true")
163+
private boolean deriveHeaderFromProject = true;
164+
149165
@Component
150166
private SourceReferenceComputer soureReferenceComputer;
151167

@@ -276,11 +292,43 @@ protected Manifest getManifest() throws IOException, MojoExecutionException {
276292
if (attributes.getValue(Name.MANIFEST_VERSION) == null) {
277293
attributes.put(Name.MANIFEST_VERSION, "1.0");
278294
}
279-
280295
ReactorProject reactorProject = DefaultReactorProject.adapt(project);
281296
attributes.putValue("Bundle-Version", reactorProject.getExpandedVersion());
282297
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+
}
283312
return mf;
284313
}
285314

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+
286334
}

0 commit comments

Comments
 (0)