diff --git a/maven-project/src/main/java/org/apache/maven/project/validation/DefaultModelValidator.java b/maven-project/src/main/java/org/apache/maven/project/validation/DefaultModelValidator.java index ef7669b401..181047ee27 100644 --- a/maven-project/src/main/java/org/apache/maven/project/validation/DefaultModelValidator.java +++ b/maven-project/src/main/java/org/apache/maven/project/validation/DefaultModelValidator.java @@ -37,6 +37,8 @@ import java.util.List; public class DefaultModelValidator implements ModelValidator { + private static final String ID_REGEX = "[A-Za-z0-9_\\-.]+"; + /////////////////////////////////////////////////////////////////////////// // ModelValidator Implementation @@ -46,9 +48,9 @@ public class DefaultModelValidator validateStringNotEmpty( "modelVersion", result, model.getModelVersion() ); - validateStringNotEmpty( "groupId", result, model.getGroupId() ); + validateId( "groupId", result, model.getGroupId() ); - validateStringNotEmpty( "artifactId", result, model.getArtifactId() ); + validateId( "artifactId", result, model.getArtifactId() ); validateStringNotEmpty( "packaging", result, model.getPackaging() ); @@ -58,13 +60,13 @@ public class DefaultModelValidator { Dependency d = (Dependency) it.next(); - validateSubElementStringNotEmpty( d, "dependencies.dependency.artifactId", result, d.getArtifactId() ); + validateId( "dependencies.dependency.artifactId", result, d.getArtifactId() ); - validateSubElementStringNotEmpty( d, "dependencies.dependency.groupId", result, d.getGroupId() ); + validateId( "dependencies.dependency.groupId", result, d.getGroupId() ); - validateSubElementStringNotEmpty( d, "dependencies.dependency.type", result, d.getType() ); + validateStringNotEmpty( "dependencies.dependency.type", result, d.getType() ); - validateSubElementStringNotEmpty( d, "dependencies.dependency.version", result, d.getVersion() ); + validateStringNotEmpty( "dependencies.dependency.version", result, d.getVersion() ); if ( Artifact.SCOPE_SYSTEM.equals( d.getScope() ) && StringUtils.isEmpty( d.getSystemPath() ) ) { @@ -138,6 +140,23 @@ public class DefaultModelValidator return result; } + private boolean validateId( String fieldName, ModelValidationResult result, String id ) + { + if ( !validateStringNotEmpty( fieldName, result, id ) ) + { + return false; + } + else + { + boolean match = id.matches( ID_REGEX ); + if ( !match ) + { + result.addMessage( "'" + fieldName + "' with value '" + id + "' does not match a valid id pattern." ); + } + return match; + } + } + private void validateRepositories( ModelValidationResult result, List repositories, String prefix ) { for ( Iterator it = repositories.iterator(); it.hasNext(); ) diff --git a/maven-project/src/test/java/org/apache/maven/project/validation/DefaultModelValidatorTest.java b/maven-project/src/test/java/org/apache/maven/project/validation/DefaultModelValidatorTest.java index 018b8792b9..ca5ed753e7 100644 --- a/maven-project/src/test/java/org/apache/maven/project/validation/DefaultModelValidatorTest.java +++ b/maven-project/src/test/java/org/apache/maven/project/validation/DefaultModelValidatorTest.java @@ -65,6 +65,18 @@ public class DefaultModelValidatorTest assertEquals( "'groupId' is missing.", result.getMessage( 0 ) ); } + public void testInvalidIds() + throws Exception + { + ModelValidationResult result = validate( "invalid-ids-pom.xml" ); + + assertEquals( 2, result.getMessageCount() ); + + assertEquals( "'groupId' with value 'o/a/m' does not match a valid id pattern.", result.getMessage( 0 ) ); + + assertEquals( "'artifactId' with value 'm$-do$' does not match a valid id pattern.", result.getMessage( 1 ) ); + } + public void testMissingType() throws Exception { diff --git a/maven-project/src/test/resources/validation/invalid-ids-pom.xml b/maven-project/src/test/resources/validation/invalid-ids-pom.xml new file mode 100644 index 0000000000..34145532a7 --- /dev/null +++ b/maven-project/src/test/resources/validation/invalid-ids-pom.xml @@ -0,0 +1,7 @@ + + 4.0.0 + o/a/m + m$-do$ + 99.44 + bleh +