[MINVOKER-223]  ensure post build script is executed only once

git-svn-id: https://svn.apache.org/repos/asf/maven/plugins/trunk@1802097 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/it/postbuild-executed-only-once/pom.xml b/src/it/postbuild-executed-only-once/pom.xml
new file mode 100644
index 0000000..4f6722f
--- /dev/null
+++ b/src/it/postbuild-executed-only-once/pom.xml
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.plugins.invoker</groupId>
+  <artifactId>fail-postbuild</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <packaging>pom</packaging>
+
+  <description>Test to check that a failure of the post-build hook fails the parent build, too.</description>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-invoker-plugin</artifactId>
+        <version>@pom.version@</version>
+        <configuration>
+          <debug>true</debug>
+          <cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
+          <pomIncludes>
+            <pomInclude>*/pom.xml</pomInclude>
+          </pomIncludes>
+          <goals>
+            <goal>validate</goal>
+          </goals>
+        </configuration>
+        <executions>
+          <execution>
+            <id>integration-test</id>
+            <phase>initialize</phase>
+            <goals>
+              <goal>run</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+
+</project>
diff --git a/src/it/postbuild-executed-only-once/src/it/project/invoker.properties b/src/it/postbuild-executed-only-once/src/it/project/invoker.properties
new file mode 100644
index 0000000..76183b9
--- /dev/null
+++ b/src/it/postbuild-executed-only-once/src/it/project/invoker.properties
@@ -0,0 +1,20 @@
+# 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.
+
+invoker.goals.1 = clean test
+invoker.goals.2 = clean test 
+invoker.systemPropertiesFile.2 = system2.properties
diff --git a/src/it/postbuild-executed-only-once/src/it/project/pom.xml b/src/it/postbuild-executed-only-once/src/it/project/pom.xml
new file mode 100644
index 0000000..9e34cd5
--- /dev/null
+++ b/src/it/postbuild-executed-only-once/src/it/project/pom.xml
@@ -0,0 +1,64 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<!--
+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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.plugins.invoker.it.post_executed_only_once</groupId>
+  <artifactId>post_executed_only_once</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <packaging>jar</packaging>
+
+  <name>post_executed_only_once</name>
+  <url>http://maven.apache.org</url>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    <create_touch_file>false</create_touch_file>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.12</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <pluginManagement>
+      <plugins>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-surefire-plugin</artifactId>
+          <version>2.20</version>
+          <configuration>
+            <systemPropertyVariables>
+              <create_touch_file>${create_touch_file}</create_touch_file>
+              <touch_file_path>${project.build.directory}</touch_file_path>
+            </systemPropertyVariables>
+          </configuration>
+        </plugin>
+      </plugins>
+    </pluginManagement>
+  </build>
+
+</project>
diff --git a/src/it/postbuild-executed-only-once/src/it/project/postbuild.bsh b/src/it/postbuild-executed-only-once/src/it/project/postbuild.bsh
new file mode 100644
index 0000000..5cbc03e
--- /dev/null
+++ b/src/it/postbuild-executed-only-once/src/it/project/postbuild.bsh
@@ -0,0 +1,40 @@
+/*
+ * 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.
+ */
+
+import java.io.*;
+import java.util.*;
+import java.util.regex.*;
+
+try
+{
+    File touchFile = new File( basedir, "target/touch.txt" );
+    System.out.println( "Checking for existence of touch file: " + touchFile );
+    if ( !touchFile.exists() )
+    {
+        System.out.println( "touch.txt not exists FAILED!" );
+        return false;
+    }
+}
+catch( Throwable t )
+{
+    t.printStackTrace();
+    return false;
+}
+
+return true;
diff --git a/src/it/postbuild-executed-only-once/src/it/project/src/main/java/org/apache/maven/plugins/invoker/it/post_executed_only_once/App.java b/src/it/postbuild-executed-only-once/src/it/project/src/main/java/org/apache/maven/plugins/invoker/it/post_executed_only_once/App.java
new file mode 100644
index 0000000..79bcfc5
--- /dev/null
+++ b/src/it/postbuild-executed-only-once/src/it/project/src/main/java/org/apache/maven/plugins/invoker/it/post_executed_only_once/App.java
@@ -0,0 +1,36 @@
+package org.apache.maven.plugins.invoker.it.post_executed_only_once;
+
+/*
+ * 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.
+ */
+
+import java.io.File;
+
+public class App
+{
+    public boolean createFile()
+        throws Exception
+    {
+        if ( Boolean.getBoolean( "create_touch_file" ) )
+        {
+            File touch = new File( System.getProperty( "touch_file_path" ), "touch.txt" );
+            return touch.createNewFile();
+        }
+        return false;
+    }
+}
diff --git a/src/it/postbuild-executed-only-once/src/it/project/src/test/java/org/apache/maven/plugins/invoker/it/post_executed_only_once/AppTest.java b/src/it/postbuild-executed-only-once/src/it/project/src/test/java/org/apache/maven/plugins/invoker/it/post_executed_only_once/AppTest.java
new file mode 100644
index 0000000..e388290
--- /dev/null
+++ b/src/it/postbuild-executed-only-once/src/it/project/src/test/java/org/apache/maven/plugins/invoker/it/post_executed_only_once/AppTest.java
@@ -0,0 +1,35 @@
+package org.apache.maven.plugins.invoker.it.post_executed_only_once;
+
+/*
+ * 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.
+ */
+
+import junit.framework.TestCase;
+import org.junit.Test;
+
+public class AppTest
+    extends TestCase
+{
+    @Test
+    public void testApp()
+        throws Exception
+    {
+        new App().createFile();
+        assertTrue( true );
+    }
+}
diff --git a/src/it/postbuild-executed-only-once/src/it/project/system2.properties b/src/it/postbuild-executed-only-once/src/it/project/system2.properties
new file mode 100644
index 0000000..bcd7772
--- /dev/null
+++ b/src/it/postbuild-executed-only-once/src/it/project/system2.properties
@@ -0,0 +1,18 @@
+# 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.
+
+create_touch_file = true
diff --git a/src/it/postbuild-executed-only-once/verify.bsh b/src/it/postbuild-executed-only-once/verify.bsh
new file mode 100644
index 0000000..b03d957
--- /dev/null
+++ b/src/it/postbuild-executed-only-once/verify.bsh
@@ -0,0 +1,41 @@
+/*
+ * 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.
+ */
+
+import java.io.*;
+import java.util.*;
+import java.util.regex.*;
+
+try
+{
+    // make sure the Invoker Plugin was indeed run and the build didn't fail somewhere else
+    File touchFile = new File( basedir, "target/it/project/target/touch.txt" );
+    System.out.println( "Checking for existence of touch file: " + touchFile );
+    if ( !touchFile.exists() )
+    {
+        System.out.println( "FAILED!" );
+        return false;
+    }
+}
+catch( Throwable t )
+{
+    t.printStackTrace();
+    return false;
+}
+
+return true;
diff --git a/src/it/selector-scripts/setup.groovy b/src/it/selector-scripts/setup.groovy
index f6b8108..ac78115 100644
--- a/src/it/selector-scripts/setup.groovy
+++ b/src/it/selector-scripts/setup.groovy
@@ -17,7 +17,6 @@
  * under the License.
  */
 
-import java.io.File
 import org.apache.commons.io.FileUtils
 
 // Previous potential target 'content' has impact on IT execution
diff --git a/src/it/selector-scripts/verify.bsh b/src/it/selector-scripts/verify.bsh
index 9612c29..422b5d9 100644
--- a/src/it/selector-scripts/verify.bsh
+++ b/src/it/selector-scripts/verify.bsh
@@ -51,7 +51,7 @@
         System.out.println( "Checking for absence of: " + touchFile );
         if ( touchFile.exists() )
         {
-            System.out.println( "FAILED!" );
+            System.out.println( file  + " should not be here -> FAILED!" );
             return false;
         }
     }
@@ -67,7 +67,7 @@
          String xml = FileUtils.fileRead( resultFile, "UTF-8" );
          if ( xml.indexOf("result=\"success\"") < 0 && xml.indexOf("result='success'") < 0 )
          {
-             System.out.println( "FAILED!" );
+             System.out.println( resultFile + " not success -> FAILED!" );
              return false;
          }
     }
@@ -83,7 +83,7 @@
          String xml = FileUtils.fileRead( resultFile, "UTF-8" );
          if ( xml.indexOf("result=\"skipped\"") < 0 && xml.indexOf("result='skipped'") < 0 )
          {
-             System.out.println( "FAILED!" );
+             System.out.println( resultFile + " not skipped -> FAILED!" );
              return false;
          }
     }
@@ -99,7 +99,7 @@
          String xml = FileUtils.fileRead( resultFile, "UTF-8" );
          if ( xml.indexOf("result=\"error\"") < 0 && xml.indexOf("result='error'") < 0 )
          {
-             System.out.println( "FAILED!" );
+             System.out.println( resultFile + " not failed -> FAILED!" );
              return false;
          }
     }
diff --git a/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java b/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java
index ba09a1b..debba76 100644
--- a/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java
+++ b/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java
@@ -1755,6 +1755,7 @@
         Map<String, Object> context = new LinkedHashMap<String, Object>();

 

         FileLogger logger = setupBuildLogFile( basedir );

+        boolean selectorResult = true;

         try

         {

             try

@@ -1764,10 +1765,12 @@
             }

             catch ( RunErrorException e )

             {

+                selectorResult = false;

                 throw e;

             }

             catch ( RunFailureException e )

             {

+                selectorResult = false;

                 return false;

             }

 

@@ -1876,7 +1879,6 @@
                     throw new RunFailureException( "Maven invocation failed. " + e.getMessage(),

                                                    BuildJob.Result.FAILURE_BUILD );

                 }

-                runPostBuildHook( basedir, context, logger );

                 verify( result, invocationIndex, invokerProperties, logger );

             }

         }

@@ -1886,6 +1888,10 @@
         }

         finally

         {

+            if ( selectorResult )

+            {

+                runPostBuildHook( basedir, context, logger );

+            }

             if ( logger != null )

             {

                 logger.close();