|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 Christoph Läubrich and others. |
| 3 | + * This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Christoph Läubrich - initial API and implementation |
| 12 | + ******************************************************************************/ |
| 13 | +package org.eclipse.tycho.wrap; |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | +import java.util.Set; |
| 17 | +import java.util.jar.Attributes; |
| 18 | +import java.util.jar.JarFile; |
| 19 | +import java.util.jar.Manifest; |
| 20 | +import java.util.regex.Pattern; |
| 21 | + |
| 22 | +import org.apache.maven.artifact.Artifact; |
| 23 | +import org.apache.maven.plugin.AbstractMojo; |
| 24 | +import org.apache.maven.plugin.MojoExecution; |
| 25 | +import org.apache.maven.plugin.MojoExecutionException; |
| 26 | +import org.apache.maven.plugin.MojoFailureException; |
| 27 | +import org.apache.maven.plugins.annotations.Component; |
| 28 | +import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 29 | +import org.apache.maven.plugins.annotations.Mojo; |
| 30 | +import org.apache.maven.plugins.annotations.Parameter; |
| 31 | +import org.apache.maven.project.MavenProject; |
| 32 | +import org.apache.maven.project.MavenProjectHelper; |
| 33 | +import org.apache.maven.settings.Settings; |
| 34 | +import org.osgi.framework.Constants; |
| 35 | + |
| 36 | +import aQute.bnd.build.Project; |
| 37 | +import aQute.bnd.maven.lib.configuration.BndConfiguration; |
| 38 | +import aQute.bnd.osgi.Analyzer; |
| 39 | +import aQute.bnd.osgi.Jar; |
| 40 | +import aQute.bnd.print.JarPrinter; |
| 41 | +import aQute.bnd.version.MavenVersion; |
| 42 | +import aQute.bnd.version.Version; |
| 43 | + |
| 44 | +@Mojo(name = "wrap", requiresProject = true, threadSafe = true, defaultPhase = LifecyclePhase.PACKAGE) |
| 45 | +public class WrapMojo extends AbstractMojo { |
| 46 | + |
| 47 | + private static final String[] HEADERS = { Constants.BUNDLE_SYMBOLICNAME, Constants.BUNDLE_VERSION }; |
| 48 | + |
| 49 | + @Component |
| 50 | + private MavenProject project; |
| 51 | + |
| 52 | + @Parameter(defaultValue = "${settings}", readonly = true) |
| 53 | + Settings settings; |
| 54 | + |
| 55 | + @Component |
| 56 | + private MojoExecution mojoExecution; |
| 57 | + |
| 58 | + @Component |
| 59 | + private MavenProjectHelper helper; |
| 60 | + |
| 61 | + /** |
| 62 | + * File path to a bnd file containing bnd instructions for this project. |
| 63 | + * Defaults to {@code bnd.bnd}. The file path can be an absolute or relative to |
| 64 | + * the project directory. |
| 65 | + * <p> |
| 66 | + * The bnd instructions for this project are merged with the bnd instructions, |
| 67 | + * if any, for the parent project. |
| 68 | + */ |
| 69 | + // This is not used and is for doc only; see |
| 70 | + // BndConfiguration#loadProperties and |
| 71 | + // AbstractBndMavenPlugin for reference |
| 72 | + @Parameter(defaultValue = Project.BNDFILE) |
| 73 | + String bndfile; |
| 74 | + |
| 75 | + /** |
| 76 | + * Bnd instructions for this project specified directly in the pom file. This is |
| 77 | + * generally be done using a {@code <![CDATA[]]>} section. If the project has a |
| 78 | + * {@link #bndfile}, then this configuration element is ignored. |
| 79 | + * <p> |
| 80 | + * The bnd instructions for this project are merged with the bnd instructions, |
| 81 | + * if any, for the parent project. |
| 82 | + */ |
| 83 | + // This is not used and is for doc only; see |
| 84 | + // BndConfiguration#loadProperties and |
| 85 | + // AbstractBndMavenPlugin for reference |
| 86 | + @Parameter |
| 87 | + String bnd; |
| 88 | + |
| 89 | + @Parameter(required = true, property = "input", defaultValue = "${project.build.directory}/${project.build.finalName}.${project.packaging}") |
| 90 | + private File input; |
| 91 | + |
| 92 | + @Parameter(required = true, property = "output", defaultValue = "${project.build.directory}/${project.build.finalName}-bundle.${project.packaging}") |
| 93 | + private File output; |
| 94 | + |
| 95 | + /** |
| 96 | + * If enabled attach the generated file as an artifact to the project |
| 97 | + */ |
| 98 | + @Parameter(required = false, defaultValue = "true", property = "attach") |
| 99 | + private boolean attach; |
| 100 | + |
| 101 | + /** |
| 102 | + * The classifier to use when attach this to the project |
| 103 | + */ |
| 104 | + @Parameter(defaultValue = "bundle", property = "classifier") |
| 105 | + private String classifier; |
| 106 | + |
| 107 | + @Override |
| 108 | + public void execute() throws MojoExecutionException, MojoFailureException { |
| 109 | + BndConfiguration configuration = new BndConfiguration(project, mojoExecution); |
| 110 | + |
| 111 | + try (Jar jar = new Jar(output.getName(), input, Pattern.compile(JarFile.MANIFEST_NAME)); |
| 112 | + Analyzer analyzer = new Analyzer(jar)) { |
| 113 | + configuration.loadProperties(analyzer); |
| 114 | + if (analyzer.getProperty(Constants.BUNDLE_VERSION) == null) { |
| 115 | + Version version = new MavenVersion(project.getVersion()).getOSGiVersion(); |
| 116 | + analyzer.setProperty(Constants.BUNDLE_VERSION, version.toString()); |
| 117 | + } |
| 118 | + if (analyzer.getProperty(Constants.BUNDLE_SYMBOLICNAME) == null) { |
| 119 | + analyzer.setProperty(Constants.BUNDLE_SYMBOLICNAME, project.getArtifactId()); |
| 120 | + } |
| 121 | + if (analyzer.getProperty(Constants.BUNDLE_NAME) == null) { |
| 122 | + analyzer.setProperty(Constants.BUNDLE_NAME, project.getName()); |
| 123 | + } |
| 124 | + Set<Artifact> artifacts = project.getArtifacts(); |
| 125 | + for (Artifact artifact : artifacts) { |
| 126 | + File cpe = artifact.getFile(); |
| 127 | + try { |
| 128 | + analyzer.addClasspath(cpe); |
| 129 | + } catch (Exception e) { |
| 130 | + // just go on... it might be not a jar or something else not usable |
| 131 | + } |
| 132 | + } |
| 133 | + Manifest manifest = analyzer.calcManifest(); |
| 134 | + jar.setManifest(manifest); |
| 135 | + jar.write(output); |
| 136 | + analyzer.getWarnings().forEach(getLog()::warn); |
| 137 | + analyzer.getErrors().forEach(getLog()::error); |
| 138 | + Attributes mainAttributes = manifest.getMainAttributes(); |
| 139 | + for (String header : HEADERS) { |
| 140 | + getLog().info(header + ": " + mainAttributes.getValue(header)); |
| 141 | + } |
| 142 | + try (JarPrinter jarPrinter = new JarPrinter()) { |
| 143 | + jarPrinter.doPrint(jar, JarPrinter.IMPEXP, false, false); |
| 144 | + getLog().info(jarPrinter.toString()); |
| 145 | + } |
| 146 | + } catch (MojoFailureException e) { |
| 147 | + throw e; |
| 148 | + } catch (MojoExecutionException e) { |
| 149 | + throw e; |
| 150 | + } catch (Exception e) { |
| 151 | + throw new MojoFailureException("wrapping input " + input + " failed: " + e, e); |
| 152 | + } |
| 153 | + if (attach) { |
| 154 | + helper.attachArtifact(project, output, classifier); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments