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.

(cherry picked from commit 9511187)
  • Loading branch information
laeubi authored and eclipse-tycho-bot committed Feb 2, 2025
1 parent 1a4ce42 commit 6c0287c
Show file tree
Hide file tree
Showing 8 changed files with 389 additions and 44 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,89 @@
/*******************************************************************************
* 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;

import java.io.File;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.annotations.Component;

import aQute.bnd.osgi.repository.ResourcesRepository;
import aQute.bnd.osgi.resource.ResourceBuilder;
import aQute.bnd.service.RepositoryPlugin;
import aQute.bnd.service.resource.SupportingResource;
import aQute.bnd.version.Version;

@Component(role = MavenReactorRepository.class)
public class MavenReactorRepository extends ResourcesRepository implements RepositoryPlugin {

@Override
public PutResult put(InputStream stream, PutOptions options) throws Exception {
throw new UnsupportedOperationException();
}

@Override
public File get(String bsn, Version version, Map<String, String> properties, DownloadListener... listeners)
throws Exception {
return null;
}

@Override
public boolean canWrite() {
return false;
}

@Override
public List<String> list(String pattern) throws Exception {
return Collections.emptyList();
}

@Override
public SortedSet<Version> versions(String bsn) throws Exception {
return Collections.emptySortedSet();
}

@Override
public String getName() {
return "Maven Reactor";
}

@Override
public String getLocation() {
return "reactor";
}

public void addProject(MavenProject project) {
addArtifact(project.getArtifact());
project.getAttachedArtifacts().forEach(this::addArtifact);
}

private void addArtifact(Artifact artifact) {
File file = artifact.getFile();
if (file == null || !file.getName().endsWith(".jar") || !file.isFile()) {
return;
}
try {
SupportingResource resource = ResourceBuilder.parse(file, file.toURI());
add(resource);
} catch (RuntimeException e) {

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@
import org.apache.maven.lifecycle.LifecycleExecutionException;
import org.apache.maven.plugin.MojoExecution;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.eclipse.tycho.bnd.MavenReactorRepository;

@Component(role = ProjectExecutionListener.class)
public class BndProjectExecutionListener implements ProjectExecutionListener {


@Requirement
private MavenReactorRepository mavenReactorRepository;

@Override
public void beforeProjectExecution(ProjectExecutionEvent event) throws LifecycleExecutionException {

Expand Down Expand Up @@ -74,7 +80,7 @@ private boolean matches(MojoExecution mojoExecution, String groupId, String arti

@Override
public void afterProjectExecutionSuccess(ProjectExecutionEvent event) throws LifecycleExecutionException {

mavenReactorRepository.addProject(event.getProject());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*******************************************************************************
* 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.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.eclipse.tycho.bnd.MavenReactorRepository;

import aQute.bnd.build.Workspace;
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;

@Component
protected MavenReactorRepository mavenReactorRepository;

protected Workspace getWorkspace() throws MojoFailureException {
try {
Workspace workspace = Workspace.getWorkspace(mavenProject.getBasedir().getParentFile());
if (workspace.getPlugins().stream().noneMatch(plugin -> plugin instanceof MavenReactorRepository)) {
workspace.addBasicPlugin(mavenReactorRepository);
workspace.refresh();
}
checkResult(workspace, workspace.isFailOk());
return workspace;
} catch (Exception e) {
throw new MojoFailureException("error while locating workspace!", e);
}
}

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,34 +12,23 @@
*******************************************************************************/
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 {
try {
Project bndProject = Workspace.getProject(mavenProject.getBasedir());
Workspace workspace = bndProject.getWorkspace();
checkResult(workspace, workspace.isFailOk());
Workspace wsp = getWorkspace();
Project bndProject = wsp.getProject(mavenProject.getBasedir().getName());
if (bndProject == null) {
getLog().info("Not a bnd workspace project!");
return;
}
synchronized (bndProject) {
execute(bndProject);
checkResult(bndProject, bndProject.isFailOk());
Expand All @@ -59,24 +48,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
Loading

0 comments on commit 6c0287c

Please sign in to comment.