diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java b/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java index ceab747477..908d633c03 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java @@ -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 : or" + + " :[:]:." + + " 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 : or" + + " :[:]:. Available lifecycle phases are: " + + getLifecyclePhaseList() + ".", lifecyclePhase ); } /* @@ -2068,4 +2088,32 @@ public class DefaultLifecycleExecutor return phaseToLifecycleMap; } + private String getLifecyclePhaseList() + { + Set phases = new LinkedHashSet(); + + for ( Lifecycle lifecycle : lifecycles ) + { + phases.addAll( lifecycle.getPhases() ); + } + + return StringUtils.join( phases.iterator(), ", " ); + } + + private boolean requiresProject( MavenSession session ) + { + List goals = session.getGoals(); + if ( goals != null ) + { + for ( String goal : goals ) + { + if ( !isGoalSpecification( goal ) ) + { + return true; + } + } + } + return false; + } + } diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/LifecyclePhaseNotFoundException.java b/maven-core/src/main/java/org/apache/maven/lifecycle/LifecyclePhaseNotFoundException.java index a23dfb6de8..86b37db3ba 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/LifecyclePhaseNotFoundException.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/LifecyclePhaseNotFoundException.java @@ -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 : or" - + " :[:]:" ); + super( message ); this.lifecyclePhase = ( lifecyclePhase != null ) ? lifecyclePhase : ""; } diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/MissingProjectException.java b/maven-core/src/main/java/org/apache/maven/lifecycle/MissingProjectException.java new file mode 100644 index 0000000000..991914557e --- /dev/null +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/MissingProjectException.java @@ -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 ); + } + +} diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/NoGoalSpecifiedException.java b/maven-core/src/main/java/org/apache/maven/lifecycle/NoGoalSpecifiedException.java new file mode 100644 index 0000000000..63ba2caaa6 --- /dev/null +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/NoGoalSpecifiedException.java @@ -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 ); + } + +}