java-utils.scm 10 KB

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