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

Fix #40 Cannot clean on Windows as long as mvnd keeps a plugin from t… #45

Merged
merged 2 commits into from
Jul 29, 2020
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 @@ -416,13 +416,14 @@ void container()
// }

final CoreExports exports = new CoreExports(containerRealm, exportedArtifacts, exportedPackages);
final CliPluginRealmCache realmCache = new CliPluginRealmCache();

container = new DefaultPlexusContainer(cc, new AbstractModule() {
@Override
protected void configure() {
bind(ILoggerFactory.class).toInstance(slf4jLoggerFactory);
bind(CoreExports.class).toInstance(exports);
bind(PluginRealmCache.class).toInstance(new CliPluginRealmCache());
bind(PluginRealmCache.class).toInstance(realmCache);
}
});

Expand All @@ -440,6 +441,7 @@ protected void configure() {
// container.getLoggerManager().setThresholds( cliRequest.request.getLoggingLevel() );

eventSpyDispatcher = container.lookup(EventSpyDispatcher.class);
eventSpyDispatcher.getEventSpies().add(realmCache.asEventSpy());

maven = container.lookup(Maven.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;
Expand All @@ -32,6 +35,7 @@
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -45,6 +49,9 @@
import javax.inject.Singleton;
import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.eventspy.EventSpy;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.PluginRealmCache;
import org.apache.maven.project.MavenProject;
Expand Down Expand Up @@ -427,6 +434,51 @@ public boolean isValid() {
private static final Logger log = LoggerFactory.getLogger(CliPluginRealmCache.class);
protected final Map<Key, ValidableCacheRecord> cache = new ConcurrentHashMap<>();
private final RecordValidator watcher;
private final EventSpy eventSpy = new EventSpy() {

private Path multiModuleProjectDirectory;

@Override
public void onEvent(Object event) throws Exception {
try {
if (event instanceof MavenExecutionRequest) {
/* Store the multiModuleProjectDirectory path */
multiModuleProjectDirectory = ((MavenExecutionRequest) event).getMultiModuleProjectDirectory().toPath()
.toRealPath();
} else if (event instanceof MavenExecutionResult) {
/* Evict the entries refering to jars under multiModuleProjectDirectory */
final Iterator<Entry<Key, ValidableCacheRecord>> i = cache.entrySet().iterator();
while (i.hasNext()) {
final Entry<Key, ValidableCacheRecord> entry = i.next();
final ValidableCacheRecord record = entry.getValue();
for (URL url : record.getRealm().getURLs()) {
if (url.getProtocol().equals("file")) {
final Path path = Paths.get(url.toURI()).toRealPath();
if (path.startsWith(multiModuleProjectDirectory)) {
log.debug(
"Removing PluginRealmCache entry {} because it refers to an artifact in the build tree {}",
entry.getKey(), path);
record.dispose();
i.remove();
break;
}
}
}
}
}
} catch (Exception e) {
log.warn("Could not notify CliPluginRealmCache", e);
}
}

@Override
public void init(Context context) throws Exception {
}

@Override
public void close() throws Exception {
}
};

public CliPluginRealmCache() {
this.watcher = System.getProperty("os.name").toLowerCase().contains("mac")
Expand Down Expand Up @@ -488,4 +540,8 @@ public void dispose() {
flush();
}

public EventSpy asEventSpy() {
return eventSpy;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void cleanInstall() throws IOException, InterruptedException {

final ClientOutput output = Mockito.mock(ClientOutput.class);
client.execute(output,
// "clean", workaround for https://github.com/mvndaemon/mvnd/issues/40
"clean",
"install", "-e", "-Dmvnd.log.level=DEBUG").assertSuccess();

Assertions.assertThat(helloPath).exists();
Expand Down
Empty file.
Empty file.
Empty file.