Skip to content

Commit

Permalink
[MENFORCER-409] Log at ERROR level when <fail> is set
Browse files Browse the repository at this point in the history
  • Loading branch information
electrum committed Dec 22, 2021
1 parent d8f257f commit 7efcd17
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 28 deletions.
Expand Up @@ -18,5 +18,5 @@
*/
File buildLog = new File( basedir, 'build.log' )
assert buildLog.text.contains( '[INFO] BUILD FAILURE' )
assert buildLog.text.contains( '[WARNING] Rule 0: org.apache.maven.plugins.enforcer.BanDistributionManagement failed with message:' )
assert buildLog.text.contains( '[ERROR] Rule 0: org.apache.maven.plugins.enforcer.BanDistributionManagement failed with message:' )
assert buildLog.text.contains( 'You have defined a repository in distributionManagement.' )
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/
File buildLog = new File( basedir, 'build.log' )
assert buildLog.text.contains( '[WARNING] Rule 0: org.apache.maven.plugins.enforcer.BanDuplicatePomDependencyVersions failed with message:' )
assert buildLog.text.contains( '[ERROR] Rule 0: org.apache.maven.plugins.enforcer.BanDuplicatePomDependencyVersions failed with message:' )
assert buildLog.text.contains( 'Found 1 duplicate dependency declarations in this project:' )
assert buildLog.text.contains( '- dependencyManagement.dependencies.dependency[org.apache.maven.plugins.enforcer.its:menforcer128_api:jar] ( 2 times )' )

Expand Down
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/
File buildLog = new File( basedir, 'build.log' )
assert buildLog.text.contains( '[WARNING] Rule 0: org.apache.maven.plugins.enforcer.BanDuplicatePomDependencyVersions failed with message:' )
assert buildLog.text.contains( '[ERROR] Rule 0: org.apache.maven.plugins.enforcer.BanDuplicatePomDependencyVersions failed with message:' )
assert buildLog.text.contains( 'Found 1 duplicate dependency declaration in this project:' )
assert buildLog.text.contains( '- dependencies.dependency[org.apache.maven.plugins.enforcer.its:menforcer152:jar] ( 2 times )' )

Expand Down
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/
File buildLog = new File( basedir, 'build.log' )
assert buildLog.text.contains( '[WARNING] Rule 0: org.apache.maven.plugins.enforcer.BanDuplicatePomDependencyVersions failed with message:' )
assert buildLog.text.contains( '[ERROR] Rule 0: org.apache.maven.plugins.enforcer.BanDuplicatePomDependencyVersions failed with message:' )
assert buildLog.text.contains( 'Found 1 duplicate dependency declaration in this project:' )
assert buildLog.text.contains( '- dependencies.dependency[org.apache.maven.plugins.enforcer.its:menforcer152:jar] ( 2 times )' )

Expand Down
Expand Up @@ -17,5 +17,5 @@
* under the License.
*/
File buildLog = new File( basedir, 'build.log' )
assert buildLog.text.contains( '[WARNING] Rule 0: org.apache.maven.plugins.enforcer.BanTransitiveDependencies failed with message:' )
assert buildLog.text.contains( '[ERROR] Rule 0: org.apache.maven.plugins.enforcer.BanTransitiveDependencies failed with message:' )

Expand Up @@ -17,5 +17,5 @@
* under the License.
*/
File buildLog = new File( basedir, 'build.log' )
assert buildLog.text.contains( '[WARNING] Rule 0: org.apache.maven.plugins.enforcer.BannedDependencies failed with message:' )
assert buildLog.text.contains( '[ERROR] Rule 0: org.apache.maven.plugins.enforcer.BannedDependencies failed with message:' )

Expand Up @@ -17,6 +17,6 @@
* under the License.
*/
File buildLog = new File( basedir, 'build.log' )
assert buildLog.text.contains( '[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequireNoRepositories failed with message:' )
assert buildLog.text.contains( '[ERROR] Rule 0: org.apache.maven.plugins.enforcer.RequireNoRepositories failed with message:' )
assert buildLog.text.contains( 'Some poms have repositories defined:' )
assert buildLog.text.contains( 'org.apache.maven.its.enforcer:test version:1.0 has repositories [com.asual.maven.public]' )
Expand Up @@ -17,6 +17,6 @@
* under the License.
*/
File buildLog = new File( basedir, 'build.log' )
assert buildLog.text.contains( '[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequireNoRepositories failed with message:' )
assert buildLog.text.contains( '[ERROR] Rule 0: org.apache.maven.plugins.enforcer.RequireNoRepositories failed with message:' )
assert buildLog.text.contains( 'Some poms have repositories defined:' )
assert buildLog.text.contains( 'org.apache.maven.its.enforcer:test version:1.0 has plugin repositories [plugin-repo]' )
Expand Up @@ -17,6 +17,6 @@
* under the License.
*/
File buildLog = new File( basedir, 'build.log' )
assert buildLog.text.contains( '[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequireNoRepositories failed with message:' )
assert buildLog.text.contains( '[ERROR] Rule 0: org.apache.maven.plugins.enforcer.RequireNoRepositories failed with message:' )
assert buildLog.text.contains( 'Some poms have repositories defined:' )
assert buildLog.text.contains( 'org.apache.maven.its.enforcer:test version:1.0 has repositories [com.asual.maven.public]' )
Expand Up @@ -17,6 +17,6 @@
* under the License.
*/
File buildLog = new File( basedir, 'build.log' )
assert buildLog.text.contains( '[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequireNoRepositories failed with message:' )
assert buildLog.text.contains( '[ERROR] Rule 0: org.apache.maven.plugins.enforcer.RequireNoRepositories failed with message:' )
assert buildLog.text.contains( 'Some poms have repositories defined:' )
assert buildLog.text.contains( 'org.apache.maven.its.enforcer:test version:1.0 has repositories [com.asual.maven.public]' )
Expand Up @@ -17,6 +17,6 @@
* under the License.
*/
File buildLog = new File( basedir, 'build.log' )
assert buildLog.text.contains( '[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequireNoRepositories failed with message:' )
assert buildLog.text.contains( '[ERROR] Rule 0: org.apache.maven.plugins.enforcer.RequireNoRepositories failed with message:' )
assert buildLog.text.contains( 'Some poms have repositories defined:' )
assert buildLog.text.contains( 'org.apache.maven.its.enforcer:test version:1.0 has plugin repositories [repo]' )
Expand Up @@ -17,6 +17,6 @@
* under the License.
*/
File buildLog = new File( basedir, 'build.log' )
assert buildLog.text.contains( '[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequireNoRepositories failed with message:' )
assert buildLog.text.contains( '[ERROR] Rule 0: org.apache.maven.plugins.enforcer.RequireNoRepositories failed with message:' )
assert buildLog.text.contains( 'Some poms have repositories defined:' )
assert buildLog.text.contains( 'org.apache.maven.its.enforcer:test version:1.0 has repositories [com.asual.maven.public]' )
Expand Up @@ -17,5 +17,5 @@
* under the License.
*/
File buildLog = new File( basedir, 'build.log' )
assert buildLog.text.contains( '[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequirePluginVersions failed with message:' )
assert buildLog.text.contains( '[ERROR] Rule 0: org.apache.maven.plugins.enforcer.RequirePluginVersions failed with message:' )
assert buildLog.text.contains( "Some plugins are missing valid versions or depend on Maven ${mavenVersion} defaults: (LATEST RELEASE SNAPSHOT are not allowed)" )
Expand Up @@ -19,9 +19,9 @@
* under the License.
*/

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.maven.enforcer.rule.api.EnforcerLevel;
import org.apache.maven.enforcer.rule.api.EnforcerRule;
Expand Down Expand Up @@ -158,8 +158,8 @@ public void execute()
// CHECKSTYLE_ON: LineLength
}

// list to store exceptions
List<String> list = new ArrayList<>();
// messages with warn/error flag
Map<String, Boolean> messages = new LinkedHashMap<>();

String currentRule = "Unknown";

Expand Down Expand Up @@ -228,31 +228,36 @@ public void execute()
if ( level == EnforcerLevel.ERROR )
{
hasErrors = true;
list.add( "Rule " + i + ": " + currentRule + " failed with message:"
+ System.lineSeparator() + exceptionMessage );
messages.put( "Rule " + i + ": " + currentRule + " failed with message:"
+ System.lineSeparator() + exceptionMessage, true );
}
else
{
list.add( "Rule " + i + ": " + currentRule + " warned with message:"
+ System.lineSeparator() + exceptionMessage );
messages.put( "Rule " + i + ": " + currentRule + " warned with message:"
+ System.lineSeparator() + exceptionMessage, false );
}
}
}
}
}

// if we found anything
// CHECKSTYLE_OFF: LineLength
if ( !list.isEmpty() )
// log any messages
messages.forEach( ( message, error ) ->
{
for ( String failure : list )
if ( fail && error )
{
log.warn( failure );
log.error( message );
}
if ( fail && hasErrors )
else
{
throw new MojoExecutionException( "Some Enforcer rules have failed. Look above for specific messages explaining why the rule failed." );
log.warn( message );
}
} );

// CHECKSTYLE_OFF: LineLength
if ( fail && hasErrors )
{
throw new MojoExecutionException( "Some Enforcer rules have failed. Look above for specific messages explaining why the rule failed." );
}
// CHECKSTYLE_ON: LineLength
}
Expand Down

0 comments on commit 7efcd17

Please sign in to comment.