diff --git a/tycho-bnd-plugin/src/main/java/org/eclipse/tycho/bnd/MavenProjectJar.java b/tycho-bnd-plugin/src/main/java/org/eclipse/tycho/bnd/MavenProjectJar.java index 51c1cb87bb..c1af88e098 100644 --- a/tycho-bnd-plugin/src/main/java/org/eclipse/tycho/bnd/MavenProjectJar.java +++ b/tycho-bnd-plugin/src/main/java/org/eclipse/tycho/bnd/MavenProjectJar.java @@ -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; @@ -34,7 +37,7 @@ public class MavenProjectJar extends Jar { private Path outputFolder; - public MavenProjectJar(MavenProject project, Predicate filter) throws IOException { + public MavenProjectJar(MavenProject project, Predicate filter, Log log) throws IOException { super(project.getId()); outputFolder = Path.of(project.getBuild().getOutputDirectory()); Files.walkFileTree(outputFolder, new FileVisitor() { @@ -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; } diff --git a/tycho-bnd-plugin/src/main/java/org/eclipse/tycho/bnd/mojos/GenerateManifestMojo.java b/tycho-bnd-plugin/src/main/java/org/eclipse/tycho/bnd/mojos/GenerateManifestMojo.java index ff410b5ee8..0d44b72ff0 100644 --- a/tycho-bnd-plugin/src/main/java/org/eclipse/tycho/bnd/mojos/GenerateManifestMojo.java +++ b/tycho-bnd-plugin/src/main/java/org/eclipse/tycho/bnd/mojos/GenerateManifestMojo.java @@ -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; @@ -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; @@ -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 @@ -63,7 +67,7 @@ public class GenerateManifestMojo extends AbstractMojo { private static final Predicate CLASS_FILTER = resource -> { if (Files.isRegularFile(resource)) { - return resource.getFileName().endsWith(".class"); + return resource.getFileName().toString().toLowerCase().endsWith(".class"); } return true; }; @@ -89,41 +93,25 @@ 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, @@ -131,11 +119,18 @@ public Jar getJarFromName(String name, String from) { List 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 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); + } } } } @@ -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) { @@ -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); + } + } + }