maven-build-system.scm 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020 Julien Lepiller <julien@lepiller.eu>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix build maven-build-system)
  19. #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  20. #:use-module (guix build utils)
  21. #:use-module (guix build maven pom)
  22. #:use-module (ice-9 match)
  23. #:export (%standard-phases
  24. maven-build))
  25. ;; Commentary:
  26. ;;
  27. ;; Builder-side code of the standard maven build procedure.
  28. ;;
  29. ;; Code:
  30. (define* (set-home #:key outputs inputs #:allow-other-keys)
  31. (let ((home (string-append (getcwd) "/build-home")))
  32. (setenv "HOME" home))
  33. (setenv "JAVA_HOME" (assoc-ref inputs "jdk"))
  34. #t)
  35. (define* (configure #:key inputs #:allow-other-keys)
  36. (let* ((m2-files (map
  37. (lambda (input)
  38. (match input
  39. ((name . dir)
  40. (let ((m2-dir (string-append dir "/lib/m2")))
  41. (if (file-exists? m2-dir) m2-dir #f)))))
  42. inputs))
  43. (m2-files (filter (lambda (a) a) m2-files)))
  44. (for-each
  45. (lambda (m2-dir)
  46. (for-each
  47. (lambda (file)
  48. (let ((dir (string-append (getenv "HOME") "/.m2/repository/"
  49. (dirname file))))
  50. (mkdir-p dir)
  51. (symlink (string-append m2-dir "/" file)
  52. (string-append dir "/" (basename file)))))
  53. (with-directory-excursion m2-dir
  54. (find-files "." ".*.(jar|pom)$"))))
  55. m2-files))
  56. (invoke "mvn" "-v")
  57. #t)
  58. (define (add-local-package local-packages group artifact version)
  59. (define (alist-set lst key val)
  60. (match lst
  61. ('() (list (cons key val)))
  62. (((k . v) lst ...)
  63. (if (equal? k key)
  64. (cons (cons key val) lst)
  65. (cons (cons k v) (alist-set lst key val))))))
  66. (alist-set local-packages group
  67. (alist-set (or (assoc-ref local-packages group) '()) artifact
  68. version)))
  69. (define (fix-pom pom-file inputs local-packages excludes)
  70. (chmod pom-file #o644)
  71. (format #t "fixing ~a~%" pom-file)
  72. (fix-pom-dependencies pom-file (map cdr inputs)
  73. #:with-plugins? #t #:with-build-dependencies? #t
  74. #:local-packages local-packages
  75. #:excludes excludes)
  76. (let* ((pom (get-pom pom-file))
  77. (java-inputs (map cdr inputs))
  78. (artifact (pom-artifactid pom))
  79. (group (pom-groupid pom java-inputs local-packages))
  80. (version (pom-version pom java-inputs local-packages)))
  81. (let loop ((modules (pom-ref pom "modules"))
  82. (local-packages
  83. (add-local-package local-packages group artifact version)))
  84. (pk 'local-packages local-packages)
  85. (match modules
  86. (#f local-packages)
  87. ('() local-packages)
  88. (((? string? _) modules ...)
  89. (loop modules local-packages))
  90. (((_ module) modules ...)
  91. (loop
  92. modules
  93. (fix-pom (string-append (dirname pom-file) "/" module "/pom.xml")
  94. inputs local-packages excludes)))))))
  95. (define* (fix-pom-files #:key inputs local-packages exclude #:allow-other-keys)
  96. (fix-pom "pom.xml" inputs local-packages exclude))
  97. (define* (build #:key outputs #:allow-other-keys)
  98. "Build the given package."
  99. (invoke "mvn" "package"
  100. ;; offline mode: don't download dependencies
  101. "-o"
  102. ;, set directory where dependencies are installed
  103. (string-append "-Duser.home=" (getenv "HOME")))
  104. #t)
  105. (define* (check #:key tests? #:allow-other-keys)
  106. "Check the given package."
  107. (when tests?
  108. (invoke "mvn" "test"
  109. (string-append "-Duser.home=" (getenv "HOME"))
  110. "-e"))
  111. #t)
  112. (define* (install #:key outputs #:allow-other-keys)
  113. "Install the given package."
  114. (let* ((out (assoc-ref outputs "out"))
  115. (java (string-append out "/lib/m2")))
  116. (invoke "mvn" "install" "-o" "-e"
  117. "-DskipTests"
  118. (string-append "-Duser.home=" (getenv "HOME")))
  119. ;; Go through the repository to find files that can be installed
  120. (with-directory-excursion (string-append (getenv "HOME") "/.m2/repository")
  121. (let ((installable
  122. (filter (lambda (file)
  123. (not (eq? 'symlink (stat:type (lstat file)))))
  124. (find-files "." "."))))
  125. (mkdir-p java)
  126. (for-each
  127. (lambda (file)
  128. (mkdir-p (string-append java "/" (dirname file)))
  129. (copy-file file (string-append java "/" file)))
  130. installable)))
  131. ;; Remove some files that are not required and introduce timestamps
  132. (for-each delete-file (find-files out "maven-metadata-local.xml"))
  133. (for-each delete-file (find-files out "_remote.repositories")))
  134. #t)
  135. (define %standard-phases
  136. ;; Everything is as with the GNU Build System except for the `configure'
  137. ;; , `build', `check' and `install' phases.
  138. (modify-phases gnu:%standard-phases
  139. (delete 'bootstrap)
  140. (add-before 'configure 'set-home set-home)
  141. (replace 'configure configure)
  142. (add-after 'configure 'fix-pom-files fix-pom-files)
  143. (replace 'build build)
  144. (replace 'check check)
  145. (replace 'install install)))
  146. (define* (maven-build #:key inputs (phases %standard-phases)
  147. #:allow-other-keys #:rest args)
  148. "Build the given package, applying all of PHASES in order."
  149. (apply gnu:gnu-build #:inputs inputs #:phases phases args))
  150. ;;; maven-build-system.scm ends here