MavenBuild Class — spring-boot Architecture
Architecture documentation for the MavenBuild class in MavenBuild.java from the spring-boot codebase.
Entity Profile
Source Code
build-plugin/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/MavenBuild.java lines 60–222
class MavenBuild {
private final File home;
private final File temp;
private final Map<String, String> pomReplacements;
private final List<String> goals = new ArrayList<>();
private final Properties properties = new Properties();
private @Nullable ProjectCallback preparation;
private @Nullable File projectDir;
MavenBuild(File home) {
this.home = home;
this.temp = createTempDirectory();
this.pomReplacements = getPomReplacements();
}
private File createTempDirectory() {
try {
return Files.createTempDirectory("maven-build").toFile().getCanonicalFile();
}
catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
private Map<String, String> getPomReplacements() {
Map<String, String> replacements = new HashMap<>();
replacements.put("java.version", "17");
replacements.put("project.groupId", "org.springframework.boot");
replacements.put("project.artifactId", "spring-boot-maven-plugin");
replacements.putAll(new Versions().asMap());
return Collections.unmodifiableMap(replacements);
}
MavenBuild project(String project) {
return project("intTest", project);
}
MavenBuild project(String root, String project) {
this.projectDir = new File("src/" + root + "/projects/" + project);
return this;
}
MavenBuild goals(String... goals) {
this.goals.addAll(Arrays.asList(goals));
return this;
}
MavenBuild systemProperty(String name, String value) {
this.properties.setProperty(name, value);
return this;
}
MavenBuild prepare(ProjectCallback callback) {
this.preparation = callback;
return this;
}
void execute(ProjectCallback callback) {
execute(callback, 0);
}
void executeAndFail(ProjectCallback callback) {
execute(callback, 1);
}
private void execute(ProjectCallback callback, int expectedExitCode) {
Invoker invoker = new DefaultInvoker();
invoker.setMavenHome(this.home);
InvocationRequest request = new DefaultInvocationRequest();
try {
Path destination = this.temp.toPath();
Assert.notNull(this.projectDir, "'projectDir' must not be null");
Path source = this.projectDir.toPath();
Files.walkFileTree(source, new SimpleFileVisitor<>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
Files.createDirectories(destination.resolve(source.relativize(dir)));
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toFile().getName().equals("pom.xml")) {
String pomXml = Files.readString(file);
for (Entry<String, String> replacement : MavenBuild.this.pomReplacements.entrySet()) {
pomXml = pomXml.replace("@" + replacement.getKey() + "@", replacement.getValue());
}
Files.writeString(destination.resolve(source.relativize(file)), pomXml,
StandardOpenOption.CREATE_NEW);
}
else {
Files.copy(file, destination.resolve(source.relativize(file)),
StandardCopyOption.REPLACE_EXISTING);
}
return FileVisitResult.CONTINUE;
}
});
String settingsXml = Files.readString(Paths.get("build", "generated-resources", "settings", "settings.xml"))
.replace("@localCentralUrl@", new File("build/test-maven-repository").toURI().toURL().toString())
.replace("@localRepositoryPath@", new File("build/local-maven-repository").getAbsolutePath());
Files.writeString(destination.resolve("settings.xml"), settingsXml, StandardOpenOption.CREATE_NEW);
request.setBaseDirectory(this.temp);
request.setJavaHome(new File(System.getProperty("java.home")));
request.setProperties(this.properties);
request.addArgs(this.goals.isEmpty() ? Collections.singletonList("package") : this.goals);
request.setUserSettingsFile(new File(this.temp, "settings.xml"));
request.setUpdateSnapshots(true);
request.setBatchMode(true);
// request.setMavenOpts("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=8000");
File target = new File(this.temp, "target");
target.mkdirs();
if (this.preparation != null) {
this.preparation.doWith(this.temp);
}
File buildLogFile = new File(target, "build.log");
try (PrintWriter buildLog = new PrintWriter(new FileWriter(buildLogFile))) {
request.setOutputHandler((line) -> {
buildLog.println(line);
buildLog.flush();
});
try {
InvocationResult result = invoker.execute(request);
assertThat(result.getExitCode()).as(contentOf(buildLogFile)).isEqualTo(expectedExitCode);
}
catch (MavenInvocationException ex) {
throw new RuntimeException(ex);
}
}
callback.doWith(this.temp);
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
finally {
FileSystemUtils.deleteRecursively(this.temp);
}
}
/**
* Action to take on a maven project directory.
*/
@FunctionalInterface
public interface ProjectCallback {
/**
* Take the action on the given project.
* @param project the project directory
* @throws Exception on error
*/
void doWith(File project) throws Exception;
}
}
Source
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free