From 5e7917e33a142ea41e7e787478182122466d13f1 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 23 Jun 2020 11:32:47 +0100 Subject: [PATCH] Upgrade to Maven Shade Plugin 3.2.4 Closes gh-22074 --- .../spring-boot-parent/build.gradle | 2 +- .../PropertiesMergingResourceTransformer.java | 22 ++++++++++++--- ...ertiesMergingResourceTransformerTests.java | 28 ++++++++++++++----- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/spring-boot-project/spring-boot-parent/build.gradle b/spring-boot-project/spring-boot-parent/build.gradle index 747f7e4589..0513dd2c7d 100644 --- a/spring-boot-project/spring-boot-parent/build.gradle +++ b/spring-boot-project/spring-boot-parent/build.gradle @@ -95,7 +95,7 @@ bom { ] } } - library("Maven Shade Plugin", "3.2.1") { + library("Maven Shade Plugin", "3.2.4") { group("org.apache.maven.plugins") { modules = [ "maven-shade-plugin" diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/PropertiesMergingResourceTransformer.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/PropertiesMergingResourceTransformer.java index 3d5141f155..a553fc8298 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/PropertiesMergingResourceTransformer.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/PropertiesMergingResourceTransformer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 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. @@ -24,7 +24,7 @@ import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; import org.apache.maven.plugins.shade.relocation.Relocator; -import org.apache.maven.plugins.shade.resource.ResourceTransformer; +import org.apache.maven.plugins.shade.resource.ReproducibleResourceTransformer; /** * Extension for the Maven @@ -35,13 +35,15 @@ import org.apache.maven.plugins.shade.resource.ResourceTransformer; * @author Andy Wilkinson * @since 1.0.0 */ -public class PropertiesMergingResourceTransformer implements ResourceTransformer { +public class PropertiesMergingResourceTransformer implements ReproducibleResourceTransformer { // Set this in pom configuration with ... private String resource; private final Properties data = new Properties(); + private long time; + /** * Return the data the properties being merged. * @return the data @@ -56,12 +58,22 @@ public class PropertiesMergingResourceTransformer implements ResourceTransformer } @Override + @Deprecated public void processResource(String resource, InputStream inputStream, List relocators) throws IOException { + processResource(resource, inputStream, relocators, 0); + } + + @Override + public void processResource(String resource, InputStream inputStream, List relocators, long time) + throws IOException { Properties properties = new Properties(); properties.load(inputStream); inputStream.close(); properties.forEach((name, value) -> process((String) name, (String) value)); + if (time > this.time) { + this.time = time; + } } private void process(String name, String value) { @@ -76,7 +88,9 @@ public class PropertiesMergingResourceTransformer implements ResourceTransformer @Override public void modifyOutputStream(JarOutputStream os) throws IOException { - os.putNextEntry(new JarEntry(this.resource)); + JarEntry jarEntry = new JarEntry(this.resource); + jarEntry.setTime(this.time); + os.putNextEntry(jarEntry); this.data.store(os, "Merged by PropertiesMergingResourceTransformer"); os.flush(); this.data.clear(); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/PropertiesMergingResourceTransformerTests.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/PropertiesMergingResourceTransformerTests.java index f221c5fad5..ad0dc6831c 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/PropertiesMergingResourceTransformerTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/PropertiesMergingResourceTransformerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 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. @@ -18,6 +18,10 @@ package org.springframework.boot.maven; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.jar.JarEntry; +import java.util.jar.JarInputStream; import java.util.jar.JarOutputStream; import org.junit.jupiter.api.Test; @@ -36,28 +40,38 @@ class PropertiesMergingResourceTransformerTests { @Test void testProcess() throws Exception { assertThat(this.transformer.hasTransformedResource()).isFalse(); - this.transformer.processResource("foo", new ByteArrayInputStream("foo=bar".getBytes()), null); + this.transformer.processResource("foo", new ByteArrayInputStream("foo=bar".getBytes()), null, 0); assertThat(this.transformer.hasTransformedResource()).isTrue(); } @Test void testMerge() throws Exception { - this.transformer.processResource("foo", new ByteArrayInputStream("foo=bar".getBytes()), null); - this.transformer.processResource("bar", new ByteArrayInputStream("foo=spam".getBytes()), null); + this.transformer.processResource("foo", new ByteArrayInputStream("foo=bar".getBytes()), null, 0); + this.transformer.processResource("bar", new ByteArrayInputStream("foo=spam".getBytes()), null, 0); assertThat(this.transformer.getData().getProperty("foo")).isEqualTo("bar,spam"); } @Test void testOutput() throws Exception { this.transformer.setResource("foo"); - this.transformer.processResource("foo", new ByteArrayInputStream("foo=bar".getBytes()), null); + long time = 1592911068000L; + this.transformer.processResource("foo", new ByteArrayInputStream("foo=bar".getBytes()), null, time); ByteArrayOutputStream out = new ByteArrayOutputStream(); JarOutputStream os = new JarOutputStream(out); this.transformer.modifyOutputStream(os); os.flush(); os.close(); - assertThat(out.toByteArray()).isNotNull(); - assertThat(out.toByteArray().length > 0).isTrue(); + byte[] bytes = out.toByteArray(); + assertThat(bytes).hasSizeGreaterThan(0); + List entries = new ArrayList<>(); + try (JarInputStream is = new JarInputStream(new ByteArrayInputStream(bytes))) { + JarEntry entry; + while ((entry = is.getNextJarEntry()) != null) { + entries.add(entry); + } + } + assertThat(entries).hasSize(1); + assertThat(entries.get(0).getTime()).isEqualTo(time); } }