Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent OutOfMemory while writing pom.xml when using indentation > 2 #10143

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<sonar-maven-plugin.version>4.0.0.4121</sonar-maven-plugin.version>
<apache-commons-io.version>2.16.1</apache-commons-io.version>
<cucumber.version>7.18.0</cucumber.version>
<maven-model-helper.version>36</maven-model-helper.version>
<maven-model-helper.version>37</maven-model-helper.version>
murdos marked this conversation as resolved.
Show resolved Hide resolved
<mustache.java.version>0.9.13</mustache.java.version>
<night-config.version>3.7.3</night-config.version>
<testcontainers.version>1.19.8</testcontainers.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package tech.jhipster.lite.module.infrastructure.secondary.javadependency.maven;

import io.fabric8.maven.Maven;
import io.fabric8.maven.XMLFormat;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
Expand All @@ -15,39 +14,32 @@
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import tech.jhipster.lite.module.domain.Indentation;
import tech.jhipster.lite.module.domain.buildproperties.BuildProperty;
import tech.jhipster.lite.module.domain.buildproperties.PropertyKey;
import tech.jhipster.lite.module.domain.buildproperties.PropertyValue;
import tech.jhipster.lite.module.domain.buildproperties.*;
import tech.jhipster.lite.module.domain.javabuild.MavenBuildExtension;
import tech.jhipster.lite.module.domain.javabuild.VersionSlug;
import tech.jhipster.lite.module.domain.javabuild.command.*;
import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileActivation;
import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileId;
import tech.jhipster.lite.module.domain.javadependency.DependencyId;
import tech.jhipster.lite.module.domain.javadependency.JavaDependency;
import tech.jhipster.lite.module.domain.javadependency.JavaDependencyClassifier;
import tech.jhipster.lite.module.domain.javadependency.JavaDependencyScope;
import tech.jhipster.lite.module.domain.javadependency.*;
import tech.jhipster.lite.module.domain.mavenplugin.*;
import tech.jhipster.lite.module.infrastructure.secondary.javadependency.JavaDependenciesCommandHandler;
import tech.jhipster.lite.shared.enumeration.domain.Enums;
import tech.jhipster.lite.shared.error.domain.Assert;
import tech.jhipster.lite.shared.error.domain.GeneratorException;
import tech.jhipster.lite.shared.generation.domain.ExcludeFromGeneratedCodeCoverage;

public class MavenCommandHandler implements JavaDependenciesCommandHandler {

private static final String COMMAND = "command";
private static final int DEFAULT_MAVEN_INDENTATION = 2;

private final Indentation indentation;
private final XMLFormat xmlFormat;
private final Path pomPath;
private final Model pomModel;

public MavenCommandHandler(Indentation indentation, Path pomPath) {
Assert.notNull("indentation", indentation);
Assert.notNull("pomPath", pomPath);

this.indentation = indentation;
this.xmlFormat = XMLFormat.builder(XMLFormat.DEFAULT).indent(indentation.spaces()).build();
this.pomPath = pomPath;
pomModel = readModel(pomPath);
}
Expand Down Expand Up @@ -428,22 +420,7 @@ private Function<MavenPluginConfiguration, Xpp3Dom> toMavenConfiguration() {
};
}

@ExcludeFromGeneratedCodeCoverage(reason = "The exception handling is hard to test and an implementation detail")
private void writePom() {
StringWriter stringWriter = new StringWriter();
Maven.writeModel(pomModel, pomPath, stringWriter);

try {
Files.writeString(pomPath, applyIndentation(stringWriter.toString()), StandardCharsets.UTF_8);
} catch (IOException e) {
throw GeneratorException.technicalError("Error writing pom: " + e.getMessage(), e);
}
}

private String applyIndentation(String pomContent) {
if (indentation.spacesCount() == DEFAULT_MAVEN_INDENTATION) {
return pomContent;
}
return pomContent.replace(" ".repeat(DEFAULT_MAVEN_INDENTATION), indentation.spaces());
Maven.writeModel(pomModel, pomPath, xmlFormat);
}
}