17
17
package org .springframework .boot .build .mavenplugin ;
18
18
19
19
import java .io .File ;
20
- import java .io .FileInputStream ;
21
20
import java .io .FileWriter ;
22
21
import java .io .IOException ;
23
- import java .io .InputStream ;
24
22
import java .io .Writer ;
25
23
import java .nio .charset .StandardCharsets ;
26
24
import java .nio .file .Files ;
27
25
import java .nio .file .Path ;
28
26
import java .nio .file .StandardCopyOption ;
29
27
import java .util .Arrays ;
30
28
import java .util .Properties ;
31
- import java .util .function . BiConsumer ;
29
+ import java .util .Set ;
32
30
33
31
import javax .inject .Inject ;
34
- import javax .xml .parsers .DocumentBuilder ;
35
- import javax .xml .parsers .DocumentBuilderFactory ;
36
- import javax .xml .parsers .ParserConfigurationException ;
37
- import javax .xml .xpath .XPath ;
38
- import javax .xml .xpath .XPathConstants ;
39
- import javax .xml .xpath .XPathExpressionException ;
40
- import javax .xml .xpath .XPathFactory ;
41
32
42
33
import io .spring .javaformat .formatter .FileEdit ;
43
34
import io .spring .javaformat .formatter .FileFormatter ;
88
79
import org .gradle .api .tasks .bundling .Jar ;
89
80
import org .gradle .api .tasks .javadoc .Javadoc ;
90
81
import org .gradle .external .javadoc .StandardJavadocDocletOptions ;
91
- import org .w3c .dom .Document ;
92
82
import org .w3c .dom .Element ;
93
83
import org .w3c .dom .Node ;
94
84
import org .w3c .dom .NodeList ;
95
- import org .xml .sax .SAXException ;
96
85
97
86
import org .springframework .boot .build .DeployedPlugin ;
98
87
import org .springframework .boot .build .MavenRepositoryPlugin ;
88
+ import org .springframework .boot .build .bom .ResolvedBom ;
89
+ import org .springframework .boot .build .bom .ResolvedBom .ResolvedLibrary ;
99
90
import org .springframework .boot .build .optional .OptionalDependenciesPlugin ;
100
91
import org .springframework .boot .build .test .DockerTestPlugin ;
101
92
import org .springframework .boot .build .test .IntegrationTestPlugin ;
102
93
import org .springframework .core .CollectionFactory ;
103
- import org .springframework .util .Assert ;
104
94
105
95
/**
106
96
* Plugin for building Spring Boot's Maven Plugin.
@@ -125,7 +115,13 @@ public void apply(Project project) {
125
115
generateHelpMojoTask );
126
116
addDocumentPluginGoalsTask (project , generatePluginDescriptorTask );
127
117
addPrepareMavenBinariesTask (project );
128
- addExtractVersionPropertiesTask (project );
118
+ TaskProvider <ExtractVersionProperties > extractVersionPropertiesTask = addExtractVersionPropertiesTask (project );
119
+ project .getTasks ()
120
+ .named (IntegrationTestPlugin .INT_TEST_TASK_NAME )
121
+ .configure ((task ) -> task .getInputs ()
122
+ .file (extractVersionPropertiesTask .map (ExtractVersionProperties ::getDestination ))
123
+ .withPathSensitivity (PathSensitivity .RELATIVE )
124
+ .withPropertyName ("versionProperties" ));
129
125
publishOptionalDependenciesInPom (project );
130
126
project .getTasks ().withType (GenerateModuleMetadata .class ).configureEach ((task ) -> task .setEnabled (false ));
131
127
}
@@ -335,9 +331,9 @@ private String replaceVersionPlaceholder(Project project, String input) {
335
331
return input .replace ("{{version}}" , project .getVersion ().toString ());
336
332
}
337
333
338
- private void addExtractVersionPropertiesTask (Project project ) {
339
- project .getTasks ().register ("extractVersionProperties" , ExtractVersionProperties .class , (task ) -> {
340
- task .setEffectiveBoms (project .getConfigurations ().create ("versionProperties" ));
334
+ private TaskProvider < ExtractVersionProperties > addExtractVersionPropertiesTask (Project project ) {
335
+ return project .getTasks ().register ("extractVersionProperties" , ExtractVersionProperties .class , (task ) -> {
336
+ task .setResolvedBoms (project .getConfigurations ().create ("versionProperties" ));
341
337
task .getDestination ()
342
338
.set (project .getLayout ()
343
339
.getBuildDirectory ()
@@ -466,25 +462,25 @@ public void createRepository() {
466
462
467
463
public abstract static class ExtractVersionProperties extends DefaultTask {
468
464
469
- private FileCollection effectiveBoms ;
465
+ private FileCollection resolvedBoms ;
470
466
471
467
@ InputFiles
472
468
@ PathSensitive (PathSensitivity .RELATIVE )
473
- public FileCollection getEffectiveBoms () {
474
- return this .effectiveBoms ;
469
+ public FileCollection getResolvedBoms () {
470
+ return this .resolvedBoms ;
475
471
}
476
472
477
- public void setEffectiveBoms (FileCollection effectiveBoms ) {
478
- this .effectiveBoms = effectiveBoms ;
473
+ public void setResolvedBoms (FileCollection resolvedBoms ) {
474
+ this .resolvedBoms = resolvedBoms ;
479
475
}
480
476
481
477
@ OutputFile
482
478
public abstract RegularFileProperty getDestination ();
483
479
484
480
@ TaskAction
485
481
public void extractVersionProperties () {
486
- EffectiveBom effectiveBom = new EffectiveBom (this .effectiveBoms .getSingleFile ());
487
- Properties versions = extractVersionProperties (effectiveBom );
482
+ ResolvedBom resolvedBom = ResolvedBom . readFrom (this .resolvedBoms .getSingleFile ());
483
+ Properties versions = extractVersionProperties (resolvedBom );
488
484
writeProperties (versions );
489
485
}
490
486
@@ -499,66 +495,18 @@ private void writeProperties(Properties versions) {
499
495
}
500
496
}
501
497
502
- private Properties extractVersionProperties (EffectiveBom effectiveBom ) {
498
+ private Properties extractVersionProperties (ResolvedBom resolvedBom ) {
503
499
Properties versions = CollectionFactory .createSortedProperties (true );
504
- versions .setProperty ("project.version" , effectiveBom .version ());
505
- effectiveBom .property ("log4j2.version" , versions ::setProperty );
506
- effectiveBom .property ("maven-jar-plugin.version" , versions ::setProperty );
507
- effectiveBom .property ("maven-war-plugin.version" , versions ::setProperty );
508
- effectiveBom .property ("build-helper-maven-plugin.version" , versions ::setProperty );
509
- effectiveBom .property ("spring-framework.version" , versions ::setProperty );
510
- effectiveBom .property ("jakarta-servlet.version" , versions ::setProperty );
511
- effectiveBom .property ("kotlin.version" , versions ::setProperty );
512
- effectiveBom .property ("assertj.version" , versions ::setProperty );
513
- effectiveBom .property ("junit-jupiter.version" , versions ::setProperty );
514
- return versions ;
515
- }
516
-
517
- }
518
-
519
- private static final class EffectiveBom {
520
-
521
- private final Document document ;
522
-
523
- private final XPath xpath ;
524
-
525
- private EffectiveBom (File bomFile ) {
526
- this .document = loadDocument (bomFile );
527
- this .xpath = XPathFactory .newInstance ().newXPath ();
528
- }
529
-
530
- private Document loadDocument (File bomFile ) {
531
- try {
532
- try (InputStream inputStream = new FileInputStream (bomFile )) {
533
- DocumentBuilderFactory builderFactory = DocumentBuilderFactory .newInstance ();
534
- DocumentBuilder builder = builderFactory .newDocumentBuilder ();
535
- return builder .parse (inputStream );
500
+ versions .setProperty ("project.version" , resolvedBom .id ().version ());
501
+ Set <String > versionProperties = Set .of ("log4j2.version" , "maven-jar-plugin.version" ,
502
+ "maven-war-plugin.version" , "build-helper-maven-plugin.version" , "spring-framework.version" ,
503
+ "jakarta-servlet.version" , "kotlin.version" , "assertj.version" , "junit-jupiter.version" );
504
+ for (ResolvedLibrary library : resolvedBom .libraries ()) {
505
+ if (library .versionProperty () != null && versionProperties .contains (library .versionProperty ())) {
506
+ versions .setProperty (library .versionProperty (), library .version ());
536
507
}
537
508
}
538
- catch (ParserConfigurationException | SAXException | IOException ex ) {
539
- throw new IllegalStateException (ex );
540
- }
541
- }
542
-
543
- private String version () {
544
- return get ("version" );
545
- }
546
-
547
- private void property (String name , BiConsumer <String , String > handler ) {
548
- handler .accept (name , get ("properties/" + name ));
549
- }
550
-
551
- private String get (String expression ) {
552
- try {
553
- Node node = (Node ) this .xpath .compile ("/project/" + expression )
554
- .evaluate (this .document , XPathConstants .NODE );
555
- String text = (node != null ) ? node .getTextContent () : null ;
556
- Assert .hasLength (text , () -> "No result for expression " + expression );
557
- return text ;
558
- }
559
- catch (XPathExpressionException ex ) {
560
- throw new IllegalStateException (ex );
561
- }
509
+ return versions ;
562
510
}
563
511
564
512
}
0 commit comments