Mirror of GNU Guix
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
6.1 KiB

;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2020 Julien Lepiller <julien@lepiller.eu>
;;; 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
;;; 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 maven-build-system)
#:use-module ((guix build gnu-build-system) #:prefix gnu:)
#:use-module (guix build utils)
#:use-module (guix build maven pom)
#:use-module (ice-9 match)
#:export (%standard-phases
;; Commentary:
;; Builder-side code of the standard maven build procedure.
;; Code:
(define* (set-home #:key outputs inputs #:allow-other-keys)
(let ((home (string-append (getcwd) "/build-home")))
(setenv "HOME" home))
(setenv "JAVA_HOME" (assoc-ref inputs "jdk"))
(define* (configure #:key inputs #:allow-other-keys)
(let* ((m2-files (map
(lambda (input)
(match input
((name . dir)
(let ((m2-dir (string-append dir "/lib/m2")))
(if (file-exists? m2-dir) m2-dir #f)))))
(m2-files (filter (lambda (a) a) m2-files)))
(lambda (m2-dir)
(lambda (file)
(let ((dir (string-append (getenv "HOME") "/.m2/repository/"
(dirname file))))
(mkdir-p dir)
(symlink (string-append m2-dir "/" file)
(string-append dir "/" (basename file)))))
(with-directory-excursion m2-dir
(find-files "." ".*.(jar|pom)$"))))
(invoke "mvn" "-v")
(define (add-local-package local-packages group artifact version)
(define (alist-set lst key val)
(match lst
('() (list (cons key val)))
(((k . v) lst ...)
(if (equal? k key)
(cons (cons key val) lst)
(cons (cons k v) (alist-set lst key val))))))
(alist-set local-packages group
(alist-set (or (assoc-ref local-packages group) '()) artifact
(define (fix-pom pom-file inputs local-packages excludes)
(chmod pom-file #o644)
(format #t "fixing ~a~%" pom-file)
(fix-pom-dependencies pom-file (map cdr inputs)
#:with-plugins? #t #:with-build-dependencies? #t
#:local-packages local-packages
#:excludes excludes)
(let* ((pom (get-pom pom-file))
(java-inputs (map cdr inputs))
(artifact (pom-artifactid pom))
(group (pom-groupid pom java-inputs local-packages))
(version (pom-version pom java-inputs local-packages)))
(let loop ((modules (pom-ref pom "modules"))
(add-local-package local-packages group artifact version)))
(pk 'local-packages local-packages)
(match modules
(#f local-packages)
('() local-packages)
(((? string? _) modules ...)
(loop modules local-packages))
(((_ module) modules ...)
(fix-pom (string-append (dirname pom-file) "/" module "/pom.xml")
inputs local-packages excludes)))))))
(define* (fix-pom-files #:key inputs local-packages exclude #:allow-other-keys)
(fix-pom "pom.xml" inputs local-packages exclude))
(define* (build #:key outputs #:allow-other-keys)
"Build the given package."
(invoke "mvn" "package"
;; offline mode: don't download dependencies
;, set directory where dependencies are installed
(string-append "-Duser.home=" (getenv "HOME")))
(define* (check #:key tests? #:allow-other-keys)
"Check the given package."
(when tests?
(invoke "mvn" "test"
(string-append "-Duser.home=" (getenv "HOME"))
(define* (install #:key outputs #:allow-other-keys)
"Install the given package."
(let* ((out (assoc-ref outputs "out"))
(java (string-append out "/lib/m2")))
(invoke "mvn" "install" "-o" "-e"
(string-append "-Duser.home=" (getenv "HOME")))
;; Go through the repository to find files that can be installed
(with-directory-excursion (string-append (getenv "HOME") "/.m2/repository")
(let ((installable
(filter (lambda (file)
(not (eq? 'symlink (stat:type (lstat file)))))
(find-files "." "."))))
(mkdir-p java)
(lambda (file)
(mkdir-p (string-append java "/" (dirname file)))
(copy-file file (string-append java "/" file)))
;; Remove some files that are not required and introduce timestamps
(for-each delete-file (find-files out "maven-metadata-local.xml"))
(for-each delete-file (find-files out "_remote.repositories")))
(define %standard-phases
;; Everything is as with the GNU Build System except for the `configure'
;; , `build', `check' and `install' phases.
(modify-phases gnu:%standard-phases
(delete 'bootstrap)
(add-before 'configure 'set-home set-home)
(replace 'configure configure)
(add-after 'configure 'fix-pom-files fix-pom-files)
(replace 'build build)
(replace 'check check)
(replace 'install install)))
(define* (maven-build #:key inputs (phases %standard-phases)
#:allow-other-keys #:rest args)
"Build the given package, applying all of PHASES in order."
(apply gnu:gnu-build #:inputs inputs #:phases phases args))
;;; maven-build-system.scm ends here