PR: MNG-518

fix up validation

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@220318 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-07-22 13:42:55 +00:00
parent 64399eb8e3
commit 36d0a854e1
12 changed files with 239 additions and 10 deletions

View File

@ -438,10 +438,7 @@ public class DefaultMavenProjectBuilder
project.setParentArtifact( parentArtifact );
}
project.setRemoteArtifactRepositories( remoteRepositories );
project.setDependencyArtifacts( createArtifacts( project.getDependencies() ) );
project.setPluginArtifacts( createPluginArtifacts( project.getBuildPlugins() ) );
// Must validate before artifact construction to make sure dependencies are good
ModelValidationResult validationResult = validator.validate( model );
if ( validationResult.getMessageCount() > 0 )
@ -450,6 +447,10 @@ public class DefaultMavenProjectBuilder
"\'.\n\n Reason(s):\n" + validationResult.render( " " ) );
}
project.setRemoteArtifactRepositories( remoteRepositories );
project.setDependencyArtifacts( createArtifacts( project.getDependencies() ) );
project.setPluginArtifacts( createPluginArtifacts( project.getBuildPlugins() ) );
return project;
}

View File

@ -145,7 +145,6 @@ public class MavenMetadataSource
{
Dependency d = (Dependency) i.next();
// TODO: validate
VersionRange versionRange = VersionRange.createFromVersionSpec( d.getVersion() );
Artifact artifact = artifactFactory.createDependencyArtifact( d.getGroupId(), d.getArtifactId(),
versionRange, d.getType(), d.getScope(),

View File

@ -18,8 +18,11 @@ package org.apache.maven.project.validation;
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 java.util.Iterator;
import java.util.List;
@ -60,7 +63,51 @@ public class DefaultModelValidator
validateStringNotEmpty( "dependencies.dependency.version", result, d.getVersion() );
}
DependencyManagement mgmt = model.getDependencyManagement();
if ( mgmt != null )
{
for ( Iterator it = mgmt.getDependencies().iterator(); it.hasNext(); )
{
Dependency d = (Dependency) it.next();
validateStringNotEmpty( "dependencyManagement.dependencies.dependency.artifactId", result,
d.getArtifactId() );
validateStringNotEmpty( "dependencyManagement.dependencies.dependency.groupId", result,
d.getGroupId() );
validateStringNotEmpty( "dependencyManagement.dependencies.dependency.version", result,
d.getVersion() );
}
}
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;
@ -69,17 +116,17 @@ public class DefaultModelValidator
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
{
@ -93,7 +140,7 @@ public class DefaultModelValidator
}
}
}
///////////////////////////////////////////////////////////////////////////
// Field validator

View File

@ -85,6 +85,66 @@ public class DefaultModelValidatorTest
assertEquals( "'version' is missing.", result.getMessage( 0 ) );
}
public void testMissingDependencyArtifactId()
throws Exception
{
ModelValidationResult result = validate( "missing-dependency-artifactId-pom.xml" );
assertEquals( 1, result.getMessageCount() );
assertEquals( "'dependencies.dependency.artifactId' is missing.", result.getMessage( 0 ) );
}
public void testMissingDependencyGroupId()
throws Exception
{
ModelValidationResult result = validate( "missing-dependency-groupId-pom.xml" );
assertEquals( 1, result.getMessageCount() );
assertEquals( "'dependencies.dependency.groupId' is missing.", result.getMessage( 0 ) );
}
public void testMissingDependencyVersion()
throws Exception
{
ModelValidationResult result = validate( "missing-dependency-version-pom.xml" );
assertEquals( 1, result.getMessageCount() );
assertEquals( "'dependencies.dependency.version' is missing.", result.getMessage( 0 ) );
}
public void testMissingDependencyManagementArtifactId()
throws Exception
{
ModelValidationResult result = validate( "missing-dependency-mgmt-artifactId-pom.xml" );
assertEquals( 1, result.getMessageCount() );
assertEquals( "'dependencyManagement.dependencies.dependency.artifactId' is missing.", result.getMessage( 0 ) );
}
public void testMissingDependencyManagementGroupId()
throws Exception
{
ModelValidationResult result = validate( "missing-dependency-mgmt-groupId-pom.xml" );
assertEquals( 1, result.getMessageCount() );
assertEquals( "'dependencyManagement.dependencies.dependency.groupId' is missing.", result.getMessage( 0 ) );
}
public void testMissingDependencyManagementVersion()
throws Exception
{
ModelValidationResult result = validate( "missing-dependency-mgmt-version-pom.xml" );
assertEquals( 1, result.getMessageCount() );
assertEquals( "'dependencyManagement.dependencies.dependency.version' is missing.", result.getMessage( 0 ) );
}
public void testMissingAll()
throws Exception
{
@ -101,6 +161,16 @@ public class DefaultModelValidatorTest
// type is inherited from the super pom
}
public void testMissingPluginArtifactId()
throws Exception
{
ModelValidationResult result = validate( "missing-plugin-artifactId-pom.xml" );
assertEquals( 1, result.getMessageCount() );
assertEquals( "'build.plugins.plugin.artifactId' is missing.", result.getMessage( 0 ) );
}
private ModelValidationResult validate( String testName )
throws Exception
{

View File

@ -0,0 +1,13 @@
<project>
<modelVersion>4.0.0</modelVersion>
<artifactId>foo</artifactId>
<groupId>foo</groupId>
<version>99.44</version>
<packaging>bleh</packaging>
<dependencies>
<dependency>
<groupId>groupId</groupId>
<version>1.0</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,13 @@
<project>
<modelVersion>4.0.0</modelVersion>
<artifactId>foo</artifactId>
<groupId>foo</groupId>
<version>99.44</version>
<packaging>bleh</packaging>
<dependencies>
<dependency>
<artifactId>artifactId</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,15 @@
<project>
<modelVersion>4.0.0</modelVersion>
<artifactId>foo</artifactId>
<groupId>foo</groupId>
<version>99.44</version>
<packaging>bleh</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>groupId</groupId>
<version>version</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

View File

@ -0,0 +1,15 @@
<project>
<modelVersion>4.0.0</modelVersion>
<artifactId>foo</artifactId>
<groupId>foo</groupId>
<version>99.44</version>
<packaging>bleh</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<artifactId>artifactId</artifactId>
<version>version</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

View File

@ -0,0 +1,15 @@
<project>
<modelVersion>4.0.0</modelVersion>
<artifactId>foo</artifactId>
<groupId>foo</groupId>
<version>99.44</version>
<packaging>bleh</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<artifactId>artifactId</artifactId>
<groupId>groupId</groupId>
</dependency>
</dependencies>
</dependencyManagement>
</project>

View File

@ -0,0 +1,13 @@
<project>
<modelVersion>4.0.0</modelVersion>
<artifactId>foo</artifactId>
<groupId>foo</groupId>
<version>99.44</version>
<packaging>bleh</packaging>
<dependencies>
<dependency>
<artifactId>artifactId</artifactId>
<groupId>groupId</groupId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,14 @@
<project>
<modelVersion>4.0.0</modelVersion>
<artifactId>foo</artifactId>
<groupId>foo</groupId>
<version>99.44</version>
<packaging>bleh</packaging>
<build>
<plugins>
<plugin>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,14 @@
<project>
<modelVersion>4.0.0</modelVersion>
<artifactId>foo</artifactId>
<groupId>foo</groupId>
<version>99.44</version>
<packaging>bleh</packaging>
<reporting>
<plugins>
<plugin>
</plugin>
</plugins>
</reporting>
</project>