Subject: [bug#48766] [PATCH 01/14] guix: java-utils: Factorize pom.xml generation.

From: Julien Lepiller <[email-masked]>
Date: Tue, 1 Jun 2021 00:44:14 +0200

* guix/build/java-utils.scm (generate-pom.xml): New procedure.
* gnu/packages/maven.scm (java-surefire-junit4): Use it.
* gnu/packages/java.scm (java-qdox, java-jsr250, java-jsr305)
(java-aopalliance, java-jboss-el-api-spec)
(java-jboss-interceptors-api-spec): Use it.
(java-qdox-M9): Ensure the generated pom file has the correct version.
---
 gnu/packages/java.scm     | 117 ++++++--------------------------------
 gnu/packages/maven.scm    |  28 ++++-----
 guix/build/java-utils.scm |  29 ++++++++++
 3 files changed, 58 insertions(+), 116 deletions(-)

diff --git a/gnu/packages/java.scm b/gnu/packages/java.scm
index a167aafc38..d7118b4ce6 100644
--- a/gnu/packages/java.scm
+++ b/gnu/packages/java.scm
@@ -3781,25 +3781,11 @@ documentation tools.")
     (arguments
      `(#:jar-name "qdox.jar"
        #:tests? #f; no tests
-       #:modules
-       ((guix build ant-build-system)
-        (guix build java-utils)
-        (guix build utils)
-        (sxml simple))
        #:phases
        (modify-phases %standard-phases
          (add-before 'install 'create-pom
-           (lambda _
-             (with-output-to-file "pom.xml"
-               (lambda _
-                 (sxml->xml
-                   `((project
-                       (modelVersion "4.0.0")
-                       (name "QDox")
-                       (groupId "com.thoughtworks.qdox")
-                       (artifactId "qdox")
-                       (version ,,version))))))
-             #t))
+           (generate-pom.xml "pom.xml" "com.thoughtworks.qdox" "qdox" ,version
+                             #:name "QDox"))
          (replace 'install
            (install-from-pom "pom.xml")))))
     (home-page "https://github.com/codehaus/qdox")
@@ -3825,7 +3811,14 @@ documentation tools.")
                                   "/qdox-" version "-sources.jar"))
               (sha256
                (base32
-                "1s2jnmx2dkwnaha12lcj26aynywgwa8sslc47z82wx8xai13y4fg"))))))
+                "1s2jnmx2dkwnaha12lcj26aynywgwa8sslc47z82wx8xai13y4fg"))))
+    (arguments
+     (substitute-keyword-arguments (package-arguments java-qdox)
+       ((#:phases phases)
+        `(modify-phases ,phases
+           (replace 'create-pom
+             (generate-pom.xml "pom.xml" "com.thoughtworks.qdox" "qdox" ,version
+                               #:name "QDox"))))))))
 
 (define-public java-jarjar
   (package
@@ -6397,25 +6390,11 @@ bottlenecks move away from the database in an effectively cached system.")
      `(#:tests? #f ; no tests included
        #:jdk ,icedtea-8
        #:jar-name "jsr250.jar"
-       #:modules ((guix build ant-build-system)
-                  (guix build utils)
-                  (guix build maven pom)
-                  (guix build java-utils)
-                  (sxml simple))
        #:phases
        (modify-phases %standard-phases
          (add-before 'install 'create-pom
-           (lambda _
-             (with-output-to-file "pom.xml"
-               (lambda _
-                 (sxml->xml
-                   `((project
-                       (modelVersion "4.0.0")
-                       (name "jsr250")
-                       (groupId "javax.annotation")
-                       (artifactId "jsr250-api")
-                       (version ,,version))))))
-             #t))
+           (generate-pom.xml "pom.xml" "javax.annotation" "jsr250-api" ,version
+                             #:name "jsr250"))
          (replace 'install
            (install-from-pom "pom.xml")))))
     (home-page "https://jcp.org/en/jsr/detail?id=250")
@@ -6444,25 +6423,10 @@ namespaces.")
     (arguments
      `(#:tests? #f ; no tests included
        #:jar-name "jsr305.jar"
-       #:modules ((guix build ant-build-system)
-                  (guix build java-utils)
-                  (guix build maven pom)
-                  (guix build utils)
-                  (sxml simple))
        #:phases
        (modify-phases %standard-phases
          (add-before 'install 'create-pom
-           (lambda _
-             (with-output-to-file "pom.xml"
-               (lambda _
-                 (sxml->xml
-                   `((project
-                       (modelVersion "4.0.0")
-                       (name "jsr305")
-                       (groupId "com.google.code.findbugs")
-                       (artifactId "jsr305")
-                       (version ,,version))))))
-             #t))
+           (generate-pom.xml "pom.xml" "com.google.code.findbugs" "jsr305" ,version))
          (replace 'install
            (install-from-pom "pom.xml")))))
     (home-page "http://findbugs.sourceforge.net/")
@@ -10493,25 +10457,10 @@ this is not a static analysis tool.)")
        #:jdk ,icedtea-8
        #:tests? #f; no tests
        #:source-dir "aopalliance/src/main"
-       #:modules ((guix build ant-build-system)
-                  (guix build utils)
-                  (guix build maven pom)
-                  (guix build java-utils)
-                  (sxml simple))
        #:phases
        (modify-phases %standard-phases
          (add-before 'install 'create-pom
-           (lambda _
-             (with-output-to-file "pom.xml"
-               (lambda _
-                 (sxml->xml
-                   `((project
-                       (modelVersion "4.0.0")
-                       (name "aopalliance")
-                       (groupId "aopalliance")
-                       (artifactId "aopalliance")
-                       (version "1.0"))))))
-             #t))
+           (generate-pom.xml "pom.xml" "aopalliance" "aopalliance" ,version))
          (replace 'install
            (install-from-pom "pom.xml")))))
     (home-page "http://aopalliance.sourceforge.net")
@@ -12110,29 +12059,14 @@ sequences to format your console output which works on every platform.")
     (build-system ant-build-system)
     (arguments
      `(#:jar-name "java-jboss-el-api_spec.jar"
-       #:modules ((guix build ant-build-system)
-                  (guix build utils)
-                  (guix build maven pom)
-                  (guix build java-utils)
-                  (sxml simple))
        #:phases
        (modify-phases %standard-phases
          ;; the origin of javax.el:javax.el-api is unknown, so we use this package
          ;; instead, which implements the same thing.  We override the pom file
          ;; to "rename" the package so it can be found by maven.
          (add-before 'install 'override-pom
-           (lambda _
-             (delete-file "pom.xml")
-             (with-output-to-file "pom.xml"
-               (lambda _
-                 (sxml->xml
-                   `(project
-                      (modelVersion "4.0.0")
-                      (name "el-api")
-                      (groupId "javax.el")
-                      (artifactId "javax.el-api")
-                      (version "3.0")))))
-             #t))
+           (generate-pom.xml "pom.xml" "javax.el" "javax.el-api" "3.0"
+                             #:name "el-api"))
          (replace 'install
            (install-from-pom "pom.xml")))))
     (inputs
@@ -12164,11 +12098,6 @@ JavaServer Pages (JSP).")
        #:jdk ,icedtea-8
        #:source-dir "."
        #:tests? #f; no tests
-       #:modules ((guix build ant-build-system)
-                  (guix build utils)
-                  (guix build maven pom)
-                  (guix build java-utils)
-                  (sxml simple))
        #:phases
        (modify-phases %standard-phases
          ;; the origin of javax.interceptor:javax.interceptor-api is unknown,
@@ -12176,18 +12105,8 @@ JavaServer Pages (JSP).")
          ;; We override the pom file to "rename" the package so it can be found
          ;; by maven.
          (add-before 'install 'override-pom
-           (lambda _
-             (delete-file "pom.xml")
-             (with-output-to-file "pom.xml"
-               (lambda _
-                 (sxml->xml
-                   `(project
-                      (modelVersion "4.0.0")
-                      (name "interceptor-api")
-                      (groupId "javax.interceptor")
-                      (artifactId "javax.interceptor-api")
-                      (version "3.0")))))
-             #t))
+           (generate-pom.xml "pom.xml" "javax.interceptor" "javax.interceptor-api"
+                             "3.0" #:name "interceptor-api"))
          (replace 'install
            (install-from-pom "pom.xml")))))
     (home-page "https://github.com/jboss/jboss-interceptors-api_spec")
diff --git a/gnu/packages/maven.scm b/gnu/packages/maven.scm
index c637e5c7ae..8f63ee9be2 100644
--- a/gnu/packages/maven.scm
+++ b/gnu/packages/maven.scm
@@ -3424,23 +3424,17 @@ starting from JUnit 4.")))
        #:phases
        (modify-phases %standard-phases
          (add-before 'install 'regenerate-own-pom
-           (lambda _
-             ;; Surefire struggles resolving artifacts because of this pom
-             ;; file, resulting in a NullPointerException when collecting
-             ;; Artifacts (and a "Failure detected." message from
-             ;; DefaultArtifactResolver).  Replace the pom file with a much
-             ;; simpler one.  Everything is shaded anyway (as used to be the
-             ;; case in 2.22), so there will not be missing dependencies.
-             (with-output-to-file "surefire-providers/surefire-junit4/pom.xml"
-               (lambda _
-                 (sxml->xml
-                   `((project
-                       (modelVersion "4.0.0")
-                       (name "Surefire JUnit4")
-                       (groupId "org.apache.maven.surefire")
-                       (artifactId "surefire-junit4")
-                       (version ,,(package-version java-surefire-common-java5)))))))
-             #t))
+           ;; Surefire struggles resolving artifacts because of this pom
+           ;; file, resulting in a NullPointerException when collecting
+           ;; Artifacts (and a "Failure detected." message from
+           ;; DefaultArtifactResolver).  Replace the pom file with a much
+           ;; simpler one.  Everything is shaded anyway (as used to be the
+           ;; case in 2.22), so there will not be missing dependencies.
+           (generate-pom.xml
+             "surefire-providers/surefire-junit4/pom.xml"
+             "org.apache.maven.surefire" "surefire-junit4"
+             ,(package-version java-surefire-common-java5)
+             #:name "Surefire JUnit4"))
          (add-before 'build 'copy-resources
            (lambda _
              (mkdir-p "build/classes")
diff --git a/guix/build/java-utils.scm b/guix/build/java-utils.scm
index a868e4d52c..6abd97e118 100644
--- a/guix/build/java-utils.scm
+++ b/guix/build/java-utils.scm
@@ -28,6 +28,7 @@
   #:use-module (sxml simple)
   #:export (ant-build-javadoc
             generate-plugin.xml
+            generate-pom.xml
             install-jars
             install-javadoc
             install-pom-file
@@ -206,3 +207,31 @@ recognize the package as a plugin, and find the entry points in the plugin."
                  ,mojos
                  (dependencies
                   ,@dependencies)))))))))
+
+(define* (generate-pom.xml pom-file groupid artifactid version
+                           #:key (dependencies '())
+                                 (name artifactid))
+  "Generates the @file{pom.xml} for a project. It is required by Maven to find
+a package, and by the java build system to know where to install a package, when
+a pom.xml doesn't already exist and installing to the maven repository."
+  (lambda _
+    (mkdir-p (dirname pom-file))
+    (with-output-to-file pom-file
+      (lambda _
+        (sxml->xml
+          (sxml-indent
+            `(project
+               (modelVersion "4.0.0")
+               (name ,name)
+               (groupId ,groupid)
+               (artifactId ,artifactid)
+               (version ,version)
+               (dependencies
+                ,@(map
+                    (match-lambda
+                      ((groupid artifactid version)
+                      `(dependency
+                         (groupId ,groupid)
+                         (artifactId ,artifactid)
+                         (version ,version))))
+                    dependencies)))))))))
-- 
2.31.1