mirror of
https://github.com/apache/maven-mvnd.git
synced 2025-09-09 14:49:06 +00:00
Support for hidden dependencies #12
This commit is contained in:
@@ -1,7 +1,25 @@
|
||||
/*
|
||||
* 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.jboss.fuse.mvnd.builder;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -10,12 +28,51 @@ import org.apache.maven.project.MavenProject;
|
||||
|
||||
interface DependencyGraph<K> {
|
||||
|
||||
static DependencyGraph<MavenProject> fromMaven(ProjectDependencyGraph graph) {
|
||||
static DependencyGraph<MavenProject> fromMaven(ProjectDependencyGraph graph, String rules) {
|
||||
List<MavenProject> projects = graph.getAllProjects();
|
||||
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)));
|
||||
|
||||
if (rules != null) {
|
||||
for (String rule : rules.split("\\s*;\\s*|\n")) {
|
||||
String[] parts = rule.split("\\s*->\\s*|\\s+before\\s+");
|
||||
if (parts.length != 2) {
|
||||
throw new IllegalArgumentException("Invalid rule: " + rule);
|
||||
}
|
||||
List<Set<MavenProject>> deps = Stream.of(parts).map(s ->
|
||||
Pattern.compile(
|
||||
Arrays.stream(s.split("\\s*,\\s*|\\s+and\\s+"))
|
||||
.map(String::trim)
|
||||
.map(r -> r.contains(":") ? r : "*:" + r)
|
||||
.map(r -> r.replaceAll("\\.", "\\.")
|
||||
.replaceAll("\\*", ".*"))
|
||||
.collect(Collectors.joining("|"))))
|
||||
.map(t -> projects.stream()
|
||||
.filter(p -> t.matcher(p.getGroupId() + ":" + p.getArtifactId()).matches())
|
||||
.collect(Collectors.toSet()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Set<MavenProject> common = deps.get(0).stream().filter(deps.get(1)::contains).collect(Collectors.toSet());
|
||||
if (!common.isEmpty()) {
|
||||
boolean leftWildcard = parts[0].contains("*");
|
||||
boolean rightWildcard = parts[1].contains("*");
|
||||
if (leftWildcard && rightWildcard) {
|
||||
throw new IllegalArgumentException("Invalid rule: " + rule + ". Both left and right parts have wildcards and match the same project.");
|
||||
} else if (leftWildcard) {
|
||||
deps.get(0).removeAll(common);
|
||||
} else if (rightWildcard) {
|
||||
deps.get(1).removeAll(common);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Invalid rule: " + rule + ". Both left and right parts match the same project.");
|
||||
}
|
||||
}
|
||||
|
||||
deps.get(1).forEach(p -> upstreams.get(p).addAll(deps.get(0)));
|
||||
deps.get(0).forEach(p -> downstreams.get(p).addAll(deps.get(1)));
|
||||
}
|
||||
}
|
||||
return new DependencyGraph<MavenProject>() {
|
||||
@Override
|
||||
public Stream<MavenProject> getDownstreamProjects(MavenProject project) {
|
||||
|
@@ -35,6 +35,7 @@ import org.slf4j.LoggerFactory;
|
||||
public class SmartBuilder implements Builder {
|
||||
|
||||
public static final String PROP_PROFILING = "smartbuilder.profiling";
|
||||
public static final String MVND_BUILDER_RULES = "mvnd.builder.rules";
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@@ -51,7 +52,10 @@ public class SmartBuilder implements Builder {
|
||||
ProjectBuildList projectBuilds, final List<TaskSegment> taskSegments,
|
||||
ReactorBuildStatus reactorBuildStatus) throws ExecutionException, InterruptedException {
|
||||
|
||||
DependencyGraph<MavenProject> graph = DependencyGraph.fromMaven(session.getProjectDependencyGraph());
|
||||
String rules = session.getTopLevelProject().getProperties()
|
||||
.getProperty(MVND_BUILDER_RULES);
|
||||
|
||||
DependencyGraph<MavenProject> graph = DependencyGraph.fromMaven(session.getProjectDependencyGraph(), rules);
|
||||
|
||||
// log overall build info
|
||||
final int degreeOfConcurrency = session.getRequest().getDegreeOfConcurrency();
|
||||
|
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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.jboss.fuse.mvnd.builder;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.maven.project.MavenProject;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DependencyGraphTest extends AbstractSmartBuilderTest {
|
||||
|
||||
@Test
|
||||
public void testRules() {
|
||||
MavenProject a = newProject("a"), b = newProject("b"), c = newProject("c");
|
||||
TestProjectDependencyGraph graph = new TestProjectDependencyGraph(a, b, c);
|
||||
graph.addDependency(b, a);
|
||||
DependencyGraph<MavenProject> dp = DependencyGraph.fromMaven(graph, "a before c");
|
||||
Assert.assertEquals(new HashSet<>(Arrays.asList(b, c)),
|
||||
dp.getDownstreamProjects(a).collect(Collectors.toSet()));
|
||||
}
|
||||
}
|
@@ -19,8 +19,9 @@ public class ProjectComparatorTest extends AbstractSmartBuilderTest {
|
||||
MavenProject a = newProject("a"), b = newProject("b"), c = newProject("c");
|
||||
TestProjectDependencyGraph graph = new TestProjectDependencyGraph(a, b, c);
|
||||
graph.addDependency(b, a);
|
||||
DependencyGraph<MavenProject> dp = DependencyGraph.fromMaven(graph, null);
|
||||
|
||||
Comparator<MavenProject> cmp = org.jboss.fuse.mvnd.builder.ProjectComparator.create0(graph, new HashMap<>(), p -> id(p));
|
||||
Comparator<MavenProject> cmp = ProjectComparator.create0(dp, new HashMap<>(), ProjectComparator::id);
|
||||
|
||||
Queue<MavenProject> queue = new PriorityQueue<>(3, cmp);
|
||||
queue.add(a);
|
||||
@@ -37,13 +38,14 @@ public class ProjectComparatorTest extends AbstractSmartBuilderTest {
|
||||
MavenProject a = newProject("a"), b = newProject("b"), c = newProject("c");
|
||||
TestProjectDependencyGraph graph = new TestProjectDependencyGraph(a, b, c);
|
||||
graph.addDependency(b, a);
|
||||
DependencyGraph<MavenProject> dp = DependencyGraph.fromMaven(graph, null);
|
||||
|
||||
HashMap<String, AtomicLong> serviceTimes = new HashMap<>();
|
||||
serviceTimes.put(id(a), new AtomicLong(1L));
|
||||
serviceTimes.put(id(b), new AtomicLong(1L));
|
||||
serviceTimes.put(id(c), new AtomicLong(3L));
|
||||
|
||||
Comparator<MavenProject> cmp = ProjectComparator.create0(graph, serviceTimes, ProjectComparator::id);
|
||||
Comparator<MavenProject> cmp = ProjectComparator.create0(dp, serviceTimes, ProjectComparator::id);
|
||||
|
||||
Queue<MavenProject> queue = new PriorityQueue<>(3, cmp);
|
||||
queue.add(a);
|
||||
|
@@ -25,13 +25,14 @@ public class ProjectExecutorServiceTest extends AbstractSmartBuilderTest {
|
||||
final MavenProject c = newProject("c");
|
||||
TestProjectDependencyGraph graph = new TestProjectDependencyGraph(a, b, c);
|
||||
graph.addDependency(b, a);
|
||||
DependencyGraph<MavenProject> dp = DependencyGraph.fromMaven(graph, null);
|
||||
|
||||
HashMap<String, AtomicLong> serviceTimes = new HashMap<>();
|
||||
serviceTimes.put(id(a), new AtomicLong(1L));
|
||||
serviceTimes.put(id(b), new AtomicLong(1L));
|
||||
serviceTimes.put(id(c), new AtomicLong(3L));
|
||||
|
||||
Comparator<MavenProject> cmp = org.jboss.fuse.mvnd.builder.ProjectComparator.create0(graph, serviceTimes, p -> id(p));
|
||||
Comparator<MavenProject> cmp = ProjectComparator.create0(dp, serviceTimes, ProjectComparator::id);
|
||||
|
||||
PausibleProjectExecutorService executor = new PausibleProjectExecutorService(1, cmp);
|
||||
|
||||
|
@@ -11,8 +11,9 @@ public class ReactorBuildQueueTest extends AbstractSmartBuilderTest {
|
||||
MavenProject a = newProject("a"), b = newProject("b"), c = newProject("c");
|
||||
TestProjectDependencyGraph graph = new TestProjectDependencyGraph(a, b, c);
|
||||
graph.addDependency(b, a);
|
||||
DependencyGraph<MavenProject> dp = DependencyGraph.fromMaven(graph, null);
|
||||
|
||||
ReactorBuildQueue schl = new ReactorBuildQueue(graph.getSortedProjects(), graph);
|
||||
ReactorBuildQueue schl = new ReactorBuildQueue(graph.getSortedProjects(), dp);
|
||||
|
||||
assertProjects(schl.getRootProjects(), a, c);
|
||||
Assert.assertFalse(schl.isEmpty());
|
||||
@@ -25,8 +26,9 @@ public class ReactorBuildQueueTest extends AbstractSmartBuilderTest {
|
||||
public void testNoDependencies() {
|
||||
MavenProject a = newProject("a"), b = newProject("b"), c = newProject("c");
|
||||
TestProjectDependencyGraph graph = new TestProjectDependencyGraph(a, b, c);
|
||||
DependencyGraph<MavenProject> dp = DependencyGraph.fromMaven(graph, null);
|
||||
|
||||
ReactorBuildQueue schl = new ReactorBuildQueue(graph.getSortedProjects(), graph);
|
||||
ReactorBuildQueue schl = new ReactorBuildQueue(graph.getSortedProjects(), dp);
|
||||
|
||||
assertProjects(schl.getRootProjects(), a, b, c);
|
||||
Assert.assertTrue(schl.isEmpty());
|
||||
@@ -38,8 +40,9 @@ public class ReactorBuildQueueTest extends AbstractSmartBuilderTest {
|
||||
TestProjectDependencyGraph graph = new TestProjectDependencyGraph(a, b, c);
|
||||
graph.addDependency(b, a);
|
||||
graph.addDependency(b, c);
|
||||
DependencyGraph<MavenProject> dp = DependencyGraph.fromMaven(graph, null);
|
||||
|
||||
ReactorBuildQueue schl = new ReactorBuildQueue(graph.getSortedProjects(), graph);
|
||||
ReactorBuildQueue schl = new ReactorBuildQueue(graph.getSortedProjects(), dp);
|
||||
|
||||
assertProjects(schl.getRootProjects(), a, c);
|
||||
Assert.assertFalse(schl.isEmpty());
|
||||
|
@@ -3,7 +3,6 @@ package org.jboss.fuse.mvnd.builder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.google.common.collect.ArrayListMultimap;
|
||||
import com.google.common.collect.ListMultimap;
|
||||
@@ -11,7 +10,7 @@ import org.apache.maven.execution.ProjectDependencyGraph;
|
||||
import org.apache.maven.project.MavenProject;
|
||||
import org.junit.Assert;
|
||||
|
||||
public class TestProjectDependencyGraph implements ProjectDependencyGraph, DependencyGraph<MavenProject> {
|
||||
public class TestProjectDependencyGraph implements ProjectDependencyGraph {
|
||||
|
||||
private final List<MavenProject> projects = new ArrayList<MavenProject>();
|
||||
|
||||
@@ -30,16 +29,6 @@ public class TestProjectDependencyGraph implements ProjectDependencyGraph, Depen
|
||||
return projects;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<MavenProject> getProjects() {
|
||||
return projects.stream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRoot(MavenProject project) {
|
||||
return getUpstreamProjects(project, false).isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MavenProject> getSortedProjects() {
|
||||
return projects;
|
||||
@@ -51,22 +40,12 @@ public class TestProjectDependencyGraph implements ProjectDependencyGraph, Depen
|
||||
return downstream.get(project);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<MavenProject> getDownstreamProjects(MavenProject project) {
|
||||
return downstream.get(project).stream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MavenProject> getUpstreamProjects(MavenProject project, boolean transitive) {
|
||||
Assert.assertFalse("not implemented", transitive);
|
||||
return upstream.get(project);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<MavenProject> getUpstreamProjects(MavenProject project) {
|
||||
return upstream.get(project).stream();
|
||||
}
|
||||
|
||||
public void addProject(MavenProject project) {
|
||||
projects.add(project);
|
||||
}
|
||||
|
Reference in New Issue
Block a user