1717package org .springframework .boot .build .mavenplugin ;
1818
1919import java .io .File ;
20- import java .io .FileInputStream ;
2120import java .io .FileWriter ;
2221import java .io .IOException ;
23- import java .io .InputStream ;
2422import java .io .Writer ;
2523import java .nio .charset .StandardCharsets ;
2624import java .nio .file .Files ;
2725import java .nio .file .Path ;
2826import java .nio .file .StandardCopyOption ;
2927import java .util .Arrays ;
3028import java .util .Properties ;
31- import java .util .function . BiConsumer ;
29+ import java .util .Set ;
3230
3331import 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 ;
4132
4233import io .spring .javaformat .formatter .FileEdit ;
4334import io .spring .javaformat .formatter .FileFormatter ;
8879import org .gradle .api .tasks .bundling .Jar ;
8980import org .gradle .api .tasks .javadoc .Javadoc ;
9081import org .gradle .external .javadoc .StandardJavadocDocletOptions ;
91- import org .w3c .dom .Document ;
9282import org .w3c .dom .Element ;
9383import org .w3c .dom .Node ;
9484import org .w3c .dom .NodeList ;
95- import org .xml .sax .SAXException ;
9685
9786import org .springframework .boot .build .DeployedPlugin ;
9887import org .springframework .boot .build .MavenRepositoryPlugin ;
88+ import org .springframework .boot .build .bom .ResolvedBom ;
89+ import org .springframework .boot .build .bom .ResolvedBom .ResolvedLibrary ;
9990import org .springframework .boot .build .optional .OptionalDependenciesPlugin ;
10091import org .springframework .boot .build .test .DockerTestPlugin ;
10192import org .springframework .boot .build .test .IntegrationTestPlugin ;
10293import org .springframework .core .CollectionFactory ;
103- import org .springframework .util .Assert ;
10494
10595/**
10696 * Plugin for building Spring Boot's Maven Plugin.
@@ -125,7 +115,13 @@ public void apply(Project project) {
125115 generateHelpMojoTask );
126116 addDocumentPluginGoalsTask (project , generatePluginDescriptorTask );
127117 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" ));
129125 publishOptionalDependenciesInPom (project );
130126 project .getTasks ().withType (GenerateModuleMetadata .class ).configureEach ((task ) -> task .setEnabled (false ));
131127 }
@@ -335,9 +331,9 @@ private String replaceVersionPlaceholder(Project project, String input) {
335331 return input .replace ("{{version}}" , project .getVersion ().toString ());
336332 }
337333
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" ));
341337 task .getDestination ()
342338 .set (project .getLayout ()
343339 .getBuildDirectory ()
@@ -466,25 +462,25 @@ public void createRepository() {
466462
467463 public abstract static class ExtractVersionProperties extends DefaultTask {
468464
469- private FileCollection effectiveBoms ;
465+ private FileCollection resolvedBoms ;
470466
471467 @ InputFiles
472468 @ PathSensitive (PathSensitivity .RELATIVE )
473- public FileCollection getEffectiveBoms () {
474- return this .effectiveBoms ;
469+ public FileCollection getResolvedBoms () {
470+ return this .resolvedBoms ;
475471 }
476472
477- public void setEffectiveBoms (FileCollection effectiveBoms ) {
478- this .effectiveBoms = effectiveBoms ;
473+ public void setResolvedBoms (FileCollection resolvedBoms ) {
474+ this .resolvedBoms = resolvedBoms ;
479475 }
480476
481477 @ OutputFile
482478 public abstract RegularFileProperty getDestination ();
483479
484480 @ TaskAction
485481 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 );
488484 writeProperties (versions );
489485 }
490486
@@ -499,66 +495,18 @@ private void writeProperties(Properties versions) {
499495 }
500496 }
501497
502- private Properties extractVersionProperties (EffectiveBom effectiveBom ) {
498+ private Properties extractVersionProperties (ResolvedBom resolvedBom ) {
503499 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 ());
536507 }
537508 }
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 ;
562510 }
563511
564512 }
0 commit comments