You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by el...@apache.org on 2021/06/19 22:56:19 UTC
[maven-dependency-analyzer] 01/01: handle different classes from
same artifact used by model and test code
This is an automated email from the ASF dual-hosted git repository.
elharo pushed a commit to branch test
in repository https://gitbox.apache.org/repos/asf/maven-dependency-analyzer.git
commit 33908e7ded67d4d5db74daad31866ec35e81344a
Author: Elliotte Rusty Harold <el...@google.com>
AuthorDate: Sat Jun 19 18:56:01 2021 -0400
handle different classes from same artifact used by model and test code
---
.../analyzer/DefaultProjectDependencyAnalyzer.java | 23 +++++++++---
.../DefaultProjectDependencyAnalyzerTest.java | 16 +++++++--
.../pom.xml | 41 ++++++++++++++++++++++
.../jarWithTestDependency/project1/Project1.java | 30 ++++++++++++++++
.../jarWithTestDependency/project2/Project2.java | 33 +++++++++++++++++
5 files changed, 136 insertions(+), 7 deletions(-)
diff --git a/src/main/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzer.java b/src/main/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzer.java
index b4b505c..72a017c 100644
--- a/src/main/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzer.java
+++ b/src/main/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzer.java
@@ -67,14 +67,17 @@ public class DefaultProjectDependencyAnalyzer
Map<Artifact, Set<String>> artifactClassMap = buildArtifactClassMap( project );
Set<String> dependencyClasses = buildDependencyClasses( project );
+ Set<String> modelDependencyClasses = buildModelDependencyClasses( project );
- Set<String> testOnlyDependencyClasses = buildTestDependencyClasses( project );
+ Set<String> testOnlyDependencyClasses = buildTestOnlyDependencyClasses( project );
Set<Artifact> declaredArtifacts = buildDeclaredArtifacts( project );
Set<Artifact> usedArtifacts = buildUsedArtifacts( artifactClassMap, dependencyClasses );
+ Set<Artifact> modelUsedArtifacts = buildUsedArtifacts( artifactClassMap, modelDependencyClasses );
- Set<Artifact> testOnlyArtifacts = buildUsedArtifacts( artifactClassMap, testOnlyDependencyClasses );
+ Set<Artifact> testArtifacts = buildUsedArtifacts( artifactClassMap, testOnlyDependencyClasses );
+ Set<Artifact> testOnlyArtifacts = removeAll( testArtifacts, modelUsedArtifacts );
Set<Artifact> usedDeclaredArtifacts = new LinkedHashSet<>( declaredArtifacts );
usedDeclaredArtifacts.retainAll( usedArtifacts );
@@ -85,10 +88,10 @@ public class DefaultProjectDependencyAnalyzer
Set<Artifact> unusedDeclaredArtifacts = new LinkedHashSet<>( declaredArtifacts );
unusedDeclaredArtifacts = removeAll( unusedDeclaredArtifacts, usedArtifacts );
- Set<Artifact> testArtifactsWithNonTestScope = getTestArtifactsWithNonTestScope( testOnlyArtifacts );
+ Set<Artifact> testOnlyArtifactsWithNonTestScope = getTestArtifactsWithNonTestScope( testOnlyArtifacts );
return new ProjectDependencyAnalysis( usedDeclaredArtifacts, usedUndeclaredArtifacts,
- unusedDeclaredArtifacts, testArtifactsWithNonTestScope );
+ unusedDeclaredArtifacts, testOnlyArtifactsWithNonTestScope );
}
catch ( IOException exception )
{
@@ -193,7 +196,7 @@ public class DefaultProjectDependencyAnalyzer
return artifactClassMap;
}
- private Set<String> buildTestDependencyClasses( MavenProject project ) throws IOException
+ private Set<String> buildTestOnlyDependencyClasses( MavenProject project ) throws IOException
{
Set<String> testOnlyDependencyClasses = new HashSet<>();
@@ -226,6 +229,16 @@ public class DefaultProjectDependencyAnalyzer
return dependencyClasses;
}
+
+ private Set<String> buildModelDependencyClasses( MavenProject project )
+ throws IOException
+ {
+
+ String outputDirectory = project.getBuild().getOutputDirectory();
+ Set<String> dependencyClasses = new HashSet<>( buildDependencyClasses( outputDirectory ) );
+
+ return dependencyClasses;
+ }
private Set<String> buildDependencyClasses( String path )
throws IOException
diff --git a/src/test/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzerTest.java b/src/test/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzerTest.java
index ffbe626..67c63f8 100644
--- a/src/test/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzerTest.java
+++ b/src/test/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzerTest.java
@@ -75,7 +75,6 @@ public class DefaultProjectDependencyAnalyzerTest
throws Exception
{
super.setUp();
-
buildTool = (BuildTool) lookup( BuildTool.ROLE );
projectTool = (ProjectTool) lookup( ProjectTool.ROLE );
@@ -279,6 +278,20 @@ public class DefaultProjectDependencyAnalyzerTest
// MSHARED-47: usedUndeclaredArtifacts=[xml-apis:xml-apis:jar:1.0.b2:compile]
// assertEquals( expectedAnalysis, actualAnalysis );
}
+
+ @Test
+ public void testJarWithDependencyUsedByTestAndModelCode()
+ throws TestToolsException, ProjectDependencyAnalyzerException {
+
+ compileProject( "jarWithCompileScopedTestAndModelDependency/pom.xml" );
+
+ MavenProject project = getProject( "jarWithCompileScopedTestDependency/pom.xml" );
+
+ ProjectDependencyAnalysis actualAnalysis = analyzer.analyze( project );
+ Set<Artifact> testArtifactsWithNonTestScope = actualAnalysis.getTestArtifactsWithNonTestScope();
+
+ assertTrue(testArtifactsWithNonTestScope.isEmpty());
+ }
@Test
public void testJarWithCompileScopedTestDependency()
@@ -500,7 +513,6 @@ public class DefaultProjectDependencyAnalyzerTest
InvocationRequest request = buildTool.createBasicInvocationRequest( pom, properties, goals, log );
request.setLocalRepositoryDirectory( localRepo );
InvocationResult result = buildTool.executeMaven( request );
-
assertNull( "Error compiling test project", result.getExecutionException() );
assertEquals( "Error compiling test project", 0, result.getExitCode() );
}
diff --git a/src/test/resources/jarWithCompileScopedTestAndModelDependency/pom.xml b/src/test/resources/jarWithCompileScopedTestAndModelDependency/pom.xml
new file mode 100644
index 0000000..2564683
--- /dev/null
+++ b/src/test/resources/jarWithCompileScopedTestAndModelDependency/pom.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ ~ 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.
+ -->
+
+<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>org.apache.maven.shared.dependency-analyzer.tests</groupId>
+ <artifactId>jarWithCompileScopedTestDependency</artifactId>
+ <packaging>pom</packaging>
+ <version>1.0</version>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ <version>3.8.1</version>
+ </dependency>
+ </dependencies>
+
+</project>
diff --git a/src/test/resources/jarWithCompileScopedTestAndModelDependency/src/main/java/jarWithTestDependency/project1/Project1.java b/src/test/resources/jarWithCompileScopedTestAndModelDependency/src/main/java/jarWithTestDependency/project1/Project1.java
new file mode 100644
index 0000000..56307a4
--- /dev/null
+++ b/src/test/resources/jarWithCompileScopedTestAndModelDependency/src/main/java/jarWithTestDependency/project1/Project1.java
@@ -0,0 +1,30 @@
+package jarWithTestDependency.project1;
+
+/*
+ * 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 org.apache.commons.lang3.ObjectUtils;
+
+public class Project1
+{
+ public Project1()
+ {
+ ObjectUtils.allNull("");
+ }
+}
diff --git a/src/test/resources/jarWithCompileScopedTestAndModelDependency/src/test/java/jarWithTestDependency/project2/Project2.java b/src/test/resources/jarWithCompileScopedTestAndModelDependency/src/test/java/jarWithTestDependency/project2/Project2.java
new file mode 100644
index 0000000..6ae19c6
--- /dev/null
+++ b/src/test/resources/jarWithCompileScopedTestAndModelDependency/src/test/java/jarWithTestDependency/project2/Project2.java
@@ -0,0 +1,33 @@
+package jarWithTestDependency.project2;
+
+/*
+ * 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 jarWithTestDependency.project1.Project1;
+import org.apache.commons.lang3.RandomUtils;
+
+public class Project2
+{
+
+ public Project2()
+ {
+ Project1 project = new Project1();
+ RandomUtils.nextBoolean();
+ }
+}