mirror of
https://github.com/apache/maven-mvnd.git
synced 2025-09-12 22:19:11 +00:00
Fix problems when reusing artifacts that are part of the build by disabling URL caching and using more atomic operations on the CliPluginRealmCache
This commit is contained in:
@@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.jboss.fuse.mvnd.common;
|
package org.jboss.fuse.mvnd.common;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
@@ -25,6 +26,10 @@ import java.util.stream.Stream;
|
|||||||
public class ServerMain {
|
public class ServerMain {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
// Disable URL caching so that the JVM does not try to cache resources
|
||||||
|
// loaded from jars that are built by a previous run
|
||||||
|
new File("txt").toURI().toURL().openConnection().setDefaultUseCaches(false);
|
||||||
|
|
||||||
final String uidStr = Environment.DAEMON_UID.systemProperty().orFail().asString();
|
final String uidStr = Environment.DAEMON_UID.systemProperty().orFail().asString();
|
||||||
final Path mvndHome = Environment.MVND_HOME.systemProperty().orFail().asPath();
|
final Path mvndHome = Environment.MVND_HOME.systemProperty().orFail().asPath();
|
||||||
URL[] classpath = Stream.concat(
|
URL[] classpath = Stream.concat(
|
||||||
|
@@ -436,7 +436,6 @@ public class DaemonMavenCli {
|
|||||||
}
|
}
|
||||||
|
|
||||||
eventSpyDispatcher = container.lookup(EventSpyDispatcher.class);
|
eventSpyDispatcher = container.lookup(EventSpyDispatcher.class);
|
||||||
eventSpyDispatcher.getEventSpies().add(realmCache.asEventSpy());
|
|
||||||
|
|
||||||
maven = container.lookup(Maven.class);
|
maven = container.lookup(Maven.class);
|
||||||
|
|
||||||
|
@@ -20,11 +20,9 @@ package org.jboss.fuse.mvnd.plugin;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
|
||||||
import java.nio.file.FileSystems;
|
import java.nio.file.FileSystems;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.nio.file.StandardWatchEventKinds;
|
import java.nio.file.StandardWatchEventKinds;
|
||||||
import java.nio.file.WatchEvent;
|
import java.nio.file.WatchEvent;
|
||||||
import java.nio.file.WatchEvent.Kind;
|
import java.nio.file.WatchEvent.Kind;
|
||||||
@@ -34,7 +32,6 @@ import java.nio.file.attribute.BasicFileAttributes;
|
|||||||
import java.nio.file.attribute.FileTime;
|
import java.nio.file.attribute.FileTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -49,9 +46,6 @@ import javax.inject.Named;
|
|||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
import org.apache.maven.RepositoryUtils;
|
import org.apache.maven.RepositoryUtils;
|
||||||
import org.apache.maven.artifact.Artifact;
|
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.model.Plugin;
|
||||||
import org.apache.maven.plugin.PluginRealmCache;
|
import org.apache.maven.plugin.PluginRealmCache;
|
||||||
import org.apache.maven.project.MavenProject;
|
import org.apache.maven.project.MavenProject;
|
||||||
@@ -434,50 +428,6 @@ public class CliPluginRealmCache
|
|||||||
private static final Logger log = LoggerFactory.getLogger(CliPluginRealmCache.class);
|
private static final Logger log = LoggerFactory.getLogger(CliPluginRealmCache.class);
|
||||||
protected final Map<Key, ValidableCacheRecord> cache = new ConcurrentHashMap<>();
|
protected final Map<Key, ValidableCacheRecord> cache = new ConcurrentHashMap<>();
|
||||||
private final RecordValidator watcher;
|
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();
|
|
||||||
} 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());
|
|
||||||
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() {
|
public CliPluginRealmCache() {
|
||||||
final String osName = System.getProperty("os.name").toLowerCase(Locale.ROOT);
|
final String osName = System.getProperty("os.name").toLowerCase(Locale.ROOT);
|
||||||
@@ -494,27 +444,20 @@ public class CliPluginRealmCache
|
|||||||
|
|
||||||
public CacheRecord get(Key key) {
|
public CacheRecord get(Key key) {
|
||||||
watcher.validateRecords();
|
watcher.validateRecords();
|
||||||
ValidableCacheRecord record = cache.get(key);
|
return cache.computeIfPresent(key, (k, r) -> {
|
||||||
if (record != null && !record.isValid()) {
|
if (!r.isValid()) {
|
||||||
record.dispose();
|
r.dispose();
|
||||||
record = null;
|
return null;
|
||||||
cache.remove(key);
|
} else {
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
return record;
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public CacheRecord put(Key key, ClassRealm pluginRealm, List<Artifact> pluginArtifacts) {
|
public CacheRecord put(Key key, ClassRealm pluginRealm, List<Artifact> pluginArtifacts) {
|
||||||
Objects.requireNonNull(pluginRealm, "pluginRealm cannot be null");
|
Objects.requireNonNull(pluginRealm, "pluginRealm cannot be null");
|
||||||
Objects.requireNonNull(pluginArtifacts, "pluginArtifacts cannot be null");
|
Objects.requireNonNull(pluginArtifacts, "pluginArtifacts cannot be null");
|
||||||
|
return cache.computeIfAbsent(key, k -> watcher.newRecord(pluginRealm, pluginArtifacts));
|
||||||
if (cache.containsKey(key)) {
|
|
||||||
throw new IllegalStateException("Duplicate plugin realm for plugin " + key);
|
|
||||||
}
|
|
||||||
|
|
||||||
ValidableCacheRecord record = watcher.newRecord(pluginRealm, pluginArtifacts);
|
|
||||||
cache.put(key, record);
|
|
||||||
|
|
||||||
return record;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void flush() {
|
public void flush() {
|
||||||
@@ -524,14 +467,6 @@ public class CliPluginRealmCache
|
|||||||
cache.clear();
|
cache.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static int pluginHashCode(Plugin plugin) {
|
|
||||||
return CliCacheUtils.pluginHashCode(plugin);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static boolean pluginEquals(Plugin a, Plugin b) {
|
|
||||||
return CliCacheUtils.pluginEquals(a, b);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void register(MavenProject project, Key key, CacheRecord record) {
|
public void register(MavenProject project, Key key, CacheRecord record) {
|
||||||
// default cache does not track plugin usage
|
// default cache does not track plugin usage
|
||||||
}
|
}
|
||||||
@@ -540,8 +475,4 @@ public class CliPluginRealmCache
|
|||||||
flush();
|
flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
public EventSpy asEventSpy() {
|
|
||||||
return eventSpy;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user