[MNG-2739] Adding validation logic to DefaultMavenTools and ProjectUtils, to make sure repositories have id and url before constructing an ArtifactRepository instance...this code is used ahead of normal model validation, so it needs to be included here. Also, introducing a new exception to handle the errors when these elements are missing.

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@659680 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
John Dennis Casey 2008-05-23 21:52:24 +00:00
parent ca2bb6a32f
commit e26974da98
9 changed files with 68 additions and 26 deletions

View File

@ -2574,6 +2574,7 @@
<field> <field>
<name>id</name> <name>id</name>
<version>4.0.0</version> <version>4.0.0</version>
<required>true</required>
<description> <description>
<![CDATA[ <![CDATA[
A unique identifier for a repository. This is used to match the repository A unique identifier for a repository. This is used to match the repository
@ -2595,6 +2596,7 @@
<field> <field>
<name>url</name> <name>url</name>
<version>4.0.0</version> <version>4.0.0</version>
<required>true</required>
<description> <description>
<![CDATA[ <![CDATA[
The url of the repository, in the form <code>protocol://hostname/path</code>. The url of the repository, in the form <code>protocol://hostname/path</code>.

View File

@ -6,12 +6,13 @@ import org.apache.maven.project.artifact.MavenMetadataSource;
import org.apache.maven.project.artifact.InvalidDependencyVersionException; import org.apache.maven.project.artifact.InvalidDependencyVersionException;
import org.apache.maven.project.InvalidProjectVersionException; import org.apache.maven.project.InvalidProjectVersionException;
import org.apache.maven.project.interpolation.ModelInterpolationException; import org.apache.maven.project.interpolation.ModelInterpolationException;
import org.apache.maven.artifact.UnknownRepositoryLayoutException; import org.apache.maven.artifact.InvalidRepositoryException;
import org.apache.maven.model.Model; import org.apache.maven.model.Model;
import org.apache.maven.model.Repository; import org.apache.maven.model.Repository;
import org.apache.maven.model.DeploymentRepository; import org.apache.maven.model.DeploymentRepository;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.MavenTools; import org.apache.maven.MavenTools;
import org.apache.maven.DefaultMavenTools;
import org.apache.maven.project.build.model.DefaultModelLineageBuilder; import org.apache.maven.project.build.model.DefaultModelLineageBuilder;
import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProject;
import org.apache.maven.project.DefaultMavenProjectBuilder; import org.apache.maven.project.DefaultMavenProjectBuilder;
@ -57,7 +58,8 @@ public privileged aspect PBEDerivativeReporterAspect
// <-- UnknownRepositoryLayoutException // <-- UnknownRepositoryLayoutException
// <---------- ProjectBuildingException // <---------- ProjectBuildingException
// ========================================================================= // =========================================================================
after( MavenProject project, File pomFile, DeploymentRepository repo ) throwing( UnknownRepositoryLayoutException cause ):
after( MavenProject project, File pomFile, DeploymentRepository repo ) throwing( InvalidRepositoryException cause ):
mavenTools_buildDeploymentArtifactRepository( repo ) && mavenTools_buildDeploymentArtifactRepository( repo ) &&
cflow( pbldr_processProjectLogic( project, pomFile ) ) cflow( pbldr_processProjectLogic( project, pomFile ) )
&& within_DefaultMavenProjectBuilder() && within_DefaultMavenProjectBuilder()
@ -83,7 +85,7 @@ public privileged aspect PBEDerivativeReporterAspect
// <------ UnknownRepositoryLayoutException // <------ UnknownRepositoryLayoutException
// <---------- ProjectBuildingException // <---------- ProjectBuildingException
// ========================================================================= // =========================================================================
after( MavenProject project, File pomFile, Repository repo ) throwing( UnknownRepositoryLayoutException cause ): after( MavenProject project, File pomFile, Repository repo ) throwing( InvalidRepositoryException cause ):
mavenTools_buildArtifactRepository( repo ) mavenTools_buildArtifactRepository( repo )
&& cflow( pbldr_processProjectLogic( project, pomFile ) ) && cflow( pbldr_processProjectLogic( project, pomFile ) )
{ {
@ -106,7 +108,7 @@ public privileged aspect PBEDerivativeReporterAspect
// <------ UnknownRepositoryLayoutException // <------ UnknownRepositoryLayoutException
// <------ ProjectBuildingException // <------ ProjectBuildingException
// ========================================================================= // =========================================================================
after( Model model, File pomFile, Repository repo ) throwing( UnknownRepositoryLayoutException cause ): after( Model model, File pomFile, Repository repo ) throwing( InvalidRepositoryException cause ):
mavenTools_buildArtifactRepository( repo ) mavenTools_buildArtifactRepository( repo )
&& cflow( mlbldr_updateRepositorySet( model, pomFile ) ) && cflow( mlbldr_updateRepositorySet( model, pomFile ) )
{ {

View File

@ -19,13 +19,14 @@ package org.apache.maven;
* under the License. * under the License.
*/ */
import org.apache.maven.artifact.UnknownRepositoryLayoutException; import org.apache.maven.artifact.InvalidRepositoryException;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.model.DeploymentRepository; import org.apache.maven.model.DeploymentRepository;
import org.apache.maven.model.Repository; import org.apache.maven.model.Repository;
import org.apache.maven.model.RepositoryPolicy; import org.apache.maven.model.RepositoryPolicy;
import org.apache.maven.project.MissingRepositoryElementException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
@ -43,7 +44,7 @@ public class DefaultMavenTools
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
public List buildArtifactRepositories( List repositories ) public List buildArtifactRepositories( List repositories )
throws UnknownRepositoryLayoutException throws InvalidRepositoryException
{ {
List repos = new ArrayList(); List repos = new ArrayList();
@ -62,7 +63,7 @@ public class DefaultMavenTools
} }
public ArtifactRepository buildDeploymentArtifactRepository( DeploymentRepository repo ) public ArtifactRepository buildDeploymentArtifactRepository( DeploymentRepository repo )
throws UnknownRepositoryLayoutException throws InvalidRepositoryException
{ {
if ( repo != null ) if ( repo != null )
{ {
@ -79,13 +80,23 @@ public class DefaultMavenTools
} }
public ArtifactRepository buildArtifactRepository( Repository repo ) public ArtifactRepository buildArtifactRepository( Repository repo )
throws UnknownRepositoryLayoutException throws InvalidRepositoryException
{ {
if ( repo != null ) if ( repo != null )
{ {
String id = repo.getId(); String id = repo.getId();
String url = repo.getUrl(); String url = repo.getUrl();
if ( id == null || id.trim().length() < 1 )
{
throw new MissingRepositoryElementException( "Repository ID must not be empty (URL is: " + url + ")." );
}
if ( url == null || url.trim().length() < 1 )
{
throw new MissingRepositoryElementException( "Repository URL must not be empty (ID is: " + id + ").", id );
}
ArtifactRepositoryPolicy snapshots = buildArtifactRepositoryPolicy( repo.getSnapshots() ); ArtifactRepositoryPolicy snapshots = buildArtifactRepositoryPolicy( repo.getSnapshots() );
ArtifactRepositoryPolicy releases = buildArtifactRepositoryPolicy( repo.getReleases() ); ArtifactRepositoryPolicy releases = buildArtifactRepositoryPolicy( repo.getReleases() );

View File

@ -19,7 +19,7 @@ package org.apache.maven;
* under the License. * under the License.
*/ */
import org.apache.maven.artifact.UnknownRepositoryLayoutException; import org.apache.maven.artifact.InvalidRepositoryException;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.model.DeploymentRepository; import org.apache.maven.model.DeploymentRepository;
import org.apache.maven.model.Repository; import org.apache.maven.model.Repository;
@ -38,11 +38,11 @@ public interface MavenTools
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
List buildArtifactRepositories( List repositories ) List buildArtifactRepositories( List repositories )
throws UnknownRepositoryLayoutException; throws InvalidRepositoryException;
ArtifactRepository buildDeploymentArtifactRepository( DeploymentRepository repo ) ArtifactRepository buildDeploymentArtifactRepository( DeploymentRepository repo )
throws UnknownRepositoryLayoutException; throws InvalidRepositoryException;
ArtifactRepository buildArtifactRepository( Repository repo ) ArtifactRepository buildArtifactRepository( Repository repo )
throws UnknownRepositoryLayoutException; throws InvalidRepositoryException;
} }

View File

@ -24,7 +24,6 @@ import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactStatus; import org.apache.maven.artifact.ArtifactStatus;
import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.InvalidRepositoryException; import org.apache.maven.artifact.InvalidRepositoryException;
import org.apache.maven.artifact.UnknownRepositoryLayoutException;
import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource; import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
@ -619,7 +618,7 @@ public class DefaultMavenProjectBuilder
normalized.add( item ); normalized.add( item );
normalizationNeeded = true; normalizationNeeded = true;
} }
catch ( UnknownRepositoryLayoutException e ) catch ( InvalidRepositoryException e )
{ {
throw new ProjectBuildingException( projectId, "Error building artifact repository for id: " + repo.getId(), e ); throw new ProjectBuildingException( projectId, "Error building artifact repository for id: " + repo.getId(), e );
} }

View File

@ -0,0 +1,20 @@
package org.apache.maven.project;
import org.apache.maven.artifact.InvalidRepositoryException;
public class MissingRepositoryElementException
extends InvalidRepositoryException
{
public MissingRepositoryElementException( String message,
String repositoryId )
{
super( message, repositoryId );
}
public MissingRepositoryElementException( String message )
{
super( message, "-unknown-" );
}
}

View File

@ -19,7 +19,7 @@ package org.apache.maven.project;
* under the License. * under the License.
*/ */
import org.apache.maven.artifact.UnknownRepositoryLayoutException; import org.apache.maven.artifact.InvalidRepositoryException;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
@ -41,7 +41,7 @@ public final class ProjectUtils
public static List buildArtifactRepositories( List repositories, public static List buildArtifactRepositories( List repositories,
ArtifactRepositoryFactory artifactRepositoryFactory, ArtifactRepositoryFactory artifactRepositoryFactory,
PlexusContainer container ) PlexusContainer container )
throws UnknownRepositoryLayoutException throws InvalidRepositoryException
{ {
List repos = new ArrayList(); List repos = new ArrayList();
@ -64,7 +64,7 @@ public final class ProjectUtils
public static ArtifactRepository buildDeploymentArtifactRepository( DeploymentRepository repo, public static ArtifactRepository buildDeploymentArtifactRepository( DeploymentRepository repo,
ArtifactRepositoryFactory artifactRepositoryFactory, ArtifactRepositoryFactory artifactRepositoryFactory,
PlexusContainer container ) PlexusContainer container )
throws UnknownRepositoryLayoutException throws InvalidRepositoryException
{ {
if ( repo != null ) if ( repo != null )
{ {
@ -83,13 +83,23 @@ public final class ProjectUtils
public static ArtifactRepository buildArtifactRepository( Repository repo, public static ArtifactRepository buildArtifactRepository( Repository repo,
ArtifactRepositoryFactory artifactRepositoryFactory, ArtifactRepositoryFactory artifactRepositoryFactory,
PlexusContainer container ) PlexusContainer container )
throws UnknownRepositoryLayoutException throws InvalidRepositoryException
{ {
if ( repo != null ) if ( repo != null )
{ {
String id = repo.getId(); String id = repo.getId();
String url = repo.getUrl(); String url = repo.getUrl();
if ( id == null || id.trim().length() < 1 )
{
throw new MissingRepositoryElementException( "Repository ID must not be empty (URL is: " + url + ")." );
}
if ( url == null || url.trim().length() < 1 )
{
throw new MissingRepositoryElementException( "Repository URL must not be empty (ID is: " + id + ").", id );
}
ArtifactRepositoryPolicy snapshots = buildArtifactRepositoryPolicy( repo.getSnapshots() ); ArtifactRepositoryPolicy snapshots = buildArtifactRepositoryPolicy( repo.getSnapshots() );
ArtifactRepositoryPolicy releases = buildArtifactRepositoryPolicy( repo.getReleases() ); ArtifactRepositoryPolicy releases = buildArtifactRepositoryPolicy( repo.getReleases() );

View File

@ -1,7 +1,6 @@
package org.apache.maven.project.error; package org.apache.maven.project.error;
import org.apache.maven.artifact.InvalidRepositoryException; import org.apache.maven.artifact.InvalidRepositoryException;
import org.apache.maven.artifact.UnknownRepositoryLayoutException;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException; import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException; import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
@ -343,7 +342,7 @@ public class DefaultProjectErrorReporter
public void reportErrorCreatingArtifactRepository( String projectId, public void reportErrorCreatingArtifactRepository( String projectId,
File pomFile, File pomFile,
Repository repo, Repository repo,
UnknownRepositoryLayoutException cause ) InvalidRepositoryException cause )
{ {
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
@ -352,7 +351,7 @@ public class DefaultProjectErrorReporter
writer.write( NEWLINE ); writer.write( NEWLINE );
writer.write( NEWLINE ); writer.write( NEWLINE );
writer.write( "Repository-Id: " ); writer.write( "Repository-Id: " );
writer.write( ((InvalidRepositoryException)cause).getRepositoryId() ); writer.write( cause.getRepositoryId() );
writer.write( NEWLINE ); writer.write( NEWLINE );
writer.write( NEWLINE ); writer.write( NEWLINE );
writer.write( "Reason: " ); writer.write( "Reason: " );
@ -369,7 +368,7 @@ public class DefaultProjectErrorReporter
public void reportErrorCreatingDeploymentArtifactRepository( MavenProject project, public void reportErrorCreatingDeploymentArtifactRepository( MavenProject project,
File pomFile, File pomFile,
DeploymentRepository repo, DeploymentRepository repo,
UnknownRepositoryLayoutException cause ) InvalidRepositoryException cause )
{ {
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
@ -378,7 +377,7 @@ public class DefaultProjectErrorReporter
writer.write( NEWLINE ); writer.write( NEWLINE );
writer.write( NEWLINE ); writer.write( NEWLINE );
writer.write( "Repository-Id: " ); writer.write( "Repository-Id: " );
writer.write( ((InvalidRepositoryException)cause).getRepositoryId() ); writer.write( cause.getRepositoryId() );
writer.write( NEWLINE ); writer.write( NEWLINE );
writer.write( NEWLINE ); writer.write( NEWLINE );
writer.write( "Reason: " ); writer.write( "Reason: " );

View File

@ -1,7 +1,6 @@
package org.apache.maven.project.error; package org.apache.maven.project.error;
import org.apache.maven.artifact.InvalidRepositoryException; import org.apache.maven.artifact.InvalidRepositoryException;
import org.apache.maven.artifact.UnknownRepositoryLayoutException;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException; import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.model.DeploymentRepository; import org.apache.maven.model.DeploymentRepository;
@ -158,7 +157,7 @@ public interface ProjectErrorReporter
void reportErrorCreatingDeploymentArtifactRepository( MavenProject project, void reportErrorCreatingDeploymentArtifactRepository( MavenProject project,
File pomFile, File pomFile,
DeploymentRepository repo, DeploymentRepository repo,
UnknownRepositoryLayoutException cause ); InvalidRepositoryException cause );
/** /**
* <b>Call Stack:</b> * <b>Call Stack:</b>
@ -179,7 +178,7 @@ public interface ProjectErrorReporter
void reportErrorCreatingArtifactRepository( String projectId, void reportErrorCreatingArtifactRepository( String projectId,
File pomFile, File pomFile,
Repository repo, Repository repo,
UnknownRepositoryLayoutException cause ); InvalidRepositoryException cause );
/** /**
* <b>Call Stack:</b> * <b>Call Stack:</b>