diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java b/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java index 0571677ce1..96cdb4be00 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java @@ -261,10 +261,8 @@ public class DefaultModelValidator validateStringNotEmpty( "build.plugins.plugin.groupId", problems, Severity.ERROR, p.getGroupId() ); - validateStringNotEmpty( "build.plugins.plugin.version", problems, errOn31, p.getVersion(), - p.getKey() ); - - validatePluginVersion( "build.plugins.plugin.version", problems, errOn30, p.getVersion(), p.getKey() ); + validatePluginVersion( "build.plugins.plugin.version", problems, p.getVersion(), p.getKey(), + request ); validateBoolean( "build.plugins.plugin.inherited", problems, errOn30, p.getInherited(), p.getKey() ); @@ -655,27 +653,31 @@ public class DefaultModelValidator return false; } - private boolean validatePluginVersion( String fieldName, ModelProblemCollector problems, Severity severity, String string, - String sourceHint ) + private boolean validatePluginVersion( String fieldName, ModelProblemCollector problems, String string, + String sourceHint, ModelBuildingRequest request ) { - if ( string == null || string.length() <= 0 ) + Severity errOn30 = getSeverity( request, ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_3_0 ); + Severity errOn31 = getSeverity( request, ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_3_1 ); + + if ( !validateNotNull( fieldName, problems, errOn31, string, sourceHint ) ) { - return true; + return false; } - if ( !hasExpression( string ) && !"RELEASE".equals( string ) && !"LATEST".equals( string ) ) + if ( string.length() > 0 && !hasExpression( string ) && !"RELEASE".equals( string ) + && !"LATEST".equals( string ) ) { return true; } if ( sourceHint != null ) { - addViolation( problems, severity, "'" + fieldName + "' must be a valid version for " + sourceHint + addViolation( problems, errOn30, "'" + fieldName + "' must be a valid version for " + sourceHint + " but is '" + string + "'." ); } else { - addViolation( problems, severity, "'" + fieldName + "' must be a valid version but is '" + string + "'." ); + addViolation( problems, errOn30, "'" + fieldName + "' must be a valid version but is '" + string + "'." ); } return false; @@ -693,7 +695,12 @@ public class DefaultModelValidator private static Severity getSeverity( ModelBuildingRequest request, int errorThreshold ) { - if ( request.getValidationLevel() < errorThreshold ) + return getSeverity( request.getValidationLevel(), errorThreshold ); + } + + private static Severity getSeverity( int validationLevel, int errorThreshold ) + { + if ( validationLevel < errorThreshold ) { return Severity.WARNING; } diff --git a/maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java b/maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java index 9e21926c4e..3d066690bf 100644 --- a/maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java +++ b/maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java @@ -286,6 +286,17 @@ public class DefaultModelValidatorTest assertViolations( result, 0, 0, 1 ); } + public void testEmptyPluginVersion() + throws Exception + { + SimpleProblemCollector result = validate( "empty-plugin-version.xml" ); + + assertViolations( result, 0, 1, 0 ); + + assertEquals( "'build.plugins.plugin.version' must be a valid version " + + "for org.apache.maven.plugins:maven-it-plugin but is ''.", result.getErrors().get( 0 ) ); + } + public void testMissingRepositoryId() throws Exception { diff --git a/maven-model-builder/src/test/resources/poms/validation/empty-plugin-version.xml b/maven-model-builder/src/test/resources/poms/validation/empty-plugin-version.xml new file mode 100644 index 0000000000..340efc5cd3 --- /dev/null +++ b/maven-model-builder/src/test/resources/poms/validation/empty-plugin-version.xml @@ -0,0 +1,34 @@ + + + + 4.0.0 + foo + bar + 1.0 + pack + + + + maven-it-plugin + + + + +