Skip to content

Commit f64d4f7

Browse files
[MDEP-787] allow ignoring non-test-scoped dependencies
1 parent 92ef9a3 commit f64d4f7

File tree

6 files changed

+175
-4
lines changed

6 files changed

+175
-4
lines changed

src/it/projects/analyze-ignore-dependency/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<ignoredDependencies>
5656
<ignoredDependency>org.apache.maven:maven-project</ignoredDependency>
5757
<ignoredDependency>org.apache.maven:maven-model</ignoredDependency>
58+
<ignoredDependency>org.apache.maven:maven-settings</ignoredDependency>
5859
</ignoredDependencies>
5960
</configuration>
6061
</plugin>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 org.apache.maven.settings.Settings;
21+
22+
public class MainTest
23+
{
24+
public Settings settings = null;
25+
26+
public void testPass()
27+
{
28+
}
29+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
26+
<groupId>org.apache.maven.its.dependency</groupId>
27+
<artifactId>test</artifactId>
28+
<version>1.0-SNAPSHOT</version>
29+
30+
<name>Test</name>
31+
<description>
32+
Test dependency:analyze with ignoredNonTestScopedDependencies
33+
</description>
34+
35+
<properties>
36+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
37+
</properties>
38+
39+
<dependencies>
40+
<dependency>
41+
<groupId>org.apache.maven</groupId>
42+
<artifactId>maven-settings</artifactId>
43+
<version>@mavenVersion@</version>
44+
</dependency>
45+
</dependencies>
46+
47+
<build>
48+
<pluginManagement>
49+
<plugins>
50+
<plugin>
51+
<artifactId>maven-dependency-plugin</artifactId>
52+
<configuration>
53+
<verbose>true</verbose>
54+
<failOnWarning>true</failOnWarning>
55+
<ignoredNonTestScopedDependencies>
56+
<ignoredNonTestScopedDependency>org.apache.maven:maven-settings</ignoredNonTestScopedDependency>
57+
</ignoredNonTestScopedDependencies>
58+
</configuration>
59+
</plugin>
60+
</plugins>
61+
</pluginManagement>
62+
</build>
63+
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 org.apache.maven.settings.Settings;
21+
22+
public class MainTest
23+
{
24+
public Settings settings = null;
25+
26+
public void testPass()
27+
{
28+
}
29+
}

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,26 @@ public abstract class AbstractAnalyzeMojo
223223
@Parameter
224224
private String[] ignoredUnusedDeclaredDependencies = new String[0];
225225

226+
/**
227+
* List of dependencies that will be ignored if they are in not test scope but are only used in test classes.
228+
* The filter syntax is:
229+
*
230+
* <pre>
231+
* [groupId]:[artifactId]:[type]:[version]
232+
* </pre>
233+
*
234+
* where each pattern segment is optional and supports full and partial <code>*</code> wildcards. An empty pattern
235+
* segment is treated as an implicit wildcard. *
236+
* <p>
237+
* For example, <code>org.apache.*</code> will match all artifacts whose group id starts with
238+
* <code>org.apache.</code>, and <code>:::*-SNAPSHOT</code> will match all snapshot artifacts.
239+
* </p>
240+
*
241+
* @since 3.3.0
242+
*/
243+
@Parameter
244+
private String[] ignoredNonTestScopedDependencies = new String[0];
245+
226246
/**
227247
* List of project packaging that will be ignored.
228248
* <br/>
@@ -332,11 +352,11 @@ private boolean checkDependencies()
332352
Map<Artifact, Set<String>> usedUndeclaredWithClasses =
333353
new LinkedHashMap<>( analysis.getUsedUndeclaredArtifactsWithClasses() );
334354
Set<Artifact> unusedDeclared = new LinkedHashSet<>( analysis.getUnusedDeclaredArtifacts() );
335-
Set<Artifact> testArtifactsWithNonTestScope = new LinkedHashSet<>(
336-
analysis.getTestArtifactsWithNonTestScope() );
355+
Set<Artifact> nonTestScope = new LinkedHashSet<>( analysis.getTestArtifactsWithNonTestScope() );
337356

338357
Set<Artifact> ignoredUsedUndeclared = new LinkedHashSet<>();
339358
Set<Artifact> ignoredUnusedDeclared = new LinkedHashSet<>();
359+
Set<Artifact> ignoredNonTestScope = new LinkedHashSet<>();
340360

341361
if ( ignoreUnusedRuntime )
342362
{
@@ -350,6 +370,9 @@ private boolean checkDependencies()
350370
ignoredUnusedDeclared.addAll( filterDependencies( unusedDeclared, ignoredDependencies ) );
351371
ignoredUnusedDeclared.addAll( filterDependencies( unusedDeclared, ignoredUnusedDeclaredDependencies ) );
352372

373+
ignoredNonTestScope.addAll( filterDependencies( nonTestScope, ignoredDependencies ) );
374+
ignoredNonTestScope.addAll( filterDependencies( nonTestScope, ignoredNonTestScopedDependencies ) );
375+
353376
boolean reported = false;
354377
boolean warning = false;
355378

@@ -386,11 +409,11 @@ private boolean checkDependencies()
386409
warning = true;
387410
}
388411

389-
if ( !testArtifactsWithNonTestScope.isEmpty() )
412+
if ( !nonTestScope.isEmpty() )
390413
{
391414
getLog().warn( "Non-test scoped test only dependencies found:" );
392415

393-
logArtifacts( testArtifactsWithNonTestScope, true );
416+
logArtifacts( nonTestScope, true );
394417
reported = true;
395418
warning = true;
396419
}
@@ -411,6 +434,14 @@ private boolean checkDependencies()
411434
reported = true;
412435
}
413436

437+
if ( verbose && !ignoredNonTestScope.isEmpty() )
438+
{
439+
getLog().info( "Ignored non-test scoped test only dependencies:" );
440+
441+
logArtifacts( ignoredNonTestScope, false );
442+
reported = true;
443+
}
444+
414445
if ( outputXML )
415446
{
416447
writeDependencyXML( usedUndeclaredWithClasses.keySet() );

0 commit comments

Comments
 (0)