Skip to content

Commit f2431f5

Browse files
hisenerslawekjaranowski
authored andcommitted
[MDEP-602] Log dependency warnings as errors when failOnWarning is set
1 parent 73c5925 commit f2431f5

File tree

6 files changed

+161
-14
lines changed

6 files changed

+161
-14
lines changed

src/it/projects/analyze/verify.groovy

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
if ( !classFile.isFile() )
23+
{
24+
throw new Exception( "Build was not forked, class missing " + classFile );
25+
}
26+
27+
File file = new File( basedir, "build.log" );
28+
assert file.exists();
29+
30+
String buildLog = file.getText( "UTF-8" );
31+
assert buildLog.contains( '[INFO] Used declared dependencies found:');
32+
assert buildLog.contains( '[INFO] org.apache.maven:maven-artifact:jar:2.0.6:compile');
33+
assert buildLog.contains( '[INFO] org.apache.maven:maven-model:jar:2.0.6:compile');
34+
assert buildLog.contains( '[WARNING] Used undeclared dependencies found:');
35+
assert buildLog.contains( '[WARNING] org.apache.maven:maven-repository-metadata:jar:2.0.6:compile');
36+
assert buildLog.contains( '[WARNING] class org.apache.maven.artifact.repository.metadata.Metadata');
37+
assert buildLog.contains( '[WARNING] Unused declared dependencies found:');
38+
assert buildLog.contains( '[WARNING] org.apache.maven:maven-project:jar:2.0.6:compile');
39+
40+
41+
return true;
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+
invoker.buildResult = failure
19+
invoker.goals = clean ${project.groupId}:${project.artifactId}:${project.version}:analyze
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
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+
<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">
23+
<modelVersion>4.0.0</modelVersion>
24+
25+
<groupId>org.apache.maven.plugins.dependency</groupId>
26+
<artifactId>mdep-602-log-on-error-level-when-failOnWarning</artifactId>
27+
<version>1.0.0-SNAPSHOT</version>
28+
29+
<description>Test that dependency:analyze logs dependency warnings as errors when failOnWarning is set to true</description>
30+
31+
<properties>
32+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
33+
</properties>
34+
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.apache.maven</groupId>
38+
<artifactId>maven-project</artifactId>
39+
<version>2.0.6</version>
40+
</dependency>
41+
</dependencies>
42+
43+
<build>
44+
<pluginManagement>
45+
<plugins>
46+
<plugin>
47+
<artifactId>maven-dependency-plugin</artifactId>
48+
<configuration>
49+
<failOnWarning>true</failOnWarning>
50+
</configuration>
51+
</plugin>
52+
</plugins>
53+
</pluginManagement>
54+
</build>
55+
</project>

src/it/projects/analyze/verify.bsh src/it/projects/mdep-602-log-on-error-level-when-failOnWarning/src/main/java/Main.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@
1717
* under the License.
1818
*/
1919

20-
import java.io.*;
21-
22-
File classFile = new File( basedir, "target/classes/Main.class" );
23-
24-
if ( !classFile.isFile() )
20+
public class Main
2521
{
26-
throw new Exception( "Build was not forked, class missing " + classFile );
2722
}
28-
29-
return true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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( '[ERROR] Unused declared dependencies found:');
25+
assert buildLog.contains( '[ERROR] org.apache.maven:maven-project:jar:2.0.6:compile' );
26+
27+
return true;

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

+18-6
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ private boolean checkDependencies()
406406

407407
if ( !usedUndeclaredWithClasses.isEmpty() )
408408
{
409-
getLog().warn( "Used undeclared dependencies found:" );
409+
logDependencyWarning( "Used undeclared dependencies found:" );
410410

411411
if ( verbose )
412412
{
@@ -422,7 +422,7 @@ private boolean checkDependencies()
422422

423423
if ( !unusedDeclared.isEmpty() )
424424
{
425-
getLog().warn( "Unused declared dependencies found:" );
425+
logDependencyWarning( "Unused declared dependencies found:" );
426426

427427
logArtifacts( unusedDeclared, true );
428428
reported = true;
@@ -431,7 +431,7 @@ private boolean checkDependencies()
431431

432432
if ( !nonTestScope.isEmpty() )
433433
{
434-
getLog().warn( "Non-test scoped test only dependencies found:" );
434+
logDependencyWarning( "Non-test scoped test only dependencies found:" );
435435

436436
logArtifacts( nonTestScope, true );
437437
reported = true;
@@ -500,7 +500,7 @@ private void logArtifacts( Set<Artifact> artifacts, boolean warn )
500500

501501
if ( warn )
502502
{
503-
getLog().warn( " " + artifact );
503+
logDependencyWarning( " " + artifact );
504504
}
505505
else
506506
{
@@ -526,10 +526,10 @@ private void logArtifacts( Map<Artifact, Set<String>> artifacts, boolean warn )
526526

527527
if ( warn )
528528
{
529-
getLog().warn( " " + entry.getKey() );
529+
logDependencyWarning( " " + entry.getKey() );
530530
for ( String clazz : entry.getValue() )
531531
{
532-
getLog().warn( " class " + clazz );
532+
logDependencyWarning( " class " + clazz );
533533
}
534534
}
535535
else
@@ -545,6 +545,18 @@ private void logArtifacts( Map<Artifact, Set<String>> artifacts, boolean warn )
545545
}
546546
}
547547

548+
private void logDependencyWarning( CharSequence content )
549+
{
550+
if ( failOnWarning )
551+
{
552+
getLog().error( content );
553+
}
554+
else
555+
{
556+
getLog().warn( content );
557+
}
558+
}
559+
548560
private void writeDependencyXML( Set<Artifact> artifacts )
549561
{
550562
if ( !artifacts.isEmpty() )

0 commit comments

Comments
 (0)