org.apache.maven.project.validation.DefaultModelValidator Maven / Gradle / Ivy
Go to download
This library is used to not only read Maven project object model files, but to assemble inheritence
and to retrieve remote models as required.
package org.apache.maven.project.validation;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed 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.artifact.Artifact;
import org.apache.maven.model.Build;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.ReportPlugin;
import org.apache.maven.model.Reporting;
import org.codehaus.plexus.util.StringUtils;
import java.util.Iterator;
import java.util.List;
/**
* @author Trygve Laugstøl
* @version $Id: DefaultModelValidator.java 264960 2005-08-31 07:39:01Z jdcasey $
*/
public class DefaultModelValidator
implements ModelValidator
{
///////////////////////////////////////////////////////////////////////////
// ModelValidator Implementation
public ModelValidationResult validate( Model model )
{
ModelValidationResult result = new ModelValidationResult();
validateStringNotEmpty( "modelVersion", result, model.getModelVersion() );
validateStringNotEmpty( "groupId", result, model.getGroupId() );
validateStringNotEmpty( "artifactId", result, model.getArtifactId() );
validateStringNotEmpty( "packaging", result, model.getPackaging() );
validateStringNotEmpty( "version", result, model.getVersion() );
for ( Iterator it = model.getDependencies().iterator(); it.hasNext(); )
{
Dependency d = (Dependency) it.next();
validateSubElementStringNotEmpty( d, "dependencies.dependency.artifactId", result, d.getArtifactId() );
validateSubElementStringNotEmpty( d, "dependencies.dependency.groupId", result, d.getGroupId() );
validateSubElementStringNotEmpty( d, "dependencies.dependency.type", result, d.getType() );
validateSubElementStringNotEmpty( d, "dependencies.dependency.version", result, d.getVersion() );
if ( Artifact.SCOPE_SYSTEM.equals( d.getScope() ) && StringUtils.isEmpty( d.getSystemPath() ) )
{
result.addMessage( "For dependency " + d + ": system-scoped dependency must specify systemPath." );
}
else if ( !Artifact.SCOPE_SYSTEM.equals( d.getScope() ) && StringUtils.isNotEmpty( d.getSystemPath() ) )
{
result.addMessage( "For dependency " + d + ": only dependency with system scope can specify systemPath." );
}
}
DependencyManagement mgmt = model.getDependencyManagement();
if ( mgmt != null )
{
for ( Iterator it = mgmt.getDependencies().iterator(); it.hasNext(); )
{
Dependency d = (Dependency) it.next();
validateSubElementStringNotEmpty( d, "dependencyManagement.dependencies.dependency.artifactId", result,
d.getArtifactId() );
validateSubElementStringNotEmpty( d, "dependencyManagement.dependencies.dependency.groupId", result,
d.getGroupId() );
if ( Artifact.SCOPE_SYSTEM.equals( d.getScope() ) && StringUtils.isEmpty( d.getSystemPath() ) )
{
result.addMessage( "For managed dependency " + d + ": system-scoped dependency must specify systemPath." );
}
else if ( !Artifact.SCOPE_SYSTEM.equals( d.getScope() ) && StringUtils.isNotEmpty( d.getSystemPath() ) )
{
result.addMessage( "For managed dependency " + d + ": only dependency with system scope can specify systemPath." );
}
}
}
Build build = model.getBuild();
if ( build != null )
{
for ( Iterator it = build.getPlugins().iterator(); it.hasNext(); )
{
Plugin p = (Plugin) it.next();
validateStringNotEmpty( "build.plugins.plugin.artifactId", result, p.getArtifactId() );
validateStringNotEmpty( "build.plugins.plugin.groupId", result, p.getGroupId() );
}
}
Reporting reporting = model.getReporting();
if ( reporting != null )
{
for ( Iterator it = reporting.getPlugins().iterator(); it.hasNext(); )
{
ReportPlugin p = (ReportPlugin) it.next();
validateStringNotEmpty( "reporting.plugins.plugin.artifactId", result, p.getArtifactId() );
validateStringNotEmpty( "reporting.plugins.plugin.groupId", result, p.getGroupId() );
}
}
forcePluginExecutionIdCollision( model, result );
return result;
}
private void forcePluginExecutionIdCollision( Model model, ModelValidationResult result )
{
Build build = model.getBuild();
if ( build != null )
{
List plugins = build.getPlugins();
if ( plugins != null )
{
for ( Iterator it = plugins.iterator(); it.hasNext(); )
{
Plugin plugin = (Plugin) it.next();
// this will force an IllegalStateException, even if we don't have to do inheritance assembly.
try
{
plugin.getExecutionsAsMap();
}
catch ( IllegalStateException collisionException )
{
result.addMessage( collisionException.getMessage() );
}
}
}
}
}
///////////////////////////////////////////////////////////////////////////
// Field validator
/**
* Asserts:
*
*
* string.length != null
* string.length > 0
*
*/
private boolean validateStringNotEmpty( String fieldName, ModelValidationResult result, String string )
{
if ( !validateNotNull( fieldName, result, string ) )
{
return false;
}
if ( string.length() > 0 )
{
return true;
}
result.addMessage( "'" + fieldName + "' is missing." );
return false;
}
/**
* Asserts:
*
*
* string.length != null
* string.length > 0
*
*/
private boolean validateSubElementStringNotEmpty( Object subElementInstance, String fieldName, ModelValidationResult result, String string )
{
if ( !validateSubElementNotNull( subElementInstance, fieldName, result, string ) )
{
return false;
}
if ( string.length() > 0 )
{
return true;
}
result.addMessage( "In " + subElementInstance + ":\n\n -> '" + fieldName + "' is missing." );
return false;
}
/**
* Asserts:
*
*
* string != null
*
*/
private boolean validateNotNull( String fieldName, ModelValidationResult result, Object object )
{
if ( object != null )
{
return true;
}
result.addMessage( "'" + fieldName + "' is missing." );
return false;
}
/**
* Asserts:
*
*
* string != null
*
*/
private boolean validateSubElementNotNull( Object subElementInstance, String fieldName, ModelValidationResult result, Object object )
{
if ( object != null )
{
return true;
}
result.addMessage( "In " + subElementInstance + ":\n\n -> '" + fieldName + "' is missing." );
return false;
}
}