Skip to content

Commit

Permalink
[MNG-1803] Provide line number information when there are errors proc…
Browse files Browse the repository at this point in the history
…essing a pom.xml

o Merged feature branch

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@949708 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
bentmann committed May 31, 2010
1 parent 1ba4a15 commit 6e7b422
Show file tree
Hide file tree
Showing 29 changed files with 439 additions and 131 deletions.
Expand Up @@ -20,6 +20,7 @@
*/

import org.apache.maven.model.Activation;
import org.apache.maven.model.InputLocation;
import org.apache.maven.model.Profile;
import org.apache.maven.model.building.ModelProblem;
import org.apache.maven.model.building.ModelProblemCollector;
Expand Down Expand Up @@ -194,7 +195,7 @@ public List getActiveProfiles()
profileSelector.getActiveProfiles( profilesById.values(), context, new ModelProblemCollector()
{

public void add( Severity severity, String message, Exception cause )
public void add( Severity severity, String message, InputLocation location, Exception cause )
{
if ( !ModelProblem.Severity.WARNING.equals( severity ) )
{
Expand Down
Expand Up @@ -19,6 +19,7 @@
* under the License.
*/

import org.apache.maven.model.InputLocation;
import org.apache.maven.model.Model;
import org.apache.maven.model.building.DefaultModelBuildingRequest;
import org.apache.maven.model.building.ModelBuildingRequest;
Expand Down Expand Up @@ -66,7 +67,7 @@ public SimpleModelProblemCollector( ModelValidationResult result )
this.result = result;
}

public void add( Severity severity, String message, Exception cause )
public void add( Severity severity, String message, InputLocation location, Exception cause )
{
if ( !ModelProblem.Severity.WARNING.equals( severity ) )
{
Expand Down
Expand Up @@ -64,7 +64,7 @@ public void injectLifecycleBindings( Model model, ModelBuildingRequest request,

if ( defaultPlugins == null )
{
problems.add( Severity.ERROR, "Unknown packaging: " + packaging, null );
problems.add( Severity.ERROR, "Unknown packaging: " + packaging, model.getLocation( "packaging" ), null );
}
else if ( !defaultPlugins.isEmpty() )
{
Expand Down
Expand Up @@ -95,7 +95,7 @@ public void buildExtensionsAssembled( ModelBuildingEvent event )
}
catch ( Exception e )
{
event.getProblems().add( Severity.ERROR, "Invalid plugin repository: " + e.getMessage(), e );
event.getProblems().add( Severity.ERROR, "Invalid plugin repository: " + e.getMessage(), null, e );
}
project.setPluginArtifactRepositories( pluginRepositories );

Expand All @@ -111,11 +111,11 @@ public void buildExtensionsAssembled( ModelBuildingEvent event )
}
catch ( PluginResolutionException e )
{
event.getProblems().add( Severity.ERROR, "Unresolveable build extension: " + e.getMessage(), e );
event.getProblems().add( Severity.ERROR, "Unresolveable build extension: " + e.getMessage(), null, e );
}
catch ( PluginVersionResolutionException e )
{
event.getProblems().add( Severity.ERROR, "Unresolveable build extension: " + e.getMessage(), e );
event.getProblems().add( Severity.ERROR, "Unresolveable build extension: " + e.getMessage(), null, e );
}

projectBuildingHelper.selectProjectRealm( project );
Expand All @@ -130,7 +130,7 @@ public void buildExtensionsAssembled( ModelBuildingEvent event )
}
catch ( Exception e )
{
event.getProblems().add( Severity.ERROR, "Invalid artifact repository: " + e.getMessage(), e );
event.getProblems().add( Severity.ERROR, "Invalid artifact repository: " + e.getMessage(), null, e );
}
project.setRemoteArtifactRepositories( remoteRepositories );
}
Expand Down
Expand Up @@ -110,6 +110,7 @@ private ProjectBuildingResult build( File pomFile, ModelSource modelSource, Proj

request.setPomFile( pomFile );
request.setModelSource( modelSource );
request.setLocationTracking( pomFile != null );

ModelBuildingResult result;
try
Expand Down Expand Up @@ -310,6 +311,7 @@ private boolean build( List<ProjectBuildingResult> results, List<InterimResult>

request.setPomFile( pomFile );
request.setTwoPhaseBuilding( true );
request.setLocationTracking( true );
request.setModelCache( modelCache );

DefaultModelBuildingListener listener =
Expand Down
Expand Up @@ -33,6 +33,8 @@
import org.apache.maven.model.Build;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.InputLocation;
import org.apache.maven.model.InputSource;
import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.apache.maven.model.Plugin;
Expand Down Expand Up @@ -321,7 +323,7 @@ else if ( !parentIds.add( currentData.getId() ) )
}
message += currentData.getId();

problems.add( ModelProblem.Severity.FATAL, message, null );
problems.add( ModelProblem.Severity.FATAL, message, null, null );
throw new ModelBuildingException( problems.getRootModel(), problems.getRootModelId(),
problems.getProblems() );
}
Expand Down Expand Up @@ -448,9 +450,11 @@ private Model readModel( ModelSource modelSource, File pomFile, ModelBuildingReq
try
{
boolean strict = request.getValidationLevel() >= ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0;
InputSource source = request.isLocationTracking() ? new InputSource() : null;

Map<String, Object> options = new HashMap<String, Object>();
options.put( ModelProcessor.IS_STRICT, Boolean.valueOf( strict ) );
options.put( ModelProcessor.INPUT_SOURCE, source );
options.put( ModelProcessor.SOURCE, modelSource );

try
Expand Down Expand Up @@ -479,18 +483,24 @@ private Model readModel( ModelSource modelSource, File pomFile, ModelBuildingReq
if ( pomFile != null )
{
problems.add( Severity.ERROR, "Malformed POM " + modelSource.getLocation() + ": " + e.getMessage(),
e );
null, e );
}
else
{
problems.add( Severity.WARNING, "Malformed POM " + modelSource.getLocation() + ": "
+ e.getMessage(), e );
+ e.getMessage(), null, e );
}
}

if ( source != null )
{
source.setModelId( ModelProblemUtils.toId( model ) );
source.setLocation( pomFile != null ? pomFile.getAbsolutePath() : null );
}
}
catch ( ModelParseException e )
{
problems.add( Severity.FATAL, "Non-parseable POM " + modelSource.getLocation() + ": " + e.getMessage(), e );
problems.add( Severity.FATAL, "Non-parseable POM " + modelSource.getLocation() + ": " + e.getMessage(), null, e );
throw new ModelBuildingException( problems.getRootModel(), problems.getRootModelId(),
problems.getProblems() );
}
Expand All @@ -509,7 +519,7 @@ private Model readModel( ModelSource modelSource, File pomFile, ModelBuildingReq
msg = e.getClass().getSimpleName();
}
}
problems.add( Severity.FATAL, "Non-readable POM " + modelSource.getLocation() + ": " + msg, e );
problems.add( Severity.FATAL, "Non-readable POM " + modelSource.getLocation() + ": " + msg, null, e );
throw new ModelBuildingException( problems.getRootModel(), problems.getRootModelId(),
problems.getProblems() );
}
Expand Down Expand Up @@ -560,7 +570,8 @@ private void configureResolver( ModelResolver modelResolver, Model model, Defaul
}
catch ( InvalidRepositoryException e )
{
problems.add( Severity.ERROR, "Invalid repository " + repository.getId() + ": " + e.getMessage(), e );
problems.add( Severity.ERROR, "Invalid repository " + repository.getId() + ": " + e.getMessage(),
repository.getLocation( "" ), e );
}
}
}
Expand All @@ -573,10 +584,11 @@ private void checkPluginVersions( List<ModelData> lineage, ModelBuildingRequest
return;
}

Map<String, Plugin> plugins = new HashMap<String, Plugin>();
Map<String, String> versions = new HashMap<String, String>();
Map<String, String> managedVersions = new HashMap<String, String>();

for ( int i = 0, n = lineage.size() - 1; i < n; i++ )
for ( int i = lineage.size() - 1; i >= 0; i-- )
{
Model model = lineage.get( i ).getModel();
Build build = model.getBuild();
Expand All @@ -588,6 +600,7 @@ private void checkPluginVersions( List<ModelData> lineage, ModelBuildingRequest
if ( versions.get( key ) == null )
{
versions.put( key, plugin.getVersion() );
plugins.put( key, plugin );
}
}
PluginManagement mngt = build.getPluginManagement();
Expand All @@ -609,7 +622,9 @@ private void checkPluginVersions( List<ModelData> lineage, ModelBuildingRequest
{
if ( versions.get( key ) == null && managedVersions.get( key ) == null )
{
problems.add( Severity.WARNING, "'build.plugins.plugin.version' for " + key + " is missing.", null );
InputLocation location = plugins.get( key ).getLocation( "" );
problems.add( Severity.WARNING, "'build.plugins.plugin.version' for " + key + " is missing.", location,
null );
}
}
}
Expand Down Expand Up @@ -685,7 +700,7 @@ private ModelData readParent( Model childModel, ModelBuildingRequest request, De
{
problems.add( Severity.ERROR, "Invalid packaging for parent POM "
+ ModelProblemUtils.toSourceHint( parentModel ) + ", must be \"pom\" but is \""
+ parentModel.getPackaging() + "\"", null );
+ parentModel.getPackaging() + "\"", parentModel.getLocation( "packaging" ), null );
}
}
else
Expand Down Expand Up @@ -730,7 +745,7 @@ private ModelData readParentLocally( Model childModel, ModelBuildingRequest requ
problems.add( Severity.WARNING, "'parent.relativePath' of POM "
+ ModelProblemUtils.toSourceHint( childModel ) + " points at " + groupId + ":" + artifactId
+ " instead of " + parent.getGroupId() + ":" + parent.getArtifactId()
+ ", please verify your project structure", null );
+ ", please verify your project structure", childModel.getLocation( "parent" ), null );
return null;
}
if ( version == null || !version.equals( parent.getVersion() ) )
Expand Down Expand Up @@ -801,7 +816,7 @@ private ModelData readParentExternally( Model childModel, ModelBuildingRequest r
{
problems.add( Severity.FATAL, "Non-resolvable parent POM "
+ ModelProblemUtils.toId( groupId, artifactId, version ) + " for "
+ ModelProblemUtils.toId( childModel ) + ": " + e.getMessage(), e );
+ ModelProblemUtils.toId( childModel ) + ": " + e.getMessage(), childModel.getLocation( "parent" ), e );
throw new ModelBuildingException( problems.getRootModel(), problems.getRootModelId(),
problems.getProblems() );
}
Expand Down Expand Up @@ -877,7 +892,7 @@ private void importDependencyManagement( Model model, ModelBuildingRequest reque
message += modelId + " -> ";
}
message += imported;
problems.add( Severity.ERROR, message, null );
problems.add( Severity.ERROR, message, null, null );

continue;
}
Expand All @@ -902,7 +917,8 @@ private void importDependencyManagement( Model model, ModelBuildingRequest reque
catch ( UnresolvableModelException e )
{
problems.add( Severity.ERROR, "Non-resolvable import POM "
+ ModelProblemUtils.toId( groupId, artifactId, version ) + ": " + e.getMessage(), e );
+ ModelProblemUtils.toId( groupId, artifactId, version ) + ": " + e.getMessage(),
dependency.getLocation( "" ), e );
continue;
}

Expand Down
Expand Up @@ -47,6 +47,8 @@ public class DefaultModelBuildingRequest

private boolean twoPhaseBuilding;

private boolean locationTracking;

private List<Profile> profiles;

private List<String> activeProfileIds;
Expand Down Expand Up @@ -155,6 +157,18 @@ public DefaultModelBuildingRequest setTwoPhaseBuilding( boolean twoPhaseBuilding
return this;
}

public boolean isLocationTracking()
{
return locationTracking;
}

public DefaultModelBuildingRequest setLocationTracking( boolean locationTracking )
{
this.locationTracking = locationTracking;

return this;
}

public List<Profile> getProfiles()
{
if ( profiles == null )
Expand Down
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.Set;

import org.apache.maven.model.InputLocation;
import org.apache.maven.model.Model;
import org.apache.maven.model.building.ModelProblem.Severity;
import org.apache.maven.model.io.ModelParseException;
Expand Down Expand Up @@ -139,25 +140,38 @@ public void addAll( List<ModelProblem> problems )
}
}

public void add( Severity severity, String message, Exception cause )
public void add( Severity severity, String message, InputLocation location, Exception cause )
{
int line = -1;
int column = -1;
String source = null;
String modelId = null;

if ( cause instanceof ModelParseException )
if ( location != null )
{
line = location.getLineNumber();
column = location.getColumnNumber();
if ( location.getSource() != null )
{
modelId = location.getSource().getModelId();
source = location.getSource().getLocation();
}
}

if ( modelId == null )
{
modelId = getModelId();
source = getSource();
}

if ( line <= 0 && column <= 0 && cause instanceof ModelParseException )
{
ModelParseException e = (ModelParseException) cause;
line = e.getLineNumber();
column = e.getColumnNumber();
}

add( severity, message, line, column, cause );
}

private void add( ModelProblem.Severity severity, String message, int line, int column, Exception cause )
{
ModelProblem problem =
new DefaultModelProblem( message, severity, getSource(), line, column, getModelId(), cause );
ModelProblem problem = new DefaultModelProblem( message, severity, source, line, column, modelId, cause );

add( problem );
}
Expand Down
Expand Up @@ -104,6 +104,18 @@ public FilterModelBuildingRequest setTwoPhaseBuilding( boolean twoPhaseBuilding
return this;
}

public boolean isLocationTracking()
{
return request.isLocationTracking();
}

public FilterModelBuildingRequest setLocationTracking( boolean locationTracking )
{
request.setLocationTracking( locationTracking );

return this;
}

public List<Profile> getProfiles()
{
return request.getProfiles();
Expand Down
Expand Up @@ -155,6 +155,22 @@ public interface ModelBuildingRequest
*/
ModelBuildingRequest setTwoPhaseBuilding( boolean twoPhaseBuilding );

/**
* Indicates whether the model should track the line/column number of the model source from which it was parsed.
*
* @return {@code true} if location tracking is enabled, {@code false} otherwise.
*/
boolean isLocationTracking();

/**
* Enables/disables the tracking of line/column numbers for the model source being parsed. By default, input
* locations are not tracked.
*
* @param locationTracking {@code true} to enable location tracking, {@code false} to disable it.
* @return This request, never {@code null}.
*/
ModelBuildingRequest setLocationTracking( boolean locationTracking );

/**
* Gets the external profiles that should be considered for model building.
*
Expand Down
Expand Up @@ -19,6 +19,8 @@
* under the License.
*/

import org.apache.maven.model.InputLocation;

/**
* Collects problems that are encountered during model building. The primary purpose of this component is to account for
* the fact that the problem reporter has/should not have information about the calling context and hence cannot provide
Expand All @@ -36,8 +38,9 @@ public interface ModelProblemCollector
*
* @param severity The severity of the problem, must not be {@code null}.
* @param message The detail message of the problem, may be {@code null}.
* @param location The location of the problem, may be {@code null}.
* @param cause The cause of the problem, may be {@code null}.
*/
void add( ModelProblem.Severity severity, String message, Exception cause );
void add( ModelProblem.Severity severity, String message, InputLocation location, Exception cause );

}

0 comments on commit 6e7b422

Please sign in to comment.