From acfd9987f6da847a183bb1139b1d56651db93d6c Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Wed, 16 Oct 2024 11:14:17 +0200 Subject: [PATCH] Add SHA256 of Maven distribution for the Maven wrapper Closes gh-1577 --- .../.mvn/wrapper/maven-wrapper.properties | 1 + .../ProjectGeneratorIntegrationTests.java | 83 ++++++++++++++++++- .../test/project/ProjectGeneratorTester.java | 8 +- 3 files changed, 84 insertions(+), 8 deletions(-) diff --git a/initializr-generator-spring/src/main/resources/maven/3/wrapper/.mvn/wrapper/maven-wrapper.properties b/initializr-generator-spring/src/main/resources/maven/3/wrapper/.mvn/wrapper/maven-wrapper.properties index d58dfb70ba..fe9deed0b0 100644 --- a/initializr-generator-spring/src/main/resources/maven/3/wrapper/.mvn/wrapper/maven-wrapper.properties +++ b/initializr-generator-spring/src/main/resources/maven/3/wrapper/.mvn/wrapper/maven-wrapper.properties @@ -17,3 +17,4 @@ wrapperVersion=3.3.2 distributionType=only-script distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip +distributionSha256Sum=4ec3f26fb1a692473aea0235c300bd20f0f9fe741947c82c1234cefd76ac3a3c diff --git a/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/ProjectGeneratorIntegrationTests.java b/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/ProjectGeneratorIntegrationTests.java index ca63602ec7..80906a1cb9 100644 --- a/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/ProjectGeneratorIntegrationTests.java +++ b/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/ProjectGeneratorIntegrationTests.java @@ -16,8 +16,12 @@ package io.spring.initializr.generator.spring; +import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; +import io.spring.initializr.generator.buildsystem.BuildSystem; +import io.spring.initializr.generator.buildsystem.gradle.GradleBuildSystem; import io.spring.initializr.generator.buildsystem.maven.MavenBuildSystem; import io.spring.initializr.generator.language.java.JavaLanguage; import io.spring.initializr.generator.project.MutableProjectDescription; @@ -39,25 +43,32 @@ * {@link ProjectGenerationConfiguration} instances. * * @author Stephane Nicoll + * @author Moritz Halbritter */ class ProjectGeneratorIntegrationTests { + private static final String SPRING_BOOT_VERSION = "3.3.0"; + + private static final String JAVA_VERSION = "17"; + + private static final String DEPENDENCY_MANAGEMENT_PLUGIN_VERSION = "1.1.6"; + private ProjectGeneratorTester projectTester; @BeforeEach void setup(@TempDir Path directory) { this.projectTester = new ProjectGeneratorTester().withDirectory(directory) .withIndentingWriterFactory() - .withBean(InitializrMetadata.class, () -> InitializrMetadataTestBuilder.withDefaults().build()); + .withBean(InitializrMetadata.class, + () -> InitializrMetadataTestBuilder.withDefaults() + .setGradleEnv(DEPENDENCY_MANAGEMENT_PLUGIN_VERSION) + .build()); } @Test void customBaseDirectoryIsUsedWhenGeneratingProject() { MutableProjectDescription description = initProjectDescription(); description.setBuildSystem(new MavenBuildSystem()); - description.setPlatformVersion(Version.parse("2.1.0.RELEASE")); - description.setLanguage(new JavaLanguage()); - description.setGroupId("com.example"); description.setBaseDirectory("test/demo-app"); ProjectStructure project = this.projectTester.generate(description); assertThat(project).filePaths() @@ -69,9 +80,73 @@ void customBaseDirectoryIsUsedWhenGeneratingProject() { "test/demo-app/src/test/java/com/example/demo/DemoApplicationTests.java", "test/demo-app/HELP.md"); } + @Test + void generatedMavenProjectBuilds(@TempDir Path mavenHome) throws Exception { + MutableProjectDescription description = initProjectDescription(); + description.setBuildSystem(new MavenBuildSystem()); + ProjectStructure project = this.projectTester.generate(description); + Path projectDirectory = project.getProjectDirectory(); + runBuild(mavenHome, projectDirectory, description); + } + + @Test + void generatedGradleProjectBuilds(@TempDir Path gradleHome) throws Exception { + MutableProjectDescription description = initProjectDescription(); + description.setBuildSystem(new GradleBuildSystem()); + ProjectStructure project = this.projectTester.generate(description); + Path projectDirectory = project.getProjectDirectory(); + runBuild(gradleHome, projectDirectory, description); + } + + private void runBuild(Path mavenHome, Path projectDirectory, MutableProjectDescription description) + throws InterruptedException, IOException { + ProcessBuilder processBuilder = createProcessBuilder(projectDirectory, description.getBuildSystem(), mavenHome); + Path output = projectDirectory.resolve("output.log"); + processBuilder.redirectError(output.toFile()); + processBuilder.redirectOutput(output.toFile()); + assertThat(processBuilder.start().waitFor()).describedAs(String.join("\n", Files.readAllLines(output))) + .isEqualTo(0); + } + + private ProcessBuilder createProcessBuilder(Path directory, BuildSystem buildSystem, Path home) { + String javaHome = System.getProperty("java.home"); + if (buildSystem.id().equals(MavenBuildSystem.ID)) { + String command = (isWindows()) ? "mvnw.cmd" : "mvnw"; + ProcessBuilder processBuilder = new ProcessBuilder(directory.resolve(command).toAbsolutePath().toString(), + "-Dmaven.repo.local=" + home.resolve("repository").toAbsolutePath(), "--batch-mode", + "--no-transfer-progress", "package"); + if (javaHome != null) { + processBuilder.environment().put("JAVA_HOME", javaHome); + } + processBuilder.environment().put("MAVEN_USER_HOME", home.toAbsolutePath().toString()); + processBuilder.directory(directory.toFile()); + return processBuilder; + } + if (buildSystem.id().equals(GradleBuildSystem.ID)) { + String command = (isWindows()) ? "gradlew.bat" : "gradlew"; + ProcessBuilder processBuilder = new ProcessBuilder(directory.resolve(command).toAbsolutePath().toString(), + "--no-daemon", "build"); + if (javaHome != null) { + processBuilder.environment().put("JAVA_HOME", javaHome); + } + processBuilder.environment().put("GRADLE_USER_HOME", home.toAbsolutePath().toString()); + processBuilder.directory(directory.toFile()); + return processBuilder; + } + throw new IllegalStateException("Unknown build system '%s'".formatted(buildSystem.id())); + } + + private boolean isWindows() { + String osName = System.getProperty("os.name"); + return osName != null && osName.startsWith("Windows"); + } + private MutableProjectDescription initProjectDescription() { MutableProjectDescription description = new MutableProjectDescription(); description.setApplicationName("DemoApplication"); + description.setPlatformVersion(Version.parse(SPRING_BOOT_VERSION)); + description.setLanguage(new JavaLanguage(JAVA_VERSION)); + description.setGroupId("com.example"); return description; } diff --git a/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/project/ProjectGeneratorTester.java b/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/project/ProjectGeneratorTester.java index 03611f21ba..b232721c5b 100644 --- a/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/project/ProjectGeneratorTester.java +++ b/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/project/ProjectGeneratorTester.java @@ -30,10 +30,10 @@ /** * A tester class for {@link ProjectGenerator}. Contrary to {@link ProjectAssetTester}, - * standard {@link ProjectGenerationConfiguration} classes are processed - * automatically.Extra beans can be added using {@linkplain #withBean(Class, Supplier) - * bean registration}, a {@linkplain #withConfiguration(Class[]) configuration class} or - * via the {@linkplain #withContextInitializer(Consumer) customization of the project + * standard {@link ProjectGenerationConfiguration} classes are processed automatically. + * Extra beans can be added using {@linkplain #withBean(Class, Supplier) bean + * registration}, a {@linkplain #withConfiguration(Class[]) configuration class} or via + * the {@linkplain #withContextInitializer(Consumer) customization of the project * generation context}. * * @author Stephane Nicoll