Remove mvnd.builder.rule* and mvnd.builder.rules.provider.* features #264

This commit is contained in:
Peter Palaga
2021-01-07 11:01:57 +01:00
committed by Guillaume Nodet
parent 9f7c328e6a
commit 82df39d984
9 changed files with 8 additions and 229 deletions

View File

@@ -83,10 +83,6 @@
<groupId>org.jline</groupId>
<artifactId>jline-terminal</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>

View File

@@ -15,19 +15,11 @@
*/
package org.mvndaemon.mvnd.builder;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@@ -37,14 +29,11 @@ import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.execution.ProjectDependencyGraph;
import org.apache.maven.project.MavenProject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* File origin:
@@ -52,9 +41,6 @@ import org.slf4j.LoggerFactory;
*/
public class DependencyGraph<K> {
private static final Logger logger = LoggerFactory.getLogger(DependencyGraph.class);
static final Pattern mvndRuleSanitizerPattern = Pattern.compile("[,\\s]+");
private final List<K> projects;
private final Map<K, List<K>> upstreams;
private final Map<K, Set<K>> transitiveUpstreams;
@@ -63,147 +49,16 @@ public class DependencyGraph<K> {
public static DependencyGraph<MavenProject> fromMaven(MavenSession session) {
final ProjectDependencyGraph graph = session.getProjectDependencyGraph();
final List<MavenProject> projects = graph.getSortedProjects();
return fromMaven(graph, getRules(projects, session));
return fromMaven(graph);
}
static String getRules(List<MavenProject> projects, MavenSession session) {
List<String> list = new ArrayList<>();
String providerScript = null;
final MavenProject topLevelProject = projects.get(0);
String providerUrl = topLevelProject.getProperties()
.getProperty(SmartBuilder.MVND_BUILDER_RULES_PROVIDER_URL);
if (providerUrl != null) {
logger.warn(SmartBuilder.MVND_BUILDER_RULES_PROVIDER_URL
+ " property is deprecated and the support for it will be removed in mvnd 0.3. See https://github.com/mvndaemon/mvnd/issues/264");
URL url;
try {
url = new URL(providerUrl);
} catch (MalformedURLException e) {
try {
url = new File(providerUrl).toURI().toURL();
} catch (MalformedURLException ex) {
url = null;
}
}
if (url == null) {
throw new RuntimeException("Bad syntax for " + SmartBuilder.MVND_BUILDER_RULES_PROVIDER_URL, null);
}
try (BufferedReader r = new BufferedReader(new InputStreamReader(url.openStream()))) {
StringBuilder sb = new StringBuilder();
char[] buf = new char[8192];
int l;
while ((l = r.read(buf)) >= 0) {
sb.append(buf, 0, l);
}
providerScript = sb.toString();
} catch (IOException e) {
throw new RuntimeException("Unable to read provider url " + SmartBuilder.MVND_BUILDER_RULES_PROVIDER_URL,
e);
}
}
if (providerScript == null) {
providerScript = topLevelProject.getProperties()
.getProperty(SmartBuilder.MVND_BUILDER_RULES_PROVIDER_SCRIPT);
}
if (providerScript != null) {
logger.warn(SmartBuilder.MVND_BUILDER_RULES_PROVIDER_SCRIPT
+ " property is deprecated and the support for it will be removed in mvnd 0.3. See https://github.com/mvndaemon/mvnd/issues/264");
Binding binding = new Binding();
GroovyShell shell = new GroovyShell(binding);
binding.setProperty("session", session);
Object result = shell.evaluate(providerScript);
if (result instanceof Iterable) {
for (Object r : (Iterable<?>) result) {
list.add(r.toString());
}
} else if (result != null) {
list.add(result.toString());
} else {
throw new RuntimeException("The provider script did not return a valid string or string collection", null);
}
list.add(result.toString());
}
String topRule = topLevelProject.getProperties().getProperty(SmartBuilder.MVND_BUILDER_RULES);
if (topRule != null) {
logger.warn(SmartBuilder.MVND_BUILDER_RULES
+ " property is deprecated and the support for it will be removed in mvnd 0.3. See https://github.com/mvndaemon/mvnd/issues/264");
list.add(topRule);
}
projects.forEach(p -> {
String rule = p.getProperties().getProperty(SmartBuilder.MVND_BUILDER_RULE);
if (rule != null) {
logger.warn(SmartBuilder.MVND_BUILDER_RULE
+ " property is deprecated and the support for it will be removed in mvnd 0.3. See https://github.com/mvndaemon/mvnd/issues/264");
rule = rule.trim();
if (!rule.isEmpty()) {
rule = mvndRuleSanitizerPattern.matcher(rule).replaceAll(",");
list.add(rule + " before " + p.getGroupId() + ":" + p.getArtifactId());
}
}
});
String rules = null;
if (!list.isEmpty()) {
rules = String.join("\n", list);
}
return rules;
}
static DependencyGraph<MavenProject> fromMaven(ProjectDependencyGraph graph, String rules) {
static DependencyGraph<MavenProject> fromMaven(ProjectDependencyGraph graph) {
final List<MavenProject> projects = graph.getSortedProjects();
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")) {
if (rule.trim().isEmpty()) {
continue;
}
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>(Collections.unmodifiableList(projects), upstreams, downstreams);
}

View File

@@ -22,7 +22,6 @@ import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.enterprise.inject.Default;
import javax.inject.Inject;
@@ -52,13 +51,8 @@ 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";
public static final String MVND_BUILDER_RULE = "mvnd.builder.rule";
public static final String MVND_BUILDER_RULES_PROVIDER_URL = "mvnd.builder.rules.provider.url";
public static final String MVND_BUILDER_RULES_PROVIDER_SCRIPT = "mvnd.builder.rules.provider.script";
private final Logger logger = LoggerFactory.getLogger(getClass());
static final Pattern mvndRuleSanitizerPattern = Pattern.compile("[,\\s]+");
private final LifecycleModuleBuilder moduleBuilder;

View File

@@ -1,36 +0,0 @@
/*
* 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.builder;
import java.util.Arrays;
import java.util.HashSet;
import java.util.stream.Collectors;
import org.apache.maven.project.MavenProject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.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");
Assertions.assertEquals(new HashSet<>(Arrays.asList(b, c)),
dp.getDownstreamProjects(a).collect(Collectors.toSet()));
}
}

View File

@@ -33,7 +33,7 @@ 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);
DependencyGraph<MavenProject> dp = DependencyGraph.fromMaven(graph);
Comparator<MavenProject> cmp = ProjectComparator.create0(dp, new HashMap<>(), ProjectComparator::id);
@@ -52,7 +52,7 @@ 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);
DependencyGraph<MavenProject> dp = DependencyGraph.fromMaven(graph);
HashMap<String, AtomicLong> serviceTimes = new HashMap<>();
serviceTimes.put(id(a), new AtomicLong(1L));

View File

@@ -39,7 +39,7 @@ 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);
DependencyGraph<MavenProject> dp = DependencyGraph.fromMaven(graph);
HashMap<String, AtomicLong> serviceTimes = new HashMap<>();
serviceTimes.put(id(a), new AtomicLong(1L));

View File

@@ -26,7 +26,7 @@ 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);
DependencyGraph<MavenProject> dp = DependencyGraph.fromMaven(graph);
ReactorBuildQueue schl = new ReactorBuildQueue(graph.getSortedProjects(), dp);
@@ -41,7 +41,7 @@ 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);
DependencyGraph<MavenProject> dp = DependencyGraph.fromMaven(graph);
ReactorBuildQueue schl = new ReactorBuildQueue(graph.getSortedProjects(), dp);
@@ -55,7 +55,7 @@ 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);
DependencyGraph<MavenProject> dp = DependencyGraph.fromMaven(graph);
ReactorBuildQueue schl = new ReactorBuildQueue(graph.getSortedProjects(), dp);

View File

@@ -1,29 +0,0 @@
/*
* 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.builder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class SmartBuilderTest {
@Test
void mvndRuleSanitizerPattern() {
String actual = SmartBuilder.mvndRuleSanitizerPattern.matcher("foo bar,baz,\n foo \n\n bar\n ".trim())
.replaceAll(",");
Assertions.assertEquals("foo,bar,baz,foo,bar", actual);
}
}

View File

@@ -53,7 +53,6 @@
<compiler.version>3.8.1</compiler.version>
<formatter-maven-plugin.version>2.12.1</formatter-maven-plugin.version>
<groovy-maven-plugin.version>2.1.1</groovy-maven-plugin.version>
<groovy.version>3.0.4</groovy.version>
<impsort-maven-plugin.version>1.4.1</impsort-maven-plugin.version>
<license-maven-plugin.version>3.0</license-maven-plugin.version>
<maven-jar-plugin.version>3.2.0</maven-jar-plugin.version>