From c3eee4ad68ec952f1cb434ae7e5d1405d7cb02f7 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 11 Feb 2022 13:32:52 +0000 Subject: [PATCH] Align Gradle's plugin's META-INF repackaging with Maven plugin's Closes gh-28562 --- .../tasks/bundling/BootArchiveSupport.java | 11 +++- .../boot/gradle/tasks/bundling/BootJar.java | 6 +- .../boot/gradle/tasks/bundling/BootWar.java | 3 +- .../bundling/AbstractBootArchiveTests.java | 62 ++++++++++++++++++- 4 files changed, 76 insertions(+), 6 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootArchiveSupport.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootArchiveSupport.java index debc5b5c8f..fb7e1a1ff3 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootArchiveSupport.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootArchiveSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -182,6 +182,15 @@ class BootArchiveSupport { spec.filesMatching("module-info.class", BootArchiveSupport::moveToRoot); } + void moveMetaInfToRoot(CopySpec spec) { + spec.eachFile((file) -> { + String path = file.getRelativeSourcePath().getPathString(); + if (path.startsWith("META-INF/") && !path.equals("META-INF/aop.xml") && !path.endsWith(".kotlin_module")) { + moveToRoot(file); + } + }); + } + private static void moveToRoot(FileCopyDetails details) { details.setRelativePath(details.getRelativeSourcePath()); } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootJar.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootJar.java index d7569c2e5d..00c2d7cf2c 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootJar.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootJar.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -91,8 +91,8 @@ public class BootJar extends Jar implements BootArchive { private void configureBootInfSpec(CopySpec bootInfSpec) { bootInfSpec.into("classes", fromCallTo(this::classpathDirectories)); bootInfSpec.into("lib", fromCallTo(this::classpathFiles)).eachFile(this.support::excludeNonZipFiles); - bootInfSpec.filesMatching("module-info.class", - (details) -> details.setRelativePath(details.getRelativeSourcePath())); + this.support.moveModuleInfoToRoot(bootInfSpec); + this.support.moveMetaInfToRoot(bootInfSpec); } private Iterable classpathDirectories() { diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootWar.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootWar.java index addcd40215..24e1de832b 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootWar.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootWar.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -74,6 +74,7 @@ public class BootWar extends War implements BootArchive { this.mainClass = project.getObjects().property(String.class); getWebInf().into("lib-provided", fromCallTo(this::getProvidedLibFiles)); this.support.moveModuleInfoToRoot(getRootSpec()); + this.support.moveMetaInfToRoot(getRootSpec()); getRootSpec().eachFile(this.support::excludeNonZipLibraryFiles); project.getConfigurations().all((configuration) -> { ResolvableDependencies incoming = configuration.getIncoming(); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/AbstractBootArchiveTests.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/AbstractBootArchiveTests.java index 96a14088a1..283c3c04f6 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/AbstractBootArchiveTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/AbstractBootArchiveTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -185,6 +185,66 @@ abstract class AbstractBootArchiveTests { } } + @Test + void metaInfEntryIsPackagedInTheRootOfTheArchive() throws IOException { + this.task.getMainClass().set("com.example.Main"); + File classpathDirectory = new File(this.temp, "classes"); + File metaInfEntry = new File(classpathDirectory, "META-INF/test"); + metaInfEntry.getParentFile().mkdirs(); + metaInfEntry.createNewFile(); + File applicationClass = new File(classpathDirectory, "com/example/Application.class"); + applicationClass.getParentFile().mkdirs(); + applicationClass.createNewFile(); + this.task.classpath(classpathDirectory); + executeTask(); + try (JarFile jarFile = new JarFile(this.task.getArchiveFile().get().getAsFile())) { + assertThat(jarFile.getEntry(this.classesPath + "com/example/Application.class")).isNotNull(); + assertThat(jarFile.getEntry("com/example/Application.class")).isNull(); + assertThat(jarFile.getEntry(this.classesPath + "META-INF/test")).isNull(); + assertThat(jarFile.getEntry("META-INF/test")).isNotNull(); + } + } + + @Test + void aopXmlIsPackagedBeneathClassesDirectory() throws IOException { + this.task.getMainClass().set("com.example.Main"); + File classpathDirectory = new File(this.temp, "classes"); + File aopXml = new File(classpathDirectory, "META-INF/aop.xml"); + aopXml.getParentFile().mkdirs(); + aopXml.createNewFile(); + File applicationClass = new File(classpathDirectory, "com/example/Application.class"); + applicationClass.getParentFile().mkdirs(); + applicationClass.createNewFile(); + this.task.classpath(classpathDirectory); + executeTask(); + try (JarFile jarFile = new JarFile(this.task.getArchiveFile().get().getAsFile())) { + assertThat(jarFile.getEntry(this.classesPath + "com/example/Application.class")).isNotNull(); + assertThat(jarFile.getEntry("com/example/Application.class")).isNull(); + assertThat(jarFile.getEntry(this.classesPath + "META-INF/aop.xml")).isNotNull(); + assertThat(jarFile.getEntry("META-INF/aop.xml")).isNull(); + } + } + + @Test + void kotlinModuleIsPackagedBeneathClassesDirectory() throws IOException { + this.task.getMainClass().set("com.example.Main"); + File classpathDirectory = new File(this.temp, "classes"); + File kotlinModule = new File(classpathDirectory, "META-INF/example.kotlin_module"); + kotlinModule.getParentFile().mkdirs(); + kotlinModule.createNewFile(); + File applicationClass = new File(classpathDirectory, "com/example/Application.class"); + applicationClass.getParentFile().mkdirs(); + applicationClass.createNewFile(); + this.task.classpath(classpathDirectory); + executeTask(); + try (JarFile jarFile = new JarFile(this.task.getArchiveFile().get().getAsFile())) { + assertThat(jarFile.getEntry(this.classesPath + "com/example/Application.class")).isNotNull(); + assertThat(jarFile.getEntry("com/example/Application.class")).isNull(); + assertThat(jarFile.getEntry(this.classesPath + "META-INF/example.kotlin_module")).isNotNull(); + assertThat(jarFile.getEntry("META-INF/example.kotlin_module")).isNull(); + } + } + @Test void classpathCanBeSetUsingAFileCollection() throws IOException { this.task.getMainClass().set("com.example.Main");