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