You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by be...@apache.org on 2008/08/10 18:40:49 UTC

svn commit: r684550 - in /maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker: BuildFailureException.java InvokerMojo.java

Author: bentmann
Date: Sun Aug 10 09:40:49 2008
New Revision: 684550

URL: http://svn.apache.org/viewvc?rev=684550&view=rev
Log:
o Refactored handling of build failures

Added:
    maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/BuildFailureException.java   (with props)
Modified:
    maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InvokerMojo.java

Added: maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/BuildFailureException.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/BuildFailureException.java?rev=684550&view=auto
==============================================================================
--- maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/BuildFailureException.java (added)
+++ maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/BuildFailureException.java Sun Aug 10 09:40:49 2008
@@ -0,0 +1,48 @@
+package org.apache.maven.plugin.invoker;
+
+/*
+ * 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 of a sub build run by the Invoker Plugin. This can be caused by an unsuccessful pre-/post-build
+ * script or a failure of the forked Maven build itself.
+ * 
+ * @author Benjamin Bentmann
+ * @version $Id$
+ */
+class BuildFailureException
+    extends Exception
+{
+
+    /**
+     * The serial version identifier for this class.
+     */
+    private static final long serialVersionUID = 236131530635863814L;
+
+    /**
+     * Creates a new exception with the specified detail message.
+     * 
+     * @param message The detail message, may be <code>null</code>.
+     */
+    public BuildFailureException( String message )
+    {
+        super( message );
+    }
+
+}

Propchange: maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/BuildFailureException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/BuildFailureException.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InvokerMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InvokerMojo.java?rev=684550&r1=684549&r2=684550&view=diff
==============================================================================
--- maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InvokerMojo.java (original)
+++ maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InvokerMojo.java Sun Aug 10 09:40:49 2008
@@ -629,9 +629,9 @@
     /**
      * Copied a directory structure with deafault exclusions (.svn, CVS, etc)
      * 
-     * @param sourceDir
-     * @param destDir
-     * @throws IOException
+     * @param sourceDir The source directory to copy, must not be <code>null</code>.
+     * @param destDir The target directory to copy to, must not be <code>null</code>.
+     * @throws IOException If the directory structure could not be copied.
      */
     private void copyDirectoryStructure( File sourceDir, File destDir )
         throws IOException
@@ -768,14 +768,7 @@
         FileLogger logger = setupLogger( basedir );
         try
         {
-            if ( !prebuild( basedir, logger ) )
-            {
-                getLog().info( "...FAILED[pre-build script did not succeed]" );
-
-                failures.add( project );
-
-                return;
-            }
+            runScript( "pre-build script", basedir, preBuildHookScript, logger );
 
             final InvocationRequest request = new DefaultInvocationRequest();
 
@@ -845,75 +838,27 @@
                 catch ( final MavenInvocationException e )
                 {
                     getLog().debug( "Error invoking Maven: " + e.getMessage(), e );
-                    getLog().info( "...FAILED[error invoking Maven]" );
-
-                    failures.add( project );
-
-                    return;
-                }
-
-                if ( result.getExecutionException() != null )
-                {
-                    if ( !suppressSummaries )
-                    {
-                        StringBuffer buffer = new StringBuffer( 256 );
-                        buffer.append( "...FAILED. " );
-                        if ( logger != null )
-                        {
-                            buffer.append( "See " );
-                            buffer.append( logger.getOutputFile().getAbsolutePath() );
-                            buffer.append( " for details." );
-                        }
-                        else
-                        {
-                            buffer.append( "See console output for details." );
-                        }
-                        getLog().info( buffer.toString() );
-                    }
-
-                    failures.add( project );
-
-                    return;
+                    throw new BuildFailureException( "Maven invocation failed. " + e.getMessage() );
                 }
-                else if ( !invokerProperties.isExpectedResult( result.getExitCode(), invocationIndex ) )
-                {
-                    if ( !suppressSummaries )
-                    {
-                        StringBuffer buffer = new StringBuffer( 256 );
-                        buffer.append( "...FAILED[code=" ).append( result.getExitCode() ).append( "]. " );
-                        if ( logger != null )
-                        {
-                            buffer.append( "See " );
-                            buffer.append( logger.getOutputFile().getAbsolutePath() );
-                            buffer.append( " for details." );
-                        }
-                        else
-                        {
-                            buffer.append( "See console output for details." );
-                        }
-                        getLog().info( buffer.toString() );
-                    }
-
-                    failures.add( project );
 
-                    return;
-                }
+                verify( result, invocationIndex, invokerProperties, logger );
             }
 
-            if ( !verify( basedir, logger ) )
-            {
-                if ( !suppressSummaries )
-                {
-                    getLog().info( "...FAILED[verify script did not succeed]." );
-                }
+            runScript( "post-build script", basedir, postBuildHookScript, logger );
 
-                failures.add( project );
-            }
-            else if ( !suppressSummaries )
+            if ( !suppressSummaries )
             {
                 getLog().info( "...SUCCESS." );
             }
         }
+        catch ( BuildFailureException e )
+        {
+            if ( !suppressSummaries )
+            {
+                getLog().info( "...FAILED. " + e.getMessage() );
+            }
+            failures.add( project );
+        }
         finally
         {
             if ( logger != null )
@@ -1013,45 +958,39 @@
     }
 
     /**
-     * Runs the pre-build-hook script of the specified project (if any).
+     * Verifies the invocation result.
      * 
-     * @param basedir The base directory of the project, must not be <code>null</code>.
-     * @param logger The logger to redirect the script output to, may be <code>null</code> to use stdout/stderr.
-     * @return <code>true</code> if the script does not exist or completed successfully, <code>false</code> otherwise.
-     * @throws MojoExecutionException If an I/O error occurred while reading the script file.
+     * @param result The invocation result to check, must not be <code>null</code>.
+     * @param invocationIndex The index of the invocation for which to check the exit code, must not be negative.
+     * @param invokerProperties The invoker properties used to check the exit code, must not be <code>null</code>.
+     * @param logger The build logger, may be <code>null</code> if logging is disabled.
+     * @throws BuildFailureException If the invocation result indicates a build failure.
      */
-    private boolean prebuild( final File basedir, final FileLogger logger )
-        throws MojoExecutionException
+    private void verify( InvocationResult result, int invocationIndex, InvokerProperties invokerProperties,
+                         FileLogger logger )
+        throws BuildFailureException
     {
-        boolean result = true;
-
-        if ( preBuildHookScript != null )
+        if ( result.getExecutionException() != null )
         {
-            result = runScript( "pre-build script", basedir, preBuildHookScript, logger );
+            throw new BuildFailureException( "The Maven invocation failed. "
+                + result.getExecutionException().getMessage() );
         }
-
-        return result;
-    }
-
-    /**
-     * Runs the post-build-hook script of the specified project (if any).
-     * 
-     * @param basedir The base directory of the project, must not be <code>null</code>.
-     * @param logger The logger to redirect the script output to, may be <code>null</code> to use stdout/stderr.
-     * @return <code>true</code> if the script does not exist or completed successfully, <code>false</code> otherwise.
-     * @throws MojoExecutionException If an I/O error occurred while reading the script file.
-     */
-    private boolean verify( final File basedir, final FileLogger logger )
-        throws MojoExecutionException
-    {
-        boolean result = true;
-
-        if ( postBuildHookScript != null )
+        else if ( !invokerProperties.isExpectedResult( result.getExitCode(), invocationIndex ) )
         {
-            result = runScript( "verification script", basedir, postBuildHookScript, logger );
+            StringBuffer buffer = new StringBuffer( 256 );
+            buffer.append( "The build exited with code " ).append( result.getExitCode() ).append( ". " );
+            if ( logger != null )
+            {
+                buffer.append( "See " );
+                buffer.append( logger.getOutputFile().getAbsolutePath() );
+                buffer.append( " for details." );
+            }
+            else
+            {
+                buffer.append( "See console output for details." );
+            }
+            throw new BuildFailureException( buffer.toString() );
         }
-
-        return result;
     }
 
     /**
@@ -1059,16 +998,21 @@
      * 
      * @param scriptDescription The description of the script to use for logging, must not be <code>null</code>.
      * @param basedir The base directory of the project, must not be <code>null</code>.
-     * @param relativeScriptPath The path to the script relative to the project base directory, must not be
-     *            <code>null</code>.
+     * @param relativeScriptPath The path to the script relative to the project base directory, may be <code>null</code>
+     *            to skip the script execution.
      * @param logger The logger to redirect the script output to, may be <code>null</code> to use stdout/stderr.
-     * @return <code>true</code> if the script does not exist or completed successfully, <code>false</code> otherwise.
      * @throws MojoExecutionException If an I/O error occurred while reading the script file.
+     * @throws BuildFailureException If the script did not return <code>true</code> of threw an exception.
      */
-    private boolean runScript( final String scriptDescription, final File basedir, final String relativeScriptPath,
-                               final FileLogger logger )
-        throws MojoExecutionException
+    private void runScript( final String scriptDescription, final File basedir, final String relativeScriptPath,
+                            final FileLogger logger )
+        throws MojoExecutionException, BuildFailureException
     {
+        if ( relativeScriptPath == null )
+        {
+            return;
+        }
+
         final File scriptFile = resolveScript( new File( basedir, relativeScriptPath ) );
 
         if ( scriptFile.exists() )
@@ -1086,7 +1030,7 @@
             {
                 String name = interpreter.getClass().getName();
                 name = name.substring( name.lastIndexOf( '.' ) + 1 );
-                getLog().debug( "Running script with " + name );
+                getLog().debug( "Running script with " + name + ": " + scriptFile );
             }
 
             String script;
@@ -1097,40 +1041,41 @@
             catch ( IOException e )
             {
                 String errorMessage =
-                    "error reading " + scriptDescription + " " + basedir.getPath() + File.separatorChar
-                        + postBuildHookScript + ", " + e.getMessage();
+                    "error reading " + scriptDescription + " " + scriptFile.getPath() + ", " + e.getMessage();
                 throw new MojoExecutionException( errorMessage, e );
             }
 
+            Object result;
             try
             {
                 if ( logger != null )
                 {
                     logger.consumeLine( "Running " + scriptDescription + " in: " + scriptFile );
                 }
-                Object result = interpreter.evaluateScript( script, classPath, globalVariables, out );
+                result = interpreter.evaluateScript( script, classPath, globalVariables, out );
                 if ( logger != null )
                 {
                     logger.consumeLine( "Finished " + scriptDescription + " in: " + scriptFile );
                 }
-                return Boolean.TRUE.equals( result ) || "true".equals( result );
             }
-            catch ( Exception e )
+            catch ( ScriptEvaluationException e )
             {
+                Throwable t = ( e.getCause() != null ) ? e.getCause() : e;
                 String errorMessage =
-                    "error evaluating " + scriptDescription + " " + basedir.getPath() + File.separatorChar
-                        + postBuildHookScript + ", " + e.getMessage();
-                getLog().error( errorMessage, e );
+                    "error evaluating " + scriptDescription + " " + scriptFile.getPath() + ", " + t.getMessage();
+                getLog().debug( errorMessage, t );
                 if ( logger != null )
                 {
-                    e.printStackTrace( logger.getPrintStream() );
+                    t.printStackTrace( logger.getPrintStream() );
                 }
-                return false;
+                throw new BuildFailureException( "The " + scriptDescription + " did not succeed. " + t.getMessage() );
             }
 
+            if ( !( Boolean.TRUE.equals( result ) || "true".equals( result ) ) )
+            {
+                throw new BuildFailureException( "The " + scriptDescription + " returned " + result + "." );
+            }
         }
-
-        return true;
     }
 
     /**