From 41e54938d5ed3b8ec2e5c72ca8d8fd29adf02f35 Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Wed, 27 May 2009 19:59:17 +0000 Subject: [PATCH] o Moved model validator into o.a.m.model and moved old impl into compat module o Added method to validate raw POM git-svn-id: https://svn.apache.org/repos/asf/maven/components/branches/MNG-2766@779289 13f79535-47bb-0310-9956-ffa450edef68 --- .../validation/DefaultModelValidator.java | 51 +++++++++++++++++ .../validation/ModelValidationResult.java | 30 ++++++++++ .../project/validation/ModelValidator.java | 12 +--- .../project/DefaultMavenProjectBuilder.java | 16 ++++-- .../project/InvalidProjectModelException.java | 2 +- .../project/AbstractMavenProjectTestCase.java | 2 +- .../validation/DefaultModelValidator.java | 32 ++++++++--- .../validation/ModelValidationResult.java | 4 +- .../model/validation/ModelValidator.java | 57 +++++++++++++++++++ 9 files changed, 178 insertions(+), 28 deletions(-) create mode 100644 maven-compat/src/main/java/org/apache/maven/project/validation/DefaultModelValidator.java create mode 100644 maven-compat/src/main/java/org/apache/maven/project/validation/ModelValidationResult.java rename {maven-core => maven-compat}/src/main/java/org/apache/maven/project/validation/ModelValidator.java (64%) rename {maven-core/src/main/java/org/apache/maven/project => maven-model-builder/src/main/java/org/apache/maven/model}/validation/DefaultModelValidator.java (92%) rename {maven-core/src/main/java/org/apache/maven/project => maven-model-builder/src/main/java/org/apache/maven/model}/validation/ModelValidationResult.java (96%) create mode 100644 maven-model-builder/src/main/java/org/apache/maven/model/validation/ModelValidator.java diff --git a/maven-compat/src/main/java/org/apache/maven/project/validation/DefaultModelValidator.java b/maven-compat/src/main/java/org/apache/maven/project/validation/DefaultModelValidator.java new file mode 100644 index 0000000000..571d9f4a8a --- /dev/null +++ b/maven-compat/src/main/java/org/apache/maven/project/validation/DefaultModelValidator.java @@ -0,0 +1,51 @@ +package org.apache.maven.project.validation; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.maven.model.Model; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.component.annotations.Requirement; + +/** + * @author Trygve Laugstøl + * @version $Id$ + */ +@Component(role = ModelValidator.class ) +@Deprecated +public class DefaultModelValidator + implements ModelValidator +{ + + @Requirement + private org.apache.maven.model.validation.ModelValidator modelValidator; + + public ModelValidationResult validate( Model model ) + { + ModelValidationResult result = new ModelValidationResult(); + + for ( String message : modelValidator.validateEffectiveModel( model, false ).getMessages() ) + { + result.addMessage( message ); + } + + return result; + } + +} diff --git a/maven-compat/src/main/java/org/apache/maven/project/validation/ModelValidationResult.java b/maven-compat/src/main/java/org/apache/maven/project/validation/ModelValidationResult.java new file mode 100644 index 0000000000..127387b2da --- /dev/null +++ b/maven-compat/src/main/java/org/apache/maven/project/validation/ModelValidationResult.java @@ -0,0 +1,30 @@ +package org.apache.maven.project.validation; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * @author Trygve Laugstøl + * @version $Id$ + */ +public class ModelValidationResult + extends org.apache.maven.model.validation.ModelValidationResult +{ + +} diff --git a/maven-core/src/main/java/org/apache/maven/project/validation/ModelValidator.java b/maven-compat/src/main/java/org/apache/maven/project/validation/ModelValidator.java similarity index 64% rename from maven-core/src/main/java/org/apache/maven/project/validation/ModelValidator.java rename to maven-compat/src/main/java/org/apache/maven/project/validation/ModelValidator.java index 95aecb3a97..5d13cb1c75 100644 --- a/maven-core/src/main/java/org/apache/maven/project/validation/ModelValidator.java +++ b/maven-compat/src/main/java/org/apache/maven/project/validation/ModelValidator.java @@ -27,6 +27,7 @@ import org.apache.maven.model.Model; * @author Trygve Laugstøl * @version $Id$ */ +@Deprecated public interface ModelValidator { @@ -34,15 +35,4 @@ public interface ModelValidator ModelValidationResult validate( Model model ); - /** - * Checks the specified model for missing or invalid values. - * - * @param model The model to validate, must not be {@code null}. - * @param lenient A flag whether validation should be lenient instead of strict. For building of projects, strict - * validation should be used to ensure proper building. For the mere retrievel of dependencies during - * artifact resolution, lenient validation should be used to account for models of poor quality. - * @return The result of the validation, never {@code null}. - */ - ModelValidationResult validate( Model model, boolean lenient ); - } diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java index bee8d1c51c..63664c2158 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java @@ -50,10 +50,10 @@ import org.apache.maven.model.plugin.PluginConfigurationExpander; import org.apache.maven.model.profile.ProfileActivationException; import org.apache.maven.model.profile.ProfileInjector; import org.apache.maven.model.profile.ProfileSelector; +import org.apache.maven.model.validation.ModelValidationResult; +import org.apache.maven.model.validation.ModelValidator; import org.apache.maven.profiles.ProfileManager; import org.apache.maven.project.artifact.ProjectArtifact; -import org.apache.maven.project.validation.ModelValidationResult; -import org.apache.maven.project.validation.ModelValidator; import org.apache.maven.repository.RepositorySystem; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; @@ -509,7 +509,7 @@ public class DefaultMavenProjectBuilder throws InvalidProjectModelException { // Must validate before artifact construction to make sure dependencies are good - ModelValidationResult validationResult = validator.validate( model, lenient ); + ModelValidationResult validationResult = validator.validateEffectiveModel( model, lenient ); String projectId = safeVersionlessKey( model.getGroupId(), model.getArtifactId() ); @@ -574,8 +574,6 @@ public class DefaultMavenProjectBuilder { return models; } - - // FIXME: Validate the parent coordinate and throw proper exceptions Artifact artifactParent = repositorySystem.createProjectArtifact( parent.getGroupId(), parent.getArtifactId(), parent.getVersion() ); @@ -727,17 +725,23 @@ public class DefaultMavenProjectBuilder private Model readModel( String projectId, File pomFile, boolean strict ) throws ProjectBuildingException { + Model model; + Map options = Collections. singletonMap( ModelReader.IS_STRICT, Boolean.valueOf( strict ) ); try { - return modelReader.read( pomFile, options ); + model = modelReader.read( pomFile, options ); } catch ( IOException e ) { throw new ProjectBuildingException( projectId, "Failed to read POM for " + projectId + " from " + pomFile + ": " + e.getMessage(), pomFile, e ); } + + validator.validateRawModel( model, !strict ); + + return model; } // Super Model Handling diff --git a/maven-core/src/main/java/org/apache/maven/project/InvalidProjectModelException.java b/maven-core/src/main/java/org/apache/maven/project/InvalidProjectModelException.java index f2c59d403c..3c3c2be12f 100644 --- a/maven-core/src/main/java/org/apache/maven/project/InvalidProjectModelException.java +++ b/maven-core/src/main/java/org/apache/maven/project/InvalidProjectModelException.java @@ -22,7 +22,7 @@ package org.apache.maven.project; import java.io.File; import org.apache.maven.artifact.InvalidRepositoryException; -import org.apache.maven.project.validation.ModelValidationResult; +import org.apache.maven.model.validation.ModelValidationResult; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; public class InvalidProjectModelException diff --git a/maven-core/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java b/maven-core/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java index 88b4bff7b8..691160d1af 100644 --- a/maven-core/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java +++ b/maven-core/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java @@ -25,7 +25,7 @@ import java.util.Arrays; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.DefaultArtifactRepository; import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; -import org.apache.maven.project.validation.ModelValidationResult; +import org.apache.maven.model.validation.ModelValidationResult; import org.codehaus.plexus.PlexusTestCase; /** diff --git a/maven-core/src/main/java/org/apache/maven/project/validation/DefaultModelValidator.java b/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java similarity index 92% rename from maven-core/src/main/java/org/apache/maven/project/validation/DefaultModelValidator.java rename to maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java index 5924c1f4a0..367661d312 100644 --- a/maven-core/src/main/java/org/apache/maven/project/validation/DefaultModelValidator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java @@ -1,4 +1,4 @@ -package org.apache.maven.project.validation; +package org.apache.maven.model.validation; /* * Licensed to the Apache Software Foundation (ASF) under one @@ -22,7 +22,6 @@ package org.apache.maven.project.validation; import java.io.File; import java.util.List; -import org.apache.maven.artifact.Artifact; import org.apache.maven.model.Build; import org.apache.maven.model.Dependency; import org.apache.maven.model.DependencyManagement; @@ -46,12 +45,30 @@ public class DefaultModelValidator { private static final String ID_REGEX = "[A-Za-z0-9_\\-.]+"; - public ModelValidationResult validate( Model model ) + public ModelValidationResult validateRawModel( Model model, boolean lenient ) { - return validate( model, false ); + ModelValidationResult result = new ModelValidationResult(); + + Parent parent = model.getParent(); + if ( parent != null ) + { + validateStringNotEmpty( "parent.groupId", result, parent.getGroupId() ); + + validateStringNotEmpty( "parent.artifactId", result, parent.getArtifactId() ); + + validateStringNotEmpty( "parent.version", result, parent.getVersion() ); + + if ( parent.getGroupId().equals( model.getGroupId() ) + && parent.getArtifactId().equals( model.getArtifactId() ) ) + { + result.addMessage( "The parent element cannot have the same ID as the project." ); + } + } + + return result; } - public ModelValidationResult validate( Model model, boolean lenient ) + public ModelValidationResult validateEffectiveModel( Model model, boolean lenient ) { ModelValidationResult result = new ModelValidationResult(); @@ -92,7 +109,7 @@ public class DefaultModelValidator validateStringNotEmpty( "dependencies.dependency.version", result, d.getVersion(), d.getManagementKey() ); - if ( Artifact.SCOPE_SYSTEM.equals( d.getScope() ) ) + if ( "system".equals( d.getScope() ) ) { String systemPath = d.getSystemPath(); @@ -127,7 +144,7 @@ public class DefaultModelValidator validateSubElementStringNotEmpty( d, "dependencyManagement.dependencies.dependency.groupId", result, d.getGroupId() ); - if ( Artifact.SCOPE_SYSTEM.equals( d.getScope() ) ) + if ( "system".equals( d.getScope() ) ) { String systemPath = d.getSystemPath(); @@ -367,4 +384,5 @@ public class DefaultModelValidator return false; } + } diff --git a/maven-core/src/main/java/org/apache/maven/project/validation/ModelValidationResult.java b/maven-model-builder/src/main/java/org/apache/maven/model/validation/ModelValidationResult.java similarity index 96% rename from maven-core/src/main/java/org/apache/maven/project/validation/ModelValidationResult.java rename to maven-model-builder/src/main/java/org/apache/maven/model/validation/ModelValidationResult.java index e0626e40c4..1dd677f1c7 100644 --- a/maven-core/src/main/java/org/apache/maven/project/validation/ModelValidationResult.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/validation/ModelValidationResult.java @@ -1,4 +1,4 @@ -package org.apache.maven.project.validation; +package org.apache.maven.model.validation; /* * Licensed to the Apache Software Foundation (ASF) under one @@ -47,7 +47,7 @@ public class ModelValidationResult public String getMessage( int i ) { - return messages.get( i ).toString(); + return messages.get( i ); } public List getMessages() diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/validation/ModelValidator.java b/maven-model-builder/src/main/java/org/apache/maven/model/validation/ModelValidator.java new file mode 100644 index 0000000000..73d807d592 --- /dev/null +++ b/maven-model-builder/src/main/java/org/apache/maven/model/validation/ModelValidator.java @@ -0,0 +1,57 @@ +package org.apache.maven.model.validation; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.maven.model.Model; + +/** + * Checks the model for missing or invalid values. + * + * @author Trygve Laugstøl + * @version $Id$ + */ +public interface ModelValidator +{ + + /** + * Checks the specified (raw) model for missing or invalid values. The raw model is directly created from the POM + * file and has not been subjected to inheritance, interpolation or profile/default injection. + * + * @param model The model to validate, must not be {@code null}. + * @param lenient A flag whether validation should be lenient instead of strict. For building of projects, strict + * validation should be used to ensure proper building. For the mere retrievel of dependencies during + * artifact resolution, lenient validation should be used to account for models of poor quality. + * @return The result of the validation, never {@code null}. + */ + ModelValidationResult validateRawModel( Model model, boolean lenient ); + + /** + * Checks the specified (effective) model for missing or invalid values. The effective model is fully assembled and + * has undergone inheritance, interpolation and other model operations. + * + * @param model The model to validate, must not be {@code null}. + * @param lenient A flag whether validation should be lenient instead of strict. For building of projects, strict + * validation should be used to ensure proper building. For the mere retrievel of dependencies during + * artifact resolution, lenient validation should be used to account for models of poor quality. + * @return The result of the validation, never {@code null}. + */ + ModelValidationResult validateEffectiveModel( Model model, boolean lenient ); + +}