2016-09-09 10:51:03 -04:00
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
|
|
|
;;; Copyright © 2016 Hartmut Goebel <h.goebel@crazy-compilers.com>
|
|
|
|
;;; Copyright © 2016 Ricardo Wurmus <rekado@elephly.net>
|
2018-10-17 15:08:31 -04:00
|
|
|
;;; Copyright © 2018 Alex Vong <alexvong1995@gmail.com>
|
2021-05-31 18:44:15 -04:00
|
|
|
;;; Copyright © 2020, 2021 Julien Lepiller <julien@lepiller.eu>
|
2016-09-09 10:51:03 -04:00
|
|
|
;;;
|
|
|
|
;;; This file is part of GNU Guix.
|
|
|
|
;;;
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
;;; your option) any later version.
|
|
|
|
;;;
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
(define-module (guix build java-utils)
|
|
|
|
#:use-module (guix build utils)
|
2020-04-05 13:54:29 -04:00
|
|
|
#:use-module (guix build syscalls)
|
|
|
|
#:use-module (guix build maven pom)
|
|
|
|
#:use-module (guix build maven plugin)
|
|
|
|
#:use-module (ice-9 match)
|
|
|
|
#:use-module (sxml simple)
|
2016-09-09 10:51:03 -04:00
|
|
|
#:export (ant-build-javadoc
|
2020-04-05 13:54:29 -04:00
|
|
|
generate-plugin.xml
|
2021-05-31 18:44:14 -04:00
|
|
|
generate-pom.xml
|
2016-09-09 10:51:03 -04:00
|
|
|
install-jars
|
2020-04-05 13:54:29 -04:00
|
|
|
install-javadoc
|
|
|
|
install-pom-file
|
|
|
|
install-from-pom))
|
2016-09-09 10:51:03 -04:00
|
|
|
|
|
|
|
(define* (ant-build-javadoc #:key (target "javadoc") (make-flags '())
|
|
|
|
#:allow-other-keys)
|
gnu: java: Return #t from all phases and snippets.
* guix/build/java-utils.scm (ant-build-javadoc): Use invoke; return #t.
* guix/build/ant-build-system.scm (generate-jar-indices): Return #t and
remove vestigal plumbing.
* gnu/packages/java.scm (classpath-bootstrap, ant-bootstrap, classpath-devel)
(icedtea-6, icedtea-7, java-plexus-sec-dispatcher, ant/java8, clojure)
(java-classpathx-servletapi, java-swt, java-qdox-1.12, java-hamcrest-core)
(java-plexus-archiver, java-plexus-sec-dispatcher, java-modello-plugins-xml)
(java-asm, java-commons-collections, java-commons-bsf, java-slf4j-api)
(java-slf4j-api, java-slf4j-simple, java-stringtemplate-3)
(java-stringtemplate, antlr3, antlr3-3.3, antlr3-3.1, java-ops4j-base-lang)
(java-ops4j-pax-tinybundles, java-ops4j-pax-exam-core-spi)
(java-fasterxml-jackson-core, java-fasterxml-jackson-databind)
(java-fasterxml-jackson-modules-base-jaxb, java-ecj-3, java-ecj-3.5)
(java-fasterxml-jackson-dataformat-yaml, java-woodstox-core)
(java-fasterxml-jackson-dataformat-xml, java-testng, java-jnacl)
(java-bouncycastle, java-powermock-core, java-powermock-modules-junit4)
(java-jansi-native, java-jansi, java-commons-httpclient, java-commons-vfs)
(java-apache-ivy, java-janino, java-logback-core): Return #t from all phases
and snippets, use invoke where appropriate, and remove vestigial plumbing.
2018-06-28 03:52:08 -04:00
|
|
|
(apply invoke `("ant" ,target ,@make-flags)))
|
2016-09-09 10:51:03 -04:00
|
|
|
|
|
|
|
(define* (install-jars jar-directory)
|
|
|
|
"Install jar files from JAR-DIRECTORY to the default target directory. This
|
|
|
|
is used in case the build.xml does not include an install target."
|
|
|
|
(lambda* (#:key outputs #:allow-other-keys)
|
|
|
|
(let ((share (string-append (assoc-ref outputs "out")
|
|
|
|
"/share/java")))
|
|
|
|
(for-each (lambda (f) (install-file f share))
|
|
|
|
(find-files jar-directory "\\.jar$"))
|
|
|
|
#t)))
|
|
|
|
|
|
|
|
(define* (install-javadoc apidoc-directory)
|
|
|
|
"Install the APIDOC-DIRECTORY to the target directory. This is used to
|
|
|
|
install javadocs when this is not done by the install target."
|
|
|
|
(lambda* (#:key outputs #:allow-other-keys)
|
|
|
|
(let* ((out (assoc-ref outputs "out"))
|
2018-10-17 15:08:31 -04:00
|
|
|
(name-version (strip-store-file-name out))
|
2016-09-09 10:51:03 -04:00
|
|
|
(docs (string-append (or (assoc-ref outputs "doc") out)
|
2018-10-17 15:08:31 -04:00
|
|
|
"/share/doc/" name-version "/")))
|
2016-09-09 10:51:03 -04:00
|
|
|
(mkdir-p docs)
|
|
|
|
(copy-recursively apidoc-directory docs)
|
|
|
|
#t)))
|
2020-04-05 13:54:29 -04:00
|
|
|
|
|
|
|
(define* (install-pom-file pom-file)
|
|
|
|
"Install a @file{.pom} file to a maven repository structure in @file{lib/m2}
|
|
|
|
that respects the file's artifact ID and group ID. This requires the parent
|
|
|
|
pom, if any, to be present in the inputs so some of this information can be
|
|
|
|
fetched."
|
|
|
|
(lambda* (#:key inputs outputs #:allow-other-keys)
|
|
|
|
(let* ((out (assoc-ref outputs "out"))
|
|
|
|
(java-inputs (append (map cdr inputs) (map cdr outputs)))
|
|
|
|
(pom-content (get-pom pom-file))
|
2021-05-31 18:44:15 -04:00
|
|
|
(version (pom-version pom-content))
|
2020-04-05 13:54:29 -04:00
|
|
|
(artifact (pom-artifactid pom-content))
|
2021-05-31 18:44:15 -04:00
|
|
|
(group (group->dir (pom-groupid pom-content)))
|
2020-04-05 13:54:29 -04:00
|
|
|
(repository (string-append out "/lib/m2/" group "/" artifact "/"
|
|
|
|
version "/"))
|
|
|
|
(pom-name (string-append repository artifact "-" version ".pom")))
|
|
|
|
(mkdir-p (dirname pom-name))
|
|
|
|
(copy-file pom-file pom-name))
|
|
|
|
#t))
|
|
|
|
|
|
|
|
(define (install-jar-file-with-pom jar pom-file inputs)
|
|
|
|
"Unpack the jar archive, add the pom file, and repack it. This is necessary
|
|
|
|
to ensure that maven can find dependencies."
|
|
|
|
(format #t "adding ~a to ~a\n" pom-file jar)
|
|
|
|
(let* ((dir (mkdtemp! "jar-contents.XXXXXX"))
|
|
|
|
(manifest (string-append dir "/META-INF/MANIFEST.MF"))
|
|
|
|
(pom (get-pom pom-file))
|
|
|
|
(artifact (pom-artifactid pom))
|
2021-05-31 18:44:15 -04:00
|
|
|
(group (pom-groupid pom))
|
|
|
|
(version (pom-version pom))
|
2020-04-05 13:54:29 -04:00
|
|
|
(pom-dir (string-append "META-INF/maven/" group "/" artifact)))
|
|
|
|
(mkdir-p (string-append dir "/" pom-dir))
|
|
|
|
(copy-file pom-file (string-append dir "/" pom-dir "/pom.xml"))
|
|
|
|
(with-directory-excursion dir
|
|
|
|
(with-output-to-file (string-append pom-dir "/pom.properties")
|
|
|
|
(lambda _
|
|
|
|
(format #t "version=~a~%" version)
|
|
|
|
(format #t "groupId=~a~%" group)
|
|
|
|
(format #t "artifactId=~a~%" artifact)))
|
|
|
|
(invoke "jar" "uf" jar (string-append pom-dir "/pom.xml")
|
|
|
|
(string-append pom-dir "/pom.properties")))
|
|
|
|
#t))
|
|
|
|
|
|
|
|
(define* (install-from-pom pom-file)
|
|
|
|
"Install a jar archive and its @var{pom-file} to a maven repository structure
|
|
|
|
in @file{lib/m2}. This requires the parent pom file, if any, to be present in
|
|
|
|
the inputs of the package being built. This phase looks either for a properly
|
|
|
|
named jar file (@file{artifactID-version.jar}) or the single jar in the build
|
|
|
|
directory. If there are more than one jar, and none is named appropriately,
|
|
|
|
the phase fails."
|
|
|
|
(lambda* (#:key inputs outputs jar-name #:allow-other-keys)
|
|
|
|
(let* ((out (assoc-ref outputs "out"))
|
|
|
|
(java-inputs (append (map cdr inputs) (map cdr outputs)))
|
|
|
|
(pom-content (get-pom pom-file))
|
2021-05-31 18:44:15 -04:00
|
|
|
(version (pom-version pom-content))
|
2020-04-05 13:54:29 -04:00
|
|
|
(artifact (pom-artifactid pom-content))
|
2021-05-31 18:44:15 -04:00
|
|
|
(group (group->dir (pom-groupid pom-content)))
|
2020-04-05 13:54:29 -04:00
|
|
|
(repository (string-append out "/lib/m2/" group "/" artifact "/"
|
|
|
|
version "/"))
|
|
|
|
;; We try to find the file that was built. If it was built from our
|
|
|
|
;; generated ant.xml file, it is name jar-name, otherwise it should
|
|
|
|
;; have the expected name for maven.
|
|
|
|
(jars (find-files "." (or jar-name (string-append artifact "-"
|
|
|
|
version ".jar"))))
|
|
|
|
;; Otherwise, we try to find any jar file.
|
|
|
|
(jars (if (null? jars)
|
2021-05-31 18:44:21 -04:00
|
|
|
(find-files "." "\\.jar$")
|
2020-04-05 13:54:29 -04:00
|
|
|
jars))
|
|
|
|
(jar-name (string-append repository artifact "-" version ".jar"))
|
|
|
|
(pom-name (string-append repository artifact "-" version ".pom")))
|
|
|
|
;; Ensure we can override the file
|
|
|
|
(chmod pom-file #o644)
|
|
|
|
(fix-pom-dependencies pom-file java-inputs)
|
|
|
|
(mkdir-p (dirname jar-name))
|
|
|
|
(copy-file pom-file pom-name)
|
|
|
|
;; If there are too many jar files, we don't know which one to install, so
|
|
|
|
;; fail.
|
|
|
|
(if (= (length jars) 1)
|
|
|
|
(begin
|
|
|
|
(copy-file (car jars) jar-name)
|
|
|
|
(install-jar-file-with-pom jar-name pom-file java-inputs))
|
|
|
|
(throw 'no-jars jars)))
|
|
|
|
#t))
|
|
|
|
|
|
|
|
(define (sxml-indent sxml)
|
|
|
|
"Adds some indentation to @var{sxml}, an sxml value, to make reviewing easier
|
|
|
|
after the value is written to an xml file."
|
|
|
|
(define (sxml-indent-aux sxml lvl)
|
|
|
|
(match sxml
|
|
|
|
((? string? str) str)
|
|
|
|
((tag ('@ attr ...) content ...)
|
|
|
|
(cond
|
|
|
|
((null? content) sxml)
|
|
|
|
((string? (car content)) sxml)
|
|
|
|
(else
|
|
|
|
`(,tag (@ ,@attr) ,(sxml-indent-content content (+ lvl 1))))))
|
|
|
|
((tag content ...)
|
|
|
|
(cond
|
|
|
|
((null? content) sxml)
|
|
|
|
((string? (car content)) sxml)
|
|
|
|
(else `(,tag ,(sxml-indent-content content (+ lvl 1))))))
|
|
|
|
(_ sxml)))
|
|
|
|
(define (sxml-indent-content sxml lvl)
|
|
|
|
(map
|
|
|
|
(lambda (sxml)
|
|
|
|
(list "\n" (string-join (make-list (* 2 lvl) " ") "")
|
|
|
|
(sxml-indent-aux sxml lvl)))
|
|
|
|
sxml))
|
|
|
|
(sxml-indent-aux sxml 0))
|
|
|
|
|
|
|
|
(define* (generate-plugin.xml pom-file goal-prefix directory source-groups
|
|
|
|
#:key
|
|
|
|
(plugin.xml "build/classes/META-INF/maven/plugin.xml"))
|
|
|
|
"Generates the @file{plugin.xml} file that is required by Maven so it can
|
|
|
|
recognize the package as a plugin, and find the entry points in the plugin."
|
|
|
|
(lambda* (#:key inputs outputs #:allow-other-keys)
|
|
|
|
(let* ((pom-content (get-pom pom-file))
|
|
|
|
(java-inputs (append (map cdr inputs) (map cdr outputs)))
|
|
|
|
(name (pom-name pom-content))
|
|
|
|
(description (pom-description pom-content))
|
|
|
|
(dependencies (pom-dependencies pom-content))
|
2021-05-31 18:44:15 -04:00
|
|
|
(version (pom-version pom-content))
|
2020-04-05 13:54:29 -04:00
|
|
|
(artifact (pom-artifactid pom-content))
|
2021-05-31 18:44:15 -04:00
|
|
|
(groupid (pom-groupid pom-content))
|
2020-04-05 13:54:29 -04:00
|
|
|
(mojos
|
|
|
|
`(mojos
|
|
|
|
,@(with-directory-excursion directory
|
|
|
|
(map
|
|
|
|
(lambda (group)
|
|
|
|
(apply generate-mojo-from-files maven-convert-type group))
|
|
|
|
source-groups)))))
|
|
|
|
(mkdir-p (dirname plugin.xml))
|
|
|
|
(with-output-to-file plugin.xml
|
|
|
|
(lambda _
|
|
|
|
(sxml->xml
|
|
|
|
(sxml-indent
|
|
|
|
`(plugin
|
|
|
|
(name ,name)
|
|
|
|
(description ,description)
|
|
|
|
(groupId ,groupid)
|
|
|
|
(artifactId ,artifact)
|
|
|
|
(version ,version)
|
|
|
|
(goalPrefix ,goal-prefix)
|
|
|
|
(isolatedRealm "false")
|
|
|
|
(inheritedByDefault "true")
|
|
|
|
,mojos
|
|
|
|
(dependencies
|
|
|
|
,@dependencies)))))))))
|
2021-05-31 18:44:14 -04:00
|
|
|
|
|
|
|
(define* (generate-pom.xml pom-file groupid artifactid version
|
|
|
|
#:key (dependencies '())
|
|
|
|
(name artifactid))
|
|
|
|
"Generates the @file{pom.xml} for a project. It is required by Maven to find
|
|
|
|
a package, and by the java build system to know where to install a package, when
|
|
|
|
a pom.xml doesn't already exist and installing to the maven repository."
|
|
|
|
(lambda _
|
|
|
|
(mkdir-p (dirname pom-file))
|
|
|
|
(with-output-to-file pom-file
|
|
|
|
(lambda _
|
|
|
|
(sxml->xml
|
|
|
|
(sxml-indent
|
|
|
|
`(project
|
|
|
|
(modelVersion "4.0.0")
|
|
|
|
(name ,name)
|
|
|
|
(groupId ,groupid)
|
|
|
|
(artifactId ,artifactid)
|
|
|
|
(version ,version)
|
|
|
|
(dependencies
|
|
|
|
,@(map
|
|
|
|
(match-lambda
|
|
|
|
((groupid artifactid version)
|
|
|
|
`(dependency
|
|
|
|
(groupId ,groupid)
|
|
|
|
(artifactId ,artifactid)
|
|
|
|
(version ,version))))
|
|
|
|
dependencies)))))))))
|