ym 0.3.58

Yummy - A modern Java build tool
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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
use anyhow::{bail, Context, Result};
use console::style;
use rayon::prelude::*;
use std::path::{Path, PathBuf};
use std::time::Instant;

use crate::compiler;
use crate::compiler::incremental;
use crate::compiler::javac;
use crate::config;
use crate::config::schema::{YmConfig, artifact_id_from_key};
use crate::jvm;
use crate::resources;
use crate::scripts;
use crate::workspace::graph::WorkspaceGraph;

/// Simple spinner that uses raw eprint! — avoids indicatif's ANSI escape issues on WSL 1.
/// Reads message from global SPINNER_MSG so resolver can update progress in-place.
struct SimpleSpinner {
    running: std::sync::Arc<std::sync::atomic::AtomicBool>,
    handle: Option<std::thread::JoinHandle<()>>,
}

impl SimpleSpinner {
    fn new(msg: &str) -> Self {
        if crate::is_progress_quiet() {
            eprintln!("  {}", msg);
            return Self {
                running: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
                handle: None,
            };
        }
        crate::set_spinner_msg(msg);
        let running = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true));
        let r = running.clone();
        let handle = std::thread::spawn(move || {
            let chars = ['', '', '', ''];
            let mut i = 0;
            while r.load(std::sync::atomic::Ordering::Relaxed) {
                let c = chars[i % chars.len()];
                let msg = crate::SPINNER_MSG.lock().map(|m| m.clone()).unwrap_or_default();
                // \r + trailing spaces ensures previous longer messages are fully overwritten
                eprint!("\r  {} {}  \x1b[K", c, msg);
                i += 1;
                std::thread::sleep(std::time::Duration::from_millis(80));
            }
        });
        Self { running, handle: Some(handle) }
    }

    fn set_message(&self, msg: impl Into<String>) {
        crate::set_spinner_msg(msg);
    }

    fn finish_and_clear(mut self) {
        self.running.store(false, std::sync::atomic::Ordering::Relaxed);
        if let Some(h) = self.handle.take() {
            h.join().ok();
        }
        eprint!("\r{}\r", " ".repeat(80));
    }
}

impl Drop for SimpleSpinner {
    fn drop(&mut self) {
        self.running.store(false, std::sync::atomic::Ordering::Relaxed);
    }
}

/// Custom output directory override (set via --output flag)
static OUTPUT_DIR_OVERRIDE: std::sync::OnceLock<String> = std::sync::OnceLock::new();

/// Verbose mode flag
static VERBOSE: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

/// Strict mode flag (warnings as errors)
static STRICT: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

/// Set the number of parallel compilation threads.
pub fn set_parallelism(threads: usize) {
    let threads = threads.max(1);
    let _ = rayon::ThreadPoolBuilder::new()
        .num_threads(threads)
        .build_global();
}

/// Set a custom output directory for compiled classes.
pub fn set_output_dir(dir: &str) {
    let _ = OUTPUT_DIR_OVERRIDE.set(dir.to_string());
}

/// Enable verbose build output.
pub fn set_verbose(v: bool) {
    VERBOSE.store(v, std::sync::atomic::Ordering::Relaxed);
}

/// Check if verbose mode is enabled.
pub fn is_verbose() -> bool {
    VERBOSE.load(std::sync::atomic::Ordering::Relaxed)
}

/// Enable strict mode (treat warnings as errors).
pub fn set_strict(v: bool) {
    STRICT.store(v, std::sync::atomic::Ordering::Relaxed);
}

/// Check if strict mode is enabled.
pub fn is_strict() -> bool {
    STRICT.load(std::sync::atomic::Ordering::Relaxed)
}

/// Build with per-phase timing breakdown
pub fn execute_with_profile(_targets: Vec<String>) -> Result<()> {
    let total_start = Instant::now();

    let (config_path, cfg) = config::load_or_find_config()?;
    let project = config::project_dir(&config_path);

    println!();
    println!("  {}", style("Build Profile").bold().underlined());
    println!();

    let config_time = total_start.elapsed();
    println!(
        "  {} config loading                               {:>6}ms",
        style("·").dim(),
        config_time.as_millis()
    );

    let jdk_start = Instant::now();
    ensure_jdk_for_config(&cfg)?;
    let jdk_time = jdk_start.elapsed();
    println!(
        "  {} JDK verification                             {:>6}ms",
        style("·").dim(),
        jdk_time.as_millis()
    );

    let dep_start = Instant::now();
    let _all_jars = resolve_deps(&project, &cfg)?;
    let compile_jars = resolve_deps_with_scopes(&project, &cfg, &["compile", "provided"])?;
    let dep_time = dep_start.elapsed();
    println!(
        "  {} dependency resolution ({} jars)            {:>6}ms",
        style("·").dim(),
        compile_jars.len(),
        dep_time.as_millis()
    );

    let compile_start = Instant::now();
    let result = compile_project(&project, &cfg, &compile_jars)?;
    let compile_time = compile_start.elapsed();

    if !result.success {
        eprint!("{}", compiler::colorize_errors(&result.errors));
        bail!("Compilation failed");
    }

    println!(
        "  {} compilation ({} files)                     {:>6}ms",
        style("·").dim(),
        result.outcome.files_compiled(),
        compile_time.as_millis()
    );

    if cfg.main.is_some() {
        let jar_start = Instant::now();
        let runtime_jars = resolve_deps_with_scopes(&project, &cfg, &["compile", "runtime"])?;
        build_release_jar(&project, &cfg, &runtime_jars, None, None)?;
        let jar_time = jar_start.elapsed();
        println!(
            "  {} JAR packaging                                {:>6}ms",
            style("·").dim(),
            jar_time.as_millis()
        );
    }

    scripts::run_script(&cfg, "postbuild", &project)?;

    let total = total_start.elapsed();
    println!();
    println!(
        "  {} total                                        {:>6}ms",
        style("").green().bold(),
        total.as_millis()
    );
    println!();

    Ok(())
}

/// Compile only (no JAR packaging). Used by dev/test commands.
pub fn compile_only(target: Option<String>) -> Result<()> {
    let targets = target.into_iter().collect();
    build_impl(targets, false, false)
}

pub fn execute(targets: Vec<String>, jar: bool) -> Result<()> {
    build_impl(targets, jar, false)
}

pub fn execute_keep_going(targets: Vec<String>, jar: bool) -> Result<()> {
    build_impl(targets, jar, true)
}

fn build_impl(targets: Vec<String>, package: bool, keep_going: bool) -> Result<()> {
    let total_start = Instant::now();

    let (config_path, cfg) = config::load_or_find_config()?;
    let project = config::project_dir(&config_path);

    // Run prebuild script
    scripts::run_script(&cfg, "prebuild", &project)?;

    // Ensure JDK is available
    ensure_jdk_for_config(&cfg)?;

    if cfg.workspaces.is_some() {
        let result = build_workspace(&project, &cfg, &targets, package, keep_going, total_start);
        scripts::run_script(&cfg, "postbuild", &project)?;
        return result;
    }

    // Single project mode
    let start = Instant::now();
    // Resolve all deps to populate cache
    let _all_jars = resolve_deps(&project, &cfg)?;
    // Compilation classpath: compile + provided (exclude runtime and test)
    let compile_jars = resolve_deps_with_scopes(&project, &cfg, &["compile", "provided"])?;
    let resolve_time = start.elapsed();

    let compile_start = Instant::now();
    let result = compile_project(&project, &cfg, &compile_jars)?;
    let compile_time = compile_start.elapsed();

    if !result.success {
        eprint!("{}", compiler::colorize_errors(&result.errors));
        bail!("Compilation failed");
    }

    if is_verbose() && !result.errors.is_empty() {
        eprint!("{}", compiler::colorize_errors(&result.errors));
    }

    println!(
        "{} dependencies {:>40}ms",
        style(format!("{:>12}", "Resolving")).green().bold(),
        resolve_time.as_millis()
    );

    print_compile_result(&cfg.name, &result, compile_time);

    if package && cfg.main.is_some() {
        // Fat JAR: compile + runtime (exclude provided and test)
        let runtime_jars = resolve_deps_with_scopes(&project, &cfg, &["compile", "runtime"])?;
        let class_dir = config::output_classes_dir(&project);
        let resource_dir = project.join("src").join("main").join("resources");
        let fp = compute_packaging_fingerprint(&class_dir, &resource_dir, &runtime_jars, &cfg)?;
        let jar_name = format!("{}-{}.jar", cfg.name, cfg.version.as_deref().unwrap_or("0.0.0"));
        let output_jar = project.join("out").join("release").join(&jar_name);

        if should_skip_packaging(&project, &fp, &output_jar) {
            let jar_size = std::fs::metadata(&output_jar).map(|m| m.len()).unwrap_or(0);
            let size_str = if jar_size >= 1024 * 1024 {
                format!("{:.1} MB", jar_size as f64 / (1024.0 * 1024.0))
            } else {
                format!("{:.0} KB", jar_size as f64 / 1024.0)
            };
            println!(
                "{} {} ({}) (up to date)",
                style(format!("{:>12}", "Packaging")).green().bold(),
                jar_name,
                size_str,
            );
        } else {
            if project.join("ym.config.java").exists() {
                build_with_plugins(&project, &cfg, &runtime_jars, None)?;
            } else {
                build_release_jar(&project, &cfg, &runtime_jars, None, None)?;
            }
            save_packaging_fingerprint(&project, &fp)?;
        }
    }

    scripts::run_script(&cfg, "postbuild", &project)?;

    print_total_time(total_start);
    Ok(())
}

/// Deduplicate JAR paths by Maven groupId:artifactId.
/// If BOM constraints specify a version for a GA, use that version.
/// Otherwise keep the highest version.
/// Extracts groupId from the cache path structure: `~/.ym/maven/{groupId}/{artifactId}/{version}/`.
/// For JARs outside the cache (e.g. workspace thin JARs), uses filename as unique key (no dedup).
fn dedup_jars_by_artifact(jars: Vec<PathBuf>, bom_constraints: &std::collections::BTreeMap<String, String>) -> Vec<PathBuf> {
    let mut ga_map: std::collections::HashMap<String, (PathBuf, String)> = std::collections::HashMap::new();
    let mut order: Vec<String> = Vec::new();
    let mut non_cache: Vec<PathBuf> = Vec::new();

    for jar in jars {
        if jar.is_dir() {
            non_cache.push(jar);
            continue;
        }
        let filename = jar.file_name()
            .map(|n| n.to_string_lossy().to_string())
            .unwrap_or_default();
        if !filename.ends_with(".jar") {
            non_cache.push(jar);
            continue;
        }

        // Try to extract groupId:artifactId from cache path:
        // .ym/maven/{groupId}/{artifactId}/{version}/{artifactId}-{version}.jar
        let path_str = jar.to_string_lossy();
        let ga_key = if let Some(maven_pos) = path_str.find("/maven/") {
            let after_caches = &path_str[maven_pos + 7..]; // skip "/maven/"
            let parts: Vec<&str> = after_caches.split('/').collect();
            if parts.len() >= 3 {
                // parts[0] = groupId, parts[1] = artifactId, parts[2] = version
                format!("{}:{}", parts[0], parts[1])
            } else {
                filename.clone()
            }
        } else {
            // Non-cache JAR (e.g. workspace thin JAR) — use filename, no dedup
            non_cache.push(jar);
            continue;
        };

        // Extract version from path: parent dir name is the version
        let version = jar.parent()
            .and_then(|p| p.file_name())
            .map(|n| n.to_string_lossy().to_string())
            .unwrap_or_default();

        // Check if BOM specifies a version for this GA
        let bom_version = bom_constraints.get(&ga_key);

        if let Some((_, existing_ver)) = ga_map.get(&ga_key) {
            let should_replace = if let Some(bv) = bom_version {
                // BOM wins: replace if current version matches BOM but existing doesn't
                version == *bv && existing_ver != bv
            } else {
                // No BOM: keep highest version
                let parse_ver = |s: &str| -> Vec<i64> {
                    s.split(|c: char| c == '.' || c == '-')
                        .map(|seg| seg.parse::<i64>().unwrap_or(0))
                        .collect()
                };
                let va = parse_ver(&version);
                let vb = parse_ver(existing_ver);
                let len = va.len().max(vb.len());
                let mut higher = false;
                for i in 0..len {
                    let a = va.get(i).copied().unwrap_or(0);
                    let b = vb.get(i).copied().unwrap_or(0);
                    if a > b { higher = true; break; }
                    if a < b { break; }
                }
                higher
            };
            if should_replace {
                ga_map.insert(ga_key, (jar, version));
            }
        } else {
            order.push(ga_key.clone());
            ga_map.insert(ga_key, (jar, version));
        }
    }

    let mut result = non_cache;
    for key in &order {
        if let Some((path, _)) = ga_map.get(key) {
            result.push(path.clone());
        }
    }
    result
}

fn print_total_time(start: Instant) {
    let elapsed = start.elapsed();
    let time = if elapsed.as_millis() > 1000 {
        format!("{:.2}s", elapsed.as_secs_f64())
    } else {
        format!("{}ms", elapsed.as_millis())
    };
    println!(
        "{} build in {}",
        style(format!("{:>12}", "Finished")).green().bold(),
        time
    );
}

fn print_workspace_summary(
    compiled: usize, cached: usize, up_to_date: usize,
    failed: usize, skipped: usize, elapsed: std::time::Duration,
) {
    let mut parts = Vec::new();
    if compiled > 0 { parts.push(format!("{} compiled", compiled)); }
    if cached > 0 { parts.push(format!("{} cached", cached)); }
    if failed > 0 { parts.push(format!("{} failed", failed)); }
    if skipped > 0 { parts.push(format!("{} skipped", skipped)); }
    if up_to_date > 0 { parts.push(format!("{} up to date", up_to_date)); }

    let icon = if failed > 0 {
        style(format!("{:>12}", "Compiling")).red().bold()
    } else {
        style(format!("{:>12}", "Compiling")).green().bold()
    };
    let time = if elapsed.as_millis() > 1000 {
        format!("{:.2}s", elapsed.as_secs_f64())
    } else {
        format!("{}ms", elapsed.as_millis())
    };
    println!("{} {} in {}", icon, parts.join(", "), time);
}

fn build_workspace(root: &Path, root_cfg: &YmConfig, targets: &[String], package: bool, keep_going: bool, total_start: Instant) -> Result<()> {
    let spinner = SimpleSpinner::new("Scanning workspace...");

    let ws = WorkspaceGraph::build(root)?;

    let packages = if !targets.is_empty() {
        let mut all = Vec::new();
        for t in targets {
            let closure = ws.transitive_closure(t)?;
            for pkg in closure {
                if !all.contains(&pkg) {
                    all.push(pkg);
                }
            }
        }
        all
    } else {
        let mut all = Vec::new();
        for name in ws.all_packages() {
            if !all.contains(&name) {
                let closure = ws.transitive_closure(&name)?;
                for pkg in closure {
                    if !all.contains(&pkg) {
                        all.push(pkg);
                    }
                }
            }
        }
        all
    };

    spinner.set_message(format!("Scanning workspace ({} modules)...", packages.len()));

    // Validate workspace dependency declarations
    for name in &packages {
        let pkg = ws.get_package(name).unwrap();
        let errors = pkg.config.validate_workspace_deps(root_cfg);
        if !errors.is_empty() {
            for e in &errors {
                eprintln!(
                    "{} {}: {}",
                    console::style(format!("{:>12}", "error")).red().bold(),
                    name, e
                );
            }
            anyhow::bail!("Invalid workspace dependency declarations in '{}'", name);
        }
    }

    // Pre-warm compiler worker pool in background during dependency resolution.
    // Worker JVM startup (3-5s per worker) overlaps with dep resolution (8s+).
    let pool_size = std::thread::available_parallelism()
        .map(|n| n.get())
        .unwrap_or(4)
        .min(packages.len());
    let pool_handle = if packages.len() > 1 {
        Some(std::thread::spawn(move || {
            compiler::worker::CompilerPool::new(pool_size).ok()
        }))
    } else {
        None
    };

    // Workspace-level dependency resolution
    let dep_start = Instant::now();

    // Collect each module's own Maven deps
    let own_module_deps: std::collections::HashMap<String, std::collections::BTreeMap<String, String>> = packages
        .iter()
        .map(|name| {
            let pkg = ws.get_package(name).unwrap();
            let mut deps = pkg.config.maven_dependencies_with_root(root_cfg);
            for (k, v) in root_cfg.resolved_resolutions(root_cfg) {
                if deps.contains_key(&k) {
                    deps.insert(k, v);
                }
            }
            (name.clone(), deps)
        })
        .collect();

    // Pre-compute transitive closures for all packages (avoids O(N³) repeated BFS)
    let closure_cache: std::collections::HashMap<String, Vec<String>> = packages
        .iter()
        .map(|name| (name.clone(), ws.transitive_closure(name).unwrap_or_default()))
        .collect();

    // Propagate Maven deps from workspace module dependencies (transitive)
    let all_module_deps: Vec<(String, std::collections::BTreeMap<String, String>)> = packages
        .iter()
        .map(|name| {
            let mut deps = own_module_deps.get(name).cloned().unwrap_or_default();
            // Walk workspace dep graph to include transitive Maven deps
            if let Some(closure) = closure_cache.get(name) {
                for ws_dep in closure {
                    if ws_dep != name {
                        if let Some(ws_dep_deps) = own_module_deps.get(ws_dep) {
                            for (k, v) in ws_dep_deps {
                                deps.entry(k.clone()).or_insert(v.clone());
                            }
                        }
                    }
                }
            }
            (name.clone(), deps)
        })
        .collect();

    let total_deps: usize = all_module_deps.iter().map(|(_, deps)| deps.len()).sum();
    spinner.set_message(format!("Resolving dependencies ({} modules, {} artifacts)...", packages.len(), total_deps));

    let cache = config::maven_cache_dir();
    let mut resolved = config::load_resolved_cache_checked(root, root_cfg)?;
    let registries = root_cfg.registry_entries();
    let mut exclusions = root_cfg.exclusions.as_ref().cloned().unwrap_or_default();
    exclusions.extend(root_cfg.per_dependency_exclusions());
    exclusions.extend(root_cfg.resolved_exclusions());

    let resolutions = root_cfg.resolved_resolutions(root_cfg);
    // Spinner stays alive during resolve — resolver updates spinner message with progress
    crate::SPINNER_ACTIVE.store(true, std::sync::atomic::Ordering::Relaxed);
    let per_module_jars = crate::workspace::resolver::resolve_workspace_deps_with_resolutions(
        &all_module_deps, &cache, &mut resolved, &registries, &exclusions, &resolutions,
    )?;
    crate::SPINNER_ACTIVE.store(false, std::sync::atomic::Ordering::Relaxed);
    spinner.finish_and_clear();
    config::save_resolved_cache(root, &resolved)?;
    let dep_time = dep_start.elapsed();
    let total_jars: usize = per_module_jars.values().next().map(|v| v.len()).unwrap_or(0);
    println!(
        "{} dependencies ({} jars) {:>25}ms",
        style(format!("{:>12}", "Resolving")).green().bold(),
        total_jars,
        dep_time.as_millis()
    );

    let cache_dir = config::cache_dir(root);
    // Pre-compute source + resource hashes for all modules (parallel, mtime fast path)
    let source_hashes_map: std::collections::HashMap<String, Vec<(String, String)>> = packages
        .par_iter()
        .filter_map(|name| {
            let pkg = ws.get_package(name)?;
            let src_dirs = vec![config::source_dir_for(&pkg.path, &pkg.config)];
            let output_dir = config::output_classes_dir(&pkg.path);
            let mut hashes = compiler::incremental::compute_source_content_hashes(
                &src_dirs, &cache_dir, &output_dir,
            ).unwrap_or_default();
            // Include resource files in cache key (resource changes must invalidate cache)
            let res_dir = pkg.path.join("src").join("main").join("resources");
            if res_dir.exists() {
                for entry in walkdir::WalkDir::new(&res_dir).into_iter().filter_map(|e| e.ok()) {
                    if entry.file_type().is_file() {
                        let rel = format!("res:{}", entry.path().strip_prefix(&res_dir)
                            .unwrap_or(entry.path()).to_string_lossy());
                        let hash = compiler::incremental::hash_file(entry.path())
                            .unwrap_or_default();
                        hashes.push((rel, hash));
                    }
                }
                hashes.sort_by(|a, b| a.0.cmp(&b.0));
            }
            Some((name.clone(), hashes))
        })
        .collect();

    let jar_sha256_index: std::collections::HashMap<String, String> = resolved.dependencies.iter()
        .filter_map(|(key, dep)| {
            let sha = dep.sha256.as_ref()?;
            let mc = crate::workspace::resolver::MavenCoord::from_versioned_key(key)?;
            let fname = mc.jar_path(std::path::Path::new(""))
                .file_name()?.to_string_lossy().to_string();
            Some((fname, sha.clone()))
        })
        .collect();

    let maven_sha256_map: std::collections::HashMap<String, Vec<(String, String)>> = packages
        .iter()
        .map(|name| {
            let jars = per_module_jars.get(name.as_str()).map(|v| v.as_slice()).unwrap_or(&[]);
            let mut sha256s: Vec<(String, String)> = jars.iter()
                .filter_map(|jar_path| {
                    let fname = jar_path.file_name()?.to_string_lossy().to_string();
                    let sha = jar_sha256_index.get(&fname)
                        .cloned()
                        .unwrap_or_else(|| compiler::incremental::hash_bytes(fname.as_bytes()));
                    Some((fname, sha))
                })
                .collect();
            sha256s.sort_by(|a, b| a.0.cmp(&b.0));
            (name.clone(), sha256s)
        })
        .collect();

    let mut abi_map: std::collections::HashMap<String, String> = std::collections::HashMap::new();

    {

    // Wave scheduling: in-degree based parallel compilation
    let mut in_degree: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
    let mut dependents: std::collections::HashMap<String, Vec<String>> = std::collections::HashMap::new();

    for pkg_name in &packages {
        let pkg = ws.get_package(pkg_name).unwrap();
        let ws_deps = pkg.config.workspace_module_deps();
        let relevant_dep_count = ws_deps.iter()
            .filter(|d| packages.contains(d))
            .count();
        in_degree.insert(pkg_name.clone(), relevant_dep_count);
        for dep in ws_deps.iter().filter(|d| packages.contains(d)) {
            dependents.entry(dep.clone()).or_default().push(pkg_name.clone());
        }
    }

    // Collect pre-warmed compiler worker pool (started during dependency resolution)
    let worker_pool = pool_handle
        .and_then(|h| h.join().ok())
        .flatten();

    let mut workspace_classpath: Vec<PathBuf> = Vec::new();
    let mut failed_modules: Vec<String> = Vec::new();

    // Workspace output stats
    let total_modules = packages.len();
    let mut compiled_count: usize = 0;
    let mut cached_count: usize = 0;
    let mut up_to_date_count: usize = 0;
    let mut skipped_count: usize = 0;
    let mut processed: usize = 0;
    let verbose = is_verbose();
    let is_tty = console::Term::stderr().is_term();

    // Print initial progress header for non-verbose mode
    if !verbose && total_modules > 1 && is_tty {
        eprint!("   Compiling [{}/{}]", processed, total_modules);
    }

    loop {
        let wave: Vec<String> = in_degree.iter()
            .filter(|(_, deg)| **deg == 0)
            .map(|(name, _)| name.clone())
            .collect();

        if wave.is_empty() { break; }

        for name in &wave {
            in_degree.remove(name);
        }

        // In keep_going mode, skip modules whose dependencies have failed
        let (compilable, skipped): (Vec<&String>, Vec<&String>) = if keep_going {
            let mut comp = Vec::new();
            let mut skip = Vec::new();
            for name in &wave {
                if has_failed_dependency(name, &failed_modules, &ws) {
                    skip.push(name);
                } else {
                    comp.push(name);
                }
            }
            (comp, skip)
        } else {
            (wave.iter().collect(), Vec::new())
        };

        for name in &skipped {
            failed_modules.push((*name).clone());
            skipped_count += 1;
            processed += 1;
            if verbose {
                println!(
                    "{} {} (depends on failed module)",
                    style(format!("{:>12}", "Skipping")).yellow().bold(),
                    name
                );
            } else if is_tty {
                eprint!("\r   Compiling [{}/{}]   ", processed, total_modules);
            }
            if let Some(deps) = dependents.get(*name) {
                for dep in deps {
                    if let Some(deg) = in_degree.get_mut(dep) {
                        *deg -= 1;
                    }
                }
            }
        }

        if compilable.is_empty() { continue; }

        let cp_snapshot = workspace_classpath.clone();
        let root_cfg_snapshot = root_cfg.clone();

        // Snapshot abi_map for this wave (immutable during parallel iteration)
        let abi_snapshot = abi_map.clone();

        let results: Vec<_> = compilable
            .par_iter()
            .map(|pkg_name| {
                let pkg = ws.get_package(pkg_name.as_str()).unwrap();
                let out_dir = config::output_classes_dir(&pkg.path);

                // ── Build compile config (needed for both cache key and compilation) ──
                let jars = per_module_jars.get(pkg_name.as_str()).cloned().unwrap_or_default();
                let mut classpath = jars;
                classpath.extend(cp_snapshot.clone());
                let mut module_cfg = pkg.config.clone();
                if module_cfg.compiler.as_ref().and_then(|c| c.args.as_ref()).is_none() {
                    let root_args = root_cfg_snapshot.compiler.as_ref().and_then(|c| c.args.clone());
                    if let Some(args) = root_args {
                        let compiler = module_cfg.compiler.get_or_insert_with(Default::default);
                        compiler.args = Some(args);
                    }
                }
                if let Some(ref root_dev) = root_cfg_snapshot.dev_dependencies {
                    let module_dev = module_cfg.dev_dependencies.get_or_insert_with(Default::default);
                    for (k, v) in root_dev {
                        module_dev.entry(k.clone()).or_insert_with(|| v.clone());
                    }
                }

                let src_hashes = source_hashes_map.get(pkg_name.as_str())
                    .map(|v| v.as_slice()).unwrap_or(&[]);
                let mvn_sha256s = maven_sha256_map.get(pkg_name.as_str())
                    .map(|v| v.as_slice()).unwrap_or(&[]);

                // Collect workspace dependency ABI hashes from abi_snapshot
                let ws_deps = pkg.config.workspace_module_deps();
                let mut dep_abi_hashes: Vec<(String, String)> = ws_deps.iter()
                    .filter_map(|dep| {
                        abi_snapshot.get(dep.as_str())
                            .map(|abi| (dep.clone(), abi.clone()))
                    })
                    .collect();
                dep_abi_hashes.sort_by(|a, b| a.0.cmp(&b.0));

                // Build a CompileConfig for cache key computation (AP JARs covered by maven SHA-256s)
                let compile_cfg = compiler::CompileConfig {
                    source_dirs: Vec::new(),
                    output_dir: std::path::PathBuf::new(),
                    classpath: Vec::new(),
                    java_version: module_cfg.target.clone(),
                    encoding: module_cfg.compiler.as_ref().and_then(|c| c.encoding.clone()),
                    annotation_processors: Vec::new(),
                    lint: module_cfg.compiler.as_ref().and_then(|c| c.lint.clone()).unwrap_or_default(),
                    extra_args: module_cfg.compiler.as_ref().and_then(|c| c.args.clone()).unwrap_or_default(),
                };

                let cache_key = compiler::incremental::compute_module_cache_key(
                    &compiler::incremental::ModuleCacheInput {
                        source_hashes: src_hashes,
                        dep_abi_hashes: &dep_abi_hashes,
                        maven_jar_sha256s: mvn_sha256s,
                        config: &compile_cfg,
                        ap_jar_sha256s: &[],
                    },
                );

                if let Ok(Some(abi_hash)) = compiler::incremental::try_restore_module_cache(
                    &cache_key, &out_dir,
                ) {
                    return (pkg_name.to_string(), Ok(compiler::CompileResult {
                        success: true,
                        outcome: compiler::CompileOutcome::Cached,
                        errors: String::new(),
                        module_abi_hash: Some(abi_hash.clone()),
                    }), std::time::Duration::ZERO, abi_hash);
                }

                let start = Instant::now();
                let result = compile_project_with_pool(&pkg.path, &module_cfg, &classpath, worker_pool.as_ref());
                let elapsed = start.elapsed();

                // Prefer ABI hash from compile result (avoids re-reading .class files)
                let abi_hash = result.as_ref().ok()
                    .and_then(|r| r.module_abi_hash.clone())
                    .unwrap_or_else(|| {
                        if result.as_ref().map(|r| r.success).unwrap_or(false) {
                            compiler::incremental::compute_module_abi_hash(&out_dir)
                                .unwrap_or_else(|_| compiler::incremental::hash_bytes(b"error"))
                        } else {
                            compiler::incremental::hash_bytes(b"error")
                        }
                    });

                // Save to content-addressed cache on successful compilation
                if result.as_ref().map(|r| r.success).unwrap_or(false) {
                    if let Err(e) = compiler::incremental::save_module_cache(
                        &cache_key, &out_dir, &abi_hash, pkg_name,
                    ) {
                        eprintln!("  Warning: failed to save build cache for '{}': {}", pkg_name, e);
                    }
                }

                (pkg_name.to_string(), result, elapsed, abi_hash)
            })
            .collect();

        for (pkg_name, result, elapsed, module_abi) in results {
            // Record ABI hash for downstream modules
            abi_map.insert(pkg_name.clone(), module_abi);

            let success = match &result {
                Ok(r) if r.success => {
                    processed += 1;
                    match r.outcome {
                        compiler::CompileOutcome::UpToDate => {
                            up_to_date_count += 1;
                            if verbose {
                                print_compile_result(&pkg_name, r, elapsed);
                            } else if is_tty {
                                eprint!("\r   Compiling [{}/{}]   ", processed, total_modules);
                            }
                        }
                        compiler::CompileOutcome::Cached => {
                            cached_count += 1;
                            if is_tty { eprint!("\r{}\r", " ".repeat(40)); }
                            print_compile_result(&pkg_name, r, elapsed);
                        }
                        compiler::CompileOutcome::Compiled(_) => {
                            compiled_count += 1;
                            if is_tty { eprint!("\r{}\r", " ".repeat(40)); }
                            print_compile_result(&pkg_name, r, elapsed);
                        }
                    }
                    if verbose && !r.errors.is_empty() {
                        eprint!("{}", compiler::colorize_errors(&r.errors));
                    }
                    true
                }
                Ok(r) => {
                    processed += 1;
                    if is_tty { eprint!("\r{}\r", " ".repeat(40)); }
                    eprint!("{}", compiler::colorize_errors(&r.errors));
                    if keep_going {
                        failed_modules.push(pkg_name.clone());
                        false
                    } else {
                        bail!("Compilation of '{}' failed", pkg_name);
                    }
                }
                Err(e) => {
                    processed += 1;
                    if is_tty { eprint!("\r{}\r", " ".repeat(40)); }
                    if keep_going {
                        eprintln!("{} compilation of '{}' failed: {}", style(format!("{:>12}", "error")).red().bold(), pkg_name, e);
                        failed_modules.push(pkg_name.clone());
                        false
                    } else {
                        bail!("Compilation of '{}' failed: {}", pkg_name, e);
                    }
                }
            };

            if success {
                let pkg = ws.get_package(&pkg_name).unwrap();
                workspace_classpath.push(config::output_classes_dir(&pkg.path));
            }

            if let Some(deps) = dependents.get(&pkg_name) {
                for dep in deps {
                    if let Some(deg) = in_degree.get_mut(dep) {
                        *deg -= 1;
                    }
                }
            }
        }
    }

    // Clear progress line
    if is_tty { eprint!("\r{}\r", " ".repeat(40)); }

    if !failed_modules.is_empty() {
        print_workspace_summary(compiled_count, cached_count, up_to_date_count,
            failed_modules.len(), skipped_count, total_start.elapsed());
        println!(
            "{} failed: {}",
            style(format!("{:>12}", "error")).red().bold(),
            failed_modules.join(", ")
        );
        bail!("Workspace build failed ({} module(s))", failed_modules.len());
    }

    print_workspace_summary(compiled_count, cached_count, up_to_date_count,
        0, skipped_count, total_start.elapsed());

    // Evict stale build cache entries (>30 days unused)
    compiler::incremental::evict_stale_build_cache();

    } // end of content-addressed cache block

    if package {
        // Package JARs for all modules:
        // - Modules with `main`: fat JAR (Spring Boot executable)
        // - Library modules (no `main`): thin JAR (own classes + resources only)
        let jar_targets: Vec<&str> = if !targets.is_empty() {
            targets.iter().map(|s| s.as_str()).collect()
        } else {
            packages.iter().map(|s| s.as_str()).collect()
        };

        // Library modules: thin JAR
        for pkg_name in &jar_targets {
            let pkg = ws.get_package(pkg_name).unwrap();
            if pkg.config.main.is_none() {
                let effective_version = pkg.config.version.as_deref()
                    .or(root_cfg.version.as_deref())
                    .unwrap_or("0.0.0");
                let jar_name = format!("{}-{}.jar", pkg.config.name, effective_version);
                let output_jar = pkg.path.join("out").join("release").join(&jar_name);
                let class_dir = config::output_classes_dir(&pkg.path);
                let resource_dir = pkg.path.join("src").join("main").join("resources");
                let fp = compute_packaging_fingerprint(&class_dir, &resource_dir, &[], &pkg.config)?;
                if !should_skip_packaging(&pkg.path, &fp, &output_jar) {
                    build_library_jar(&pkg.path, &pkg.config, root_cfg.version.as_deref())?;
                    save_packaging_fingerprint(&pkg.path, &fp)?;
                }
            }
        }

        // App modules: fat JAR (filter to only main modules)
        let jar_targets: Vec<&str> = jar_targets.into_iter()
            .filter(|name| ws.get_package(name).map(|p| p.config.main.is_some()).unwrap_or(false))
            .collect();

        for jar_target in &jar_targets {
            let pkg = ws.get_package(jar_target).unwrap();
            let closure = ws.transitive_closure(jar_target)?;
            let mut all_deps = Vec::new();
            let ws_module_names: std::collections::HashSet<String> = closure.iter()
                .map(|n| n.to_string())
                .collect();

            // Package workspace modules as thin JARs (like Gradle's jar task).
            // This avoids class/resource duplication between BOOT-INF/classes/ and BOOT-INF/lib/.
            let effective_version = pkg.config.version.as_deref()
                .or(root_cfg.version.as_deref())
                .unwrap_or("0.0.0");
            for pkg_name in &closure {
                let p = ws.get_package(pkg_name).unwrap();
                if pkg_name != *jar_target {
                    let thin_jar = package_thin_jar(&p.path, &p.config, effective_version)?;
                    all_deps.push(thin_jar);
                }
            }

            // Resolve all workspace modules' external Maven deps
            crate::RESOLVER_QUIET.store(true, std::sync::atomic::Ordering::Relaxed);
            for pkg_name in &closure {
                let p = ws.get_package(pkg_name).unwrap();
                let module_jars = resolve_deps_with_scopes(&p.path, &p.config, &["compile", "runtime"])?;
                all_deps.extend(module_jars);
            }
            // Also resolve the target app module's own Maven deps
            let runtime_jars = resolve_deps_with_scopes(&pkg.path, &pkg.config, &["compile", "runtime"])?;
            crate::RESOLVER_QUIET.store(false, std::sync::atomic::Ordering::Relaxed);
            all_deps.extend(runtime_jars);

            // Collect BOM constraints for version selection during dedup
            let bom_constraints = collect_plugin_managed_versions(&pkg.path, &pkg.config)
                .unwrap_or_default();

            // Deduplicate: by path first, then by groupId:artifactId
            // Use BOM-managed version when available, otherwise keep highest version
            all_deps.sort();
            all_deps.dedup();
            all_deps = dedup_jars_by_artifact(all_deps, &bom_constraints);
            all_deps.retain(|path| {
                if path.is_dir() { return true; }
                let file_name = path.file_name()
                    .map(|n| n.to_string_lossy().to_string())
                    .unwrap_or_default();
                for module_name in &ws_module_names {
                    if file_name.starts_with(&format!("{}-", module_name))
                        && file_name.ends_with(".jar")
                        && !file_name.contains(".thin.") // keep our thin JARs
                    {
                        return false;
                    }
                }
                true
            });

            let class_dir = config::output_classes_dir(&pkg.path);
            let resource_dir = pkg.path.join("src").join("main").join("resources");
            let fp = compute_packaging_fingerprint(&class_dir, &resource_dir, &all_deps, &pkg.config)?;
            let effective_version = pkg.config.version.as_deref()
                .or(root_cfg.version.as_deref())
                .unwrap_or("0.0.0");
            let jar_name = format!("{}-{}.jar", pkg.config.name, effective_version);
            let output_jar = pkg.path.join("out").join("release").join(&jar_name);

            if should_skip_packaging(&pkg.path, &fp, &output_jar) {
                let jar_size = std::fs::metadata(&output_jar).map(|m| m.len()).unwrap_or(0);
                let size_str = if jar_size >= 1024 * 1024 {
                    format!("{:.1} MB", jar_size as f64 / (1024.0 * 1024.0))
                } else {
                    format!("{:.0} KB", jar_size as f64 / 1024.0)
                };
                println!(
                    "{} {} ({}) (up to date)",
                    style(format!("{:>12}", "Packaging")).green().bold(),
                    jar_name,
                    size_str,
                );
            } else {
                if pkg.config.main.is_none() {
                    // Library module: thin JAR (only own classes + resources)
                    build_library_jar(&pkg.path, &pkg.config, root_cfg.version.as_deref())?;
                } else if pkg.path.join("ym.config.java").exists() {
                    build_with_plugins(&pkg.path, &pkg.config, &all_deps, root_cfg.version.as_deref())?;
                } else {
                    build_release_jar(&pkg.path, &pkg.config, &all_deps, None, root_cfg.version.as_deref())?;
                }
                save_packaging_fingerprint(&pkg.path, &fp)?;
            }
        }

        if !jar_targets.is_empty() {
            println!(
                "{} {}",
                style(format!("{:>12}", "")).dim(),
                style(root.join("out").join("release").display()).dim()
            );
        }
    }

    let total_time = total_start.elapsed();
    if total_time.as_millis() > 1000 {
        println!(
            "{} build in {:.2}s",
            style(format!("{:>12}", "Finished")).green().bold(),
            total_time.as_secs_f64()
        );
    } else {
        println!(
            "{} build in {}ms",
            style(format!("{:>12}", "Finished")).green().bold(),
            total_time.as_millis()
        );
    }

    Ok(())
}

/// Check if a module depends (directly) on any failed module.
fn has_failed_dependency(pkg_name: &str, failed: &[String], ws: &WorkspaceGraph) -> bool {
    if let Some(pkg) = ws.get_package(pkg_name) {
        for dep in pkg.config.workspace_module_deps() {
            if failed.contains(&dep) {
                return true;
            }
        }
    }
    false
}


fn print_compile_result(name: &str, result: &compiler::CompileResult, elapsed: std::time::Duration) {
    match result.outcome {
        compiler::CompileOutcome::UpToDate => {
            println!(
                "{} {} (up to date)",
                style(format!("{:>12}", "Compiling")).green().bold(),
                name,
            );
        }
        compiler::CompileOutcome::Cached => {
            println!(
                "{} {} (cached) {:>30}ms",
                style(format!("{:>12}", "Compiling")).green().bold(),
                name,
                elapsed.as_millis()
            );
        }
        compiler::CompileOutcome::Compiled(n) => {
            println!(
                "{} {} ({} files) {:>27}ms",
                style(format!("{:>12}", "Compiling")).green().bold(),
                name,
                n,
                elapsed.as_millis()
            );
        }
    }
}

/// Build a thin library JAR containing only the module's own classes and resources.
fn build_library_jar(project: &Path, cfg: &YmConfig, root_version: Option<&str>) -> Result<()> {
    let classes_dir = config::output_classes_dir(project);
    let release_dir = project.join("out").join("release");
    std::fs::create_dir_all(&release_dir)?;

    let effective_version = cfg.version.as_deref()
        .or(root_version)
        .unwrap_or("0.0.0");
    let jar_name = format!("{}-{}.jar", cfg.name, effective_version);
    let jar_path = release_dir.join(&jar_name);

    let pack_start = Instant::now();

    // Build JAR from out/classes (already includes copied resources)
    let mut cmd = std::process::Command::new("jar");
    cmd.arg("cf").arg(&jar_path).arg("-C").arg(&classes_dir).arg(".");

    let status = cmd.status()?;
    if !status.success() {
        bail!("Failed to create library JAR for {}", cfg.name);
    }

    let jar_size = std::fs::metadata(&jar_path).map(|m| m.len()).unwrap_or(0);
    let size_str = if jar_size >= 1024 * 1024 {
        format!("{:.1} MB", jar_size as f64 / (1024.0 * 1024.0))
    } else if jar_size >= 1024 {
        format!("{:.1} KB", jar_size as f64 / 1024.0)
    } else {
        format!("{} B", jar_size)
    };
    println!(
        "{} {} ({}) {:>10}",
        style(format!("{:>12}", "Packaging")).green().bold(),
        jar_name,
        size_str,
        style(format!("{:.0}ms", pack_start.elapsed().as_millis())).dim()
    );
    println!(
        "{} {}",
        style(format!("{:>12}", "")).dim(),
        style(release_dir.display()).dim()
    );

    Ok(())
}

/// Build a Spring Boot nested JAR (executable) containing loader classes at the root,
/// application classes under BOOT-INF/classes/, and dependency JARs (STORED) under BOOT-INF/lib/.
pub(crate) fn build_release_jar(project: &Path, cfg: &YmConfig, jars: &[PathBuf], output_base: Option<&Path>, root_version: Option<&str>) -> Result<()> {
    use std::io::{Read, Write};

    let out = config::output_classes_dir(project);
    let base = output_base.unwrap_or(project);
    let release_dir = base.join("out").join("release");
    std::fs::create_dir_all(&release_dir)?;

    let effective_version = cfg.version.as_deref()
        .or(root_version)
        .unwrap_or("0.0.0");
    let jar_name = format!("{}-{}.jar", cfg.name, effective_version);
    let jar_path = release_dir.join(&jar_name);

    let pack_start = Instant::now();

    // ── Step 1: Find spring-boot-loader JAR ──────────────────────────────
    let loader_jar: Option<PathBuf> = {
        // First, look in the jars list
        let from_jars = jars.iter().find(|j| {
            let name = j.file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default();
            name.contains("spring-boot-loader") && !name.contains("spring-boot-loader-tools")
        });
        if let Some(found) = from_jars {
            Some(found.clone())
        } else {
            // Detect spring-boot version from spring-boot-autoconfigure JAR
            let version = jars.iter().find_map(|j| {
                let name = j.file_name()?.to_string_lossy().to_string();
                let prefix = "spring-boot-autoconfigure-";
                if name.starts_with(prefix) && name.ends_with(".jar") {
                    Some(name[prefix.len()..name.len() - 4].to_string())
                } else {
                    None
                }
            });
            if let Some(ver) = version {
                let cache_dir = dirs::home_dir()
                    .expect("Cannot determine home directory")
                    .join(crate::config::CACHE_DIR)
                    .join(crate::config::MAVEN_CACHE_DIR)
                    .join("org.springframework.boot")
                    .join("spring-boot-loader");
                // Try to find the loader JAR in the cache, or download it
                let candidate = cache_dir.join(&ver).join(format!("spring-boot-loader-{}.jar", ver));
                if candidate.exists() {
                    Some(candidate)
                } else {
                    // Download from Maven Central
                    let url = format!(
                        "https://repo1.maven.org/maven2/org/springframework/boot/spring-boot-loader/{}/spring-boot-loader-{}.jar",
                        ver, ver
                    );
                    let dest_dir = cache_dir.join(&ver);
                    let _ = std::fs::create_dir_all(&dest_dir);
                    let dest = dest_dir.join(format!("spring-boot-loader-{}.jar", ver));
                    println!(
                        "{} spring-boot-loader-{}.jar from Maven Central",
                        style(format!("{:>12}", "Downloading")).green().bold(),
                        ver
                    );
                    match reqwest::blocking::get(&url) {
                        Ok(resp) if resp.status().is_success() => {
                            match resp.bytes() {
                                Ok(bytes) => {
                                    let _ = std::fs::write(&dest, &bytes);
                                    if dest.exists() { Some(dest) } else { None }
                                }
                                Err(_) => None,
                            }
                        }
                        _ => None,
                    }
                }
            } else {
                None
            }
        }
    };

    // Detect Spring Boot version for MANIFEST.MF
    let spring_boot_version: String = jars.iter().find_map(|j| {
        let name = j.file_name()?.to_string_lossy().to_string();
        let prefix = "spring-boot-autoconfigure-";
        if name.starts_with(prefix) && name.ends_with(".jar") {
            Some(name[prefix.len()..name.len() - 4].to_string())
        } else {
            None
        }
    }).unwrap_or_else(|| "unknown".to_string());

    // If no loader JAR found, fall back to old flat JAR behavior with warning
    if loader_jar.is_none() {
        println!(
            "{} spring-boot-loader JAR not found, falling back to flat JAR packaging",
            style(format!("{:>12}", "warning")).yellow().bold(),
        );
        return build_release_jar_flat(project, cfg, jars, output_base, root_version);
    }
    let loader_jar = loader_jar.unwrap();

    // Determine loader JAR filename so we can exclude it from BOOT-INF/lib/
    let loader_jar_filename = loader_jar.file_name()
        .map(|n| n.to_string_lossy().to_string())
        .unwrap_or_default();

    // ── Step 2: Create the output JAR directly (no staging directory) ────
    let jar_file = std::fs::File::create(&jar_path)?;
    let mut zip_writer = zip::ZipWriter::new(std::io::BufWriter::new(jar_file));
    let deflated_options = zip::write::SimpleFileOptions::default()
        .compression_method(zip::CompressionMethod::Deflated);
    let stored_options = zip::write::SimpleFileOptions::default()
        .compression_method(zip::CompressionMethod::Stored);

    let total_deps = jars.len();

    // ── Step 3: Write META-INF/MANIFEST.MF (must be first entry) ─────────
    eprint!(
        "\r{} {} [manifest] {:.1}s   ",
        style(format!("{:>12}", "Packaging")).green().bold(),
        jar_name,
        pack_start.elapsed().as_secs_f64()
    );

    let main_class = cfg.main.as_deref().unwrap_or("com.example.Application");
    let manifest = format!(
        "Manifest-Version: 1.0\n\
         Main-Class: org.springframework.boot.loader.launch.JarLauncher\n\
         Start-Class: {}\n\
         Spring-Boot-Version: {}\n\
         Spring-Boot-Classes: BOOT-INF/classes/\n\
         Spring-Boot-Lib: BOOT-INF/lib/\n\
         Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx\n\
         Spring-Boot-Layers-Index: BOOT-INF/layers.idx\n\
         Implementation-Title: {}\n\
         Implementation-Version: {}\n\
         Build-Jdk-Spec: 25\n\
         Built-By: ym {}\n\n",
        main_class, spring_boot_version, cfg.name, effective_version, env!("CARGO_PKG_VERSION")
    );

    zip_writer.add_directory("META-INF/", deflated_options)?;
    zip_writer.start_file("META-INF/MANIFEST.MF", deflated_options)?;
    zip_writer.write_all(manifest.as_bytes())?;

    // ── Step 4: Write FileSystemProvider service file ─────────────────────
    zip_writer.add_directory("META-INF/services/", deflated_options)?;
    zip_writer.start_file("META-INF/services/java.nio.file.spi.FileSystemProvider", deflated_options)?;
    zip_writer.write_all(b"org.springframework.boot.loader.nio.file.NestedFileSystemProvider\n")?;

    // ── Step 5: Extract loader classes from spring-boot-loader JAR ───────
    eprint!(
        "\r{} {} [loader classes] {:.1}s   ",
        style(format!("{:>12}", "Packaging")).green().bold(),
        jar_name,
        pack_start.elapsed().as_secs_f64()
    );
    {
        let loader_file = std::fs::File::open(&loader_jar)
            .with_context(|| format!("Failed to open spring-boot-loader JAR: {}", loader_jar.display()))?;
        let mut loader_archive = zip::ZipArchive::new(std::io::BufReader::new(loader_file))?;

        // Collect directory entries to add them first
        let mut loader_dirs: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
        for i in 0..loader_archive.len() {
            let entry = match loader_archive.by_index(i) {
                Ok(e) => e,
                Err(_) => continue,
            };
            let name = entry.name().to_string();
            if name.starts_with("org/springframework/boot/loader/") {
                // Collect parent directories
                let mut parts: Vec<&str> = name.split('/').collect();
                if !entry.is_dir() {
                    parts.pop(); // remove filename
                }
                let mut dir = String::new();
                for part in parts {
                    if part.is_empty() { continue; }
                    dir.push_str(part);
                    dir.push('/');
                    loader_dirs.insert(dir.clone());
                }
            }
        }
        // Also add "org/" and "org/springframework/" etc.
        loader_dirs.insert("org/".to_string());
        loader_dirs.insert("org/springframework/".to_string());
        loader_dirs.insert("org/springframework/boot/".to_string());
        loader_dirs.insert("org/springframework/boot/loader/".to_string());

        for dir in &loader_dirs {
            let _ = zip_writer.add_directory(dir, deflated_options);
        }

        for i in 0..loader_archive.len() {
            let mut entry = match loader_archive.by_index(i) {
                Ok(e) => e,
                Err(_) => continue,
            };
            let name = entry.name().to_string();
            // Only extract org/springframework/boot/loader/** class files
            if !name.starts_with("org/springframework/boot/loader/") || entry.is_dir() {
                continue;
            }
            zip_writer.start_file(&name, deflated_options)?;
            let mut buf = Vec::new();
            entry.read_to_end(&mut buf)?;
            zip_writer.write_all(&buf)?;
        }
    }

    // ── Step 6: Write BOOT-INF/classes/ (application classes + resources) ─
    eprint!(
        "\r{} {} [app classes] {:.1}s   ",
        style(format!("{:>12}", "Packaging")).green().bold(),
        jar_name,
        pack_start.elapsed().as_secs_f64()
    );

    zip_writer.add_directory("BOOT-INF/", deflated_options)?;
    zip_writer.add_directory("BOOT-INF/classes/", deflated_options)?;

    if out.exists() {
        for walk_entry in walkdir::WalkDir::new(&out).sort_by_file_name() {
            let walk_entry = walk_entry?;
            let path = walk_entry.path();
            let relative = path.strip_prefix(&out)?;
            let name = relative.to_string_lossy().replace('\\', "/");
            if name.is_empty() {
                continue;
            }

            let boot_name = format!("BOOT-INF/classes/{}", name);
            if walk_entry.file_type().is_dir() {
                let dir_name = if boot_name.ends_with('/') { boot_name } else { format!("{}/", boot_name) };
                zip_writer.add_directory(dir_name, deflated_options)?;
            } else {
                zip_writer.start_file(&boot_name, deflated_options)?;
                let mut f = std::fs::File::open(path)?;
                std::io::copy(&mut f, &mut zip_writer)?;
            }
        }
    }

    // ── Step 7: Write BOOT-INF/lib/ (dependency JARs as STORED entries) ──
    zip_writer.add_directory("BOOT-INF/lib/", stored_options)?;

    let mut classpath_entries: Vec<String> = Vec::new();
    let mut dep_jar_filenames: Vec<String> = Vec::new();

    for (idx, dep) in jars.iter().enumerate() {
        if !dep.exists() || dep.is_dir() {
            continue;
        }

        let dep_filename = dep.file_name()
            .map(|n| n.to_string_lossy().to_string())
            .unwrap_or_else(|| dep.display().to_string());

        // Skip the loader JAR itself — its classes are already at the root
        if dep_filename == loader_jar_filename {
            continue;
        }

        // Skip non-JAR files
        if !dep_filename.ends_with(".jar") {
            continue;
        }

        eprint!(
            "\r{} {} [{}/{}] {:.1}s   ",
            style(format!("{:>12}", "Packaging")).green().bold(),
            jar_name,
            idx + 1,
            total_deps,
            pack_start.elapsed().as_secs_f64()
        );

        let lib_entry_name = format!("BOOT-INF/lib/{}", dep_filename);

        // Read the entire JAR file and write as STORED entry
        let mut jar_bytes = Vec::new();
        let mut f = std::fs::File::open(dep)?;
        f.read_to_end(&mut jar_bytes)?;

        zip_writer.start_file(&lib_entry_name, stored_options)?;
        zip_writer.write_all(&jar_bytes)?;

        classpath_entries.push(format!("- \"BOOT-INF/lib/{}\"", dep_filename));
        dep_jar_filenames.push(dep_filename);
    }

    // Clear progress line
    eprint!("\r{}\r", " ".repeat(80));

    // ── Step 8: Write BOOT-INF/classpath.idx ─────────────────────────────
    eprint!(
        "\r{} {} [classpath.idx] {:.1}s   ",
        style(format!("{:>12}", "Packaging")).green().bold(),
        jar_name,
        pack_start.elapsed().as_secs_f64()
    );

    let classpath_idx = classpath_entries.join("\n") + "\n";
    zip_writer.start_file("BOOT-INF/classpath.idx", deflated_options)?;
    zip_writer.write_all(classpath_idx.as_bytes())?;

    // ── Step 9: Write BOOT-INF/layers.idx ────────────────────────────────
    let mut layers_idx = String::new();
    layers_idx.push_str("- \"dependencies\":\n");
    for dep_name in &dep_jar_filenames {
        layers_idx.push_str(&format!("  - \"BOOT-INF/lib/{}\"\n", dep_name));
    }
    layers_idx.push_str("- \"spring-boot-loader\":\n");
    layers_idx.push_str("  - \"org/\"\n");
    layers_idx.push_str("- \"snapshot-dependencies\":\n");
    layers_idx.push_str("- \"application\":\n");
    layers_idx.push_str("  - \"BOOT-INF/classes/\"\n");
    layers_idx.push_str("  - \"BOOT-INF/classpath.idx\"\n");
    layers_idx.push_str("  - \"BOOT-INF/layers.idx\"\n");
    layers_idx.push_str("  - \"META-INF/\"\n");

    zip_writer.start_file("BOOT-INF/layers.idx", deflated_options)?;
    zip_writer.write_all(layers_idx.as_bytes())?;

    // ── Step 10: Finalize ────────────────────────────────────────────────
    zip_writer.finish()?;

    let pack_elapsed = pack_start.elapsed();
    let jar_size = std::fs::metadata(&jar_path).map(|m| m.len()).unwrap_or(0);
    let size_str = if jar_size >= 1024 * 1024 {
        format!("{:.1} MB", jar_size as f64 / (1024.0 * 1024.0))
    } else {
        format!("{:.0} KB", jar_size as f64 / 1024.0)
    };
    eprint!("\r{}\r", " ".repeat(80));
    println!(
        "{} {} ({}) {:>22}ms",
        style(format!("{:>12}", "Packaging")).green().bold(),
        jar_name,
        size_str,
        pack_elapsed.as_millis()
    );

    Ok(())
}

/// Fallback: build a flat/uber JAR (used when spring-boot-loader is not found).
fn build_release_jar_flat(project: &Path, cfg: &YmConfig, jars: &[PathBuf], output_base: Option<&Path>, root_version: Option<&str>) -> Result<()> {
    use std::io::Read;

    let out = config::output_classes_dir(project);
    let base = output_base.unwrap_or(project);
    let release_dir = base.join("out").join("release");
    std::fs::create_dir_all(&release_dir)?;

    let effective_version = cfg.version.as_deref()
        .or(root_version)
        .unwrap_or("0.0.0");
    let jar_name = format!("{}-{}.jar", cfg.name, effective_version);
    let jar_path = release_dir.join(&jar_name);

    let staging = project.join("out").join(".release-staging");
    if staging.exists() {
        std::fs::remove_dir_all(&staging)?;
    }
    std::fs::create_dir_all(&staging)?;

    incremental::copy_dir_recursive(&out, &staging)?;

    let mut mergeable: std::collections::HashMap<String, Vec<String>> = std::collections::HashMap::new();

    let total_deps = jars.len();
    let pack_start = Instant::now();
    for (idx, dep) in jars.iter().enumerate() {
        if !dep.exists() {
            continue;
        }

        eprint!(
            "\r{} {} [{}/{}] {:.1}s   ",
            style(format!("{:>12}", "Packaging")).green().bold(),
            jar_name,
            idx + 1,
            total_deps,
            pack_start.elapsed().as_secs_f64()
        );

        if dep.is_dir() {
            incremental::copy_dir_recursive(dep, &staging)?;
        } else {
            let file = match std::fs::File::open(dep) {
                Ok(f) => f,
                Err(_) => continue,
            };
            let mut archive = match zip::ZipArchive::new(std::io::BufReader::new(file)) {
                Ok(a) => a,
                Err(_) => continue,
            };

            for i in 0..archive.len() {
                let mut entry = match archive.by_index(i) {
                    Ok(e) => e,
                    Err(_) => continue,
                };
                let name = entry.name().to_string();

                if name == "META-INF/MANIFEST.MF"
                    || name.starts_with('/')
                    || name.contains("..")
                    || (name.starts_with("META-INF/") && (
                        name.ends_with(".SF")
                        || name.ends_with(".DSA")
                        || name.ends_with(".RSA")
                        || name.ends_with(".EC")
                    ))
                {
                    continue;
                }

                let is_mergeable = !entry.is_dir() && (
                    name.starts_with("META-INF/services/") ||
                    name == "META-INF/spring.factories" ||
                    (name.starts_with("META-INF/spring/") && name.ends_with(".imports"))
                );
                if is_mergeable {
                    let mut content = String::new();
                    let _ = entry.read_to_string(&mut content);
                    mergeable.entry(name).or_default().push(content);
                    continue;
                }

                if entry.is_dir() {
                    let _ = std::fs::create_dir_all(staging.join(&name));
                } else {
                    let target = staging.join(&name);
                    if let Some(parent) = target.parent() {
                        let _ = std::fs::create_dir_all(parent);
                    }
                    if let Ok(mut out_file) = std::fs::File::create(&target) {
                        let _ = std::io::copy(&mut entry, &mut out_file);
                    }
                }
            }
        }
    }
    eprint!("\r{}\r", " ".repeat(60));

    for (meta_file, contents) in &mergeable {
        let merged_path = staging.join(meta_file);
        if let Some(parent) = merged_path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        if contents.len() == 1 {
            std::fs::write(&merged_path, &contents[0])?;
        } else {
            let mut seen = std::collections::HashSet::new();
            let mut merged = String::new();
            for content in contents {
                for line in content.lines() {
                    let trimmed = line.trim();
                    if !trimmed.is_empty() && !trimmed.starts_with('#') && seen.insert(trimmed.to_string()) {
                        merged.push_str(line);
                        merged.push('\n');
                    }
                }
            }
            std::fs::write(&merged_path, &merged)?;
        }
    }

    let _ = std::fs::remove_file(staging.join("META-INF").join("MANIFEST.MF"));

    let manifest_dir = staging.join("META-INF");
    std::fs::create_dir_all(&manifest_dir)?;
    let mut manifest = String::from("Manifest-Version: 1.0\n");
    if let Some(ref main) = cfg.main {
        manifest.push_str(&format!("Main-Class: {}\n", main));
    }
    manifest.push_str(&format!("Implementation-Title: {}\n", cfg.name));
    if let Some(ref ver) = cfg.version {
        manifest.push_str(&format!("Implementation-Version: {}\n", ver));
    }
    manifest.push_str(&format!("Implementation-Vendor: {}\n", cfg.group_id));
    manifest.push_str(&format!("Built-By: ym {}\n", env!("CARGO_PKG_VERSION")));
    manifest.push('\n');
    std::fs::write(manifest_dir.join("MANIFEST.MF"), &manifest)?;

    let jar_file = std::fs::File::create(&jar_path)?;
    let mut zip_writer = zip::ZipWriter::new(std::io::BufWriter::new(jar_file));
    let zip_options = zip::write::SimpleFileOptions::default();

    zip_writer.add_directory("META-INF/", zip_options)?;
    zip_writer.start_file("META-INF/MANIFEST.MF", zip_options)?;
    std::io::copy(
        &mut std::fs::File::open(manifest_dir.join("MANIFEST.MF"))?,
        &mut zip_writer,
    )?;

    for walk_entry in walkdir::WalkDir::new(&staging).sort_by_file_name() {
        let walk_entry = walk_entry?;
        let path = walk_entry.path();
        let relative = path.strip_prefix(&staging)?;
        let name = relative.to_string_lossy().replace('\\', "/");
        if name.is_empty() || name == "META-INF" || name == "META-INF/MANIFEST.MF" {
            continue;
        }

        if walk_entry.file_type().is_dir() {
            let dir_name = if name.ends_with('/') { name } else { format!("{}/", name) };
            zip_writer.add_directory(dir_name, zip_options)?;
        } else {
            zip_writer.start_file(&name, zip_options)?;
            let mut f = std::fs::File::open(path)?;
            std::io::copy(&mut f, &mut zip_writer)?;
        }
    }

    zip_writer.finish()?;

    let pack_elapsed = pack_start.elapsed();
    let jar_size = std::fs::metadata(&jar_path).map(|m| m.len()).unwrap_or(0);
    let size_str = if jar_size >= 1024 * 1024 {
        format!("{:.1} MB", jar_size as f64 / (1024.0 * 1024.0))
    } else {
        format!("{:.0} KB", jar_size as f64 / 1024.0)
    };
    eprint!("\r{}\r", " ".repeat(80));
    println!(
        "{} {} ({}) {:>22}ms",
        style(format!("{:>12}", "Packaging")).green().bold(),
        jar_name,
        size_str,
        pack_elapsed.as_millis()
    );

    let _ = std::fs::remove_dir_all(&staging);

    Ok(())
}


/// 通过插件系统执行打包。
/// 解析 plugins,下载插件 JAR,启动 JVM 执行 ConfigRunner,
/// 由插件决定如何打包(Spring Boot JAR、fat JAR 等)。
/// Ensure spring-boot-loader JAR is downloaded to Maven cache.
/// Called before plugin-based packaging so the plugin can find it.
fn ensure_spring_boot_loader(jars: &[PathBuf]) {
    // Detect spring-boot version from spring-boot-autoconfigure JAR
    let version = jars.iter().find_map(|j| {
        let name = j.file_name()?.to_string_lossy().to_string();
        let prefix = "spring-boot-autoconfigure-";
        if name.starts_with(prefix) && name.ends_with(".jar") {
            Some(name[prefix.len()..name.len() - 4].to_string())
        } else {
            None
        }
    });
    if let Some(ver) = version {
        let cache_dir = dirs::home_dir()
            .expect("Cannot determine home directory")
            .join(crate::config::CACHE_DIR)
            .join(crate::config::MAVEN_CACHE_DIR)
            .join("org.springframework.boot")
            .join("spring-boot-loader");
        let candidate = cache_dir.join(&ver).join(format!("spring-boot-loader-{}.jar", ver));
        if !candidate.exists() {
            let url = format!(
                "https://repo1.maven.org/maven2/org/springframework/boot/spring-boot-loader/{}/spring-boot-loader-{}.jar",
                ver, ver
            );
            let dest_dir = cache_dir.join(&ver);
            let _ = std::fs::create_dir_all(&dest_dir);
            println!(
                "{} spring-boot-loader-{}.jar from Maven Central",
                style(format!("{:>12}", "Downloading")).green().bold(),
                ver
            );
            if let Ok(resp) = reqwest::blocking::get(&url) {
                if resp.status().is_success() {
                    if let Ok(bytes) = resp.bytes() {
                        let _ = std::fs::write(&candidate, &bytes);
                    }
                }
            }
        }
    }
}

pub fn build_with_plugins(
    project: &Path,
    cfg: &YmConfig,
    runtime_jars: &[PathBuf],
    root_version: Option<&str>,
) -> Result<()> {
    let out = config::output_classes_dir(project);
    let resources_dir = project.join("src").join("main").join("resources");
    let effective_version = cfg.version.as_deref()
        .or(root_version)
        .unwrap_or("0.0.0");
    let jar_name = format!("{}-{}.jar", cfg.name, effective_version);

    println!(
        "{} {} (plugins)",
        style(format!("{:>12}", "Packaging")).green().bold(),
        jar_name,
    );

    // 收集插件 JAR 的 classpath
    let plugin_cp = resolve_plugin_classpath(project, cfg)?;
    if plugin_cp.is_empty() {
        bail!("No plugin JARs found. Ensure plugins are installed.");
    }

    // runtime classpath 字符串
    let runtime_cp: String = runtime_jars.iter()
        .filter(|j| j.exists())
        .map(|j| j.to_string_lossy().to_string())
        .collect::<Vec<_>>()
        .join(":");

    // java 可执行文件
    let java_home = jvm::ensure_jdk(cfg.target.as_deref().unwrap_or("25"), None, false)?;
    let java = if java_home.as_os_str() == "system" {
        PathBuf::from("java")
    } else {
        java_home.join("bin").join("java")
    };

    // ym.json 序列化为临时文件,传给 ConfigRunner
    let config_json_path = project.join("out").join(".ym-config.json");
    if let Some(parent) = config_json_path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let config_json = serde_json::to_string(cfg)?;
    std::fs::write(&config_json_path, &config_json)?;

    let pack_start = Instant::now();

    // 检查是否有 ym.config.java,复制为合法文件名后编译
    let ym_config_java = project.join("ym.config.java");
    let mut extra_cp = String::new();
    if ym_config_java.exists() {
        let config_out = project.join("out").join(".ym-config-classes");
        std::fs::create_dir_all(&config_out)?;

        // ym.config.java → YmConfig.java(合法 Java 文件名)
        let temp_source = config_out.join("YmConfig.java");
        std::fs::copy(&ym_config_java, &temp_source)?;

        let javac = if java_home.as_os_str() == "system" {
            PathBuf::from("javac")
        } else {
            java_home.join("bin").join("javac")
        };
        let javac_status = std::process::Command::new(&javac)
            .arg("--enable-preview")
            .arg("--source").arg("25")
            .arg("-cp").arg(&plugin_cp)
            .arg("-d").arg(&config_out)
            .arg(&temp_source)
            .status()?;
        if !javac_status.success() {
            bail!("Failed to compile ym.config.java");
        }
        extra_cp = format!(":{}", config_out.display());
    }

    // 调用 ym.internal.ConfigRunner
    let full_cp = format!("{}{}", plugin_cp, extra_cp);
    let status = std::process::Command::new(&java)
        .arg("--enable-preview")
        .arg("-cp").arg(&full_cp)
        .arg(format!("-Dym.project.dir={}", project.display()))
        .arg(format!("-Dym.config.json={}", config_json_path.display()))
        .arg(format!("-Dym.runtime.classpath={}", runtime_cp))
        .arg(format!("-Dym.project.name={}", cfg.name))
        .arg(format!("-Dym.project.version={}", effective_version))
        .arg("ym.internal.ConfigRunner")
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::inherit())
        .output()
        .with_context(|| "Failed to start JVM for plugin execution")?;

    if !status.status.success() {
        bail!("Plugin execution failed (exit code: {})", status.status.code().unwrap_or(-1));
    }

    // 解析 Build Plan JSON(stdout)
    let build_plan = String::from_utf8_lossy(&status.stdout);
    eprintln!(
        "{} Build Plan received ({} bytes)",
        style(format!("{:>12}", "Plugins")).cyan().bold(),
        build_plan.len()
    );

    // TODO: 解析 Build Plan JSON,执行 Task DAG
    // 当前阶段:直接交给插件 Task 在 JVM 中执行完成
    // 后续:ym 核心解析 DAG 并调度

    let pack_elapsed = pack_start.elapsed();
    let output_jar = project.join("out").join("release").join(&jar_name);
    if output_jar.exists() {
        let jar_size = std::fs::metadata(&output_jar).map(|m| m.len()).unwrap_or(0);
        let size_str = if jar_size >= 1024 * 1024 {
            format!("{:.1} MB", jar_size as f64 / (1024.0 * 1024.0))
        } else {
            format!("{:.0} KB", jar_size as f64 / 1024.0)
        };
        println!(
            "{} {} ({}) {:>22}ms",
            style(format!("{:>12}", "Packaged")).green().bold(),
            jar_name,
            size_str,
            pack_elapsed.as_millis()
        );
    }

    Ok(())
}

/// Resolve plugin classpath: extract plugin dependencies from devDependencies,
/// resolve their full transitive dependency tree via ym's Maven resolver,
/// and return the complete classpath string.
/// Package a workspace module's compiled classes + resources into a thin JAR.
/// Similar to Gradle's `jar` task. Output: <module>/out/release/<name>.thin.<version>.jar
fn package_thin_jar(project: &Path, cfg: &config::schema::YmConfig, version: &str) -> Result<PathBuf> {
    let classes_dir = config::output_classes_dir(project);
    let jar_name = format!("{}.thin.{}.jar", cfg.name, version);
    let release_dir = project.join("out").join("release");
    std::fs::create_dir_all(&release_dir)?;
    let jar_path = release_dir.join(&jar_name);

    // Skip if up-to-date (thin JAR newer than classes dir)
    if jar_path.exists() {
        let jar_mtime = std::fs::metadata(&jar_path).and_then(|m| m.modified()).ok();
        let classes_mtime = walkdir::WalkDir::new(&classes_dir)
            .into_iter()
            .flatten()
            .filter(|e| e.file_type().is_file())
            .filter_map(|e| e.metadata().ok()?.modified().ok())
            .max();
        if let (Some(jm), Some(cm)) = (jar_mtime, classes_mtime) {
            if jm >= cm {
                return Ok(jar_path);
            }
        }
    }

    write_classes_jar(&jar_path, &classes_dir, &cfg.name, version)?;
    Ok(jar_path)
}

fn resolve_plugin_classpath(project: &Path, cfg: &YmConfig) -> Result<String> {
    // Build a config that contains only plugin-related devDependencies
    let mut plugin_cfg = cfg.clone();
    let mut plugin_deps = std::collections::BTreeMap::new();

    if let Some(ref dev_deps) = cfg.dev_dependencies {
        for (key, value) in dev_deps {
            let artifact_id = artifact_id_from_key(key);
            if artifact_id.contains("yummy-plugin") {
                plugin_deps.insert(key.clone(), value.clone());
            }
        }
    }
    if let Some(ref deps) = cfg.dependencies {
        for (key, value) in deps {
            let artifact_id = artifact_id_from_key(key);
            if artifact_id.contains("yummy-plugin") {
                plugin_deps.insert(key.clone(), value.clone());
            }
        }
    }

    if plugin_deps.is_empty() {
        return Ok(String::new());
    }

    // Use ym's standard dependency resolver to get the full transitive classpath
    plugin_cfg.dependencies = Some(plugin_deps);
    plugin_cfg.dev_dependencies = None;
    let jars = resolve_deps_with_scopes(project, &plugin_cfg, &["compile", "runtime"])?;

    Ok(jars.iter()
        .filter(|j| j.exists())
        .map(|j| j.to_string_lossy().to_string())
        .collect::<Vec<_>>()
        .join(":"))
}

/// Scan all yummy-plugin JARs in Maven cache for `dependencyManagement` declarations.
/// Downloads BOM POMs and extracts managed versions.
/// Plugin version is used as BOM version (e.g., yummy-plugin-spring-boot:4.0.3 → spring-boot-dependencies:4.0.3).
pub fn collect_plugin_managed_versions(project: &Path, cfg: &YmConfig) -> Result<std::collections::BTreeMap<String, String>> {
    use std::sync::Mutex;
    static LOGGED_BOMS: std::sync::LazyLock<Mutex<std::collections::HashSet<String>>> =
        std::sync::LazyLock::new(|| Mutex::new(std::collections::HashSet::new()));

    let mut managed = std::collections::BTreeMap::new();
    let cache_dir = config::maven_cache_dir();
    let plugin_base = cache_dir.join("sh.yummy");

    if !plugin_base.exists() { return Ok(managed); }

    // Scan sh.yummy/<artifact>/<version>/<artifact>-<version>.jar
    for artifact_dir in std::fs::read_dir(&plugin_base)?.flatten() {
        let artifact_name = artifact_dir.file_name().to_string_lossy().to_string();
        if !artifact_name.contains("yummy-plugin") { continue; }

        for version_dir in std::fs::read_dir(artifact_dir.path())?.flatten() {
            let version = version_dir.file_name().to_string_lossy().to_string();
            let jar_path = version_dir.path().join(format!("{}-{}.jar", artifact_name, version));
            if !jar_path.exists() { continue; }

            // Read plugin metadata from JAR
            let file = match std::fs::File::open(&jar_path) {
                Ok(f) => f,
                Err(_) => continue,
            };
            let mut archive = match zip::ZipArchive::new(std::io::BufReader::new(file)) {
                Ok(a) => a,
                Err(_) => continue,
            };

            for i in 0..archive.len() {
                let mut entry = match archive.by_index(i) {
                    Ok(e) => e,
                    Err(_) => continue,
                };
                let name = entry.name().to_string();
                if !name.starts_with("META-INF/ym-plugins/") || !name.ends_with(".json") { continue; }

                let mut content = String::new();
                use std::io::Read;
                let _ = entry.read_to_string(&mut content);

                // Extract dependencyManagement field (format: "groupId:artifactId" or "groupId:artifactId:version")
                let dm_coord = extract_json_field(&content, "dependencyManagement");
                if let Some(bom_ga) = dm_coord {
                    let bom_parts: Vec<&str> = bom_ga.split(':').collect();
                    if bom_parts.len() < 2 { continue; }

                    // BOM version: from metadata GAV if present, otherwise use plugin version
                    let bom_version = if bom_parts.len() >= 3 {
                        bom_parts[2].to_string()
                    } else {
                        version.clone()
                    };

                    let bom_pom_path = cache_dir
                        .join(bom_parts[0])
                        .join(bom_parts[1])
                        .join(&bom_version)
                        .join(format!("{}-{}.pom", bom_parts[1], bom_version));

                    // Download BOM POM if not cached
                    if !bom_pom_path.exists() {
                        let url = format!(
                            "https://repo1.maven.org/maven2/{}/{}/{}/{}-{}.pom",
                            bom_parts[0].replace('.', "/"),
                            bom_parts[1], bom_version, bom_parts[1], bom_version
                        );
                        if let Some(parent) = bom_pom_path.parent() {
                            let _ = std::fs::create_dir_all(parent);
                        }
                        if let Ok(client) = client_for_bom() {
                            if let Ok(resp) = client.get(&url).send() {
                                if resp.status().is_success() {
                                    if let Ok(bytes) = resp.bytes() {
                                        let _ = std::fs::write(&bom_pom_path, &bytes);
                                    }
                                }
                            }
                        }
                    }

                    // Parse BOM POM and extract managed versions
                    if bom_pom_path.exists() {
                        if let Ok(pom_content) = std::fs::read_to_string(&bom_pom_path) {
                            if let Ok(doc) = roxmltree::Document::parse(&pom_content) {
                                let props = crate::workspace::resolver::collect_pom_properties(&doc);
                                if let Ok(client) = client_for_bom() {
                                    let bom_managed = crate::workspace::resolver::collect_managed_versions_with_bom(
                                        &doc, &props, &client, &cache_dir,
                                        &cfg.registry_entries(), 0,
                                    );
                                    for (k, v) in bom_managed {
                                        managed.entry(k).or_insert(v);
                                    }
                                    let bom_key = format!("{}:{}", bom_ga, managed.len());
                                    if LOGGED_BOMS.lock().unwrap().insert(bom_key) {
                                        eprintln!(
                                            "{} Applied {} ({} version constraints)",
                                            console::style(format!("{:>12}", "BOM")).cyan().bold(),
                                            bom_ga,
                                            managed.len()
                                        );
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    Ok(managed)
}

fn client_for_bom() -> Result<reqwest::blocking::Client> {
    Ok(reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(30))
        .build()?)
}

fn extract_json_field(json: &str, field: &str) -> Option<String> {
    let pattern = format!("\"{}\"", field);
    let idx = json.find(&pattern)?;
    let rest = &json[idx + pattern.len()..];
    let colon = rest.find(':')?;
    let rest = &rest[colon + 1..];
    let quote_start = rest.find('"')?;
    let rest = &rest[quote_start + 1..];
    let quote_end = rest.find('"')?;
    Some(rest[..quote_end].to_string())
}

/// Compute a fingerprint for packaging inputs (class files, dependencies, resources, config).
/// If all inputs are unchanged and the output JAR exists, packaging can be skipped.
fn compute_packaging_fingerprint(
    class_dir: &Path,
    resource_dir: &Path,
    dep_jars: &[PathBuf],
    cfg: &YmConfig,
) -> Result<String> {
    use sha2::{Digest, Sha256};
    let mut hasher = Sha256::new();

    // 1. Dependency JARs: path + size (sorted for determinism)
    let mut dep_entries: Vec<(String, u64)> = Vec::new();
    for jar in dep_jars {
        if let Ok(meta) = std::fs::metadata(jar) {
            dep_entries.push((jar.to_string_lossy().to_string(), meta.len()));
        }
    }
    dep_entries.sort_by(|a, b| a.0.cmp(&b.0));
    for (path, size) in &dep_entries {
        hasher.update(b"dep:");
        hasher.update(path.as_bytes());
        hasher.update(&size.to_le_bytes());
    }

    // 2. Class files: path + size + mtime (sorted)
    if class_dir.exists() {
        let mut class_entries: Vec<(String, u64, u64)> = Vec::new();
        for entry in walkdir::WalkDir::new(class_dir).sort_by_file_name() {
            let entry = entry?;
            if entry.file_type().is_file() {
                let rel = entry.path().strip_prefix(class_dir)
                    .unwrap_or(entry.path())
                    .to_string_lossy().to_string();
                let meta = entry.metadata()?;
                let mtime = meta.modified().ok()
                    .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
                    .map(|d| d.as_secs())
                    .unwrap_or(0);
                class_entries.push((rel, meta.len(), mtime));
            }
        }
        for (path, size, mtime) in &class_entries {
            hasher.update(b"cls:");
            hasher.update(path.as_bytes());
            hasher.update(&size.to_le_bytes());
            hasher.update(&mtime.to_le_bytes());
        }
    }

    // 3. Resource files: path + size + mtime (sorted)
    if resource_dir.exists() {
        let mut res_entries: Vec<(String, u64, u64)> = Vec::new();
        for entry in walkdir::WalkDir::new(resource_dir).sort_by_file_name() {
            let entry = entry?;
            if entry.file_type().is_file() {
                let rel = entry.path().strip_prefix(resource_dir)
                    .unwrap_or(entry.path())
                    .to_string_lossy().to_string();
                let meta = entry.metadata()?;
                let mtime = meta.modified().ok()
                    .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
                    .map(|d| d.as_secs())
                    .unwrap_or(0);
                res_entries.push((rel, meta.len(), mtime));
            }
        }
        for (path, size, mtime) in &res_entries {
            hasher.update(b"res:");
            hasher.update(path.as_bytes());
            hasher.update(&size.to_le_bytes());
            hasher.update(&mtime.to_le_bytes());
        }
    }

    // 4. Config: main_class + version
    if let Some(ref main) = cfg.main {
        hasher.update(b"main:");
        hasher.update(main.as_bytes());
    }
    if let Some(ref ver) = cfg.version {
        hasher.update(b"ver:");
        hasher.update(ver.as_bytes());
    }
    hasher.update(b"name:");
    hasher.update(cfg.name.as_bytes());
    hasher.update(b"group:");
    hasher.update(cfg.group_id.as_bytes());

    Ok(format!("{:x}", hasher.finalize()))
}

/// Check if packaging can be skipped by comparing fingerprint with stored value.
fn should_skip_packaging(project: &Path, fingerprint: &str, output_jar: &Path) -> bool {
    if !output_jar.exists() {
        return false;
    }
    let name = project.file_name().unwrap_or_default().to_string_lossy().to_string();
    let fps = load_packaging_fingerprints(project);
    fps.get(&name).map(|s| s.as_str()) == Some(fingerprint)
}

/// Save the packaging fingerprint after a successful build.
fn save_packaging_fingerprint(project: &Path, fingerprint: &str) -> Result<()> {
    let name = project.file_name().unwrap_or_default().to_string_lossy().to_string();
    let fp_path = packaging_fingerprints_path(project);
    if let Some(parent) = fp_path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let mut fps = load_packaging_fingerprints(project);
    fps.insert(name, fingerprint.to_string());
    let json = serde_json::to_string_pretty(&fps)?;
    std::fs::write(&fp_path, json)?;
    Ok(())
}

fn packaging_fingerprints_path(project: &Path) -> PathBuf {
    let root = config::find_workspace_root(project).unwrap_or_else(|| project.to_path_buf());
    root.join(config::CACHE_DIR).join("packaging-fingerprints.json")
}

fn load_packaging_fingerprints(project: &Path) -> std::collections::HashMap<String, String> {
    let fp_path = packaging_fingerprints_path(project);
    std::fs::read_to_string(&fp_path)
        .ok()
        .and_then(|s| serde_json::from_str(&s).ok())
        .unwrap_or_default()
}

// Content-addressed cache replaces the old mtime+size workspace/module fingerprint system.
// Module-level cache keys are computed in the wave loop using:
//   compute_module_cache_key() + try_restore_module_cache() + save_module_cache()
// from compiler::incremental.

/// Resolve dependencies filtered by scope. Used for scope-specific classpath construction.
pub fn resolve_deps_with_scopes(project: &Path, cfg: &YmConfig, scopes: &[&str]) -> Result<Vec<PathBuf>> {
    use crate::workspace::resolver::RegistryEntry;
    let mut registries: Vec<RegistryEntry> = Vec::new();
    let mut resolutions = cfg.resolved_resolutions(cfg);

    // Apply BOM managed versions from plugins as constraints ("at least this version").
    // Unlike resolutions (forced), constraints only upgrade versions, never downgrade.
    // Artifacts not in the dependency tree are ignored (constraints don't introduce new deps).
    let constraints = collect_plugin_managed_versions(project, cfg).unwrap_or_default();

    // Resolve deps: if inside a workspace, resolve { workspace = true } from root
    let deps = if let Some(ws_root) = config::find_workspace_root(project) {
        if ws_root != project {
            let root_config_path = ws_root.join(config::CONFIG_FILE);
            if let Ok(root_cfg) = config::load_config(&root_config_path) {
                let errors = cfg.validate_workspace_deps(&root_cfg);
                if !errors.is_empty() {
                    anyhow::bail!("{}", errors.join("; "));
                }
                let mut d = cfg.maven_dependencies_for_scopes_with_root(scopes, &root_cfg);
                for (k, v) in root_cfg.resolved_resolutions(&root_cfg) {
                    if d.contains_key(&k) {
                        d.insert(k.clone(), v.clone());
                    }
                    resolutions.insert(k, v);
                }
                registries.extend(root_cfg.registry_entries());
                d
            } else {
                cfg.maven_dependencies_for_scopes(scopes)
            }
        } else {
            cfg.maven_dependencies_for_scopes(scopes)
        }
    } else {
        cfg.maven_dependencies_for_scopes(scopes)
    };

    let current_entries = cfg.registry_entries();
    for entry in current_entries {
        if !registries.iter().any(|e| e.url == entry.url) {
            registries.insert(0, entry);
        }
    }

    let cache = config::maven_cache_dir();

    if deps.is_empty() {
        // Even with no Maven deps, may have URL/Git/lib deps
        if scopes.contains(&"compile") {
            let mut jars = resolve_url_deps(cfg, &cache)?;
            jars.extend(resolve_git_deps(cfg, &cache)?);
            jars.extend(resolve_lib_dirs(project, cfg));
            return Ok(jars);
        }
        return Ok(vec![]);
    }

    let mut resolved = config::load_resolved_cache_checked(project, cfg)?;
    let mut exclusions = cfg.exclusions.as_ref().cloned().unwrap_or_default();
    exclusions.extend(cfg.per_dependency_exclusions());
    exclusions.extend(cfg.resolved_exclusions());
    // Also inherit exclusions from workspace root
    if let Some(ws_root) = config::find_workspace_root(project) {
        if ws_root != project {
            if let Ok(root_cfg) = config::load_config(&ws_root.join(config::CONFIG_FILE)) {
                exclusions.extend(root_cfg.exclusions.as_ref().cloned().unwrap_or_default());
                exclusions.extend(root_cfg.per_dependency_exclusions());
                exclusions.extend(root_cfg.resolved_exclusions());
            }
        }
    }

    // Build dep_scopes: map each direct dep's GA to its declared scope
    let dep_scopes = build_dep_scope_map(cfg, scopes);

    let jars = crate::workspace::resolver::resolve_and_download_with_constraints(
        &deps, &cache, &mut resolved, &registries, &exclusions, &resolutions, &constraints, &dep_scopes,
    )?;
    config::save_resolved_cache(project, &resolved)?;

    // Filter out JARs whose transitive scope doesn't match requested scopes
    let mut filtered = filter_jars_by_scope(&resolved, &jars, &cache, scopes);

    // Add URL/Git dependencies (they don't have Maven scope tracking,
    // so include them if compile scope is requested)
    if scopes.contains(&"compile") {
        filtered.extend(resolve_url_deps(cfg, &cache)?);
        filtered.extend(resolve_git_deps(cfg, &cache)?);
        filtered.extend(resolve_lib_dirs(project, cfg));
    }

    Ok(filtered)
}

/// Build a mapping of "groupId:artifactId" -> scope from the config's direct dependencies.
fn build_dep_scope_map(cfg: &YmConfig, _scopes: &[&str]) -> std::collections::HashMap<String, String> {
    use crate::config::schema::{DependencyValue, is_maven_dep};
    let mut map = std::collections::HashMap::new();
    // Process [dependencies]
    if let Some(ref deps) = cfg.dependencies {
        for (key, value) in deps {
            if !is_maven_dep(key) { continue; }
            let resolved = cfg.resolve_key(key);
            let scope = match value {
                DependencyValue::Simple(_) => "compile".to_string(),
                DependencyValue::Detailed(spec) => spec.scope.clone().unwrap_or_else(|| "compile".to_string()),
            };
            map.insert(resolved, scope);
        }
    }
    // Process [devDependencies] — effective scope "provided"
    if let Some(ref dev_deps) = cfg.dev_dependencies {
        for (key, _value) in dev_deps {
            if !is_maven_dep(key) { continue; }
            let resolved = cfg.resolve_key(key);
            map.insert(resolved, "provided".to_string());
        }
    }
    map
}

/// Filter JAR list by checking each JAR's scope in the resolved cache.
/// Only include JARs whose scope is in the allowed scopes list.
fn filter_jars_by_scope(
    resolved: &crate::config::schema::ResolvedCache,
    jars: &[std::path::PathBuf],
    cache: &std::path::Path,
    scopes: &[&str],
) -> Vec<std::path::PathBuf> {
    jars.iter()
        .filter(|jar| {
            // Extract versioned_key from jar path: cache/group/artifact/version/artifact-version.jar
            if let Some(vk) = jar_path_to_versioned_key(jar, cache) {
                if let Some(entry) = resolved.dependencies.get(&vk) {
                    if let Some(ref scope) = entry.scope {
                        return scopes.contains(&scope.as_str());
                    }
                }
            }
            // If no scope info, include by default (backwards compat)
            true
        })
        .cloned()
        .collect()
}

/// Convert a JAR path back to "groupId:artifactId:version" key.
fn jar_path_to_versioned_key(jar: &std::path::Path, cache: &std::path::Path) -> Option<String> {
    let rel = jar.strip_prefix(cache).ok()?;
    let components: Vec<_> = rel.components().collect();
    // Expected: group / artifact / version / artifact-version.jar
    if components.len() >= 3 {
        let group = components[0].as_os_str().to_string_lossy();
        let artifact = components[1].as_os_str().to_string_lossy();
        let version = components[2].as_os_str().to_string_lossy();
        Some(format!("{}:{}:{}", group, artifact, version))
    } else {
        None
    }
}

pub fn resolve_deps(project: &Path, cfg: &YmConfig) -> Result<Vec<PathBuf>> {
    use crate::workspace::resolver::RegistryEntry;
    let mut registries: Vec<RegistryEntry> = Vec::new();
    let mut resolutions = cfg.resolved_resolutions(cfg);

    // Resolve deps: if inside a workspace, resolve { workspace = true } from root
    let deps = if let Some(ws_root) = config::find_workspace_root(project) {
        if ws_root != project {
            let root_config_path = ws_root.join(config::CONFIG_FILE);
            if let Ok(root_cfg) = config::load_config(&root_config_path) {
                let errors = cfg.validate_workspace_deps(&root_cfg);
                if !errors.is_empty() {
                    anyhow::bail!("{}", errors.join("; "));
                }
                let mut d = cfg.maven_dependencies_with_root(&root_cfg);
                for (k, v) in root_cfg.resolved_resolutions(&root_cfg) {
                    if d.contains_key(&k) {
                        d.insert(k.clone(), v.clone());
                    }
                    resolutions.insert(k, v);
                }
                registries.extend(root_cfg.registry_entries());
                d
            } else {
                cfg.maven_dependencies()
            }
        } else {
            cfg.maven_dependencies()
        }
    } else {
        cfg.maven_dependencies()
    };

    // Collect registries from current config
    let current_entries = cfg.registry_entries();
    for entry in current_entries {
        if !registries.iter().any(|e| e.url == entry.url) {
            registries.insert(0, entry);
        }
    }

    let cache = config::maven_cache_dir();

    if deps.is_empty() {
        // Even with no Maven deps, may have URL/Git/lib deps
        let mut jars = resolve_url_deps(cfg, &cache)?;
        jars.extend(resolve_git_deps(cfg, &cache)?);
        jars.extend(resolve_lib_dirs(project, cfg));
        return Ok(jars);
    }

    let mut resolved = config::load_resolved_cache_checked(project, cfg)?;
    let mut exclusions = cfg.exclusions.as_ref().cloned().unwrap_or_default();
    exclusions.extend(cfg.per_dependency_exclusions());
    exclusions.extend(cfg.resolved_exclusions());
    // Also inherit exclusions from workspace root
    if let Some(ws_root) = config::find_workspace_root(project) {
        if ws_root != project {
            if let Ok(root_cfg) = config::load_config(&ws_root.join(config::CONFIG_FILE)) {
                exclusions.extend(root_cfg.exclusions.as_ref().cloned().unwrap_or_default());
                exclusions.extend(root_cfg.per_dependency_exclusions());
                exclusions.extend(root_cfg.resolved_exclusions());
            }
        }
    }

    // Build dep_scopes: map each direct dep's GA to its declared scope (all scopes)
    let dep_scopes = build_dep_scope_map(cfg, &["compile", "provided", "runtime", "test"]);
    let constraints = collect_plugin_managed_versions(project, cfg).unwrap_or_default();
    let jars = crate::workspace::resolver::resolve_and_download_with_constraints(
        &deps, &cache, &mut resolved, &registries, &exclusions, &resolutions, &constraints, &dep_scopes,
    )?;
    config::save_resolved_cache(project, &resolved)?;

    // Check for dependency version conflicts
    let conflicts = crate::workspace::resolver::check_conflicts(&resolved);
    if !conflicts.is_empty() {
        for (ga, versions) in &conflicts {
            eprintln!(
                "{} version conflict: {} has versions: {}",
                console::style(format!("{:>12}", "warning")).yellow().bold(),
                console::style(ga).bold(),
                versions.join(", ")
            );
        }
        eprintln!(
            "             Use [resolutions] in package.toml to pin a specific version"
        );
    }

    // Resolve URL dependencies
    let url_jars = resolve_url_deps(cfg, &cache)?;
    let mut all_jars = jars;
    all_jars.extend(url_jars);

    // Resolve Git dependencies
    let git_jars = resolve_git_deps(cfg, &cache)?;
    all_jars.extend(git_jars);

    // Add local lib directories
    all_jars.extend(resolve_lib_dirs(project, cfg));

    Ok(all_jars)
}

/// Like resolve_deps but skip JAR downloads. Returns expected cache paths.
/// Used by `ym idea --json` so importing is never blocked by network I/O.
/// Batch resolve for workspace: pre-loaded root config avoids repeated I/O.
/// Skips save_resolved_cache (read-only, for idea --json).
pub fn resolve_deps_no_download_with_root(
    project: &Path,
    cfg: &YmConfig,
    root_cfg: &YmConfig,
    shared_cache_dir: &Path,
    root_registries: &[crate::workspace::resolver::RegistryEntry],
    root_resolutions: &std::collections::BTreeMap<String, String>,
) -> Result<Vec<PathBuf>> {
    let mut registries: Vec<crate::workspace::resolver::RegistryEntry> = root_registries.to_vec();
    let mut resolutions = cfg.resolved_resolutions(cfg);
    for (k, v) in root_resolutions {
        resolutions.insert(k.clone(), v.clone());
    }

    let deps = cfg.maven_dependencies_with_root(root_cfg);

    let current_entries = cfg.registry_entries();
    for entry in current_entries {
        if !registries.iter().any(|e| e.url == entry.url) {
            registries.insert(0, entry);
        }
    }

    if deps.is_empty() {
        return Ok(resolve_lib_dirs(project, cfg));
    }

    let mut resolved = config::load_resolved_cache_checked(project, cfg)?;
    let mut exclusions = cfg.exclusions.as_ref().cloned().unwrap_or_default();
    exclusions.extend(cfg.per_dependency_exclusions());
    exclusions.extend(cfg.resolved_exclusions());

    let dep_scopes = build_dep_scope_map(cfg, &["compile", "provided", "runtime", "test"]);
    let jars = crate::workspace::resolver::resolve_no_download(
        &deps, shared_cache_dir, &mut resolved, &registries, &exclusions, &resolutions, &dep_scopes,
    )?;

    let mut all_jars = jars;
    all_jars.extend(resolve_lib_dirs(project, cfg));
    Ok(all_jars)
}

pub fn resolve_deps_no_download(project: &Path, cfg: &YmConfig) -> Result<Vec<PathBuf>> {
    use crate::workspace::resolver::RegistryEntry;
    let mut registries: Vec<RegistryEntry> = Vec::new();
    let mut resolutions = cfg.resolved_resolutions(cfg);

    let deps = if let Some(ws_root) = config::find_workspace_root(project) {
        if ws_root != project {
            let root_config_path = ws_root.join(config::CONFIG_FILE);
            if let Ok(root_cfg) = config::load_config(&root_config_path) {
                let errors = cfg.validate_workspace_deps(&root_cfg);
                if !errors.is_empty() {
                    anyhow::bail!("{}", errors.join("; "));
                }
                let mut d = cfg.maven_dependencies_with_root(&root_cfg);
                for (k, v) in root_cfg.resolved_resolutions(&root_cfg) {
                    if d.contains_key(&k) {
                        d.insert(k.clone(), v.clone());
                    }
                    resolutions.insert(k, v);
                }
                registries.extend(root_cfg.registry_entries());
                d
            } else {
                cfg.maven_dependencies()
            }
        } else {
            cfg.maven_dependencies()
        }
    } else {
        cfg.maven_dependencies()
    };

    let current_entries = cfg.registry_entries();
    for entry in current_entries {
        if !registries.iter().any(|e| e.url == entry.url) {
            registries.insert(0, entry);
        }
    }

    let cache = config::maven_cache_dir();

    if deps.is_empty() {
        let jars = resolve_lib_dirs(project, cfg);
        return Ok(jars);
    }

    let mut resolved = config::load_resolved_cache_checked(project, cfg)?;
    let mut exclusions = cfg.exclusions.as_ref().cloned().unwrap_or_default();
    exclusions.extend(cfg.per_dependency_exclusions());
    exclusions.extend(cfg.resolved_exclusions());

    let dep_scopes = build_dep_scope_map(cfg, &["compile", "provided", "runtime", "test"]);
    let jars = crate::workspace::resolver::resolve_no_download(
        &deps, &cache, &mut resolved, &registries, &exclusions, &resolutions, &dep_scopes,
    )?;
    config::save_resolved_cache(project, &resolved)?;

    let mut all_jars = jars;
    all_jars.extend(resolve_lib_dirs(project, cfg));

    Ok(all_jars)
}

pub fn compile_project(
    project: &Path,
    cfg: &YmConfig,
    classpath: &[PathBuf],
) -> Result<compiler::CompileResult> {
    let src = config::source_dir_for(project, cfg);
    let out = if let Some(custom) = OUTPUT_DIR_OVERRIDE.get() {
        PathBuf::from(custom)
    } else {
        config::output_classes_dir(project)
    };
    let cache = config::cache_dir(project);

    let encoding = cfg.compiler.as_ref().and_then(|c| c.encoding.clone());

    let ap_jars = resolve_annotation_processors(project, cfg, classpath)?;

    let lint = cfg.compiler.as_ref().and_then(|c| c.lint.clone()).unwrap_or_default();
    let mut extra_args = cfg.compiler.as_ref().and_then(|c| c.args.clone()).unwrap_or_else(|| {
        // Inherit compiler args from workspace root if not specified in module
        if let Some(ws_root) = config::find_workspace_root(project) {
            if let Ok(root_cfg) = config::load_config(&ws_root.join(config::CONFIG_FILE)) {
                return root_cfg.compiler.as_ref().and_then(|c| c.args.clone()).unwrap_or_default();
            }
        }
        Vec::new()
    });

    if is_strict() && !extra_args.iter().any(|a| a == "-Werror") {
        extra_args.push("-Werror".to_string());
    }

    let compile_cfg = compiler::CompileConfig {
        source_dirs: vec![src.clone()],
        output_dir: out.clone(),
        classpath: classpath.to_vec(),
        java_version: cfg.target.clone(),
        encoding,
        annotation_processors: ap_jars,
        lint,
        extra_args,
    };

    let custom_res_ext = cfg.compiler.as_ref().and_then(|c| c.resource_extensions.as_ref());
    let res_exclude = cfg.compiler.as_ref().and_then(|c| c.resource_exclude.as_ref());
    resources::copy_resources_with_extensions(&src, &out, custom_res_ext.map(|v| v.as_slice()), res_exclude.map(|v| v.as_slice()))?;

    let resources_dir = project.join("src").join("main").join("resources");
    if resources_dir.exists() {
        resources::copy_resources_with_extensions(&resources_dir, &out, custom_res_ext.map(|v| v.as_slice()), res_exclude.map(|v| v.as_slice()))?;
    }

    incremental::incremental_compile(&compile_cfg, &cache, None)
}

/// Compile a project using a compiler worker pool (for workspace builds).
pub fn compile_project_with_pool(
    project: &Path,
    cfg: &YmConfig,
    classpath: &[PathBuf],
    pool: Option<&compiler::worker::CompilerPool>,
) -> Result<compiler::CompileResult> {
    let src = config::source_dir_for(project, cfg);
    let out = if let Some(custom) = OUTPUT_DIR_OVERRIDE.get() {
        PathBuf::from(custom)
    } else {
        config::output_classes_dir(project)
    };
    let cache = config::cache_dir(project);

    let encoding = cfg.compiler.as_ref().and_then(|c| c.encoding.clone());
    let ap_jars = resolve_annotation_processors(project, cfg, classpath)?;
    let lint = cfg.compiler.as_ref().and_then(|c| c.lint.clone()).unwrap_or_default();
    let mut extra_args = cfg.compiler.as_ref().and_then(|c| c.args.clone()).unwrap_or_else(|| {
        if let Some(ws_root) = config::find_workspace_root(project) {
            if let Ok(root_cfg) = config::load_config(&ws_root.join(config::CONFIG_FILE)) {
                return root_cfg.compiler.as_ref().and_then(|c| c.args.clone()).unwrap_or_default();
            }
        }
        Vec::new()
    });
    if is_strict() && !extra_args.iter().any(|a| a == "-Werror") {
        extra_args.push("-Werror".to_string());
    }

    let compile_cfg = compiler::CompileConfig {
        source_dirs: vec![src.clone()],
        output_dir: out.clone(),
        classpath: classpath.to_vec(),
        java_version: cfg.target.clone(),
        encoding,
        annotation_processors: ap_jars,
        lint,
        extra_args,
    };

    let custom_res_ext = cfg.compiler.as_ref().and_then(|c| c.resource_extensions.as_ref());
    let res_exclude = cfg.compiler.as_ref().and_then(|c| c.resource_exclude.as_ref());
    resources::copy_resources_with_extensions(&src, &out, custom_res_ext.map(|v| v.as_slice()), res_exclude.map(|v| v.as_slice()))?;

    let resources_dir = project.join("src").join("main").join("resources");
    if resources_dir.exists() {
        resources::copy_resources_with_extensions(&resources_dir, &out, custom_res_ext.map(|v| v.as_slice()), res_exclude.map(|v| v.as_slice()))?;
    }

    incremental::incremental_compile(&compile_cfg, &cache, pool)
}

fn resolve_annotation_processors(project: &Path, cfg: &YmConfig, classpath: &[PathBuf]) -> Result<Vec<PathBuf>> {
    if let Some(coords) = cfg.compiler.as_ref().and_then(|c| c.annotation_processors.as_ref()) {
        if !coords.is_empty() {
            let deps = cfg.maven_dependencies();
            let cache = config::maven_cache_dir();
            let mut jars = Vec::new();
            for coord in coords {
                // Resolve @scope/name to groupId:artifactId for lookup
                let resolved = cfg.resolve_key(coord);
                if let Some(version) = deps.get(&resolved) {
                    // Direct version available — resolve from cache
                    let mc = crate::workspace::resolver::MavenCoord::parse(&resolved, version)?;
                    let jar = mc.jar_path(&cache);
                    if jar.exists() {
                        jars.push(jar);
                    }
                } else {
                    // Workspace-inherited dep: version not in local maven_dependencies().
                    // Fall back to searching the already-resolved classpath by artifactId.
                    let artifact_id = artifact_id_from_key(coord);
                    if let Some(jar) = classpath.iter().find(|p| {
                        p.file_name()
                            .and_then(|f| f.to_str())
                            .map(|f| f.starts_with(artifact_id))
                            .unwrap_or(false)
                    }) {
                        jars.push(jar.clone());
                    }
                }
            }
            return Ok(jars);
        }
    }

    // Auto-discover: only look in devDependencies jars (like Gradle's annotationProcessor config).
    // This prevents compile-scope jars (e.g. auto-service via selenium) from being accidentally
    // loaded as annotation processors when their own dependencies aren't on the processor path.
    let mut dev_artifact_ids = collect_dev_dependency_artifact_ids(cfg);
    // Inherit workspace root devDependencies if module has none
    if dev_artifact_ids.is_empty() {
        if let Some(ws_root) = config::find_workspace_root(project) {
            if let Ok(root_cfg) = config::load_config(&ws_root.join(config::CONFIG_FILE)) {
                dev_artifact_ids = collect_dev_dependency_artifact_ids(&root_cfg);
            }
        }
    }
    if dev_artifact_ids.is_empty() {
        return Ok(vec![]);
    }
    Ok(discover_annotation_processors_from_dev_deps(classpath, &dev_artifact_ids))
}

/// Collect artifact IDs from devDependencies for annotation processor filtering.
fn collect_dev_dependency_artifact_ids(cfg: &YmConfig) -> Vec<String> {
    let mut ids = Vec::new();
    if let Some(ref dev_deps) = cfg.dev_dependencies {
        for key in dev_deps.keys() {
            ids.push(artifact_id_from_key(key).to_string());
        }
    }
    ids
}

/// Discover annotation processors only from jars that match devDependencies artifact IDs.
fn discover_annotation_processors_from_dev_deps(classpath: &[PathBuf], dev_artifact_ids: &[String]) -> Vec<PathBuf> {
    classpath
        .iter()
        .filter(|jar| {
            let fname = jar.file_name().and_then(|f| f.to_str()).unwrap_or("");
            jar.extension().and_then(|e| e.to_str()) == Some("jar")
                && jar.exists()
                && dev_artifact_ids.iter().any(|id| fname.starts_with(id.as_str()))
                && has_annotation_processor(jar)
        })
        .cloned()
        .collect()
}

pub fn has_annotation_processor(jar_path: &Path) -> bool {
    let file = match std::fs::File::open(jar_path) {
        Ok(f) => f,
        Err(_) => return false,
    };
    let mut archive = match zip::ZipArchive::new(file) {
        Ok(a) => a,
        Err(_) => return false,
    };
    archive
        .by_name("META-INF/services/javax.annotation.processing.Processor")
        .is_ok()
}

/// Ensure the JDK is available based on config.
pub fn ensure_jdk_for_config(cfg: &YmConfig) -> Result<()> {
    // Priority: jvm.version > target > .java-version file > default "21"
    let version = cfg
        .jvm
        .as_ref()
        .and_then(|j| j.version.clone())
        .or_else(|| cfg.target.clone())
        .or_else(read_java_version_file)
        .unwrap_or_else(|| "21".to_string());

    let vendor = cfg.jvm.as_ref().and_then(|j| j.vendor.as_deref());
    let auto_download = cfg
        .jvm
        .as_ref()
        .and_then(|j| j.auto_download)
        .unwrap_or(false);

    let java_home = jvm::ensure_jdk(&version, vendor, auto_download)?;

    if java_home != Path::new("system") && java_home.exists() {
        if !crate::is_json_quiet() && is_verbose() {
            let jdk_name = java_home.file_name()
                .map(|n| n.to_string_lossy().to_string())
                .unwrap_or_else(|| format!("JDK {}", version));
            println!(
                "{} jdk {} ({})",
                style(format!("{:>12}", "Using")).green().bold(),
                &jdk_name,
                style(java_home.display()).dim()
            );
        }
        unsafe {
            std::env::set_var("JAVA_HOME", &java_home);
            let bin_dir = java_home.join("bin");
            if let Ok(current_path) = std::env::var("PATH") {
                let sep = if cfg!(windows) { ";" } else { ":" };
                std::env::set_var(
                    "PATH",
                    format!("{}{}{}", bin_dir.display(), sep, current_path),
                );
            }
        }
    } else if !crate::is_json_quiet() && is_verbose() {
        let javac_path = jvm::which_javac()
            .and_then(|p| p.parent().and_then(|b| b.parent()).map(|h| h.to_path_buf()));
        if let Some(home) = javac_path {
            let jdk_name = home.file_name()
                .map(|n| n.to_string_lossy().to_string())
                .unwrap_or_else(|| "System JDK".to_string());
            println!(
                "{} jdk {} ({})",
                style(format!("{:>12}", "Using")).green().bold(),
                &jdk_name,
                style(home.display()).dim()
            );
        }
    }

    Ok(())
}

/// Read `.java-version` file from project root (compatible with SDKMAN, jEnv).
fn read_java_version_file() -> Option<String> {
    let cwd = std::env::current_dir().ok()?;
    let file = cwd.join(".java-version");
    if file.exists() {
        let content = std::fs::read_to_string(&file).ok()?;
        let version = content.trim().to_string();
        if !version.is_empty() {
            return Some(version);
        }
    }
    None
}

/// Full compilation (ignoring incremental cache)
#[allow(dead_code)]
pub fn compile_project_full(
    project: &Path,
    cfg: &YmConfig,
    classpath: &[PathBuf],
) -> Result<compiler::CompileResult> {
    let src = config::source_dir_for(project, cfg);
    let out = config::output_classes_dir(project);

    let encoding = cfg.compiler.as_ref().and_then(|c| c.encoding.clone());

    let compile_cfg = compiler::CompileConfig {
        source_dirs: vec![src],
        output_dir: out,
        classpath: classpath.to_vec(),
        java_version: cfg.target.clone(),
        encoding,
        annotation_processors: vec![],
        lint: vec![],
        extra_args: vec![],
    };

    javac::compile(&compile_cfg)
}

/// Download URL dependencies and return their JAR paths
fn resolve_url_deps(cfg: &YmConfig, cache: &Path) -> Result<Vec<PathBuf>> {
    let url_deps = cfg.url_dependencies();
    if url_deps.is_empty() {
        return Ok(vec![]);
    }

    let jar_dir = cache.join("url-deps");
    std::fs::create_dir_all(&jar_dir)?;

    let mut jars = Vec::new();
    for (_key, url, _scope) in &url_deps {
        let filename = url.rsplit('/').next().unwrap_or("unknown.jar");
        let jar_path = jar_dir.join(filename);

        if !jar_path.exists() {
            if !crate::is_json_quiet() {
                println!(
                    "{} {}...",
                    console::style(format!("{:>12}", "Downloading")).green().bold(),
                    filename
                );
            }
            let client = reqwest::blocking::Client::builder()
                .user_agent(format!("ym/{}", env!("CARGO_PKG_VERSION")))
                .build()?;
            let response = client.get(url).send()?;
            if !response.status().is_success() {
                eprintln!(
                    "{} failed to download {}: HTTP {}",
                    console::style(format!("{:>12}", "warning")).yellow().bold(),
                    url,
                    response.status()
                );
                continue;
            }
            let bytes = response.bytes()?;
            std::fs::write(&jar_path, &bytes)?;
            if !crate::is_json_quiet() {
                println!(
                    "{} {}",
                    console::style(format!("{:>12}", "Downloaded")).green().bold(),
                    filename
                );
            }
        }

        jars.push(jar_path);
    }

    Ok(jars)
}

/// Clone Git dependencies and return their built JAR paths
fn resolve_git_deps(cfg: &YmConfig, cache: &Path) -> Result<Vec<PathBuf>> {
    let git_deps = cfg.git_dependencies();
    if git_deps.is_empty() {
        return Ok(vec![]);
    }

    let git_dir = cache.join("git-deps");
    std::fs::create_dir_all(&git_dir)?;

    let mut jars = Vec::new();
    for (name, git_url, git_ref, _scope) in &git_deps {
        let repo_dir = git_dir.join(name);

        // Clone or update
        if !repo_dir.exists() {
            if !crate::is_json_quiet() {
                println!(
                    "{} {}...",
                    console::style(format!("{:>12}", "Cloning")).green().bold(),
                    name
                );
            }
            let mut cmd = std::process::Command::new("git");
            cmd.arg("clone").arg("--depth").arg("1");
            if let Some(r) = git_ref {
                cmd.arg("--branch").arg(r);
            }
            cmd.arg(git_url).arg(&repo_dir);
            let status = cmd.status()?;
            if !status.success() {
                eprintln!(
                    "{} failed to clone {}",
                    console::style(format!("{:>12}", "warning")).yellow().bold(),
                    git_url
                );
                continue;
            }
        }

        // Look for output JARs: check if it has package.toml (ym project)
        let pkg_toml = repo_dir.join(config::CONFIG_FILE);
        if pkg_toml.exists() {
            // Build with ym
            if !crate::is_json_quiet() {
                println!(
                    "{} Git dependency {}...",
                    console::style(format!("{:>12}", "Building")).green().bold(),
                    name
                );
            }
            let status = std::process::Command::new("ymc")
                .arg("build")
                .current_dir(&repo_dir)
                .status();
            match status {
                Ok(s) if s.success() => {
                    // Collect output classes directory
                    let out = repo_dir.join("out").join("classes");
                    if out.exists() {
                        jars.push(out);
                    }
                }
                _ => {
                    eprintln!(
                        "{} failed to build Git dependency {}",
                        console::style(format!("{:>12}", "warning")).yellow().bold(),
                        name
                    );
                }
            }
        } else {
            // Look for pre-built JARs in the repo
            for entry in std::fs::read_dir(&repo_dir)? {
                let entry = entry?;
                if entry.path().extension().is_some_and(|e| e == "jar") {
                    jars.push(entry.path());
                }
            }
        }
    }

    Ok(jars)
}

/// Scan `compiler.libs` directories for JAR files and return their paths.
pub fn resolve_lib_dirs(project: &Path, cfg: &YmConfig) -> Vec<PathBuf> {
    let lib_dirs = match cfg.compiler.as_ref().and_then(|c| c.libs.as_ref()) {
        Some(dirs) => dirs,
        None => return vec![],
    };

    let mut jars = Vec::new();
    for dir in lib_dirs {
        let abs_dir = if std::path::Path::new(dir).is_absolute() {
            PathBuf::from(dir)
        } else {
            project.join(dir)
        };
        if !abs_dir.exists() {
            continue;
        }
        if let Ok(entries) = std::fs::read_dir(&abs_dir) {
            for entry in entries.flatten() {
                let path = entry.path();
                if path.extension().is_some_and(|e| e == "jar") {
                    jars.push(path);
                }
            }
        }
    }
    jars
}

/// Write a standard JAR from a classes directory, including META-INF/MANIFEST.MF
/// and proper directory entries (required for Spring Boot nested JAR scanning).
pub fn write_classes_jar(
    jar_path: &Path,
    classes_dir: &Path,
    name: &str,
    version: &str,
) -> anyhow::Result<()> {
    let jar_file = std::fs::File::create(jar_path)?;
    let mut zip = zip::ZipWriter::new(std::io::BufWriter::new(jar_file));
    let options = zip::write::SimpleFileOptions::default();

    // META-INF/MANIFEST.MF
    zip.add_directory("META-INF/", options)?;
    zip.start_file("META-INF/MANIFEST.MF", options)?;
    std::io::Write::write_all(&mut zip, format!(
        "Manifest-Version: 1.0\nImplementation-Title: {}\nImplementation-Version: {}\nBuilt-By: ym\n",
        name, version
    ).as_bytes())?;

    // Classes + directory entries
    let mut class_count = 0u32;
    if classes_dir.exists() {
        let mut added_dirs = std::collections::HashSet::new();
        for entry in walkdir::WalkDir::new(classes_dir) {
            let entry = entry?;
            let path = entry.path();
            let rel = path.strip_prefix(classes_dir)?;
            let name = rel.to_string_lossy().replace('\\', "/");
            if name.is_empty() { continue; }

            if entry.file_type().is_dir() {
                let dir_name = if name.ends_with('/') { name } else { format!("{}/", name) };
                if added_dirs.insert(dir_name.clone()) {
                    zip.add_directory(&dir_name, options)?;
                }
            } else {
                if name.ends_with(".class") {
                    class_count += 1;
                }
                zip.start_file(&name, options)?;
                let mut f = std::fs::File::open(path)?;
                std::io::copy(&mut f, &mut zip)?;
            }
        }
    }

    if class_count == 0 {
        eprintln!(
            "  {} JAR for '{}' contains no .class files — classes dir '{}' is empty or missing. Skipping.",
            console::style("Warning:").yellow().bold(), name, classes_dir.display()
        );
    }

    zip.finish()?;
    Ok(())
}