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

Speed improvements #472

Merged
merged 6 commits into from
Sep 7, 2021
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
3 changes: 3 additions & 0 deletions daemon/src/main/java/org/apache/maven/cli/DaemonMavenCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import org.apache.maven.plugin.MavenPluginManager;
import org.apache.maven.plugin.PluginArtifactsCache;
import org.apache.maven.plugin.PluginRealmCache;
import org.apache.maven.plugin.version.PluginVersionResolver;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.artifact.ProjectArtifactsCache;
import org.apache.maven.properties.internal.SystemProperties;
Expand Down Expand Up @@ -112,6 +113,7 @@
import org.mvndaemon.mvnd.logging.smart.LoggingExecutionListener;
import org.mvndaemon.mvnd.logging.smart.LoggingOutputStream;
import org.mvndaemon.mvnd.nativ.CLibrary;
import org.mvndaemon.mvnd.plugin.CachingPluginVersionResolver;
import org.mvndaemon.mvnd.plugin.CliMavenPluginManager;
import org.mvndaemon.mvnd.transfer.DaemonMavenTransferListener;
import org.slf4j.ILoggerFactory;
Expand Down Expand Up @@ -535,6 +537,7 @@ protected void configure() {
bind(PluginRealmCache.class).to(InvalidatingPluginRealmCache.class);
bind(ProjectArtifactsCache.class).to(InvalidatingProjectArtifactsCache.class);
bind(MavenPluginManager.class).to(CliMavenPluginManager.class);
bind(PluginVersionResolver.class).to(CachingPluginVersionResolver.class);
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed 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.graph;

/*
* 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.
*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.maven.execution.ProjectDependencyGraph;
import org.apache.maven.project.DuplicateProjectException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectSorter;
import org.codehaus.plexus.util.dag.CycleDetectedException;

/**
* Describes the inter-dependencies between projects in the reactor.
*
* @author Benjamin Bentmann
*/
public class DefaultProjectDependencyGraph
implements ProjectDependencyGraph {

private ProjectSorter sorter;

private List<MavenProject> allProjects;

private Map<String, Integer> order;

private Map<String, MavenProject> projects;

/**
* Creates a new project dependency graph based on the specified projects.
*
* @param projects The projects to create the dependency graph with
* @throws DuplicateProjectException
* @throws CycleDetectedException
*/
public DefaultProjectDependencyGraph(Collection<MavenProject> projects)
throws CycleDetectedException, DuplicateProjectException {
super();
this.allProjects = Collections.unmodifiableList(new ArrayList<>(projects));
this.sorter = new ProjectSorter(projects);
this.order = new HashMap<>();
this.projects = new HashMap<>();
List<MavenProject> sorted = this.sorter.getSortedProjects();
for (int index = 0; index < sorted.size(); index++) {
MavenProject project = sorted.get(index);
String id = ProjectSorter.getId(project);
this.projects.put(id, project);
this.order.put(id, index);
}
}

/**
* Creates a new project dependency graph based on the specified projects.
*
* @param allProjects All collected projects.
* @param projects The projects to create the dependency graph with.
*
* @throws DuplicateProjectException
* @throws CycleDetectedException
* @since 3.5.0
*/
public DefaultProjectDependencyGraph(final List<MavenProject> allProjects,
final Collection<MavenProject> projects)
throws CycleDetectedException, DuplicateProjectException {
super();
this.allProjects = Collections.unmodifiableList(new ArrayList<>(allProjects));
this.sorter = new ProjectSorter(projects);
this.order = new HashMap<>();
this.projects = new HashMap<>();
List<MavenProject> sorted = this.sorter.getSortedProjects();
for (int index = 0; index < sorted.size(); index++) {
MavenProject project = sorted.get(index);
String id = ProjectSorter.getId(project);
this.projects.put(id, project);
this.order.put(id, index);
}
}

/**
* @since 3.5.0
*/
public List<MavenProject> getAllProjects() {
return this.allProjects;
}

public List<MavenProject> getSortedProjects() {
return new ArrayList<>(sorter.getSortedProjects());
}

public List<MavenProject> getDownstreamProjects(MavenProject project, boolean transitive) {
Objects.requireNonNull(project, "project cannot be null");

Set<String> projectIds = new HashSet<>();

getDownstreamProjects(ProjectSorter.getId(project), projectIds, transitive);

return getSortedProjects(projectIds);
}

private void getDownstreamProjects(String projectId, Set<String> projectIds, boolean transitive) {
for (String id : sorter.getDependents(projectId)) {
if (projectIds.add(id) && transitive) {
getDownstreamProjects(id, projectIds, transitive);
}
}
}

public List<MavenProject> getUpstreamProjects(MavenProject project, boolean transitive) {
Objects.requireNonNull(project, "project cannot be null");

Set<String> projectIds = new HashSet<>();

getUpstreamProjects(ProjectSorter.getId(project), projectIds, transitive);

return getSortedProjects(projectIds);
}

private void getUpstreamProjects(String projectId, Collection<String> projectIds, boolean transitive) {
for (String id : sorter.getDependencies(projectId)) {
if (projectIds.add(id) && transitive) {
getUpstreamProjects(id, projectIds, transitive);
}
}
}

private List<MavenProject> getSortedProjects(Set<String> projectIds) {
return projectIds.stream()
.sorted(Comparator.comparing(order::get))
.map(projects::get)
.collect(Collectors.toList());
}

@Override
public String toString() {
return sorter.getSortedProjects().toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ static DependencyGraph<MavenProject> fromMaven(ProjectDependencyGraph graph) {
Map<MavenProject, List<MavenProject>> upstreams = projects.stream()
.collect(Collectors.toMap(p -> p, p -> graph.getUpstreamProjects(p, false)));
Map<MavenProject, List<MavenProject>> downstreams = projects.stream()
.collect(
Collectors.toMap(p -> p, p -> graph.getDownstreamProjects(p, false)));
return new DependencyGraph<MavenProject>(Collections.unmodifiableList(projects), upstreams, downstreams);
.collect(Collectors.toMap(p -> p, p -> graph.getDownstreamProjects(p, false)));
return new DependencyGraph<>(Collections.unmodifiableList(projects), upstreams, downstreams);
}

public DependencyGraph(List<K> projects, Map<K, List<K>> upstreams, Map<K, List<K>> downstreams) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed 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 java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.inject.Named;
import javax.inject.Singleton;
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.SessionData;
import org.eclipse.aether.repository.RemoteRepository;
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();

@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(":"));
}

}
2 changes: 1 addition & 1 deletion dist/src/main/distro/bin/mvnd-bash-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ _mvnd()

local mvnd_opts="-1"
local mvnd_long_opts="--color|--completion|--purge|--serial|--status|--stop"
local mvnd_properties="-Djava.home|-Djdk.java.options|-Dmaven.multiModuleProjectDirectory|-Dmaven.repo.local|-Dmaven.settings|-Dmvnd.buildTime|-Dmvnd.builder|-Dmvnd.daemonStorage|-Dmvnd.debug|-Dmvnd.duplicateDaemonGracePeriod|-Dmvnd.enableAssertions|-Dmvnd.expirationCheckDelay|-Dmvnd.home|-Dmvnd.idleTimeout|-Dmvnd.jvmArgs|-Dmvnd.keepAlive|-Dmvnd.logPurgePeriod|-Dmvnd.logback|-Dmvnd.maxHeapSize|-Dmvnd.maxLostKeepAlive|-Dmvnd.minHeapSize|-Dmvnd.minThreads|-Dmvnd.noBuffering|-Dmvnd.noDaemon|-Dmvnd.pluginRealmEvictPattern|-Dmvnd.propertiesPath|-Dmvnd.registry|-Dmvnd.rollingWindowSize|-Dmvnd.serial|-Dmvnd.socketFamily|-Dmvnd.threads|-Dstyle.color|-Duser.dir|-Duser.home"
local mvnd_properties="-Djava.home|-Djdk.java.options|-Dmaven.multiModuleProjectDirectory|-Dmaven.repo.local|-Dmaven.settings|-Dmvnd.buildTime|-Dmvnd.builder|-Dmvnd.daemonStorage|-Dmvnd.debug|-Dmvnd.duplicateDaemonGracePeriod|-Dmvnd.enableAssertions|-Dmvnd.expirationCheckDelay|-Dmvnd.home|-Dmvnd.idleTimeout|-Dmvnd.jvmArgs|-Dmvnd.keepAlive|-Dmvnd.logPurgePeriod|-Dmvnd.logback|-Dmvnd.maxHeapSize|-Dmvnd.maxLostKeepAlive|-Dmvnd.minHeapSize|-Dmvnd.minThreads|-Dmvnd.noBuffering|-Dmvnd.noDaemon|-Dmvnd.pluginRealmEvictPattern|-Dmvnd.propertiesPath|-Dmvnd.registry|-Dmvnd.rollingWindowSize|-Dmvnd.serial|-Dmvnd.socketFamily|-Dmvnd.syncContextFactory|-Dmvnd.threads|-Dstyle.color|-Duser.dir|-Duser.home"
local opts="-am|-amd|-B|-C|-c|-cpu|-D|-e|-emp|-ep|-f|-fae|-ff|-fn|-gs|-h|-l|-N|-npr|-npu|-nsu|-o|-P|-pl|-q|-rf|-s|-T|-t|-U|-up|-V|-v|-X|${mvnd_opts}"
local long_opts="--also-make|--also-make-dependents|--batch-mode|--strict-checksums|--lax-checksums|--check-plugin-updates|--define|--errors|--encrypt-master-password|--encrypt-password|--file|--fail-at-end|--fail-fast|--fail-never|--global-settings|--help|--log-file|--non-recursive|--no-plugin-registry|--no-plugin-updates|--no-snapshot-updates|--offline|--activate-profiles|--projects|--quiet|--resume-from|--settings|--threads|--toolchains|--update-snapshots|--update-plugins|--show-version|--version|--debug|${mvnd_long_opts}"

Expand Down