Skip to content

Commit efa5c66

Browse files
[MDEP-928] Allow to exclude classes from dependency:analyze
1 parent 1ee4a22 commit efa5c66

File tree

8 files changed

+195
-8
lines changed

8 files changed

+195
-8
lines changed

pom.xml

-6
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,6 @@ under the License.
243243
<groupId>org.apache.maven.shared</groupId>
244244
<artifactId>maven-dependency-analyzer</artifactId>
245245
<version>1.14.0</version>
246-
<exclusions>
247-
<exclusion>
248-
<groupId>org.apache.maven</groupId>
249-
<artifactId>*</artifactId>
250-
</exclusion>
251-
</exclusions>
252246
</dependency>
253247
<dependency>
254248
<groupId>org.apache.maven.shared</groupId>
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 = ${project.groupId}:${project.artifactId}:${project.version}:analyze
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 excluded classes
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-artifact</artifactId>
43+
<version>@mavenVersion@</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.apache.maven</groupId>
47+
<artifactId>maven-core</artifactId>
48+
<version>@mavenVersion@</version>
49+
</dependency>
50+
</dependencies>
51+
52+
<build>
53+
<pluginManagement>
54+
<plugins>
55+
<plugin>
56+
<artifactId>maven-dependency-plugin</artifactId>
57+
<version>@project.version@</version>
58+
<configuration>
59+
<outputXML>true</outputXML>
60+
<scriptableOutput>true</scriptableOutput>
61+
<verbose>true</verbose>
62+
<excludedClasses>
63+
<exclude>org.example.Bad.*</exclude>
64+
</excludedClasses>
65+
</configuration>
66+
</plugin>
67+
</plugins>
68+
</pluginManagement>
69+
</build>
70+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
// prepare a class with wrong bytecode
21+
def badClass = new File(basedir, 'target/classes/org/example/BadClass.class')
22+
23+
badClass.getParentFile().mkdirs()
24+
badClass << 'some content'
25+
26+
assert badClass.isFile()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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.artifact.Artifact;
21+
import org.apache.maven.artifact.repository.metadata.Metadata;
22+
import org.apache.maven.model.Model;
23+
24+
public class Main
25+
{
26+
public static final String SCOPE_COMPILE = Artifact.SCOPE_COMPILE;
27+
28+
public Model model = null;
29+
30+
public Metadata metadata = null;
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
File classFile = new File( basedir, "target/classes/Main.class" )
21+
assert classFile.exists()
22+
assert classFile.isFile() : "Build was not forked, class missing " + classFile
23+
24+
File file = new File( basedir, "build.log" )
25+
assert file.exists()
26+
27+
String buildLog = file.getText( "UTF-8" )
28+
assert buildLog.contains( '[WARNING] Used undeclared dependencies found:')
29+
assert buildLog.contains( '[WARNING] org.apache.maven:maven-model:jar:3.6.3:compile')
30+
assert buildLog.contains( '[WARNING] Unused declared dependencies found:')
31+
assert buildLog.contains( '[WARNING] org.apache.maven:maven-core:jar:3.6.3:compile')

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,14 @@ public abstract class AbstractAnalyzeMojo extends AbstractMojo {
266266
@Parameter
267267
private List<String> ignoredPackagings = Arrays.asList("pom", "ear");
268268

269+
/**
270+
* List Excluded classes patterns from analyze. Java regular expression pattern is applied to full class name.
271+
*
272+
* @since 3.7.0
273+
*/
274+
@Parameter(property = "mdep.analyze.excludedClasses")
275+
private Set<String> excludedClasses;
276+
269277
// Mojo methods -----------------------------------------------------------
270278

271279
/*
@@ -321,7 +329,7 @@ protected final boolean isSkip() {
321329
private boolean checkDependencies() throws MojoExecutionException {
322330
ProjectDependencyAnalysis analysis;
323331
try {
324-
analysis = createProjectDependencyAnalyzer().analyze(project);
332+
analysis = createProjectDependencyAnalyzer().analyze(project, excludedClasses);
325333

326334
if (usedDependencies != null) {
327335
analysis = analysis.forceDeclaredDependenciesUsage(usedDependencies);

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.maven.plugins.dependency.analyze;
2020

2121
import java.util.Locale;
22+
import java.util.Set;
2223

2324
import org.apache.maven.plugins.annotations.Component;
2425
import org.apache.maven.plugins.annotations.Execute;
@@ -75,6 +76,14 @@ public class AnalyzeReportMojo extends AbstractMavenReport {
7576
@Parameter(property = "mdep.analyze.skip", defaultValue = "false")
7677
private boolean skip;
7778

79+
/**
80+
* List Excluded classes patterns from analyze. Java regular expression pattern is applied to full class name.
81+
*
82+
* @since 3.7.0
83+
*/
84+
@Parameter(property = "mdep.analyze.excludedClasses")
85+
private Set<String> excludedClasses;
86+
7887
/**
7988
* Internationalization component
8089
*/
@@ -91,7 +100,7 @@ public void executeReport(Locale locale) throws MavenReportException {
91100
// Step 1: Analyze the project
92101
ProjectDependencyAnalysis analysis;
93102
try {
94-
analysis = analyzer.analyze(project);
103+
analysis = analyzer.analyze(project, excludedClasses);
95104

96105
if (usedDependencies != null) {
97106
analysis = analysis.forceDeclaredDependenciesUsage(usedDependencies);

0 commit comments

Comments
 (0)