Skip to content

Commit

Permalink
[MDEP-787] allow ignoring non-test-scoped dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Feb 13, 2022
1 parent 92ef9a3 commit f64d4f7
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/it/projects/analyze-ignore-dependency/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<ignoredDependencies>
<ignoredDependency>org.apache.maven:maven-project</ignoredDependency>
<ignoredDependency>org.apache.maven:maven-model</ignoredDependency>
<ignoredDependency>org.apache.maven:maven-settings</ignoredDependency>
</ignoredDependencies>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.maven.settings.Settings;

public class MainTest
{
public Settings settings = null;

public void testPass()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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.

invoker.goals = clean ${project.groupId}:${project.artifactId}:${project.version}:analyze
63 changes: 63 additions & 0 deletions src/it/projects/analyze-ignore-non-test-scope-dependency/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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.its.dependency</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>

<name>Test</name>
<description>
Test dependency:analyze with ignoredNonTestScopedDependencies
</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-settings</artifactId>
<version>@mavenVersion@</version>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<verbose>true</verbose>
<failOnWarning>true</failOnWarning>
<ignoredNonTestScopedDependencies>
<ignoredNonTestScopedDependency>org.apache.maven:maven-settings</ignoredNonTestScopedDependency>
</ignoredNonTestScopedDependencies>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.maven.settings.Settings;

public class MainTest
{
public Settings settings = null;

public void testPass()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,26 @@ public abstract class AbstractAnalyzeMojo
@Parameter
private String[] ignoredUnusedDeclaredDependencies = new String[0];

/**
* List of dependencies that will be ignored if they are in not test scope but are only used in test classes.
* The filter syntax is:
*
* <pre>
* [groupId]:[artifactId]:[type]:[version]
* </pre>
*
* where each pattern segment is optional and supports full and partial <code>*</code> wildcards. An empty pattern
* segment is treated as an implicit wildcard. *
* <p>
* For example, <code>org.apache.*</code> will match all artifacts whose group id starts with
* <code>org.apache.</code>, and <code>:::*-SNAPSHOT</code> will match all snapshot artifacts.
* </p>
*
* @since 3.3.0
*/
@Parameter
private String[] ignoredNonTestScopedDependencies = new String[0];

/**
* List of project packaging that will be ignored.
* <br/>
Expand Down Expand Up @@ -332,11 +352,11 @@ private boolean checkDependencies()
Map<Artifact, Set<String>> usedUndeclaredWithClasses =
new LinkedHashMap<>( analysis.getUsedUndeclaredArtifactsWithClasses() );
Set<Artifact> unusedDeclared = new LinkedHashSet<>( analysis.getUnusedDeclaredArtifacts() );
Set<Artifact> testArtifactsWithNonTestScope = new LinkedHashSet<>(
analysis.getTestArtifactsWithNonTestScope() );
Set<Artifact> nonTestScope = new LinkedHashSet<>( analysis.getTestArtifactsWithNonTestScope() );

Set<Artifact> ignoredUsedUndeclared = new LinkedHashSet<>();
Set<Artifact> ignoredUnusedDeclared = new LinkedHashSet<>();
Set<Artifact> ignoredNonTestScope = new LinkedHashSet<>();

if ( ignoreUnusedRuntime )
{
Expand All @@ -350,6 +370,9 @@ private boolean checkDependencies()
ignoredUnusedDeclared.addAll( filterDependencies( unusedDeclared, ignoredDependencies ) );
ignoredUnusedDeclared.addAll( filterDependencies( unusedDeclared, ignoredUnusedDeclaredDependencies ) );

ignoredNonTestScope.addAll( filterDependencies( nonTestScope, ignoredDependencies ) );
ignoredNonTestScope.addAll( filterDependencies( nonTestScope, ignoredNonTestScopedDependencies ) );

boolean reported = false;
boolean warning = false;

Expand Down Expand Up @@ -386,11 +409,11 @@ private boolean checkDependencies()
warning = true;
}

if ( !testArtifactsWithNonTestScope.isEmpty() )
if ( !nonTestScope.isEmpty() )
{
getLog().warn( "Non-test scoped test only dependencies found:" );

logArtifacts( testArtifactsWithNonTestScope, true );
logArtifacts( nonTestScope, true );
reported = true;
warning = true;
}
Expand All @@ -411,6 +434,14 @@ private boolean checkDependencies()
reported = true;
}

if ( verbose && !ignoredNonTestScope.isEmpty() )
{
getLog().info( "Ignored non-test scoped test only dependencies:" );

logArtifacts( ignoredNonTestScope, false );
reported = true;
}

if ( outputXML )
{
writeDependencyXML( usedUndeclaredWithClasses.keySet() );
Expand Down

0 comments on commit f64d4f7

Please sign in to comment.