From bc84964c765656cd53bc85345b30d780157818dc Mon Sep 17 00:00:00 2001
From: "Artyom V. Poptsov" <poptsov.artyom@gmail.com>
Date: Sat, 18 Jun 2022 17:05:36 +0300
Subject: [PATCH] gnu: Add maven-shared-invoker

* gnu/packages/maven.scm (maven-shared-invoker): New variable.
* gnu/packages/patches/maven-shared-invoker-exception-handler-fix.patch,
  gnu/packages/patches/maven-shared-invoker-rename-test-classes.patch: Add to
  the repository.
* gnu/local.mk (dist_patch_DATA): Add new Maven shared invoker patches.
---
 gnu/local.mk                                  |   2 +
 gnu/packages/maven.scm                        |  37 +++++
 ...shared-invoker-exception-handler-fix.patch |  28 ++++
 ...n-shared-invoker-rename-test-classes.patch | 126 ++++++++++++++++++
 4 files changed, 193 insertions(+)
 create mode 100644 gnu/packages/patches/maven-shared-invoker-exception-handler-fix.patch
 create mode 100644 gnu/packages/patches/maven-shared-invoker-rename-test-classes.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index 0176585485..6d7aafb848 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1479,6 +1479,8 @@ dist_patch_DATA =						\
   %D%/packages/patches/maxima-defsystem-mkdir.patch		\
   %D%/packages/patches/maven-generate-component-xml.patch		\
   %D%/packages/patches/maven-generate-javax-inject-named.patch		\
+  %D%/packages/patches/maven-shared-invoker-exception-handler-fix.patch	\
+  %D%/packages/patches/maven-shared-invoker-rename-test-classes.patch	\
   %D%/packages/patches/mcrypt-CVE-2012-4409.patch			\
   %D%/packages/patches/mcrypt-CVE-2012-4426.patch			\
   %D%/packages/patches/mcrypt-CVE-2012-4527.patch			\
diff --git a/gnu/packages/maven.scm b/gnu/packages/maven.scm
index 817fee1c71..57f469ed5d 100644
--- a/gnu/packages/maven.scm
+++ b/gnu/packages/maven.scm
@@ -765,6 +765,43 @@ plexus-utils in Maven.  It is not a 100% API compatible replacement but a
 replacement with improvements.")
     (license license:asl2.0)))
 
+(define-public maven-shared-invoker
+  (package
+    (name "maven-shared-invoker")
+    (version "3.2.0")
+    (source (origin
+              (method url-fetch)
+              (uri (string-append "mirror://apache/maven/shared/"
+                                  "maven-invoker-" version "-source-release.zip"))
+              (sha256
+               (base32
+                "0yhgxvwpmyfhqaksdfmj9c4ml4pj60gnin8bq1a92ximf1dyyjyc"))
+              (patches
+               (search-patches
+                "maven-shared-invoker-exception-handler-fix.patch"
+                "maven-shared-invoker-rename-test-classes.patch"))))
+    (build-system ant-build-system)
+    (arguments
+     `(#:jar-name "maven-shared-invoker.jar"
+       #:source-dir "src/main/java"
+       #:tests? #f))                      ; Tests require Maven itself
+    (propagated-inputs
+     (list maven-parent-pom-35))
+    (native-inputs
+     (list unzip
+           maven-surefire-plugin
+           java-javax-inject
+           java-junit))
+    (home-page "https://maven.apache.org/shared/maven-invoker/index.html")
+    (synopsis "Invoke Maven programmatically")
+    (description "The API of this module is concerned with firing a Maven
+build in a new JVM.  It accomplishes its task by building up a conventional
+Maven command line from options given in the current request, along with those
+global options specified in the invoker itself.  Once it has the command line,
+the invoker will execute it, and capture the resulting exit code or any
+exception thrown to signal a failure to execute.")
+    (license license:asl2.0)))
+
 (define-public maven-plugin-annotations
   (package
     (name "maven-plugin-annotations")
diff --git a/gnu/packages/patches/maven-shared-invoker-exception-handler-fix.patch b/gnu/packages/patches/maven-shared-invoker-exception-handler-fix.patch
new file mode 100644
index 0000000000..eb572f7c26
--- /dev/null
+++ b/gnu/packages/patches/maven-shared-invoker-exception-handler-fix.patch
@@ -0,0 +1,28 @@
+From 30e9cde32c1e2513ca1f656fa5043c05935f2645 Mon Sep 17 00:00:00 2001
+From: "Artyom V. Poptsov" <poptsov.artyom@gmail.com>
+Date: Tue, 14 Jun 2022 23:53:13 +0300
+Subject: [PATCH 1/2] MavenCommandLineBuilder: Fix exception handling
+
+* src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java
+  (setGoals): Catch 'Exception' instead of 'CommandLineException' as
+  'CommandLineException' is never thrown in the "try" block.
+---
+ .../apache/maven/shared/invoker/MavenCommandLineBuilder.java    | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java b/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java
+index 7c7e985..eaf8333 100644
+--- a/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java
++++ b/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java
+@@ -257,7 +257,7 @@ public class MavenCommandLineBuilder
+             {
+                 cli.createArg().setLine( StringUtils.join( goals.iterator(), " " ) );
+             }
+-            catch ( CommandLineException e )
++            catch ( Exception e )
+             {
+                 throw new CommandLineConfigurationException( "Problem to set goals: " + e.getMessage(), e );
+             }
+-- 
+2.25.1
+
diff --git a/gnu/packages/patches/maven-shared-invoker-rename-test-classes.patch b/gnu/packages/patches/maven-shared-invoker-rename-test-classes.patch
new file mode 100644
index 0000000000..7ffb6fed9b
--- /dev/null
+++ b/gnu/packages/patches/maven-shared-invoker-rename-test-classes.patch
@@ -0,0 +1,126 @@
+From 4bce3183b25c44ab406c2f4d8541a0a520b15a3d Mon Sep 17 00:00:00 2001
+From: "Artyom V. Poptsov" <poptsov.artyom@gmail.com>
+Date: Wed, 15 Jun 2022 07:09:29 +0300
+Subject: [PATCH 2/2] test: Rename some classes to avoid name conflicts
+
+* src/test/resources/test-build-should-timeout/src/main/java/org/apache/maven/shared/invoker/App.java:
+  Rename to
+  "src/test/resources/test-build-should-timeout/src/main/java/org/apache/maven/shared/invoker/AppTimeout.java"
+* src/test/resources/test-build-should-timeout/src/test/java/org/apache/maven/shared/invoker/AppTest.java
+  Rename to
+  "src/test/resources/test-build-should-timeout/src/test/java/org/apache/maven/shared/invoker/AppTimeoutTest.java"
+* src/test/resources/test-build-should-succeed/src/test/java/org/apache/maven/shared/invoker/AppTest.java:
+  Rename to
+  "src/test/resources/test-build-should-succeed/src/test/java/org/apache/maven/shared/invoker/AppShouldSucceedTest.java"
+* src/test/resources/test-build-should-succeed/src/main/java/org/apache/maven/shared/invoker/App.java:
+  Rename to
+  "src/test/resources/test-build-should-timeout/src/main/java/org/apache/maven/shared/invoker/AppTimeout.java".
+---
+ ...AppTest.java => AppShouldSucceedTest.java} |  4 +--
+ .../org/apache/maven/shared/invoker/App.java  | 32 -------------------
+ .../maven/shared/invoker/AppTimeout.java}     |  2 +-
+ .../{AppTest.java => AppTimeoutTest.java}     |  2 +-
+ 4 files changed, 4 insertions(+), 36 deletions(-)
+ rename src/test/resources/test-build-should-succeed/src/test/java/org/apache/maven/shared/invoker/{AppTest.java => AppShouldSucceedTest.java} (94%)
+ delete mode 100644 src/test/resources/test-build-should-timeout/src/main/java/org/apache/maven/shared/invoker/App.java
+ rename src/test/resources/{test-build-should-succeed/src/main/java/org/apache/maven/shared/invoker/App.java => test-build-should-timeout/src/main/java/org/apache/maven/shared/invoker/AppTimeout.java} (97%)
+ rename src/test/resources/test-build-should-timeout/src/test/java/org/apache/maven/shared/invoker/{AppTest.java => AppTimeoutTest.java} (97%)
+
+diff --git a/src/test/resources/test-build-should-succeed/src/test/java/org/apache/maven/shared/invoker/AppTest.java b/src/test/resources/test-build-should-succeed/src/test/java/org/apache/maven/shared/invoker/AppShouldSucceedTest.java
+similarity index 94%
+rename from src/test/resources/test-build-should-succeed/src/test/java/org/apache/maven/shared/invoker/AppTest.java
+rename to src/test/resources/test-build-should-succeed/src/test/java/org/apache/maven/shared/invoker/AppShouldSucceedTest.java
+index 785d70c..9b5bb0a 100644
+--- a/src/test/resources/test-build-should-succeed/src/test/java/org/apache/maven/shared/invoker/AppTest.java
++++ b/src/test/resources/test-build-should-succeed/src/test/java/org/apache/maven/shared/invoker/AppShouldSucceedTest.java
+@@ -26,7 +26,7 @@ import junit.framework.TestSuite;
+ /**
+  * Unit test for simple App.
+  */
+-public class AppTest 
++public class AppShouldSucceedTest
+     extends TestCase
+ {
+     /**
+@@ -34,7 +34,7 @@ public class AppTest
+      *
+      * @param testName name of the test case
+      */
+-    public AppTest( String testName )
++    public AppShouldSucceedTest( String testName )
+     {
+         super( testName );
+     }
+diff --git a/src/test/resources/test-build-should-timeout/src/main/java/org/apache/maven/shared/invoker/App.java b/src/test/resources/test-build-should-timeout/src/main/java/org/apache/maven/shared/invoker/App.java
+deleted file mode 100644
+index 753a51c..0000000
+--- a/src/test/resources/test-build-should-timeout/src/main/java/org/apache/maven/shared/invoker/App.java
++++ /dev/null
+@@ -1,32 +0,0 @@
+-package org.apache.maven.shared.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.
+- */
+-
+-/**
+- * Hello world!
+- *
+- */
+-public class App 
+-{
+-    public static void main( String[] args )
+-    {
+-        System.out.println( "Hello World!" );
+-    }
+-}
+diff --git a/src/test/resources/test-build-should-succeed/src/main/java/org/apache/maven/shared/invoker/App.java b/src/test/resources/test-build-should-timeout/src/main/java/org/apache/maven/shared/invoker/AppTimeout.java
+similarity index 97%
+rename from src/test/resources/test-build-should-succeed/src/main/java/org/apache/maven/shared/invoker/App.java
+rename to src/test/resources/test-build-should-timeout/src/main/java/org/apache/maven/shared/invoker/AppTimeout.java
+index 753a51c..d94b104 100644
+--- a/src/test/resources/test-build-should-succeed/src/main/java/org/apache/maven/shared/invoker/App.java
++++ b/src/test/resources/test-build-should-timeout/src/main/java/org/apache/maven/shared/invoker/AppTimeout.java
+@@ -23,7 +23,7 @@ package org.apache.maven.shared.invoker;
+  * Hello world!
+  *
+  */
+-public class App 
++public class AppTimeout
+ {
+     public static void main( String[] args )
+     {
+diff --git a/src/test/resources/test-build-should-timeout/src/test/java/org/apache/maven/shared/invoker/AppTest.java b/src/test/resources/test-build-should-timeout/src/test/java/org/apache/maven/shared/invoker/AppTimeoutTest.java
+similarity index 97%
+rename from src/test/resources/test-build-should-timeout/src/test/java/org/apache/maven/shared/invoker/AppTest.java
+rename to src/test/resources/test-build-should-timeout/src/test/java/org/apache/maven/shared/invoker/AppTimeoutTest.java
+index 27808ea..2bdeda4 100644
+--- a/src/test/resources/test-build-should-timeout/src/test/java/org/apache/maven/shared/invoker/AppTest.java
++++ b/src/test/resources/test-build-should-timeout/src/test/java/org/apache/maven/shared/invoker/AppTimeoutTest.java
+@@ -24,7 +24,7 @@ import org.junit.Test;
+ /**
+  * Unit test for simple App.
+  */
+-public class AppTest
++public class AppTimeoutTest
+ {
+ 
+     /**
+-- 
+2.25.1
+
-- 
2.25.1

