From 0d9b22fe348def0b51e54c1f9eabcf27b10442cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Thu, 8 Jul 2021 10:24:08 +0200 Subject: [PATCH] #177 Automatically translate maven-license information to osgi (#178) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bundle-Header Signed-off-by: Christoph Läubrich --- .../tycho/packaging/PackagePluginMojo.java | 52 ++++++++++++++++++- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/tycho-packaging-plugin/src/main/java/org/eclipse/tycho/packaging/PackagePluginMojo.java b/tycho-packaging-plugin/src/main/java/org/eclipse/tycho/packaging/PackagePluginMojo.java index 36822842bf..8e28ef5288 100644 --- a/tycho-packaging-plugin/src/main/java/org/eclipse/tycho/packaging/PackagePluginMojo.java +++ b/tycho-packaging-plugin/src/main/java/org/eclipse/tycho/packaging/PackagePluginMojo.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2011 Sonatype Inc. and others. + * 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 @@ -9,6 +9,7 @@ * * 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; @@ -20,13 +21,17 @@ 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; @@ -46,6 +51,7 @@ 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 @@ -146,6 +152,16 @@ public class PackagePluginMojo extends AbstractTychoPackagingMojo { @Parameter private SourceReferences sourceReferences = new SourceReferences(); + /** + * Whether to derive OSGi-Headers from the maven-pom configuration, currently the following + * header are supported + * + */ + @Parameter(defaultValue = "true") + private boolean deriveHeaderFromProject = true; + @Component private SourceReferenceComputer soureReferenceComputer; @@ -276,11 +292,43 @@ protected Manifest getManifest() throws IOException, MojoExecutionException { 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 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 headerComputer) { + if (attributes.getValue(hv) == null) { + String header = headerComputer.get(); + if (header != null && !header.isBlank()) { + attributes.putValue(hv, header); + } + } + } + }