[MNG-3779] Improve error message when trying to execute moojo with no pom.xml

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@928903 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2010-03-29 21:16:26 +00:00
parent 8aba4e9b9b
commit 57fd3fb279
4 changed files with 135 additions and 6 deletions

View File

@ -248,8 +248,23 @@ public class DefaultLifecycleExecutor
try
{
if ( !session.isUsingPOMsFromFilesystem() && requiresProject( session ) )
{
throw new MissingProjectException( "The goal you specified requires a project to execute"
+ " but there is no POM in this directory (" + session.getExecutionRootDirectory() + ")."
+ " Please verify you invoked Maven from the correct directory." );
}
projectBuilds = calculateProjectBuilds( session );
if ( projectBuilds.isEmpty() )
{
throw new NoGoalSpecifiedException( "No goals have been specified for this build."
+ " You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or"
+ " <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>."
+ " Available lifecycle phases are: " + getLifecyclePhaseList() + "." );
}
projectIndex = new ProjectIndex( session.getProjects() );
}
catch ( Exception e )
@ -564,7 +579,9 @@ public class DefaultLifecycleExecutor
if ( mojoDescriptor.isProjectRequired() && !session.isUsingPOMsFromFilesystem() )
{
Throwable cause =
new IllegalStateException( "Goal requires a project to execute but there is no POM in this build." );
new MissingProjectException( "Goal requires a project to execute"
+ " but there is no POM in this directory (" + session.getExecutionRootDirectory() + ")."
+ " Please verify you invoked Maven from the correct directory." );
throw new LifecycleExecutionException( mojoExecution, null, cause );
}
@ -1080,7 +1097,10 @@ public class DefaultLifecycleExecutor
if ( lifecycle == null )
{
throw new LifecyclePhaseNotFoundException( lifecyclePhase );
throw new LifecyclePhaseNotFoundException( "Unknown lifecycle phase \"" + lifecyclePhase
+ "\". You must specify a valid lifecycle phase" + " or a goal in the format <plugin-prefix>:<goal> or"
+ " <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: "
+ getLifecyclePhaseList() + ".", lifecyclePhase );
}
/*
@ -2068,4 +2088,32 @@ public class DefaultLifecycleExecutor
return phaseToLifecycleMap;
}
private String getLifecyclePhaseList()
{
Set<String> phases = new LinkedHashSet<String>();
for ( Lifecycle lifecycle : lifecycles )
{
phases.addAll( lifecycle.getPhases() );
}
return StringUtils.join( phases.iterator(), ", " );
}
private boolean requiresProject( MavenSession session )
{
List<String> goals = session.getGoals();
if ( goals != null )
{
for ( String goal : goals )
{
if ( !isGoalSpecification( goal ) )
{
return true;
}
}
}
return false;
}
}

View File

@ -33,13 +33,12 @@ public class LifecyclePhaseNotFoundException
/**
* Creates a new exception to indicate that the specified lifecycle phase is not defined by any known lifecycle.
*
* @param message The detail message, may be {@code null}.
* @param lifecyclePhase The name of the lifecycle phase that could not be located, may be {@code null}.
*/
public LifecyclePhaseNotFoundException( String lifecyclePhase )
public LifecyclePhaseNotFoundException( String message, String lifecyclePhase )
{
super( "Unknown lifecycle phase \"" + lifecyclePhase + "\". You must specify a valid lifecycle phase"
+ " or a goal in the format <plugin-prefix>:<goal> or"
+ " <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>" );
super( message );
this.lifecyclePhase = ( lifecyclePhase != null ) ? lifecyclePhase : "";
}

View File

@ -0,0 +1,41 @@
package org.apache.maven.lifecycle;
/*
* 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.
*/
/**
* Signals a failure to execute a lifecycle phase or mojo because a project is required but not present.
*
* @author Benjamin Bentmann
*/
public class MissingProjectException
extends Exception
{
/**
* Creates a new exception.
*
* @param message The detail message, may be {@code null}.
*/
public MissingProjectException( String message )
{
super( message );
}
}

View File

@ -0,0 +1,41 @@
package org.apache.maven.lifecycle;
/*
* 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.
*/
/**
* Signals a failure to build because no goal was specified.
*
* @author Benjamin Bentmann
*/
public class NoGoalSpecifiedException
extends Exception
{
/**
* Creates a new exception.
*
* @param message The detail message, may be {@code null}.
*/
public NoGoalSpecifiedException( String message )
{
super( message );
}
}