-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for exporting bndruns in bndworkspace projects
This adds support for "building" bndrun files by exporting them into a runnable jar.
- Loading branch information
Showing
6 changed files
with
214 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
tycho-bnd-plugin/src/main/java/org/eclipse/tycho/bnd/mojos/AbstractBndMojo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2025 Christoph Läubrich 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: | ||
* Christoph Läubrich - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.tycho.bnd.mojos; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.apache.maven.execution.MavenSession; | ||
import org.apache.maven.plugin.AbstractMojo; | ||
import org.apache.maven.plugin.MojoFailureException; | ||
import org.apache.maven.plugins.annotations.Parameter; | ||
import org.apache.maven.project.MavenProject; | ||
|
||
import aQute.service.reporter.Report; | ||
|
||
public abstract class AbstractBndMojo extends AbstractMojo { | ||
|
||
@Parameter(property = "project", readonly = true) | ||
protected MavenProject mavenProject; | ||
|
||
@Parameter(property = "session", readonly = true) | ||
protected MavenSession session; | ||
|
||
protected void checkResult(Report report, boolean errorOk) throws MojoFailureException { | ||
List<String> warnings = report.getWarnings(); | ||
for (String warning : warnings) { | ||
getLog().warn(warning); | ||
} | ||
warnings.clear(); | ||
List<String> errors = report.getErrors(); | ||
for (String error : errors) { | ||
getLog().error(error); | ||
} | ||
if (errorOk) { | ||
errors.clear(); | ||
return; | ||
} | ||
if (errors.size() > 0) { | ||
throw new MojoFailureException(errors.stream().collect(Collectors.joining(System.lineSeparator()))); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
tycho-bnd-plugin/src/main/java/org/eclipse/tycho/bnd/mojos/BndRunMojo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2025 Christoph Läubrich 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: | ||
* Christoph Läubrich - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.tycho.bnd.mojos; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.attribute.FileTime; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.plugin.MojoFailureException; | ||
import org.apache.maven.plugins.annotations.Component; | ||
import org.apache.maven.plugins.annotations.LifecyclePhase; | ||
import org.apache.maven.plugins.annotations.Mojo; | ||
import org.apache.maven.plugins.annotations.Parameter; | ||
import org.apache.maven.plugins.annotations.ResolutionScope; | ||
import org.apache.maven.project.MavenProjectHelper; | ||
|
||
import aQute.bnd.build.Workspace; | ||
import aQute.bnd.exporter.executable.ExecutableJarExporter; | ||
import aQute.bnd.exporter.runbundles.RunbundlesExporter; | ||
import aQute.bnd.osgi.JarResource; | ||
import aQute.bnd.osgi.Resource; | ||
import biz.aQute.resolve.Bndrun; | ||
|
||
@Mojo(name = "run", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true) | ||
public class BndRunMojo extends AbstractBndMojo { | ||
|
||
private static final String BNDRUN = ".bndrun"; | ||
|
||
@Parameter(property = "bndrun.exports") | ||
private Set<String> exports; | ||
|
||
@Parameter | ||
private Map<String, String> options; | ||
|
||
@Parameter(property = "bndrun.exporter", defaultValue = ExecutableJarExporter.EXECUTABLE_JAR) | ||
private String exporter; | ||
|
||
@Parameter(property = "bndrun.attatch", defaultValue = "true") | ||
private boolean attatch; | ||
|
||
@Component | ||
MavenProjectHelper helper; | ||
|
||
@Override | ||
public void execute() throws MojoExecutionException, MojoFailureException { | ||
if (exports == null || exports.isEmpty()) { | ||
return; | ||
} | ||
List<BndRunFile> bndRuns = getBndRuns(); | ||
if (bndRuns.isEmpty()) { | ||
return; | ||
} | ||
Workspace workspace = getWorkspace(); | ||
checkResult(workspace, workspace.isFailOk()); | ||
for (BndRunFile bndRunFile : bndRuns) { | ||
getLog().info(String.format("Exporting %s ...", bndRunFile.name())); | ||
Bndrun run = getBndRun(workspace, bndRunFile); | ||
Path export = export(run, bndRunFile); | ||
if (export != null) { | ||
getLog().info(String.format("Exported to %s", export)); | ||
if (attatch && Files.isRegularFile(export)) { | ||
helper.attachArtifact(mavenProject, "zip", bndRunFile.name(), export.toFile()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private Path export(Bndrun run, BndRunFile bndRunFile) throws MojoFailureException { | ||
try { | ||
Entry<String, Resource> result = run.export(exporter, Objects.requireNonNullElse(options, Map.of())); | ||
try (Resource export = result.getValue()) { | ||
if (exporter.equals(RunbundlesExporter.RUNBUNDLES)) { | ||
if (export instanceof JarResource jar) { | ||
Path exportPath = Path.of(mavenProject.getBuild().getDirectory()).resolve("export") | ||
.resolve(bndRunFile.name()); | ||
jar.getJar().writeFolder(exportPath.toFile()); | ||
return exportPath; | ||
} | ||
return null; | ||
} else { | ||
Path exportPath = Path.of(mavenProject.getBuild().getDirectory()) | ||
.resolve(bndRunFile.name() + ".zip"); | ||
export.write(exportPath); | ||
Files.setLastModifiedTime(exportPath, FileTime.fromMillis(export.lastModified())); | ||
return exportPath; | ||
} | ||
} | ||
} catch (Exception e) { | ||
throw new MojoFailureException("error exporting bnd run!", e); | ||
} | ||
} | ||
|
||
private Bndrun getBndRun(Workspace workspace, BndRunFile run) throws MojoFailureException { | ||
try { | ||
return Bndrun.createBndrun(workspace, run.path().toFile()); | ||
} catch (Exception e) { | ||
throw new MojoFailureException("error creating bnd run!", e); | ||
} | ||
} | ||
|
||
private Workspace getWorkspace() throws MojoFailureException { | ||
try { | ||
return Workspace.findWorkspace(mavenProject.getBasedir()); | ||
} catch (Exception e) { | ||
throw new MojoFailureException("error while locating workspace!", e); | ||
} | ||
} | ||
|
||
private List<BndRunFile> getBndRuns() throws MojoExecutionException { | ||
List<BndRunFile> bndRuns = new ArrayList<>(); | ||
Path path = mavenProject.getBasedir().toPath(); | ||
try { | ||
Iterator<Path> iterator = Files.list(path).iterator(); | ||
while (iterator.hasNext()) { | ||
Path child = iterator.next(); | ||
String fn = child.getFileName().toString(); | ||
if (fn.endsWith(BNDRUN)) { | ||
String key = fn.substring(0, fn.length() - BNDRUN.length()); | ||
if (exports.contains(key)) { | ||
bndRuns.add(new BndRunFile(key, path)); | ||
} | ||
} | ||
} | ||
} catch (IOException e) { | ||
throw new MojoExecutionException(e); | ||
} | ||
return bndRuns; | ||
} | ||
|
||
private static record BndRunFile(String name, Path path) { | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters