Skip to content

Commit

Permalink
[Core] delete obsolete ".m2/.cache/m2e/" directory and old logback logs
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Oct 13, 2022
1 parent 03b0152 commit 85119c2
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

package org.eclipse.m2e.core.ui.internal;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;
Expand All @@ -22,16 +27,31 @@
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.resource.ResourceLocator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.ui.preferences.ScopedPreferenceStore;
import org.eclipse.ui.progress.UIJob;

import org.codehaus.plexus.util.FileUtils;

import org.eclipse.m2e.core.internal.IMavenConstants;
import org.eclipse.m2e.core.internal.MavenPluginActivator;
import org.eclipse.m2e.core.internal.lifecyclemapping.discovery.IMavenDiscovery;
import org.eclipse.m2e.core.ui.internal.archetype.ArchetypePlugin;
import org.eclipse.m2e.core.ui.internal.console.MavenConsoleImpl;
Expand Down Expand Up @@ -87,6 +107,17 @@ public void start(BundleContext context) throws Exception {
mavenUpdateConfigurationChangeListener = new MavenUpdateConfigurationChangeListener();
workspace.addResourceChangeListener(mavenUpdateConfigurationChangeListener, IResourceChangeEvent.POST_CHANGE);

// Automatically delete obsolete caches
// TODO: can be removed when some time has passed and it is unlikely old workspaces that need clean-up are used.
MavenPluginActivator mavenPlugin = MavenPluginActivator.getDefault();
IPath nexusCache = Platform.getStateLocation(mavenPlugin.getBundle()).append("nexus");
FileUtils.deleteDirectory(nexusCache.toFile());

File localRepo = mavenPlugin.getRepositoryRegistry().getLocalRepository().getBasedir();
Path m2eCache = localRepo.toPath().resolve(".cache/m2e/");
if(Files.isDirectory(m2eCache)) {
deleteLegacyCacheDirectory(m2eCache);
}
}

@Override
Expand Down Expand Up @@ -145,14 +176,6 @@ public synchronized IMavenDiscovery getMavenDiscovery() {
return null;
}

/**
* @param discovery
*/
public void ungetMavenDiscovery(IMavenDiscovery discovery) {
// TODO Auto-generated method ungetMavenDiscovery

}

/**
* @return
*/
Expand Down Expand Up @@ -180,4 +203,48 @@ public ArchetypePlugin getArchetypePlugin() {
return this.archetypeManager.getService();
}

private static void deleteLegacyCacheDirectory(Path m2eCache) {
Path resolve = m2eCache.resolve("DELETE_ME.txt");
if(Files.isRegularFile(resolve)) {
return;
}
new UIJob("Delete legacy M2E cache") {
@Override
public IStatus runInUIThread(IProgressMonitor monitor) {
boolean[] askAgain = new boolean[] {true};
MessageDialog dialog = new MessageDialog(getDisplay().getActiveShell(), "Delete obsolete M2E cache?", null,
"A cache directory used by previous M2E versions was detected:\n\n" + m2eCache + "\n\n"
+ "It's no longer used by newer M2E versions and, unless older Eclipse installations need it, can be safely deleted.",
MessageDialog.QUESTION, 0, "Keep Cache", "Delete Cache") {
@Override
protected Control createCustomArea(Composite parent) {
Button checkbox = new Button(parent, SWT.CHECK);
checkbox.setText("Don't ask me again?");
checkbox.addSelectionListener(
SelectionListener.widgetSelectedAdapter(e -> askAgain[0] = !checkbox.getSelection()));
return super.createCustomArea(parent);
}
};
int selection = dialog.open();
if(selection == 1) {
try {
FileUtils.deleteDirectory(m2eCache.toFile());
} catch(IOException e) {
return Status.error("Failed to delete legacy M2E cache", e);
}
} else if(!askAgain[0]) {
try {
Files.writeString(resolve,
"""
This cache directory was created by a previous Maven2Eclipse 1.x version and is no longer used since M2E 2.0.
Unless older Eclipse installations need it, it can be deleted safely."
""");
} catch(IOException ex) { // Ignore
}
}
return Status.OK_STATUS;
}
}.schedule(10_000);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ public void init(IWorkbench workbench, IStructuredSelection selection) {
}
}

@Override
public void dispose() {
M2EUIPluginActivator.getDefault().ungetMavenDiscovery(discovery);
super.dispose();
}

public ProjectImportConfiguration getProjectImportConfiguration() {
return importConfiguration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@

import org.eclipse.aether.RepositorySystem;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;

import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.util.FileUtils;

import org.apache.maven.project.DefaultProjectBuilder;

Expand Down Expand Up @@ -118,9 +115,6 @@ public void start(final BundleContext context) throws Exception {
this.protocolHandlerService = context.registerService(URLStreamHandlerService.class,
new MvnProtocolHandlerService(), FrameworkUtil.asDictionary(properties));

// Automatically delete now obsolete nexus cache (can be removed again if some time has passed and it is unlikely an old workspace that need to be cleaned up is used).
IPath nexusCache = Platform.getStateLocation(context.getBundle()).append("nexus");
FileUtils.deleteDirectory(nexusCache.toFile());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@

package org.eclipse.m2e.logback.configuration;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;
import java.util.SortedMap;
import java.util.Timer;
Expand Down Expand Up @@ -69,22 +70,35 @@ public void configure(LoggerContext lc) {
private synchronized void configureLogback(LoggerContext lc) {
try {
Bundle bundle = Platform.getBundle("org.eclipse.m2e.logback"); // This is a fragment -> FrameworkUtil.getBundle() returns host
File stateDir = Platform.getStateLocation(bundle).toFile();
File configFile = new File(stateDir, "logback." + bundle.getVersion() + ".xml"); //$NON-NLS-1$ //$NON-NLS-2$
LOG.info("Logback config file: " + configFile.getAbsolutePath()); //$NON-NLS-1$
Path stateDir = Platform.getStateLocation(bundle).toFile().toPath();
Path configFile = stateDir.resolve("logback." + bundle.getVersion() + ".xml"); //$NON-NLS-1$ //$NON-NLS-2$
LOG.info("Logback config file: " + configFile.toAbsolutePath()); //$NON-NLS-1$

if(!configFile.isFile()) {
if(!Files.isRegularFile(configFile)) {
// Copy the default config file to the actual config file, to allow user adjustments
try (InputStream is = bundle.getEntry("defaultLogbackConfiguration/logback.xml").openStream()) { //$NON-NLS-1$
configFile.getParentFile().mkdirs();
Files.copy(is, configFile.toPath());
Files.createDirectories(configFile.getParent());
Files.copy(is, configFile);
}
}
if(System.getProperty(PROPERTY_LOG_DIRECTORY, "").length() <= 0) { //$NON-NLS-1$
System.setProperty(PROPERTY_LOG_DIRECTORY, stateDir.getAbsolutePath());
System.setProperty(PROPERTY_LOG_DIRECTORY, stateDir.toAbsolutePath().toString());
}
loadConfiguration(lc, configFile.toUri().toURL());

//Delete old logs in legacy logback plug-in's state location. Can sum up to 1GB of disk-space.
// TODO: can be removed when some time has passed and it is unlikely old workspaces that need clean-up are used.
Path legacyLogbackState = stateDir.resolveSibling("org.eclipse.m2e.logback.configuration");
if(Files.isDirectory(legacyLogbackState)) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(legacyLogbackState)) {
for(Path path : stream) {
if(Files.isRegularFile(path)) {
Files.delete(path);
}
}
}
Files.delete(legacyLogbackState);
}
loadConfiguration(lc, configFile.toURI().toURL());

} catch(Exception e) {
LOG.log(Status.warning("Exception while setting up logging:" + e.getMessage(), e)); //$NON-NLS-1$
}
Expand Down

0 comments on commit 85119c2

Please sign in to comment.