Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tycho-4.0.x] Fix wrong (bnd resource) path is used under windows #3871

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import java.util.function.Predicate;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;

import aQute.bnd.osgi.Jar;
Expand All @@ -34,7 +37,7 @@ public class MavenProjectJar extends Jar {

private Path outputFolder;

public MavenProjectJar(MavenProject project, Predicate<Path> filter) throws IOException {
public MavenProjectJar(MavenProject project, Predicate<Path> filter, Log log) throws IOException {
super(project.getId());
outputFolder = Path.of(project.getBuild().getOutputDirectory());
Files.walkFileTree(outputFolder, new FileVisitor<Path>() {
Expand All @@ -46,8 +49,15 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
String path = outputFolder.relativize(file).toString();
putResource(path, new MavenProjectResource(file));
Path relativePath = outputFolder.relativize(file);
if (filter.test(file)) {
String path = StreamSupport.stream(relativePath.spliterator(), false).map(Path::toString)
.collect(Collectors.joining("/"));
log.debug("Adding " + path + " to project jar...");
putResource(path, new MavenProjectResource(file));
} else {
log.debug("Ignore " + relativePath + " because it is filtered");
}
return FileVisitResult.CONTINUE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
*******************************************************************************/
package org.eclipse.tycho.bnd.mojos;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.function.Predicate;
import java.util.jar.Manifest;
import java.util.regex.Matcher;
import java.util.stream.Collectors;

Expand All @@ -26,6 +28,7 @@
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
Expand Down Expand Up @@ -53,6 +56,7 @@
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.Processor;
import aQute.lib.manifest.ManifestUtil;

/**
* The mojos support generation of the manifest file like it is done in PDE if
Expand All @@ -63,7 +67,7 @@ public class GenerateManifestMojo extends AbstractMojo {

private static final Predicate<Path> CLASS_FILTER = resource -> {
if (Files.isRegularFile(resource)) {
return resource.getFileName().endsWith(".class");
return resource.getFileName().toString().toLowerCase().endsWith(".class");
}
return true;
};
Expand All @@ -89,53 +93,44 @@ public void execute() throws MojoExecutionException, MojoFailureException {
File basedir = mavenProject.getBasedir();
File instructionsFile = new File(basedir, TychoConstants.PDE_BND);
if (instructionsFile.isFile()) {
Log log = getLog();
log.debug("Generate final manifest for project " + mavenProject.getId());
try (Project project = new Project(getWorkspace(), basedir, instructionsFile);
ProjectBuilder builder = new ProjectBuilder(project) {
@Override
public Jar getJarFromName(String name, String from) {
Matcher m = TychoConstants.PLATFORM_URL_PATTERN.matcher(name);
if (m.matches()) {
TargetPlatform targetPlatform = projectManager.getTargetPlatform(mavenProject)
.orElse(null);
if (targetPlatform == null) {
return null;
}
String pluginId = m.group(2);
try {
ArtifactKey artifact = targetPlatform
.resolveArtifact(ArtifactType.TYPE_ECLIPSE_PLUGIN, pluginId, null);
File artifactLocation = targetPlatform.getArtifactLocation(artifact);
if (artifactLocation == null) {
return null;
}
return new Jar(artifactLocation);
} catch (Exception e) {
return null;
}
}
return super.getJarFromName(name, from);
}
};
Jar jar = new MavenProjectJar(mavenProject, CLASS_FILTER)) {
ProjectBuilder builder = new AutomaticManifestProjectBuilder(project);
Jar jar = new MavenProjectJar(mavenProject, CLASS_FILTER, log)) {
setupProject(project);
BundleClassPath bundleClassPath = osgi
.getBundleClassPath(DefaultReactorProject.adapt(mavenProject));
builder.setBase(project.getBase());
builder.setJar(jar);
for (ClasspathEntry cpe : bundleClassPath.getClasspathEntries()) {
cpe.getLocations().forEach(project::addClasspath);
cpe.getLocations().forEach(cp -> {
log.debug("Adding classpath " + cp);
try {
project.addClasspath(cp);
} catch (RuntimeException e) {
getLog().warn("Adding classpath file " + cp + " failed: " + e);
}
});
}
try {
pluginRealmHelper.visitPluginExtensions(mavenProject, session, ClasspathContributor.class,
cpc -> {
List<ClasspathEntry> list = cpc.getAdditionalClasspathEntries(mavenProject,
Artifact.SCOPE_COMPILE);
if (list != null && !list.isEmpty()) {
log.debug("Adding additional classpath entries from contributor "
+ cpc.getClass().getSimpleName());
for (ClasspathEntry entry : list) {
try {
builder.addClasspath(entry.getLocations());
} catch (IOException e) {
e.printStackTrace();
List<File> locations = entry.getLocations();
for (File file : locations) {
try {
log.debug(" --> " + file);
builder.addClasspath(file);
} catch (Exception e) {
log.warn("Adding additional classpath file " + file + " failed: "
+ e);
}
}
}
}
Expand All @@ -144,6 +139,17 @@ public Jar getJarFromName(String name, String from) {
throw new MojoExecutionException("can't call classpath contributors", e);
}
builder.build();
builder.getWarnings().forEach(log::warn);
builder.getErrors().forEach(log::error);
Manifest manifest = jar.getManifest();
if (manifest == null) {
log.debug("No Manifest was generated!");
} else if (log.isDebugEnabled()) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ManifestUtil.write(manifest, outputStream);
String str = new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
log.debug("Generated final manifest for " + mavenProject.getId() + ":\r\n" + str);
}
} catch (MojoExecutionException e) {
throw e;
} catch (Exception e) {
Expand All @@ -170,4 +176,34 @@ Workspace getWorkspace() throws Exception {
return workspace;
}

private final class AutomaticManifestProjectBuilder extends ProjectBuilder {
private AutomaticManifestProjectBuilder(Project project) {
super(project);
}

@Override
public Jar getJarFromName(String name, String from) {
Matcher m = TychoConstants.PLATFORM_URL_PATTERN.matcher(name);
if (m.matches()) {
TargetPlatform targetPlatform = projectManager.getTargetPlatform(mavenProject).orElse(null);
if (targetPlatform == null) {
return null;
}
String pluginId = m.group(2);
try {
ArtifactKey artifact = targetPlatform.resolveArtifact(ArtifactType.TYPE_ECLIPSE_PLUGIN, pluginId,
null);
File artifactLocation = targetPlatform.getArtifactLocation(artifact);
if (artifactLocation == null) {
return null;
}
return new Jar(artifactLocation);
} catch (Exception e) {
return null;
}
}
return super.getJarFromName(name, from);
}
}

}
Loading