From a8ea012d3b5ffae946e7e3377703b3fda409841d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Wed, 25 Oct 2023 12:45:33 +0200 Subject: [PATCH] Delete orphan files that are copied/generated by bnd automatic manifest Currently we copy everything that would normally go to the jar into the output folder, but if on the next build the resource is not put anymore we have some orphan files. This records what resources are created as part of the project build and finally deletes any orphans on close. Fix https://github.com/eclipse-pde/eclipse.pde/issues/587 --- .../pde/internal/core/bnd/FileResource.java | 2 +- .../pde/internal/core/bnd/ProjectJar.java | 42 ++++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/bnd/FileResource.java b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/bnd/FileResource.java index 7a58ea6b45..c78e6357ed 100644 --- a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/bnd/FileResource.java +++ b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/bnd/FileResource.java @@ -112,7 +112,7 @@ private static void addResources(Jar jar, IContainer container, String prefix, P return; } for (IResource resource : container.members()) { - if (filter == null || filter.test(container)) { + if (filter == null || filter.test(resource)) { if (resource instanceof IFile) { IPath projectRelativePath = resource.getProjectRelativePath(); String relativePath = projectRelativePath.toString(); diff --git a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/bnd/ProjectJar.java b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/bnd/ProjectJar.java index 8f6eb62df4..d7b3e94881 100644 --- a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/bnd/ProjectJar.java +++ b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/bnd/ProjectJar.java @@ -14,6 +14,8 @@ package org.eclipse.pde.internal.core.bnd; import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; import java.util.function.Predicate; import java.util.jar.JarFile; import java.util.jar.Manifest; @@ -27,9 +29,11 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.QualifiedName; import org.eclipse.jdt.core.IClasspathEntry; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.JavaCore; +import org.eclipse.pde.internal.core.PDECore; import org.eclipse.pde.internal.core.project.PDEProject; import aQute.bnd.osgi.Jar; @@ -38,13 +42,29 @@ public class ProjectJar extends Jar { + public static final QualifiedName GENERATED_PROPERTY = new QualifiedName(PDECore.PLUGIN_ID, "bndgenerated"); //$NON-NLS-1$ + private final IContainer outputFolder; private IFile manifestFile; + private final Map orphanFilesMap = new HashMap<>(); public ProjectJar(IProject project, Predicate filter) throws CoreException { super(project.getName()); outputFolder = PDEProject.getJavaOutputFolder(project); - FileResource.addResources(this, outputFolder, filter); + Predicate resourceScanner = r -> { + if (r instanceof IFile f) { + try { + String path = r.getPersistentProperty(GENERATED_PROPERTY); + if (path != null) { + orphanFilesMap.put(path, f); + } + } catch (CoreException e) { + // can't use that the ... + } + } + return filter.test(r); + }; + FileResource.addResources(this, outputFolder, resourceScanner); if (project.hasNature(JavaCore.NATURE_ID)) { IJavaProject javaProject = JavaCore.create(project); IWorkspaceRoot workspaceRoot = project.getWorkspace().getRoot(); @@ -54,7 +74,7 @@ public ProjectJar(IProject project, Predicate filter) throws CoreExce IPath location = cp.getOutputLocation(); if (location != null) { IFolder otherOutputFolder = workspaceRoot.getFolder(location); - FileResource.addResources(this, otherOutputFolder, filter); + FileResource.addResources(this, otherOutputFolder, resourceScanner); } } } @@ -127,11 +147,13 @@ public boolean putResource(String path, Resource resource, boolean overwrite) { file.create(stream, true, null); } } + file.setPersistentProperty(GENERATED_PROPERTY, path); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } + orphanFilesMap.remove(path); return super.putResource(path, new FileResource(file), overwrite); } @@ -152,4 +174,20 @@ public String toString() { return "Project" + super.toString(); //$NON-NLS-1$ } + @Override + public void close() { + cleanup(); + super.close(); + } + + private void cleanup() { + for (IFile file : orphanFilesMap.values()) { + try { + file.delete(true, null); + } catch (Exception e) { + // must wait for next clean then... + } + } + } + }