Skip to content

Commit 38d34f8

Browse files
committed
[MDEP-317] - Add testcase and print project name in output
Add testcase for multimodule project with invalid dependency managed exclusion where one of the child modules uses this dependency Add project name in output when violations are found.
1 parent 5dd4a59 commit 38d34f8

File tree

11 files changed

+305
-22
lines changed

11 files changed

+305
-22
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
# 'install' is for this IT to work with Maven 2.2.1. Not required for Maven 3.0+.
19+
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:analyze-exclusions
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
<parent>
27+
<groupId>org.apache.maven.its.dependency</groupId>
28+
<artifactId>test-parent</artifactId>
29+
<version>1.0-SNAPSHOT</version>
30+
</parent>
31+
32+
<artifactId>test-module1</artifactId>
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>org.apache.maven</groupId>
37+
<artifactId>maven-core</artifactId>
38+
</dependency>
39+
</dependencies>
40+
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
package foo;
20+
21+
import org.apache.maven.artifact.Artifact;
22+
import org.apache.maven.artifact.repository.metadata.Metadata;
23+
import org.apache.maven.model.Model;
24+
25+
public class Main
26+
{
27+
public static final String SCOPE_COMPILE = Artifact.SCOPE_COMPILE;
28+
29+
public Model model = null;
30+
31+
public Metadata metadata = null;
32+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
<parent>
27+
<groupId>org.apache.maven.its.dependency</groupId>
28+
<artifactId>test-parent</artifactId>
29+
<version>1.0-SNAPSHOT</version>
30+
</parent>
31+
32+
<artifactId>test-module2</artifactId>
33+
34+
<dependencies>
35+
</dependencies>
36+
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
package foo;
20+
21+
import org.apache.maven.artifact.Artifact;
22+
import org.apache.maven.artifact.repository.metadata.Metadata;
23+
import org.apache.maven.model.Model;
24+
25+
public class Main
26+
{
27+
public static final String SCOPE_COMPILE = Artifact.SCOPE_COMPILE;
28+
29+
public Model model = null;
30+
31+
public Metadata metadata = null;
32+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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-parent</artifactId>
28+
<version>1.0-SNAPSHOT</version>
29+
<packaging>pom</packaging>
30+
31+
<name>Test</name>
32+
<description>
33+
Test dependency:analyze on a multi-module project
34+
</description>
35+
36+
<properties>
37+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
38+
</properties>
39+
40+
<modules>
41+
<module>module1</module>
42+
<module>module2</module>
43+
</modules>
44+
45+
<dependencyManagement>
46+
<dependencies>
47+
<dependency>
48+
<groupId>org.apache.maven</groupId>
49+
<artifactId>maven-core</artifactId>
50+
<version>3.9.6</version>
51+
<exclusions>
52+
<exclusion>
53+
<groupId>javax.servlet</groupId>
54+
<artifactId>javax.servlet-api</artifactId>
55+
</exclusion>
56+
</exclusions>
57+
</dependency>
58+
</dependencies>
59+
</dependencyManagement>
60+
61+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 file = new File( basedir, "build.log" );
21+
assert file.exists();
22+
23+
String buildLog = file.getText( "UTF-8" );
24+
assert buildLog.contains( '[WARNING] test-module1 defines following unnecessary excludes');
25+
assert buildLog.contains( '[WARNING] org.apache.maven:maven-core:');
26+
assert buildLog.contains( '[WARNING] - javax.servlet:javax.servlet-api');
27+
28+
assert !buildLog.contains( '[WARNING] test-module2 defines following unnecessary excludes');
29+
30+
return true;

src/it/projects/analyze-invalid-exclude/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<artifactId>test</artifactId>
2828
<version>1.0-SNAPSHOT</version>
2929

30-
<name>Test</name>
30+
<name>test-module</name>
3131
<description>
3232
Test dependency:analyze-exclusion
3333
</description>

src/it/projects/analyze-invalid-exclude/verify.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ File file = new File( basedir, "build.log" );
2121
assert file.exists();
2222

2323
String buildLog = file.getText( "UTF-8" );
24-
assert buildLog.contains( '[WARNING] The following dependencies defines unnecessary excludes');
24+
assert buildLog.contains( '[WARNING] test-module defines following unnecessary excludes');
2525
assert buildLog.contains( '[WARNING] org.apache.maven:maven-artifact:');
2626
assert buildLog.contains( '[WARNING] - javax.annotation:javax.annotation-api');
2727
assert buildLog.contains( '[WARNING] - javax.activation:javax.activation-api');

src/main/java/org/apache/maven/plugins/dependency/exclusion/AnalyzeExclusionsMojo.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
120120

121121
if (!checker.getViolations().isEmpty()) {
122122
if (failOnWarning) {
123-
logViolations(checker.getViolations(), (value) -> getLog().error(value));
123+
logViolations(project.getName(), checker.getViolations(), (value) -> getLog().error(value));
124124
throw new MojoExecutionException("Invalid exclusions found");
125125
} else {
126-
logViolations(checker.getViolations(), (value) -> getLog().warn(value));
126+
logViolations(project.getName(), checker.getViolations(), (value) -> getLog().warn(value));
127127
}
128128
}
129129
}
@@ -135,8 +135,8 @@ private boolean matchesDependency(Artifact artifact, Dependency dependency) {
135135
&& Objects.equals(stripToEmpty(artifact.getClassifier()), stripToEmpty(dependency.getClassifier()));
136136
}
137137

138-
private void logViolations(Map<Coordinates, List<Coordinates>> violations, final Consumer<String> logger) {
139-
logger.accept("The following dependencies defines unnecessary excludes");
138+
private void logViolations(String name, Map<Coordinates, List<Coordinates>> violations, Consumer<String> logger) {
139+
logger.accept(name + " defines following unnecessary excludes");
140140
violations.forEach((dependency, invalidExclusions) -> {
141141
logger.accept(" " + dependency + ":");
142142
invalidExclusions.forEach(invalidExclusion -> {

0 commit comments

Comments
 (0)