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
This commit is contained in:
Benjamin Bentmann 2009-05-27 19:59:17 +00:00
parent 90db120ca1
commit 41e54938d5
9 changed files with 178 additions and 28 deletions

View File

@ -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 <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
* @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;
}
}

View File

@ -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 <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
* @version $Id$
*/
public class ModelValidationResult
extends org.apache.maven.model.validation.ModelValidationResult
{
}

View File

@ -27,6 +27,7 @@ import org.apache.maven.model.Model;
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a> * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
* @version $Id$ * @version $Id$
*/ */
@Deprecated
public interface ModelValidator public interface ModelValidator
{ {
@ -34,15 +35,4 @@ public interface ModelValidator
ModelValidationResult validate( Model model ); 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 );
} }

View File

@ -50,10 +50,10 @@ import org.apache.maven.model.plugin.PluginConfigurationExpander;
import org.apache.maven.model.profile.ProfileActivationException; import org.apache.maven.model.profile.ProfileActivationException;
import org.apache.maven.model.profile.ProfileInjector; import org.apache.maven.model.profile.ProfileInjector;
import org.apache.maven.model.profile.ProfileSelector; 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.profiles.ProfileManager;
import org.apache.maven.project.artifact.ProjectArtifact; 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.apache.maven.repository.RepositorySystem;
import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement; import org.codehaus.plexus.component.annotations.Requirement;
@ -509,7 +509,7 @@ public class DefaultMavenProjectBuilder
throws InvalidProjectModelException throws InvalidProjectModelException
{ {
// Must validate before artifact construction to make sure dependencies are good // 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() ); String projectId = safeVersionlessKey( model.getGroupId(), model.getArtifactId() );
@ -574,8 +574,6 @@ public class DefaultMavenProjectBuilder
{ {
return models; return models;
} }
// FIXME: Validate the parent coordinate and throw proper exceptions
Artifact artifactParent = Artifact artifactParent =
repositorySystem.createProjectArtifact( parent.getGroupId(), parent.getArtifactId(), parent.getVersion() ); repositorySystem.createProjectArtifact( parent.getGroupId(), parent.getArtifactId(), parent.getVersion() );
@ -727,17 +725,23 @@ public class DefaultMavenProjectBuilder
private Model readModel( String projectId, File pomFile, boolean strict ) private Model readModel( String projectId, File pomFile, boolean strict )
throws ProjectBuildingException throws ProjectBuildingException
{ {
Model model;
Map<String, Object> options = Map<String, Object> options =
Collections.<String, Object> singletonMap( ModelReader.IS_STRICT, Boolean.valueOf( strict ) ); Collections.<String, Object> singletonMap( ModelReader.IS_STRICT, Boolean.valueOf( strict ) );
try try
{ {
return modelReader.read( pomFile, options ); model = modelReader.read( pomFile, options );
} }
catch ( IOException e ) catch ( IOException e )
{ {
throw new ProjectBuildingException( projectId, "Failed to read POM for " + projectId + " from " + pomFile throw new ProjectBuildingException( projectId, "Failed to read POM for " + projectId + " from " + pomFile
+ ": " + e.getMessage(), pomFile, e ); + ": " + e.getMessage(), pomFile, e );
} }
validator.validateRawModel( model, !strict );
return model;
} }
// Super Model Handling // Super Model Handling

View File

@ -22,7 +22,7 @@ package org.apache.maven.project;
import java.io.File; import java.io.File;
import org.apache.maven.artifact.InvalidRepositoryException; 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; import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
public class InvalidProjectModelException public class InvalidProjectModelException

View File

@ -25,7 +25,7 @@ import java.util.Arrays;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.DefaultArtifactRepository; import org.apache.maven.artifact.repository.DefaultArtifactRepository;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; 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; import org.codehaus.plexus.PlexusTestCase;
/** /**

View File

@ -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 * 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.io.File;
import java.util.List; import java.util.List;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Build; import org.apache.maven.model.Build;
import org.apache.maven.model.Dependency; import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement; import org.apache.maven.model.DependencyManagement;
@ -46,12 +45,30 @@ public class DefaultModelValidator
{ {
private static final String ID_REGEX = "[A-Za-z0-9_\\-.]+"; 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(); ModelValidationResult result = new ModelValidationResult();
@ -92,7 +109,7 @@ public class DefaultModelValidator
validateStringNotEmpty( "dependencies.dependency.version", result, d.getVersion(), validateStringNotEmpty( "dependencies.dependency.version", result, d.getVersion(),
d.getManagementKey() ); d.getManagementKey() );
if ( Artifact.SCOPE_SYSTEM.equals( d.getScope() ) ) if ( "system".equals( d.getScope() ) )
{ {
String systemPath = d.getSystemPath(); String systemPath = d.getSystemPath();
@ -127,7 +144,7 @@ public class DefaultModelValidator
validateSubElementStringNotEmpty( d, "dependencyManagement.dependencies.dependency.groupId", result, validateSubElementStringNotEmpty( d, "dependencyManagement.dependencies.dependency.groupId", result,
d.getGroupId() ); d.getGroupId() );
if ( Artifact.SCOPE_SYSTEM.equals( d.getScope() ) ) if ( "system".equals( d.getScope() ) )
{ {
String systemPath = d.getSystemPath(); String systemPath = d.getSystemPath();
@ -367,4 +384,5 @@ public class DefaultModelValidator
return false; return false;
} }
} }

View File

@ -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 * Licensed to the Apache Software Foundation (ASF) under one
@ -47,7 +47,7 @@ public class ModelValidationResult
public String getMessage( int i ) public String getMessage( int i )
{ {
return messages.get( i ).toString(); return messages.get( i );
} }
public List<String> getMessages() public List<String> getMessages()

View File

@ -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 <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
* @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 );
}