Copilot commented on code in PR #716:
URL: 
https://github.com/apache/maven-invoker-plugin/pull/716#discussion_r2997651251


##########
src/it/multiple-pre-post-build/pom.xml:
##########
@@ -0,0 +1,61 @@
+<?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>multiple-pre-post-build</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <packaging>pom</packaging>
+
+  <description>Test to check that multiple pre-build and post-build script are 
executed</description>

Review Comment:
   Grammar: the description should use the plural “scripts” (multiple pre-build 
and post-build scripts).
   ```suggestion
     <description>Test to check that multiple pre-build and post-build scripts 
are executed</description>
   ```



##########
src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java:
##########
@@ -1973,21 +1976,23 @@ private boolean runSelectorHook(File basedir, 
Map<String, Object> context, FileL
         return true;
     }
 
-    private void runPreBuildHook(File basedir, Map<String, Object> context, 
FileLogger logger)
+    private void runPreBuildHook(File basedir, Map<String, Object> context, 
FileLogger logger, int invocationIndex)
             throws MojoExecutionException, RunFailureException {
         try {
-            scriptRunner.run("pre-build script", basedir, preBuildHookScript, 
context, logger);
+            String hookName = invocationIndex > 0 ? preBuildHookScript + "." + 
invocationIndex : preBuildHookScript;
+            scriptRunner.run("pre-build script", basedir, hookName, context, 
logger);
         } catch (ScriptException e) {
             throw new RunFailureException(BuildJob.Result.FAILURE_PRE_HOOK, e);
         } catch (IOException e) {
             throw new MojoExecutionException(e.getMessage(), e);
         }
     }
 
-    private void runPostBuildHook(File basedir, Map<String, Object> context, 
FileLogger logger)
+    private void runPostBuildHook(File basedir, Map<String, Object> context, 
FileLogger logger, int invocationIndex)
             throws MojoExecutionException, RunFailureException {
         try {
-            scriptRunner.run("post-build script", basedir, 
postBuildHookScript, context, logger);
+            String hookName = invocationIndex > 0 ? postBuildHookScript + "." 
+ invocationIndex : postBuildHookScript;
+            scriptRunner.run("post-build script", basedir, hookName, context, 
logger);

Review Comment:
   Same issue as pre-build hook naming: if `invoker.postBuildHookScript` is 
configured with an extension, concatenating `.<index>` yields names like 
`postbuild.groovy.1` instead of `postbuild.1.groovy`, so the per-invocation 
post hook won’t be found. The hook name generation should insert the index 
before the extension when one exists.



##########
src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java:
##########
@@ -1942,9 +1944,10 @@ private boolean runBuild(
                     throw new RunFailureException(
                             "Maven invocation failed. " + e.getMessage(), 
BuildJob.Result.FAILURE_BUILD);
                 }
+                runPostBuildHook(basedir, context, logger, invocationIndex);

Review Comment:
   `runPostBuildHook(..., invocationIndex)` is only called after a successful 
Maven invocation + `verify(...)`. If the invocation/verify fails (throws), the 
per-invocation post-build hook will be skipped, which is inconsistent with 
existing behavior where the post-build hook runs even on build failure (see IT 
`fail-run-postbuild`). Consider moving the per-invocation post hook into a 
`finally` within the loop so it runs once per invocation regardless of the 
build outcome.



##########
src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java:
##########
@@ -1973,21 +1976,23 @@ private boolean runSelectorHook(File basedir, 
Map<String, Object> context, FileL
         return true;
     }
 
-    private void runPreBuildHook(File basedir, Map<String, Object> context, 
FileLogger logger)
+    private void runPreBuildHook(File basedir, Map<String, Object> context, 
FileLogger logger, int invocationIndex)
             throws MojoExecutionException, RunFailureException {
         try {
-            scriptRunner.run("pre-build script", basedir, preBuildHookScript, 
context, logger);
+            String hookName = invocationIndex > 0 ? preBuildHookScript + "." + 
invocationIndex : preBuildHookScript;
+            scriptRunner.run("pre-build script", basedir, hookName, context, 
logger);

Review Comment:
   When `invoker.preBuildHookScript` includes a file extension (e.g. 
`prebuild.groovy`), building the indexed hook name by simple concatenation 
produces `prebuild.groovy.1`, which won’t be discovered by the script runner. 
Consider inserting the index before the extension when one is present (and 
keeping the current behavior when no extension is provided).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to