Skip to content

Commit

Permalink
Add support for exporting bndruns in bndworkspace projects
Browse files Browse the repository at this point in the history
This adds support for "building" bndrun files by exporting them into a
runnable jar.
  • Loading branch information
laeubi committed Feb 2, 2025
1 parent 9330061 commit 708520b
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 40 deletions.
5 changes: 5 additions & 0 deletions tycho-bnd-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
<artifactId>biz.aQute.bnd.embedded-repo</artifactId>
<version>${bnd.version}</version>
</dependency>
<dependency>
<groupId>biz.aQute.bnd</groupId>
<artifactId>biz.aQute.resolve</artifactId>
<version>${bnd.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
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())));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,13 @@
*******************************************************************************/
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.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import aQute.bnd.build.Project;
import aQute.bnd.build.Workspace;
import aQute.service.reporter.Report;

public abstract class AbstractBndProjectMojo extends AbstractMojo {

@Parameter(property = "project", readonly = true)
protected MavenProject mavenProject;

@Parameter(property = "session", readonly = true)
protected MavenSession session;
public abstract class AbstractBndProjectMojo extends AbstractBndMojo {

@Override
public final void execute() throws MojoExecutionException, MojoFailureException {
Expand All @@ -59,24 +45,5 @@ public final void execute() throws MojoExecutionException, MojoFailureException

}

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())));
}
}

protected abstract void execute(Project project) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,15 @@
import org.apache.maven.model.Model;
import org.apache.maven.model.io.ModelReader;
import org.apache.maven.model.io.ModelWriter;
import org.apache.maven.plugin.AbstractMojo;
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.project.MavenProject;

@Mojo(name = "initialize", defaultPhase = LifecyclePhase.INITIALIZE)
public class BndInitMojo extends AbstractMojo {

@Parameter(property = "project", readonly = true)
protected MavenProject mavenProject;
public class BndInitMojo extends AbstractBndMojo {

/**
* The filename of the tycho generated POM file.
Expand Down
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) {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ protected void initModel(Model model, Reader artifactReader, Path artifactFile)
addPluginExecution(bndPlugin, execution -> {
execution.setId("build");
execution.addGoal("build");
execution.addGoal("run");
});
} catch (Exception e) {
if (e instanceof IOException io) {
Expand Down

0 comments on commit 708520b

Please sign in to comment.