java-utils.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 Hartmut Goebel <h.goebel@crazy-compilers.com>
  3. ;;; Copyright © 2016 Ricardo Wurmus <rekado@elephly.net>
  4. ;;; Copyright © 2018 Alex Vong <alexvong1995@gmail.com>
  5. ;;; Copyright © 2020, 2021 Julien Lepiller <julien@lepiller.eu>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (guix build java-utils)
  22. #:use-module (guix build utils)
  23. #:use-module (guix build syscalls)
  24. #:use-module (guix build maven pom)
  25. #:use-module (guix build maven plugin)
  26. #:use-module (ice-9 match)
  27. #:use-module (sxml simple)
  28. #:export (ant-build-javadoc
  29. generate-plugin.xml
  30. generate-pom.xml
  31. install-jars
  32. install-javadoc
  33. install-pom-file
  34. install-from-pom))
  35. (define* (ant-build-javadoc #:key (target "javadoc") (make-flags '())
  36. #:allow-other-keys)
  37. (apply invoke `("ant" ,target ,@make-flags)))
  38. (define* (install-jars jar-directory)
  39. "Install jar files from JAR-DIRECTORY to the default target directory. This
  40. is used in case the build.xml does not include an install target."
  41. (lambda* (#:key outputs #:allow-other-keys)
  42. (let ((share (string-append (assoc-ref outputs "out")
  43. "/share/java")))
  44. (for-each (lambda (f) (install-file f share))
  45. (find-files jar-directory "\\.jar$"))
  46. #t)))
  47. (define* (install-javadoc apidoc-directory)
  48. "Install the APIDOC-DIRECTORY to the target directory. This is used to
  49. install javadocs when this is not done by the install target."
  50. (lambda* (#:key outputs #:allow-other-keys)
  51. (let* ((out (assoc-ref outputs "out"))
  52. (name-version (strip-store-file-name out))
  53. (docs (string-append (or (assoc-ref outputs "doc") out)
  54. "/share/doc/" name-version "/")))
  55. (mkdir-p docs)
  56. (copy-recursively apidoc-directory docs)
  57. #t)))
  58. (define* (install-pom-file pom-file)
  59. "Install a @file{.pom} file to a maven repository structure in @file{lib/m2}
  60. that respects the file's artifact ID and group ID. This requires the parent
  61. pom, if any, to be present in the inputs so some of this information can be
  62. fetched."
  63. (lambda* (#:key inputs outputs #:allow-other-keys)
  64. (let* ((out (assoc-ref outputs "out"))
  65. (java-inputs (append (map cdr inputs) (map cdr outputs)))
  66. (pom-content (get-pom pom-file))
  67. (version (pom-version pom-content))
  68. (artifact (pom-artifactid pom-content))
  69. (group (group->dir (pom-groupid pom-content)))
  70. (repository (string-append out "/lib/m2/" group "/" artifact "/"
  71. version "/"))
  72. (pom-name (string-append repository artifact "-" version ".pom")))
  73. (mkdir-p (dirname pom-name))
  74. (copy-file pom-file pom-name))
  75. #t))
  76. (define (install-jar-file-with-pom jar pom-file inputs)
  77. "Unpack the jar archive, add the pom file, and repack it. This is necessary
  78. to ensure that maven can find dependencies."
  79. (format #t "adding ~a to ~a\n" pom-file jar)
  80. (let* ((dir (mkdtemp! "jar-contents.XXXXXX"))
  81. (manifest (string-append dir "/META-INF/MANIFEST.MF"))
  82. (pom (get-pom pom-file))
  83. (artifact (pom-artifactid pom))
  84. (group (pom-groupid pom))
  85. (version (pom-version pom))
  86. (pom-dir (string-append "META-INF/maven/" group "/" artifact)))
  87. (mkdir-p (string-append dir "/" pom-dir))
  88. (copy-file pom-file (string-append dir "/" pom-dir "/pom.xml"))
  89. (with-directory-excursion dir
  90. (with-output-to-file (string-append pom-dir "/pom.properties")
  91. (lambda _
  92. (format #t "version=~a~%" version)
  93. (format #t "groupId=~a~%" group)
  94. (format #t "artifactId=~a~%" artifact)))
  95. (invoke "jar" "uf" jar (string-append pom-dir "/pom.xml")
  96. (string-append pom-dir "/pom.properties")))
  97. #t))
  98. (define* (install-from-pom pom-file)
  99. "Install a jar archive and its @var{pom-file} to a maven repository structure
  100. in @file{lib/m2}. This requires the parent pom file, if any, to be present in
  101. the inputs of the package being built. This phase looks either for a properly
  102. named jar file (@file{artifactID-version.jar}) or the single jar in the build
  103. directory. If there are more than one jar, and none is named appropriately,
  104. the phase fails."
  105. (lambda* (#:key inputs outputs jar-name #:allow-other-keys)
  106. (let* ((out (assoc-ref outputs "out"))
  107. (java-inputs (append (map cdr inputs) (map cdr outputs)))
  108. (pom-content (get-pom pom-file))
  109. (version (pom-version pom-content))
  110. (artifact (pom-artifactid pom-content))
  111. (group (group->dir (pom-groupid pom-content)))
  112. (repository (string-append out "/lib/m2/" group "/" artifact "/"
  113. version "/"))
  114. ;; We try to find the file that was built. If it was built from our
  115. ;; generated ant.xml file, it is name jar-name, otherwise it should
  116. ;; have the expected name for maven.
  117. (jars (find-files "." (or jar-name (string-append artifact "-"
  118. version ".jar"))))
  119. ;; Otherwise, we try to find any jar file.
  120. (jars (if (null? jars)
  121. (find-files "." "\\.jar$")
  122. jars))
  123. (jar-name (string-append repository artifact "-" version ".jar"))
  124. (pom-name (string-append repository artifact "-" version ".pom")))
  125. ;; Ensure we can override the file
  126. (chmod pom-file #o644)
  127. (fix-pom-dependencies pom-file java-inputs)
  128. (mkdir-p (dirname jar-name))
  129. (copy-file pom-file pom-name)
  130. ;; If there are too many jar files, we don't know which one to install, so
  131. ;; fail.
  132. (if (= (length jars) 1)
  133. (begin
  134. (copy-file (car jars) jar-name)
  135. (install-jar-file-with-pom jar-name pom-file java-inputs))
  136. (throw 'no-jars jars)))
  137. #t))
  138. (define (sxml-indent sxml)
  139. "Adds some indentation to @var{sxml}, an sxml value, to make reviewing easier
  140. after the value is written to an xml file."
  141. (define (sxml-indent-aux sxml lvl)
  142. (match sxml
  143. ((? string? str) str)
  144. ((tag ('@ attr ...) content ...)
  145. (cond
  146. ((null? content) sxml)
  147. ((string? (car content)) sxml)
  148. (else
  149. `(,tag (@ ,@attr) ,(sxml-indent-content content (+ lvl 1))))))
  150. ((tag content ...)
  151. (cond
  152. ((null? content) sxml)
  153. ((string? (car content)) sxml)
  154. (else `(,tag ,(sxml-indent-content content (+ lvl 1))))))
  155. (_ sxml)))
  156. (define (sxml-indent-content sxml lvl)
  157. (map
  158. (lambda (sxml)
  159. (list "\n" (string-join (make-list (* 2 lvl) " ") "")
  160. (sxml-indent-aux sxml lvl)))
  161. sxml))
  162. (sxml-indent-aux sxml 0))
  163. (define* (generate-plugin.xml pom-file goal-prefix directory source-groups
  164. #:key
  165. (plugin.xml "build/classes/META-INF/maven/plugin.xml"))
  166. "Generates the @file{plugin.xml} file that is required by Maven so it can
  167. recognize the package as a plugin, and find the entry points in the plugin."
  168. (lambda* (#:key inputs outputs #:allow-other-keys)
  169. (let* ((pom-content (get-pom pom-file))
  170. (java-inputs (append (map cdr inputs) (map cdr outputs)))
  171. (name (pom-name pom-content))
  172. (description (pom-description pom-content))
  173. (dependencies (pom-dependencies pom-content))
  174. (version (pom-version pom-content))
  175. (artifact (pom-artifactid pom-content))
  176. (groupid (pom-groupid pom-content))
  177. (mojos
  178. `(mojos
  179. ,@(with-directory-excursion directory
  180. (map
  181. (lambda (group)
  182. (apply generate-mojo-from-files maven-convert-type group))
  183. source-groups)))))
  184. (mkdir-p (dirname plugin.xml))
  185. (with-output-to-file plugin.xml
  186. (lambda _
  187. (sxml->xml
  188. (sxml-indent
  189. `(plugin
  190. (name ,name)
  191. (description ,description)
  192. (groupId ,groupid)
  193. (artifactId ,artifact)
  194. (version ,version)
  195. (goalPrefix ,goal-prefix)
  196. (isolatedRealm "false")
  197. (inheritedByDefault "true")
  198. ,mojos
  199. (dependencies
  200. ,@dependencies)))))))))
  201. (define* (generate-pom.xml pom-file groupid artifactid version
  202. #:key (dependencies '())
  203. (name artifactid))
  204. "Generates the @file{pom.xml} for a project. It is required by Maven to find
  205. a package, and by the java build system to know where to install a package, when
  206. a pom.xml doesn't already exist and installing to the maven repository."
  207. (lambda _
  208. (mkdir-p (dirname pom-file))
  209. (with-output-to-file pom-file
  210. (lambda _
  211. (sxml->xml
  212. (sxml-indent
  213. `(project
  214. (modelVersion "4.0.0")
  215. (name ,name)
  216. (groupId ,groupid)
  217. (artifactId ,artifactid)
  218. (version ,version)
  219. (dependencies
  220. ,@(map
  221. (match-lambda
  222. ((groupid artifactid version)
  223. `(dependency
  224. (groupId ,groupid)
  225. (artifactId ,artifactid)
  226. (version ,version))))
  227. dependencies)))))))))