maven-build-system.scm 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #t)
  98. (define* (build #:key outputs #:allow-other-keys)
  99. "Build the given package."
  100. (invoke "mvn" "package"
  101. ;; offline mode: don't download dependencies
  102. "-o"
  103. ;, set directory where dependencies are installed
  104. (string-append "-Duser.home=" (getenv "HOME")))
  105. #t)
  106. (define* (check #:key tests? #:allow-other-keys)
  107. "Check the given package."
  108. (when tests?
  109. (invoke "mvn" "test"
  110. (string-append "-Duser.home=" (getenv "HOME"))
  111. "-e"))
  112. #t)
  113. (define* (install #:key outputs #:allow-other-keys)
  114. "Install the given package."
  115. (let* ((out (assoc-ref outputs "out"))
  116. (java (string-append out "/lib/m2")))
  117. (invoke "mvn" "install" "-o" "-e"
  118. "-DskipTests"
  119. (string-append "-Duser.home=" (getenv "HOME")))
  120. ;; Go through the repository to find files that can be installed
  121. (with-directory-excursion (string-append (getenv "HOME") "/.m2/repository")
  122. (let ((installable
  123. (filter (lambda (file)
  124. (not (eq? 'symlink (stat:type (lstat file)))))
  125. (find-files "." "."))))
  126. (mkdir-p java)
  127. (for-each
  128. (lambda (file)
  129. (mkdir-p (string-append java "/" (dirname file)))
  130. (copy-file file (string-append java "/" file)))
  131. installable)))
  132. ;; Remove some files that are not required and introduce timestamps
  133. (for-each delete-file (find-files out "maven-metadata-local.xml"))
  134. (for-each delete-file (find-files out "_remote.repositories")))
  135. #t)
  136. (define %standard-phases
  137. ;; Everything is as with the GNU Build System except for the `configure'
  138. ;; , `build', `check' and `install' phases.
  139. (modify-phases gnu:%standard-phases
  140. (delete 'bootstrap)
  141. (add-before 'configure 'set-home set-home)
  142. (replace 'configure configure)
  143. (add-after 'configure 'fix-pom-files fix-pom-files)
  144. (replace 'build build)
  145. (replace 'check check)
  146. (replace 'install install)))
  147. (define* (maven-build #:key inputs (phases %standard-phases)
  148. #:allow-other-keys #:rest args)
  149. "Build the given package, applying all of PHASES in order."
  150. (apply gnu:gnu-build #:inputs inputs #:phases phases args))
  151. ;;; maven-build-system.scm ends here