XD-3338 Prevent optional dependencies to be automatically added as includes

This commit is contained in:
Marius Bogoevici
2015-10-05 17:18:20 -04:00
committed by Mark Pollack
parent d992e0b852
commit 28d1bee0dc
9 changed files with 82 additions and 8 deletions

View File

@@ -59,7 +59,7 @@ public class ModuleLauncher {
public static final String MODULE_AGGREGATOR_RUNNER_THREAD_NAME = "module-aggregator-runner";
private Log log = LogFactory.getLog(ModuleLauncher.class);
private static Log log = LogFactory.getLog(ModuleLauncher.class);
private static final String DEFAULT_EXTENSION = "jar";

View File

@@ -207,7 +207,7 @@ public class AetherModuleResolver implements ModuleResolver {
DependencyResult dependencyResult =
repositorySystem.resolveDependencies(session,
new DependencyRequest(collectRequest,
new InclusionExclusionDependencyFilter(includeArtifacts, excludePatterns)));
new ModuleDependencyFilter(includeArtifacts, excludePatterns)));
for (ArtifactResult artifactResult : dependencyResult.getArtifactResults()) {
// we are only interested in the jars
if ("jar".equalsIgnoreCase(artifactResult.getArtifact().getExtension())) {

View File

@@ -31,21 +31,29 @@ import org.springframework.util.ObjectUtils;
*
* @author Marius Bogoevici
*/
public class InclusionExclusionDependencyFilter implements DependencyFilter {
public class ModuleDependencyFilter implements DependencyFilter {
private final PatternExclusionsDependencyFilter patternExclusionsDependencyFilter;
private final Artifact[] includes;
private boolean acceptOptional = false;
public InclusionExclusionDependencyFilter(Artifact[] includes, String... excludes) {
public ModuleDependencyFilter(Artifact[] includes, String... excludes) {
this.patternExclusionsDependencyFilter = new PatternExclusionsDependencyFilter(excludes);
this.includes = includes != null ? includes : new Artifact[0];
}
public void setAcceptOptional(boolean acceptOptional) {
this.acceptOptional = acceptOptional;
}
@Override
public boolean accept(DependencyNode node, List<DependencyNode> parents) {
// optional nodes are rejected conditionally
// nodes included explicitly are always accepted
return isIncludedDirectly(node) || patternExclusionsDependencyFilter.accept(node, parents);
return (acceptOptional || !node.getDependency().isOptional())
&& (isIncludedDirectly(node) || patternExclusionsDependencyFilter.accept(node, parents));
}
private boolean isIncludedDirectly(DependencyNode node) {

View File

@@ -20,7 +20,15 @@ import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
import static org.hamcrest.object.HasToString.hasToString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@@ -33,18 +41,15 @@ import java.util.Map;
import java.util.UUID;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.hamcrest.Matchers;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.util.SocketUtils;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import wiremock.org.mortbay.resource.FileResource;
/**
* @author David Turanski
@@ -66,6 +71,23 @@ public class AetherModuleResolverTests {
assertEquals(resource.getFile().getName(), "foo-bar-1.0.0.jar");
}
@Test
public void testResolveLocalWithIncludes() throws IOException {
ClassPathResource cpr = new ClassPathResource("local-repo");
File localRepository = cpr.getFile();
AetherModuleResolver defaultModuleResolver = new AetherModuleResolver(localRepository, null);
Resource[] resources = defaultModuleResolver.resolve(
new Coordinates("foo.bar", "foo-bar", "jar", "", "1.0.0"),
new Coordinates[]{new Coordinates("qux.bar", "qux-bar", "jar", "", "1.0.0")},new String[]{});
assertThat(resources, arrayWithSize(2));
assertTrue(resources[0].exists());
assertTrue(resources[1].exists());
// the optional dependency 'foo.baz:foo-baz:1.0.0'of 'foo.bar:foo-bar' is not included
assertThat(resources, arrayContainingInAnyOrder(
hasToString(containsString("foo-bar-1.0.0.jar")),
hasToString(containsString("qux-bar-1.0.0.jar"))));
}
@Test(expected = RuntimeException.class)
public void testResolveDoesNotExist() throws IOException {
ClassPathResource cpr = new ClassPathResource("local-repo");

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>foo.bar</groupId>
<artifactId>foo-bar</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>foo-bar</name>
<dependencies>
<dependency>
<groupId>foo.baz</groupId>
<artifactId>foo-baz</artifactId>
<version>1.0.0</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>foo.baz</groupId>
<artifactId>foo-baz</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>foo-baz</name>
</project>

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>qux-bar</groupId>
<artifactId>qux-bar</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>qux-bar</name>
</project>