Skip to content

Commit 130aecd

Browse files
authored
[MDEP-708] dependency:analyze should recommend test scope where possible (apache#80)
* added nonTestScopeTestArtifact logic
1 parent 949f8ec commit 130aecd

File tree

8 files changed

+251
-2
lines changed

8 files changed

+251
-2
lines changed

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ under the License.
210210
<dependency>
211211
<groupId>org.apache.maven.shared</groupId>
212212
<artifactId>maven-dependency-analyzer</artifactId>
213-
<version>1.11.2</version>
213+
<version>1.11.3-SNAPSHOT</version>
214214
<exclusions>
215215
<exclusion>
216216
<artifactId>maven-project</artifactId>
@@ -435,6 +435,7 @@ under the License.
435435
<filterProperties>
436436
<repository.proxy.url>${repository.proxy.url}</repository.proxy.url>
437437
</filterProperties>
438+
<addTestClassPath>true</addTestClassPath>
438439
</configuration>
439440
</plugin>
440441
<plugin>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
invoker.goals = clean ${project.groupId}:${project.artifactId}:${project.version}:analyze-report
19+
# don't know why it fails with Maven 2 on some weird java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
20+
# no time to investigate, Maven 2 is not supported any more
21+
invoker.maven.version = 3.0+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one
4+
~ or more contributor license agreements. See the NOTICE file
5+
~ distributed with this work for additional information
6+
~ regarding copyright ownership. The ASF licenses this file
7+
~ to you under the Apache License, Version 2.0 (the
8+
~ "License"); you may not use this file except in compliance
9+
~ with the License. You may obtain a copy of the License at
10+
~
11+
~ http://www.apache.org/licenses/LICENSE-2.0
12+
~
13+
~ Unless required by applicable law or agreed to in writing,
14+
~ software distributed under the License is distributed on an
15+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
~ KIND, either express or implied. See the License for the
17+
~ specific language governing permissions and limitations
18+
~ under the License.
19+
-->
20+
21+
<project xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
<groupId>org.apache.maven.plugins.dependency.its</groupId>
26+
<artifactId>nonTestScopeTestArtifactsAnalysis</artifactId>
27+
<version>1.0.0-SNAPSHOT</version>
28+
29+
<properties>
30+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
31+
<maven.compiler.release>1.7</maven.compiler.release>
32+
<maven.compiler.sources>1.7</maven.compiler.sources>
33+
</properties>
34+
35+
<build>
36+
<pluginManagement>
37+
<plugins>
38+
<plugin>
39+
<artifactId>maven-dependency-plugin</artifactId>
40+
<configuration>
41+
<outputXML>true</outputXML>
42+
<scriptableOutput>true</scriptableOutput>
43+
<verbose>true</verbose>
44+
</configuration>
45+
</plugin>
46+
</plugins>
47+
</pluginManagement>
48+
</build>
49+
50+
<dependencies>
51+
<dependency>
52+
<groupId>junit</groupId>
53+
<artifactId>junit</artifactId>
54+
<version>4.12</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.assertj</groupId>
58+
<artifactId>assertj-core</artifactId>
59+
<!-- use 2.9.1 for Java 7 projects -->
60+
<version>2.9.1</version>
61+
<scope>runtime</scope>
62+
</dependency>
63+
<!-- Integration test for scope analysis -->
64+
<dependency>
65+
<groupId>org.jsoup</groupId>
66+
<artifactId>jsoup</artifactId>
67+
<version>1.13.1</version>
68+
<!--<scope>test</scope>-->
69+
</dependency>
70+
<dependency>
71+
<groupId>org.apache.maven</groupId>
72+
<artifactId>maven-project</artifactId>
73+
<version>2.0.6</version>
74+
</dependency>
75+
</dependencies>
76+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package hello;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
public class Hello
23+
{
24+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package hello;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import org.junit.Test;
23+
import static org.junit.Assert.*;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
public class HelloTest {
28+
29+
@Test
30+
public void testAdd() {
31+
assertEquals( 1 + 1, 2 );
32+
assertThat( "aaa" ).contains( "a" );
33+
}
34+
35+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import java.io.*;
21+
import org.jsoup.Jsoup;
22+
import org.jsoup.helper.Validate;
23+
import org.jsoup.nodes.Document;
24+
import org.jsoup.nodes.Element;
25+
import org.jsoup.select.Elements;
26+
27+
File htmlFile = new File( basedir, "target/dependency-analysis.html" );
28+
29+
if ( !htmlFile.isFile() )
30+
{
31+
throw new Exception( "Dependency analysis file missing " + htmlFile );
32+
}
33+
if ( htmlFile.length() == 0 )
34+
{
35+
throw new Exception( "Dependency analysis file empty " + htmlFile );
36+
}
37+
38+
Document doc = Jsoup.parse( htmlFile, "UTF-8");
39+
40+
Elements entry = doc.select( "#contentBox > section > section:nth-child(5) > table > tbody > tr.b" );
41+
42+
if( entry.isEmpty() )
43+
{
44+
throw new Exception ( "Can't find table entry under CompileScopedTestDependencies header " + htmlFile);
45+
}
46+
else if ( entry.size() != 1 )
47+
{
48+
throw new Exception ( "Table entry under CompileScopedTestDependencies header has " + entry.size() +
49+
"rows but only 1 was expected " + htmlFile );
50+
}
51+
52+
Element groupIdElement = doc.select(
53+
"#contentBox > section > section:nth-child(5) > table > tbody > tr.b > td:nth-child(1)" ).first();
54+
55+
String groupId = groupIdElement.text();
56+
57+
if ( !groupId.equals( "junit" ) )
58+
{
59+
throw new Exception ( "Table entry under CompileScopedTestDependencies header groupId is not junit. Value is " +
60+
groupId + " in " + htmlFile);
61+
}
62+
63+
return true;

src/main/java/org/apache/maven/plugins/dependency/analyze/AbstractAnalyzeMojo.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
/**
5151
* Analyzes the dependencies of this project and determines which are: used and declared; used and undeclared; unused
52-
* and declared.
52+
* and declared; compile scoped but only used in tests.
5353
*
5454
* @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
5555
* @since 2.0-alpha-5
@@ -318,6 +318,8 @@ private boolean checkDependencies()
318318
Set<Artifact> usedDeclared = new LinkedHashSet<>( analysis.getUsedDeclaredArtifacts() );
319319
Set<Artifact> usedUndeclared = new LinkedHashSet<>( analysis.getUsedUndeclaredArtifacts() );
320320
Set<Artifact> unusedDeclared = new LinkedHashSet<>( analysis.getUnusedDeclaredArtifacts() );
321+
Set<Artifact> testArtifactsWithNonTestScope = new LinkedHashSet<>(
322+
analysis.getTestArtifactsWithNonTestScope() );
321323

322324
Set<Artifact> ignoredUsedUndeclared = new LinkedHashSet<>();
323325
Set<Artifact> ignoredUnusedDeclared = new LinkedHashSet<>();
@@ -357,6 +359,15 @@ private boolean checkDependencies()
357359
warning = true;
358360
}
359361

362+
if ( !testArtifactsWithNonTestScope.isEmpty() )
363+
{
364+
getLog().warn( "Non-test scoped test only dependencies found:" );
365+
366+
logArtifacts( testArtifactsWithNonTestScope, true );
367+
reported = true;
368+
warning = true;
369+
}
370+
360371
if ( verbose && !ignoredUsedUndeclared.isEmpty() )
361372
{
362373
getLog().info( "Ignored used undeclared dependencies:" );

src/main/java/org/apache/maven/plugins/dependency/analyze/AnalyzeReportView.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,24 @@ public void generateReport( ProjectDependencyAnalysis analysis, Sink sink, Resou
108108
}
109109
sink.section2_();
110110

111+
// Generate Non-Test Scoped Test Dependencies:
112+
sink.section2();
113+
sink.sectionTitle2();
114+
sink.text( "Compile Scoped Test Dependencies" );
115+
sink.sectionTitle2_();
116+
if ( analysis.getTestArtifactsWithNonTestScope().isEmpty() )
117+
{
118+
sink.paragraph();
119+
sink.text( "None" );
120+
sink.paragraph_();
121+
sink.horizontalRule();
122+
}
123+
else
124+
{
125+
generateDependenciesTable( sink, analysis.getTestArtifactsWithNonTestScope().iterator() );
126+
}
127+
sink.section2_();
128+
111129
sink.section1_();
112130

113131
// Closing the report

0 commit comments

Comments
 (0)