Skip to content

Commit

Permalink
Add SHA256 of Maven distribution for the Maven wrapper
Browse files Browse the repository at this point in the history
Closes gh-1577
  • Loading branch information
mhalbritter committed Oct 16, 2024
1 parent 6b2774d commit acfd998
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit acfd998

Please sign in to comment.