maven-build-system.scm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020, 2021 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 (fix-pom pom-file inputs local-packages excludes)
  59. (chmod pom-file #o644)
  60. (format #t "fixing ~a~%" pom-file)
  61. (fix-pom-dependencies pom-file (map cdr inputs)
  62. #:with-plugins? #t #:with-build-dependencies? #t
  63. #:with-modules? #t
  64. #:local-packages local-packages
  65. #:excludes excludes))
  66. (define* (fix-pom-files #:key inputs local-packages exclude #:allow-other-keys)
  67. (let ((local-packages (pom-local-packages "pom.xml" #:local-packages local-packages)))
  68. (format (current-error-port) "Fix pom files with local packages: ~a~%" local-packages)
  69. (for-each
  70. (lambda (pom)
  71. (when (file-exists? pom)
  72. (fix-pom pom inputs local-packages exclude)))
  73. (pom-and-submodules "pom.xml")))
  74. #t)
  75. (define* (build #:key outputs #:allow-other-keys)
  76. "Build the given package."
  77. (invoke "mvn" "package"
  78. ;; offline mode: don't download dependencies
  79. "-o"
  80. ;, set directory where dependencies are installed
  81. (string-append "-Duser.home=" (getenv "HOME")))
  82. #t)
  83. (define* (check #:key tests? #:allow-other-keys)
  84. "Check the given package."
  85. (when tests?
  86. (invoke "mvn" "test"
  87. (string-append "-Duser.home=" (getenv "HOME"))
  88. "-e"))
  89. #t)
  90. (define* (install #:key outputs #:allow-other-keys)
  91. "Install the given package."
  92. (let* ((out (assoc-ref outputs "out"))
  93. (java (string-append out "/lib/m2")))
  94. (invoke "mvn" "install" "-o" "-e"
  95. "-DskipTests"
  96. (string-append "-Duser.home=" (getenv "HOME")))
  97. ;; Go through the repository to find files that can be installed
  98. (with-directory-excursion (string-append (getenv "HOME") "/.m2/repository")
  99. (let ((installable
  100. (filter (lambda (file)
  101. (not (eq? 'symlink (stat:type (lstat file)))))
  102. (find-files "." "."))))
  103. (mkdir-p java)
  104. (for-each
  105. (lambda (file)
  106. (mkdir-p (string-append java "/" (dirname file)))
  107. (copy-file file (string-append java "/" file)))
  108. installable)))
  109. ;; Remove some files that are not required and introduce timestamps
  110. (for-each delete-file (find-files out "maven-metadata-local.xml"))
  111. (for-each delete-file (find-files out "_remote.repositories")))
  112. #t)
  113. (define %standard-phases
  114. ;; Everything is as with the GNU Build System except for the `configure'
  115. ;; , `build', `check' and `install' phases.
  116. (modify-phases gnu:%standard-phases
  117. (delete 'bootstrap)
  118. (add-before 'configure 'set-home set-home)
  119. (replace 'configure configure)
  120. (add-after 'configure 'fix-pom-files fix-pom-files)
  121. (replace 'build build)
  122. (replace 'check check)
  123. (replace 'install install)))
  124. (define* (maven-build #:key inputs (phases %standard-phases)
  125. #:allow-other-keys #:rest args)
  126. "Build the given package, applying all of PHASES in order."
  127. (apply gnu:gnu-build #:inputs inputs #:phases phases args))
  128. ;;; maven-build-system.scm ends here