Provide distributions for both maven 3.9.x and 4.0.x (#796)

This commit is contained in:
Guillaume Nodet
2023-03-08 00:03:49 +01:00
committed by GitHub
parent 1f99fb8cb7
commit 95b40a3d8a
38 changed files with 3895 additions and 36 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.project;
import java.util.Objects;
import org.apache.maven.building.Source;
import org.apache.maven.model.building.ModelCache;
public class SnapshotModelCache implements ModelCache {
private final ModelCache globalCache;
private final ModelCache reactorCache;
public SnapshotModelCache(ModelCache globalCache, ModelCache reactorCache) {
this.globalCache = Objects.requireNonNull(globalCache);
this.reactorCache = Objects.requireNonNull(reactorCache);
}
public Object get(Source path, String tag) {
return reactorCache.get(path, tag);
}
public void put(Source path, String tag, Object data) {
reactorCache.put(path, tag, data);
}
@Override
public void put(String groupId, String artifactId, String version, String tag, Object data) {
getDelegate(version).put(groupId, artifactId, version, tag, data);
}
@Override
public Object get(String groupId, String artifactId, String version, String tag) {
return getDelegate(version).get(groupId, artifactId, version, tag);
}
private ModelCache getDelegate(String version) {
return version.contains("SNAPSHOT") || version.contains("${") ? reactorCache : globalCache;
}
}

View File

@@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.project;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.apache.maven.model.building.ModelCache;
import org.apache.maven.repository.internal.DefaultModelCacheFactory;
import org.apache.maven.repository.internal.ModelCacheFactory;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.sisu.Priority;
import static org.mvndaemon.mvnd.common.Environment.MVND_NO_MODEL_CACHE;
@Singleton
@Named
@Priority(10)
public class SnapshotModelCacheFactory implements ModelCacheFactory {
private final ModelCacheFactory factory;
private final ModelCache globalCache;
@Inject
public SnapshotModelCacheFactory(DefaultModelCacheFactory factory) {
this.factory = factory;
this.globalCache = factory.createCache(new DefaultRepositorySystemSession());
}
@Override
public ModelCache createCache(RepositorySystemSession session) {
boolean noModelCache =
Boolean.parseBoolean(MVND_NO_MODEL_CACHE.asOptional().orElse(MVND_NO_MODEL_CACHE.getDefault()));
ModelCache reactorCache = factory.createCache(session);
ModelCache globalCache = noModelCache ? reactorCache : this.globalCache;
return new SnapshotModelCache(globalCache, reactorCache);
}
}

View File

@@ -0,0 +1,281 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.settings;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.maven.api.model.ActivationFile;
import org.apache.maven.api.settings.Activation;
import org.apache.maven.api.settings.ActivationOS;
import org.apache.maven.api.settings.ActivationProperty;
import org.apache.maven.api.settings.Profile;
import org.apache.maven.api.settings.Repository;
import org.apache.maven.api.settings.RepositoryPolicy;
import org.apache.maven.api.settings.Settings;
import org.apache.maven.settings.v4.SettingsMerger;
/**
* Several convenience methods to handle settings
*
* @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
*/
public final class SettingsUtilsV4 {
private SettingsUtilsV4() {
// don't allow construction.
}
/**
* @param dominant
* @param recessive
*/
public static Settings merge(Settings dominant, Settings recessive) {
return new SettingsMerger().merge(dominant, recessive, true, Collections.emptyMap());
}
/**
* @param modelProfile
* @return a profile
*/
public static Profile convertToSettingsProfile(org.apache.maven.api.model.Profile modelProfile) {
Profile.Builder profile = Profile.newBuilder();
profile.id(modelProfile.getId());
org.apache.maven.api.model.Activation modelActivation = modelProfile.getActivation();
if (modelActivation != null) {
Activation.Builder activation = Activation.newBuilder();
activation.activeByDefault(modelActivation.isActiveByDefault());
activation.jdk(modelActivation.getJdk());
org.apache.maven.api.model.ActivationProperty modelProp = modelActivation.getProperty();
if (modelProp != null) {
ActivationProperty prop = ActivationProperty.newBuilder()
.name(modelProp.getName())
.value(modelProp.getValue())
.build();
activation.property(prop);
}
org.apache.maven.api.model.ActivationOS modelOs = modelActivation.getOs();
if (modelOs != null) {
ActivationOS os = ActivationOS.newBuilder()
.arch(modelOs.getArch())
.family(modelOs.getFamily())
.name(modelOs.getName())
.version(modelOs.getVersion())
.build();
activation.os(os);
}
ActivationFile modelFile = modelActivation.getFile();
if (modelFile != null) {
org.apache.maven.api.settings.ActivationFile file =
org.apache.maven.api.settings.ActivationFile.newBuilder()
.exists(modelFile.getExists())
.missing(modelFile.getMissing())
.build();
activation.file(file);
}
profile.activation(activation.build());
}
profile.properties(modelProfile.getProperties().entrySet().stream()
.collect(Collectors.toMap(
e -> e.getKey().toString(), e -> e.getValue().toString())));
List<org.apache.maven.api.model.Repository> repos = modelProfile.getRepositories();
if (repos != null) {
List<Repository> repositories = new ArrayList<>();
for (org.apache.maven.api.model.Repository repo : repos) {
repositories.add(convertToSettingsRepository(repo));
}
profile.repositories(repositories);
}
List<org.apache.maven.api.model.Repository> pluginRepos = modelProfile.getPluginRepositories();
if (pluginRepos != null) {
List<Repository> repositories = new ArrayList<>();
for (org.apache.maven.api.model.Repository pluginRepo : pluginRepos) {
repositories.add(convertToSettingsRepository(pluginRepo));
}
profile.pluginRepositories(repositories);
}
return profile.build();
}
/**
* @param settingsProfile
* @return a profile
*/
public static org.apache.maven.api.model.Profile convertFromSettingsProfile(Profile settingsProfile) {
org.apache.maven.api.model.Profile.Builder profile = org.apache.maven.api.model.Profile.newBuilder();
profile.id(settingsProfile.getId());
Activation settingsActivation = settingsProfile.getActivation();
if (settingsActivation != null) {
org.apache.maven.api.model.Activation.Builder activation =
org.apache.maven.api.model.Activation.newBuilder();
activation.activeByDefault(settingsActivation.isActiveByDefault());
activation.jdk(settingsActivation.getJdk());
ActivationProperty settingsProp = settingsActivation.getProperty();
if (settingsProp != null) {
activation.property(org.apache.maven.api.model.ActivationProperty.newBuilder()
.name(settingsProp.getName())
.value(settingsProp.getValue())
.build());
}
ActivationOS settingsOs = settingsActivation.getOs();
if (settingsOs != null) {
activation.os(org.apache.maven.api.model.ActivationOS.newBuilder()
.arch(settingsOs.getArch())
.family(settingsOs.getFamily())
.name(settingsOs.getName())
.version(settingsOs.getVersion())
.build());
}
org.apache.maven.api.settings.ActivationFile settingsFile = settingsActivation.getFile();
if (settingsFile != null) {
activation.file(ActivationFile.newBuilder()
.exists(settingsFile.getExists())
.missing(settingsFile.getMissing())
.build());
}
profile.activation(activation.build());
}
profile.properties(settingsProfile.getProperties());
List<Repository> repos = settingsProfile.getRepositories();
if (repos != null) {
profile.repositories(repos.stream()
.map(SettingsUtilsV4::convertFromSettingsRepository)
.collect(Collectors.toList()));
}
List<Repository> pluginRepos = settingsProfile.getPluginRepositories();
if (pluginRepos != null) {
profile.pluginRepositories(pluginRepos.stream()
.map(SettingsUtilsV4::convertFromSettingsRepository)
.collect(Collectors.toList()));
}
org.apache.maven.api.model.Profile value = profile.build();
value.setSource("settings.xml");
return value;
}
/**
* @param settingsRepo
* @return a repository
*/
private static org.apache.maven.api.model.Repository convertFromSettingsRepository(Repository settingsRepo) {
org.apache.maven.api.model.Repository.Builder repo = org.apache.maven.api.model.Repository.newBuilder();
repo.id(settingsRepo.getId());
repo.layout(settingsRepo.getLayout());
repo.name(settingsRepo.getName());
repo.url(settingsRepo.getUrl());
if (settingsRepo.getSnapshots() != null) {
repo.snapshots(convertRepositoryPolicy(settingsRepo.getSnapshots()));
}
if (settingsRepo.getReleases() != null) {
repo.releases(convertRepositoryPolicy(settingsRepo.getReleases()));
}
return repo.build();
}
/**
* @param settingsPolicy
* @return a RepositoryPolicy
*/
private static org.apache.maven.api.model.RepositoryPolicy convertRepositoryPolicy(
RepositoryPolicy settingsPolicy) {
org.apache.maven.api.model.RepositoryPolicy policy = org.apache.maven.api.model.RepositoryPolicy.newBuilder()
.enabled(Boolean.toString(settingsPolicy.isEnabled()))
.updatePolicy(settingsPolicy.getUpdatePolicy())
.checksumPolicy(settingsPolicy.getChecksumPolicy())
.build();
return policy;
}
/**
* @param modelRepo
* @return a repository
*/
private static Repository convertToSettingsRepository(org.apache.maven.api.model.Repository modelRepo) {
Repository repo = Repository.newBuilder()
.id(modelRepo.getId())
.layout(modelRepo.getLayout())
.name(modelRepo.getName())
.url(modelRepo.getUrl())
.snapshots(modelRepo.getSnapshots() != null ? convertRepositoryPolicy(modelRepo.getSnapshots()) : null)
.releases(modelRepo.getReleases() != null ? convertRepositoryPolicy(modelRepo.getReleases()) : null)
.build();
return repo;
}
/**
* @param modelPolicy
* @return a RepositoryPolicy
*/
private static RepositoryPolicy convertRepositoryPolicy(org.apache.maven.api.model.RepositoryPolicy modelPolicy) {
RepositoryPolicy policy = RepositoryPolicy.newBuilder()
.enabled(modelPolicy.isEnabled())
.updatePolicy(modelPolicy.getUpdatePolicy())
.checksumPolicy(modelPolicy.getChecksumPolicy())
.build();
return policy;
}
/**
* @param settings could be null
* @return a new instance of settings or null if settings was null.
*/
public static org.apache.maven.settings.Settings copySettings(org.apache.maven.settings.Settings settings) {
if (settings == null) {
return null;
}
return new org.apache.maven.settings.Settings(settings.getDelegate());
}
}

View File

@@ -0,0 +1,136 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.mvndaemon.mvnd.cache.invalidating;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.DefaultPluginDescriptorCache;
import org.apache.maven.plugin.InvalidPluginDescriptorException;
import org.apache.maven.plugin.PluginDescriptorParsingException;
import org.apache.maven.plugin.PluginResolutionException;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.sisu.Priority;
import org.mvndaemon.mvnd.cache.Cache;
import org.mvndaemon.mvnd.cache.CacheFactory;
import org.mvndaemon.mvnd.cache.CacheRecord;
@Singleton
@Named
@Priority(10)
public class InvalidatingPluginDescriptorCache extends DefaultPluginDescriptorCache {
@FunctionalInterface
public interface PluginDescriptorSupplier {
PluginDescriptor load()
throws PluginResolutionException, PluginDescriptorParsingException, InvalidPluginDescriptorException;
}
protected static class Record implements CacheRecord {
private final PluginDescriptor descriptor;
public Record(PluginDescriptor descriptor) {
this.descriptor = descriptor;
}
@Override
public Stream<Path> getDependencyPaths() {
return Optional.ofNullable(descriptor.getArtifacts()).orElse(Collections.emptyList()).stream()
.map(artifact -> artifact.getFile().toPath());
}
@Override
public void invalidate() {
ClassRealm realm = descriptor.getClassRealm();
try {
realm.getWorld().disposeRealm(realm.getId());
} catch (NoSuchRealmException e) {
// ignore
}
}
}
final Cache<Key, Record> cache;
@Inject
public InvalidatingPluginDescriptorCache(CacheFactory cacheFactory) {
this.cache = cacheFactory.newCache();
}
@Override
public Key createKey(Plugin plugin, List<RemoteRepository> repositories, RepositorySystemSession session) {
return super.createKey(plugin, repositories, session);
}
@Override
public PluginDescriptor get(Key key) {
Record r = cache.get(key);
return r != null ? clone(r.descriptor) : null;
}
public PluginDescriptor get(Key key, PluginDescriptorSupplier supplier)
throws PluginDescriptorParsingException, PluginResolutionException, InvalidPluginDescriptorException {
try {
Record r = cache.computeIfAbsent(key, k -> {
try {
return new Record(clone(supplier.load()));
} catch (PluginDescriptorParsingException
| PluginResolutionException
| InvalidPluginDescriptorException e) {
throw new RuntimeException(e);
}
});
return clone(r.descriptor);
} catch (RuntimeException e) {
if (e.getCause() instanceof PluginDescriptorParsingException) {
throw (PluginDescriptorParsingException) e.getCause();
}
if (e.getCause() instanceof PluginResolutionException) {
throw (PluginResolutionException) e.getCause();
}
if (e.getCause() instanceof InvalidPluginDescriptorException) {
throw (InvalidPluginDescriptorException) e.getCause();
}
throw e;
}
}
@Override
public void put(Key key, PluginDescriptor descriptor) {
cache.put(key, new Record(clone(descriptor)));
}
@Override
public void flush() {
cache.clear();
}
}

View File

@@ -0,0 +1,121 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.mvndaemon.mvnd.cache.invalidating;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Stream;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.DefaultPluginRealmCache;
import org.apache.maven.plugin.PluginContainerException;
import org.apache.maven.plugin.PluginResolutionException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
import org.eclipse.sisu.Priority;
import org.mvndaemon.mvnd.cache.Cache;
import org.mvndaemon.mvnd.cache.CacheFactory;
@Singleton
@Named
@Priority(10)
public class InvalidatingPluginRealmCache extends DefaultPluginRealmCache {
protected static class Record implements org.mvndaemon.mvnd.cache.CacheRecord {
final CacheRecord record;
public Record(CacheRecord record) {
this.record = record;
}
@Override
public Stream<Path> getDependencyPaths() {
return record.getArtifacts().stream()
.map(artifact -> artifact.getFile().toPath());
}
@Override
public void invalidate() {
ClassRealm realm = record.getRealm();
try {
realm.getWorld().disposeRealm(realm.getId());
} catch (NoSuchRealmException e) {
// ignore
}
}
}
final Cache<Key, Record> cache;
@Inject
public InvalidatingPluginRealmCache(CacheFactory cacheFactory) {
cache = cacheFactory.newCache();
}
@Override
public CacheRecord get(Key key) {
Record r = cache.get(key);
return r != null ? r.record : null;
}
@Override
public CacheRecord get(Key key, PluginRealmSupplier supplier)
throws PluginResolutionException, PluginContainerException {
try {
Record r = cache.computeIfAbsent(key, k -> {
try {
return new Record(supplier.load());
} catch (PluginResolutionException | PluginContainerException e) {
throw new RuntimeException(e);
}
});
return r.record;
} catch (RuntimeException e) {
if (e.getCause() instanceof PluginResolutionException) {
throw (PluginResolutionException) e.getCause();
}
if (e.getCause() instanceof PluginContainerException) {
throw (PluginContainerException) e.getCause();
}
throw e;
}
}
@Override
public CacheRecord put(Key key, ClassRealm pluginRealm, List<Artifact> pluginArtifacts) {
CacheRecord record = super.put(key, pluginRealm, pluginArtifacts);
super.cache.remove(key);
cache.put(key, new Record(record));
return record;
}
@Override
public void flush() {
cache.clear();
}
@Override
public void register(MavenProject project, Key key, CacheRecord record) {}
}

View File

@@ -0,0 +1,190 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.mvndaemon.mvnd.cache.invalidating;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.eventspy.AbstractEventSpy;
import org.apache.maven.eventspy.EventSpy;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.project.MavenProject;
import org.eclipse.sisu.Typed;
import org.mvndaemon.mvnd.common.Environment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Named
@Singleton
@Typed(EventSpy.class)
public class InvalidatingRealmCacheEventSpy extends AbstractEventSpy {
private static final Logger LOG = LoggerFactory.getLogger(InvalidatingRealmCacheEventSpy.class);
private final InvalidatingPluginRealmCache pluginCache;
private final InvalidatingExtensionRealmCache extensionCache;
private final InvalidatingProjectArtifactsCache projectArtifactsCache;
private Path multiModuleProjectDirectory;
private String pattern;
private PathMatcher matcher;
@Inject
public InvalidatingRealmCacheEventSpy(
InvalidatingPluginRealmCache cache,
InvalidatingExtensionRealmCache extensionCache,
InvalidatingProjectArtifactsCache projectArtifactsCache) {
this.pluginCache = cache;
this.extensionCache = extensionCache;
this.projectArtifactsCache = projectArtifactsCache;
}
@Override
public void onEvent(Object event) throws Exception {
try {
if (event instanceof MavenExecutionRequest) {
/* Store the multiModuleProjectDirectory path */
multiModuleProjectDirectory = ((MavenExecutionRequest) event)
.getMultiModuleProjectDirectory()
.toPath();
pattern = Environment.MVND_PLUGIN_REALM_EVICT_PATTERN
.asOptional()
.orElse(Environment.MVND_PLUGIN_REALM_EVICT_PATTERN.getDefault());
if (!pattern.isEmpty()) {
String[] patterns = pattern.split(",");
List<PathMatcher> matchers = new ArrayList<>();
for (String pattern : patterns) {
if (pattern.startsWith("mvn:")) {
String[] parts = pattern.substring("mvn:".length()).split(":");
String groupId, artifactId, version;
if (parts.length >= 3) {
version = parts[2];
} else {
version = "*";
}
if (parts.length >= 2) {
groupId = parts[0];
artifactId = parts[1];
} else {
groupId = "*";
artifactId = parts[0];
}
pattern = "glob:**/" + ("*".equals(groupId) ? "" : groupId.replace('.', '/') + "/")
+ artifactId + "/" + ("*".equals(version) ? "**" : version + "/**");
}
matchers.add(getPathMatcher(pattern));
}
if (matchers.size() == 1) {
matcher = matchers.iterator().next();
} else {
matcher = path -> matchers.stream().anyMatch(f -> f.matches(path));
}
}
} else if (event instanceof MavenExecutionResult) {
/* Evict the entries referring to jars under multiModuleProjectDirectory */
pluginCache.cache.removeIf(this::shouldEvict);
extensionCache.cache.removeIf(this::shouldEvict);
MavenExecutionResult mer = (MavenExecutionResult) event;
List<MavenProject> projects = mer.getTopologicallySortedProjects();
projectArtifactsCache.cache.removeIf(
(k, r) -> shouldEvict(projects, (InvalidatingProjectArtifactsCache.CacheKey) k, r));
}
} catch (Exception e) {
LOG.warn("Could not notify CliPluginRealmCache", e);
}
}
private boolean shouldEvict(
List<MavenProject> projects,
InvalidatingProjectArtifactsCache.CacheKey k,
InvalidatingProjectArtifactsCache.Record v) {
return projects.stream().anyMatch(p -> k.matches(p.getGroupId(), p.getArtifactId(), p.getVersion()));
}
private boolean shouldEvict(InvalidatingPluginRealmCache.Key k, InvalidatingPluginRealmCache.Record v) {
try {
for (URL url : v.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 {}",
k,
path);
return true;
} else if (matcher != null && matcher.matches(path)) {
LOG.debug(
"Removing PluginRealmCache entry {} because its components {} matches the eviction pattern '{}'",
k,
path,
pattern);
return true;
}
}
}
return false;
} catch (URISyntaxException e) {
return true;
}
}
private boolean shouldEvict(InvalidatingExtensionRealmCache.Key k, InvalidatingExtensionRealmCache.Record v) {
try {
for (URL url : v.record.getRealm().getURLs()) {
if (url.getProtocol().equals("file")) {
final Path path = Paths.get(url.toURI());
if (path.startsWith(multiModuleProjectDirectory)) {
LOG.debug(
"Removing ExtensionRealmCache entry {} because it refers to an artifact in the build tree {}",
k,
path);
return true;
} else if (matcher != null && matcher.matches(path)) {
LOG.debug(
"Removing ExtensionRealmCache entry {} because its components {} matches the eviction pattern '{}'",
k,
path,
pattern);
return true;
}
}
}
return false;
} catch (URISyntaxException e) {
return true;
}
}
private static PathMatcher getPathMatcher(String pattern) {
if (!pattern.startsWith("glob:") && !pattern.startsWith("regex:")) {
pattern = "glob:" + pattern;
}
return FileSystems.getDefault().getPathMatcher(pattern);
}
}

View File

@@ -0,0 +1,93 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.mvndaemon.mvnd.plugin;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.maven.artifact.repository.metadata.io.MetadataReader;
import org.apache.maven.plugin.MavenPluginManager;
import org.apache.maven.plugin.version.PluginVersionRequest;
import org.apache.maven.plugin.version.PluginVersionResolutionException;
import org.apache.maven.plugin.version.PluginVersionResolver;
import org.apache.maven.plugin.version.PluginVersionResult;
import org.apache.maven.plugin.version.internal.DefaultPluginVersionResolver;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.SessionData;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.version.VersionScheme;
import org.eclipse.sisu.Priority;
import org.eclipse.sisu.Typed;
@Named
@Singleton
@Priority(10)
@Typed(PluginVersionResolver.class)
public class CachingPluginVersionResolver extends DefaultPluginVersionResolver {
private static final Object CACHE_KEY = new Object();
@Inject
public CachingPluginVersionResolver(
RepositorySystem repositorySystem,
MetadataReader metadataReader,
MavenPluginManager pluginManager,
VersionScheme versionScheme) {
super(repositorySystem, metadataReader, pluginManager, versionScheme);
}
@Override
public PluginVersionResult resolve(PluginVersionRequest request) throws PluginVersionResolutionException {
Map<String, PluginVersionResult> cache =
getCache(request.getRepositorySession().getData());
String key = getKey(request);
PluginVersionResult result = cache.get(key);
if (result == null) {
result = super.resolve(request);
cache.putIfAbsent(key, result);
}
return result;
}
@SuppressWarnings("unchecked")
private Map<String, PluginVersionResult> getCache(SessionData data) {
Map<String, PluginVersionResult> cache = (Map<String, PluginVersionResult>) data.get(CACHE_KEY);
while (cache == null) {
cache = new ConcurrentHashMap<>(256);
if (data.set(CACHE_KEY, null, cache)) {
break;
}
cache = (Map<String, PluginVersionResult>) data.get(CACHE_KEY);
}
return cache;
}
private static String getKey(PluginVersionRequest request) {
return Stream.concat(
Stream.of(request.getGroupId(), request.getArtifactId()),
request.getRepositories().stream().map(RemoteRepository::getId))
.collect(Collectors.joining(":"));
}
}