Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MENFORCER-409] Log at ERROR level when <fail> is set #133

Merged
merged 1 commit into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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.' )
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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:' )

Original file line number Diff line number Diff line change
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:' )

Original file line number Diff line number Diff line change
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]' )
Original file line number Diff line number Diff line change
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]' )
Original file line number Diff line number Diff line change
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]' )
Original file line number Diff line number Diff line change
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]' )
Original file line number Diff line number Diff line change
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]' )
Original file line number Diff line number Diff line change
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]' )
Original file line number Diff line number Diff line change
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)" )
Original file line number Diff line number Diff line change
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