From 4073cf8334325fa51ca38e727cbd88b67c502226 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 2 Mar 2022 15:19:09 +0000 Subject: [PATCH] Only apply special handling of META-INF to jar files The changes for gh-28562 attempted to align the Gradle plugin's handling of META-INF with the Maven plugin's behavior. Unfortunately, they want too far, applying the handling to both jar and war files when the Maven plugin only applies it to jar files. This commit reworks the changes so that they only apply to jar files. Closes gh-30026 --- .../tasks/bundling/BootArchiveSupport.java | 13 +--- .../boot/gradle/tasks/bundling/BootJar.java | 11 +++- .../boot/gradle/tasks/bundling/BootWar.java | 1 - .../bundling/AbstractBootArchiveTests.java | 60 ------------------ .../gradle/tasks/bundling/BootJarTests.java | 62 ++++++++++++++++++- 5 files changed, 73 insertions(+), 74 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 fb7e1a1ff318..a143a63647f0 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 @@ -179,19 +179,10 @@ private boolean isZip(InputStream inputStream) throws IOException { } void moveModuleInfoToRoot(CopySpec spec) { - spec.filesMatching("module-info.class", BootArchiveSupport::moveToRoot); + spec.filesMatching("module-info.class", this::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) { + 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 00c2d7cf2cef..229ee3fb813d 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 @@ -92,7 +92,7 @@ private void configureBootInfSpec(CopySpec bootInfSpec) { bootInfSpec.into("classes", fromCallTo(this::classpathDirectories)); bootInfSpec.into("lib", fromCallTo(this::classpathFiles)).eachFile(this.support::excludeNonZipFiles); this.support.moveModuleInfoToRoot(bootInfSpec); - this.support.moveMetaInfToRoot(bootInfSpec); + moveMetaInfToRoot(bootInfSpec); } private Iterable classpathDirectories() { @@ -107,6 +107,15 @@ private Iterable classpathEntries(Spec filter) { return (this.classpath != null) ? this.classpath.filter(filter) : Collections.emptyList(); } + private 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")) { + this.support.moveToRoot(file); + } + }); + } + @Override public void copy() { this.support.configureManifest(getManifest(), getMainClass().get(), CLASSES_DIRECTORY, LIB_DIRECTORY, 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 24e1de832b33..6a9b5878bf82 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 @@ -74,7 +74,6 @@ public BootWar() { 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 283c3c04f6c8..5a33a52e9ac9 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 @@ -185,66 +185,6 @@ void moduleInfoClassIsPackagedInTheRootOfTheArchive() throws IOException { } } - @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"); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootJarTests.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootJarTests.java index 5a99b1fa6a45..8e3bf128b4a8 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootJarTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootJarTests.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. @@ -103,6 +103,66 @@ void classpathIndexPointsToBootInfLibs() throws IOException { } } + @Test + void metaInfEntryIsPackagedInTheRootOfTheArchive() throws IOException { + getTask().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(); + getTask().classpath(classpathDirectory); + executeTask(); + try (JarFile jarFile = new JarFile(getTask().getArchiveFile().get().getAsFile())) { + assertThat(jarFile.getEntry("BOOT-INF/classes/com/example/Application.class")).isNotNull(); + assertThat(jarFile.getEntry("com/example/Application.class")).isNull(); + assertThat(jarFile.getEntry("BOOT-INF/classes/META-INF/test")).isNull(); + assertThat(jarFile.getEntry("META-INF/test")).isNotNull(); + } + } + + @Test + void aopXmlIsPackagedBeneathClassesDirectory() throws IOException { + getTask().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(); + getTask().classpath(classpathDirectory); + executeTask(); + try (JarFile jarFile = new JarFile(getTask().getArchiveFile().get().getAsFile())) { + assertThat(jarFile.getEntry("BOOT-INF/classes/com/example/Application.class")).isNotNull(); + assertThat(jarFile.getEntry("com/example/Application.class")).isNull(); + assertThat(jarFile.getEntry("BOOT-INF/classes/META-INF/aop.xml")).isNotNull(); + assertThat(jarFile.getEntry("META-INF/aop.xml")).isNull(); + } + } + + @Test + void kotlinModuleIsPackagedBeneathClassesDirectory() throws IOException { + getTask().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(); + getTask().classpath(classpathDirectory); + executeTask(); + try (JarFile jarFile = new JarFile(getTask().getArchiveFile().get().getAsFile())) { + assertThat(jarFile.getEntry("BOOT-INF/classes/com/example/Application.class")).isNotNull(); + assertThat(jarFile.getEntry("com/example/Application.class")).isNull(); + assertThat(jarFile.getEntry("BOOT-INF/classes/META-INF/example.kotlin_module")).isNotNull(); + assertThat(jarFile.getEntry("META-INF/example.kotlin_module")).isNull(); + } + } + private File createPopulatedJar() throws IOException { addContent(); executeTask();