# maven patches from roptat -rw-r--r-- 30.1 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
From 40e64c504a01b0092142204854e47baa0ff7d211 Mon Sep 17 00:00:00 2001
From: Julien Lepiller <julien@lepiller.eu>
Date: Fri, 24 Dec 2021 11:39:11 +0000
Subject: [PATCH] Guix Packaging Meetup December 18 Maven Patch Set

---
 Makefile.am                       |   1 +
 gnu/packages/java.scm             |  75 +++++
 gnu/packages/maven-parent-pom.scm |  29 ++
 guix/build-system/maven.scm       |   4 +-
 guix/build/maven-build-system.scm |  10 +-
 guix/build/maven/pom2.scm         | 515 ++++++++++++++++++++++++++++++
 6 files changed, 627 insertions(+), 7 deletions(-)
 create mode 100644 guix/build/maven/pom2.scm

diff --git a/Makefile.am b/Makefile.am
index c4ccee65f1..3ffc45ab8f 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -243,6 +243,7 @@ MODULES =					\
   guix/build/maven/java.scm			\
   guix/build/maven/plugin.scm			\
   guix/build/maven/pom.scm			\
+  guix/build/maven/pom2.scm			\
   guix/build/graft.scm				\
   guix/build/bournish.scm			\
   guix/build/qt-utils.scm			\
diff --git a/gnu/packages/java.scm b/gnu/packages/java.scm
index 6332b58b98..945ee63ad7 100644
--- a/gnu/packages/java.scm
+++ b/gnu/packages/java.scm
@@ -84,6 +84,7 @@ (define-module (gnu packages java)
   #:use-module (gnu packages pkg-config)
   #:use-module (gnu packages perl)
   #:use-module (gnu packages popt)
+  #:use-module (gnu packages python)
   #:use-module (gnu packages kerberos)
   #:use-module (gnu packages xml)
   #:use-module (gnu packages xorg)
@@ -14294,3 +14295,77 @@ (define (input-jar input)
 @item The PlusCal translator.
 @end itemize")
       (license license:expat))))
+
+(define-public java-javax-websocket-api-client
+  (package
+    (name "java-javax-websocket-api-client")
+    (version "1.1")
+    (source (origin
+              (method git-fetch)
+              (uri (git-reference
+                    (url "https://github.com/javaee/websocket-spec")
+                    (commit version)))
+              (file-name (git-file-name name version))
+              (sha256
+               (base32
+                "0akvr078amiy1rypcjfxbr0gfj2q0qiyb7fc7xhiq4ajiy58d13k"))))
+    (build-system ant-build-system)
+    (arguments
+     `(#:tests? #f; no tests
+       #:jar-name ,(string-append name ".jar")
+       #:source-dir "api/client/src/main/java"
+       #:phases
+       (modify-phases %standard-phases
+         (replace 'install (install-from-pom "api/client/pom.xml"))
+	 (add-after 'install 'install-parent (install-pom-file "api/pom.xml")))))
+    (propagated-inputs (list java-jvnet-parent-pom))
+    (home-page "https://github.com/javaee/websocket-spec")
+    (synopsis
+     "Websocket in Java")
+    (description
+     "This package defines a set of Java APIs for the development of websocket
+applications.")
+    (license license:cddl1.1)))
+
+(define-public java-javax-websocket-api-server
+  (package
+    (inherit java-javax-websocket-api-client)
+    (name "java-javax-websocket-api-server")
+    (arguments
+     `(#:tests? #f; no tests
+       #:jar-name ,(string-append name ".jar")
+       #:source-dir "api/server/src/main/java"
+       #:phases
+       (modify-phases %standard-phases
+         (replace 'install (install-from-pom "api/server/pom.xml")))))
+    (propagated-inputs (list java-javax-websocket-api-client))))
+
+(define-public ugs
+  (package
+    (name "ugs")
+    (version "2.0.8")
+    (source (origin
+              (method git-fetch)
+              (uri (git-reference
+                    (url "https://github.com/winder/Universal-G-Code-Sender")
+                    (commit (string-append "v" version))))
+              (file-name (git-file-name name version))
+              (sha256
+               (base32
+                "0p9sy3h3msd30q9gv2as1gqgkvf4iigjw7iw2dx62748hf9m0z0x"))))
+    (build-system maven-build-system)
+    (arguments
+     `(#:tests? #f)) ; require mockito 2
+    (inputs
+     `(("python" ,python-3)))           ; For ./scripts/update_languages.py
+    (native-inputs
+     `(("java-junit"   ,java-junit)
+       ("java-mockito" ,java-mockito-1)
+       ("java-hamcrest-core" ,java-hamcrest-core)))
+    (home-page "https://github.com/winder/Universal-G-Code-Sender")
+    (synopsis
+     "A cross-platform G-Code sender for GRBL, Smoothieware, TinyG and G2core.")
+    (description
+     "Universal G-Code Sender is a Java based, cross platform G-Code
+sender, compatible with GRBL, TinyG, g2core and Smoothieware.")
+    (license license:gpl3)))
diff --git a/gnu/packages/maven-parent-pom.scm b/gnu/packages/maven-parent-pom.scm
index a47fbfa8c1..0f942e06dc 100644
--- a/gnu/packages/maven-parent-pom.scm
+++ b/gnu/packages/maven-parent-pom.scm
@@ -332,6 +332,35 @@ (define-public java-sonatype-oss-parent-pom-9
              (replace 'install
                (install-pom-file #$(package-source this-package)))))))))
 
+(define-public java-jvnet-parent-pom
+  (hidden-package
+    (package
+      (name "java-jvnet-parent-pom")
+      (version "5")
+      (source (origin
+                (method url-fetch)
+                (uri (string-append "https://repo1.maven.org/maven2/net/java/"
+                                    "jvnet-parent/" version "/jvnet-parent-"
+                                    version ".pom"))
+                (sha256
+                 (base32
+                  "02nbr5jk9ynaaky5zzbdb8m3dc2yj7bzn0njl3wngayxv7w9kxhs"))))
+      (build-system ant-build-system)
+      (arguments
+       (list
+         #:tests? #f
+         #:phases
+         #~(modify-phases %standard-phases
+             (delete 'unpack)
+             (delete 'configure)
+             (delete 'build)
+             (replace 'install
+               (install-pom-file #$(package-source this-package))))))
+      (home-page "https://mvnrepository.com/artifact/net.java/jvnet-parent")
+      (synopsis "Java.net parent pom")
+      (description "This package contains the Java.net parent pom file.")
+      (license license:asl2.0))))
+
 (define* (make-plexus-parent-pom version hash #:optional parent)
   (hidden-package
     (package
diff --git a/guix/build-system/maven.scm b/guix/build-system/maven.scm
index 0af5922692..e15230e97e 100644
--- a/guix/build-system/maven.scm
+++ b/guix/build-system/maven.scm
@@ -46,7 +46,7 @@ (define-module (guix build-system maven)
 (define %maven-build-system-modules
   ;; Build-side modules imported by default.
   `((guix build maven-build-system)
-    (guix build maven pom)
+    (guix build maven pom2)
     ,@%gnu-build-system-modules))
 
 (define (default-maven)
@@ -161,7 +161,7 @@ (define* (maven-build name inputs
                       (system (%current-system))
                       (imported-modules %maven-build-system-modules)
                       (modules '((guix build maven-build-system)
-                                 (guix build maven pom)
+                                 (guix build maven pom2)
                                  (guix build utils))))
   "Build SOURCE using PATCHELF, and with INPUTS. This assumes that SOURCE
 provides its own binaries."
diff --git a/guix/build/maven-build-system.scm b/guix/build/maven-build-system.scm
index b3d97c81ea..7e77c4bcf7 100644
--- a/guix/build/maven-build-system.scm
+++ b/guix/build/maven-build-system.scm
@@ -19,7 +19,7 @@
 (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 (guix build maven pom2)
   #:use-module (ice-9 match)
   #:export (%standard-phases
             maven-build))
@@ -60,22 +60,22 @@ (define* (configure #:key inputs #:allow-other-keys)
   (invoke "mvn" "-v")
   #t)
 
-(define (fix-pom pom-file inputs local-packages excludes)
+(define (fix-pom pom-file inputs local-packages excludes tests?)
   (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
-                        #:with-modules? #t
+                        #:with-modules? #t #:with-tests? tests?
                         #:local-packages local-packages
                         #:excludes excludes))
 
-(define* (fix-pom-files #:key inputs local-packages exclude #:allow-other-keys)
+(define* (fix-pom-files #:key inputs local-packages exclude tests? #:allow-other-keys)
   (let ((local-packages (pom-local-packages "pom.xml" #:local-packages local-packages)))
     (format (current-error-port) "Fix pom files with local packages: ~a~%" local-packages)
     (for-each
       (lambda (pom)
         (when (file-exists? pom)
-          (fix-pom pom inputs local-packages exclude)))
+          (fix-pom pom inputs local-packages exclude tests?)))
       (pom-and-submodules "pom.xml")))
   #t)
 
diff --git a/guix/build/maven/pom2.scm b/guix/build/maven/pom2.scm
new file mode 100644
index 0000000000..718203d7e3
--- /dev/null
+++ b/guix/build/maven/pom2.scm
@@ -0,0 +1,515 @@
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2019-2021 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
+;;; 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 maven pom2)
+  #:use-module (sxml simple)
+  #:use-module (system foreign)
+  #:use-module (ice-9 match)
+  #:use-module (srfi srfi-1)
+  #:export (add-local-package
+            get-pom
+            pom-ref
+            pom-description
+            pom-name
+            pom-version
+            pom-artifactid
+            pom-groupid
+            pom-dependencies
+            group->dir
+            pom-and-submodules
+            pom-local-packages
+            fix-pom-dependencies))
+
+(define (add-local-package local-packages group artifact version)
+  "Takes @var{local-packages}, a list of local packages, and adds a new one
+for @var{group}:@var{artifact} at @var{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
+      version)))
+
+(define (get-pom file)
+  "Return the content of a @file{.pom} file."
+  (let ((pom-content (call-with-input-file file xml->sxml)))
+    (match pom-content
+      (('*TOP* _ (_ ('@ _ ...) content ...))
+        content)
+      (('*TOP* (_ ('@ _ ...) content ...))
+       content)
+      (('*TOP* _ (_ content ...))
+        content)
+      (('*TOP* (_ content ...))
+       content))))
+
+(define (pom-ref content attr)
+  "Gets a value associated to @var{attr} in @var{content}, an sxml value that
+represents a @file{.pom} file content, or parts of it."
+  (or
+    (assoc-ref
+      content
+      (string->symbol
+        (string-append "http://maven.apache.org/POM/4.0.0:" attr)))
+    (assoc-ref content (string->symbol attr))))
+
+(define (get-parent content)
+  (pom-ref content "parent"))
+
+(define* (find-parent content inputs #:optional local-packages)
+  "Find the parent pom for the pom file with @var{content} in a package's
+@var{inputs}.  When the parent pom cannot be found in @var{inputs}, but
+@var{local-packages} is defined, the parent pom is looked up in it.
+
+@var{local-packages} is an association list of groupID to an association list
+of artifactID to version number.
+
+The result is an sxml document that describes the content of the parent pom, or
+of an hypothetical parent pom if it was generated from @var{local-packages}.
+If no result is found, the result is @code{#f}."
+  (let ((parent (pom-ref content "parent")))
+    (if parent
+        (let* ((groupid (car (pom-ref parent "groupId")))
+               (artifactid (car (pom-ref parent "artifactId")))
+               (version (car (pom-ref parent "version")))
+               (pom-file (string-append "lib/m2/" (group->dir groupid)
+                                        "/" artifactid "/" version "/"
+                                        artifactid "-" version ".pom"))
+               (java-inputs (filter
+                              (lambda (input)
+                                (file-exists? (string-append input "/" pom-file)))
+                              inputs))
+               (java-inputs (map (lambda (input) (string-append input "/" pom-file))
+                                 java-inputs)))
+          (if (null? java-inputs)
+              (let ((version (assoc-ref (assoc-ref local-packages groupid) artifactid)))
+                (if version
+                    `((groupId ,groupid)
+                      (artifactId ,artifactid)
+                      (version ,version))
+                    #f))
+              (get-pom (car java-inputs))))
+        #f)))
+
+(define* (pom-groupid content)
+  "Find the groupID of a pom file, potentially looking at its parent pom file.
+See @code{find-parent} for the meaning of the arguments."
+  (if content
+    (let ((res (or (pom-ref content "groupId")
+                   (pom-ref (pom-ref content "parent") "groupId"))))
+      (cond
+        ((string? res) res)
+        ((null? res) #f)
+        ((list? res) (car res))
+        (else #f)))
+    #f))
+
+(define (pom-artifactid content)
+  "Find the artifactID of a pom file, from its sxml @var{content}."
+  (let ((res (pom-ref content "artifactId")))
+    (if (and res (>= (length res) 1))
+      (car res)
+      #f)))
+
+(define* (pom-version content)
+  "Find the version of a pom file, potentially looking at its parent pom file.
+See @code{find-parent} for the meaning of the arguments."
+  (if content
+    (let ((res (or (pom-ref content "version")
+                   (pom-ref (pom-ref content "parent") "version"))))
+      (cond
+        ((string? res) res)
+        ((null? res) #f)
+        ((list? res) (car res))
+        (else #f)))
+    #f))
+
+(define (pom-name content)
+  "Return the name of the package as contained in the sxml @var{content} of the
+pom file."
+  (let ((res (pom-ref content "name")))
+    (if (and res (>= (length res) 1))
+      (car res)
+      #f)))
+
+(define (pom-description content)
+  "Return the description of the package as contained in the sxml @var{content}
+of the pom file."
+  (let ((res (pom-ref content "description")))
+    (if (and res (>= (length res) 1))
+      (car res)
+      #f)))
+
+(define (pom-dependencies content)
+  "Return the list of dependencies listed in the sxml @var{content} of the pom
+file."
+  (filter
+    (lambda (a) a)
+    (map
+      (match-lambda
+        ((? string? _) #f)
+        (('http://maven.apache.org/POM/4.0.0:dependency content ...)
+         (let loop ((content content) (groupid #f) (artifactid #f) (version #f) (scope #f))
+           (match content
+             ('()
+              `(dependency
+                 (groupId ,groupid)
+                 (artifactId ,artifactid)
+                 (version ,version)
+                 ,@(if scope `((scope ,scope)) '())))
+             (((? string? _) content ...)
+              (loop content groupid artifactid version scope))
+             ((('http://maven.apache.org/POM/4.0.0:scope scope) content ...)
+              (loop content groupid artifactid version scope))
+             ((('http://maven.apache.org/POM/4.0.0:groupId groupid) content ...)
+              (loop content groupid artifactid version scope))
+             ((('http://maven.apache.org/POM/4.0.0:artifactId artifactid) content ...)
+              (loop content groupid artifactid version scope))
+             ((('http://maven.apache.org/POM/4.0.0:version version) content ...)
+              (loop content groupid artifactid version scope))
+             ((_ content ...)
+              (loop content groupid artifactid version scope))))))
+      (pom-ref content "dependencies"))))
+
+(define version-compare
+  (let ((strverscmp
+         (let ((sym (or (dynamic-func "strverscmp" (dynamic-link))
+                        (error "could not find `strverscmp' (from GNU libc)"))))
+           (pointer->procedure int sym (list '* '*)))))
+    (lambda (a b)
+      "Return '> when A denotes a newer version than B,
+'< when A denotes a older version than B,
+or '= when they denote equal versions."
+      (let ((result (strverscmp (string->pointer a) (string->pointer b))))
+        (cond ((positive? result) '>)
+              ((negative? result) '<)
+              (else '=))))))
+
+(define (version>? a b)
+  "Return #t when A denotes a version strictly newer than B."
+  (eq? '> (version-compare a b)))
+
+(define (fix-maven-xml sxml)
+  "When writing an xml file from an sxml representation, it is not possible to
+use namespaces in tag names.  This procedure takes an @var{sxml} representation
+of a pom file and removes the namespace uses.  It also adds the required bits
+to re-declare the namespaces in the top-level element."
+  (define (fix-xml sxml)
+    (match sxml
+      ((tag ('@ opts ...) rest ...)
+       (if (> (string-length (symbol->string tag))
+              (string-length "http://maven.apache.org/POM/4.0.0:"))
+         (let* ((tag (symbol->string tag))
+                (tag (substring tag (string-length
+                                      "http://maven.apache.org/POM/4.0.0:")))
+                (tag (string->symbol tag)))
+           `(,tag (@ ,@opts) ,@(map fix-xml rest)))
+         `(,tag (@ ,@opts) ,@(map fix-xml rest))))
+      ((tag (rest ...))
+       (if (> (string-length (symbol->string tag))
+              (string-length "http://maven.apache.org/POM/4.0.0:"))
+         (let* ((tag (symbol->string tag))
+                (tag (substring tag (string-length
+                                      "http://maven.apache.org/POM/4.0.0:")))
+                (tag (string->symbol tag)))
+           `(,tag ,@(map fix-xml rest)))
+         `(,tag ,@(map fix-xml rest))))
+      ((tag rest ...)
+       (if (> (string-length (symbol->string tag))
+              (string-length "http://maven.apache.org/POM/4.0.0:"))
+         (let* ((tag (symbol->string tag))
+                (tag (substring tag (string-length
+                                      "http://maven.apache.org/POM/4.0.0:")))
+                (tag (string->symbol tag)))
+           `(,tag ,@(map fix-xml rest)))
+         `(,tag ,@(map fix-xml rest))))
+      (_ sxml)))
+
+  `((*TOP* (*PI* xml "version=\"1.0\" encoding=\"UTF-8\"")
+     (project (@ (xmlns "http://maven.apache.org/POM/4.0.0")
+                 (xmlns:xsi "http://www.w3.org/2001/XMLSchema-instance")
+                 (xmlns:schemaLocation "http://maven.apache.org/POM/4.0.0
+                   http://maven.apache.org/xsd/maven-4.0.0.xsd"))
+       ,(map fix-xml sxml)))))
+
+(define (pom-and-submodules pom-file)
+  "Given @var{pom-file}, the file name of a pom, return the list of pom file
+names that correspond to itself and its submodules, recursively."
+  (define (get-modules modules)
+    (match modules
+      (#f '())
+      ('() '())
+      (((? string? _) rest ...) (get-modules rest))
+      ((('http://maven.apache.org/POM/4.0.0:module mod) rest ...)
+       (let ((pom (string-append (dirname pom-file) "/" mod "/pom.xml")))
+         (if (file-exists? pom)
+             (cons pom (get-modules rest))
+             (get-modules rest))))))
+
+  (let* ((pom (get-pom pom-file))
+         (modules (get-modules (pom-ref pom "modules"))))
+    (cons pom-file
+          (apply append (map pom-and-submodules modules)))))
+
+(define* (pom-local-packages pom-file #:key (local-packages '()))
+  "Given @var{pom-file}, a pom file name, return a list of local packages that
+this repository contains."
+  (let loop ((modules (pom-and-submodules pom-file))
+             (local-packages local-packages))
+    (match modules
+      (() local-packages)
+      ((module modules ...)
+       (let* ((pom (get-pom module))
+              (version (pom-version pom))
+              (artifactid (pom-artifactid pom))
+              (groupid (pom-groupid pom)))
+         (loop modules
+               (add-local-package local-packages groupid artifactid version)))))))
+
+(define (group->dir group)
+  "Convert a group ID to a directory path."
+  (string-join (string-split group #\.) "/"))
+
+(define* (fix-pom-dependencies pom-file inputs
+                               #:key with-plugins? with-build-dependencies?
+                                     with-modules? with-tests? (excludes '())
+                                     (local-packages '()))
+  "Open @var{pom-file}, and override its content, rewriting its dependencies
+to set their version to the latest version available in the @var{inputs}.
+
+@var{#:with-plugins?} controls whether plugins are also overridden.
+@var{#:with-build-dependencies?} controls whether build dependencies (whose
+scope is not empty) are also overridden.  By default build dependencies and
+plugins are not overridden.
+
+@var{#:excludes} is an association list of groupID to a list of artifactIDs.
+When a pair (groupID, artifactID) is present in the list, its entry is
+removed instead of being overridden.  If the entry is ignored because of the
+previous arguments, the entry is not removed.
+
+@var{#:local-packages} is an association list that contains additional version
+information for packages that are not in @var{inputs}.  If the package is
+not found in @var{inputs}, information from this list is used instead to determine
+the latest version of the package.  This is an association list of group IDs
+to another association list of artifact IDs to a version number.
+
+Returns nothing, but overrides the @var{pom-file} as a side-effect."
+  (define pom (get-pom pom-file))
+
+  (define (ls dir)
+    (let ((dir (opendir dir)))
+      (let loop ((res '()))
+        (let ((entry (readdir dir)))
+          (if (eof-object? entry)
+              res
+              (loop (cons entry res)))))))
+
+  (define fix-pom
+    (match-lambda
+      ('() '())
+      ((tag rest ...)
+       (match tag
+         (('http://maven.apache.org/POM/4.0.0:dependencies deps ...)
+          `((http://maven.apache.org/POM/4.0.0:dependencies ,(fix-deps deps))
+            ,@(fix-pom rest)))
+         (('http://maven.apache.org/POM/4.0.0:dependencyManagement deps ...)
+          `((http://maven.apache.org/POM/4.0.0:dependencyManagement
+              ,(fix-dep-management deps))
+            ,@(fix-pom rest)))
+         (('http://maven.apache.org/POM/4.0.0:build build ...)
+          (if with-plugins?
+              `((http://maven.apache.org/POM/4.0.0:build ,(fix-build build))
+                ,@(fix-pom rest))
+              (cons tag (fix-pom rest))))
+         (('http://maven.apache.org/POM/4.0.0:modules modules ...)
+          (if with-modules?
+              `((http://maven.apache.org/POM/4.0.0:modules ,(fix-modules modules))
+                ,@(fix-pom rest))
+              (cons tag (fix-pom rest))))
+         (tag (cons tag (fix-pom rest)))))))
+
+  (define fix-modules
+    (match-lambda
+      ('() '())
+      ((tag rest ...)
+       (match tag
+        (('http://maven.apache.org/POM/4.0.0:module module)
+         (if (file-exists? (string-append (dirname pom-file) "/" module "/pom.xml"))
+             `((http://maven.apache.org/POM/4.0.0:module ,module) ,@(fix-modules rest))
+             (fix-modules rest)))
+        (tag (cons tag (fix-modules rest)))))))
+
+  (define fix-dep-management
+    (match-lambda
+      ('() '())
+      ((tag rest ...)
+       (match tag
+         (('http://maven.apache.org/POM/4.0.0:dependencies deps ...)
+          `((http://maven.apache.org/POM/4.0.0:dependencies ,(fix-deps deps #t))
+            ,@(fix-dep-management rest)))
+         (tag (cons tag (fix-dep-management rest)))))))
+
+  (define* (fix-deps deps #:optional optional?)
+    (match deps
+      ('() '())
+      ((tag rest ...)
+       (match tag
+         (('http://maven.apache.org/POM/4.0.0:dependency dep ...)
+          `((http://maven.apache.org/POM/4.0.0:dependency ,(fix-dep dep optional?))
+            ,@(fix-deps rest optional?)))
+         (tag (cons tag (fix-deps rest optional?)))))))
+
+  (define fix-build
+    (match-lambda
+      ('() '())
+      ((tag rest ...)
+       (match tag
+         (('http://maven.apache.org/POM/4.0.0:pluginManagement management ...)
+          `((http://maven.apache.org/POM/4.0.0:pluginManagement
+              ,(fix-management management))
+            ,@(fix-build rest)))
+         (('http://maven.apache.org/POM/4.0.0:plugins plugins ...)
+          `((http://maven.apache.org/POM/4.0.0:plugins
+              ,(fix-plugins plugins))
+            ,@(fix-build rest)))
+         (('http://maven.apache.org/POM/4.0.0:extensions extensions ...)
+          `((http://maven.apache.org/POM/4.0.0:extensions
+              ,(fix-extensions extensions))
+            ,@(fix-build rest)))
+         (tag (cons tag (fix-build rest)))))))
+
+  (define* (fix-extensions extensions #:optional optional?)
+    (match extensions
+      ('() '())
+      ((tag rest ...)
+       (match tag
+         (('http://maven.apache.org/POM/4.0.0:extension extension ...)
+          (let ((group (or (pom-groupid extension) "org.apache.maven.plugins"))
+                (artifact (pom-artifactid extension)))
+            (if (member artifact (or (assoc-ref excludes group) '()))
+              (fix-extensions rest optional?)
+              `((http://maven.apache.org/POM/4.0.0:extension
+                  ,(fix-plugin extension optional?)); extensions are similar to plugins
+                ,@(fix-extensions rest optional?)))))
+         (tag (cons tag (fix-extensions rest optional?)))))))
+
+  (define fix-management
+    (match-lambda
+      ('() '())
+      ((tag rest ...)
+       (match tag
+         (('http://maven.apache.org/POM/4.0.0:plugins plugins ...)
+          `((http://maven.apache.org/POM/4.0.0:plugins
+              ,(fix-plugins plugins #t))
+            ,@(fix-management rest)))
+         (tag (cons tag (fix-management rest)))))))
+
+  (define* (fix-plugins plugins #:optional optional?)
+    (match plugins
+      ('() '())
+      ((tag rest ...)
+       (match tag
+         (('http://maven.apache.org/POM/4.0.0:plugin plugin ...)
+          (let ((group (or (pom-groupid plugin) "org.apache.maven.plugins"))
+                (artifact (pom-artifactid plugin)))
+            (if (member artifact (or (assoc-ref excludes group) '()))
+              (fix-plugins rest optional?)
+              `((http://maven.apache.org/POM/4.0.0:plugin
+                  ,(fix-plugin plugin optional?))
+                ,@(fix-plugins rest optional?)))))
+         (tag (cons tag (fix-plugins rest optional?)))))))
+
+  (define* (fix-plugin plugin #:optional optional?)
+    (let* ((artifact (pom-artifactid plugin))
+           (group (or (pom-groupid plugin) "org.apache.maven.plugins"))
+           (version (or (assoc-ref (assoc-ref local-packages group) artifact)
+                        (find-version inputs group artifact optional?)
+                        (pom-version plugin))))
+      (if (pom-version plugin)
+        (map
+          (lambda (tag)
+            (match tag
+              (('http://maven.apache.org/POM/4.0.0:version _)
+               `(http://maven.apache.org/POM/4.0.0:version ,version))
+              (('version _)
+               `(http://maven.apache.org/POM/4.0.0:version ,version))
+              (tag tag)))
+          plugin)
+        (cons `(http://maven.apache.org/POM/4.0.0:version ,version) plugin))))
+
+  (define* (fix-dep dep #:optional optional?)
+    (let* ((artifact (pom-artifactid dep))
+           (group (or (pom-groupid dep) (pom-groupid pom)))
+           (scope (pom-ref dep "scope"))
+           (is-optional? (equal? (pom-ref dep "optional") '("true"))))
+      (format (current-error-port) "maven: ~a:~a :: ~a (optional: ~a)~%"
+              group artifact scope optional?)
+      (if (or (and (not (equal? scope '("test"))) (not is-optional?))
+              (and (not (equal? scope '("test"))) with-build-dependencies?)
+	      (and (equal? scope '("test")) with-tests?))
+          (let ((version (or (assoc-ref (assoc-ref local-packages group) artifact)
+                             (find-version inputs group artifact optional?)
+                             (pom-version dep))))
+            (if (pom-version dep)
+              (map
+                (lambda (tag)
+                  (match tag
+                    (('http://maven.apache.org/POM/4.0.0:version _)
+                     `(http://maven.apache.org/POM/4.0.0:version ,version))
+                    (('version _)
+                     `(http://maven.apache.org/POM/4.0.0:version ,version))
+                    (_ tag)))
+                dep)
+              (cons `(http://maven.apache.org/POM/4.0.0:version ,version) dep)))
+          dep)))
+
+  (define (find-packaged-version inputs group artifact)
+    (let* ((directory (string-append "lib/m2/" (group->dir group)
+                                     "/" artifact))
+           (java-inputs (filter
+                          (lambda (input)
+                            (file-exists? (string-append input "/" directory)))
+                          inputs))
+           (java-inputs (map (lambda (input) (string-append input "/" directory))
+                             java-inputs))
+           (versions (append-map ls java-inputs))
+           (versions (sort versions version>?)))
+      (if (null? versions)
+          #f
+          (car versions))))
+
+  (define* (find-version inputs group artifact #:optional optional?)
+    (let ((packaged-version (find-packaged-version inputs group artifact))
+          (local-version (assoc-ref (assoc-ref local-packages group) artifact)))
+      (or local-version packaged-version
+          (if optional?
+            #f
+            (begin
+              (format (current-error-port) "maven: ~a:~a is missing from inputs~%"
+                      group artifact)
+              (throw 'no-such-input group artifact))))))
+
+  (let ((tmpfile (string-append pom-file ".tmp")))
+    (with-output-to-file tmpfile
+      (lambda _
+        (sxml->xml (fix-maven-xml (fix-pom pom)))))
+    (rename-file tmpfile pom-file)))
-- 
2.34.0