diff --git a/src/main/java/tech/jhipster/lite/common/domain/FileUtils.java b/src/main/java/tech/jhipster/lite/common/domain/FileUtils.java index dce9cd5d544..65e5014f94d 100644 --- a/src/main/java/tech/jhipster/lite/common/domain/FileUtils.java +++ b/src/main/java/tech/jhipster/lite/common/domain/FileUtils.java @@ -23,7 +23,8 @@ public class FileUtils { public static final String REGEXP_PREFIX_MULTILINE = "(?m)"; public static final String REGEXP_PREFIX_DOTALL = "(?s)"; - public static final String DOT_STAR_REGEX = ".*"; + public static final String REGEXP_DOT_STAR = ".*"; + public static final String REGEXP_SPACE_STAR = "[ \t]*"; private FileUtils() {} diff --git a/src/main/java/tech/jhipster/lite/generator/buildtool/maven/domain/Maven.java b/src/main/java/tech/jhipster/lite/generator/buildtool/maven/domain/Maven.java index 0714ac9e034..85219f4ce68 100644 --- a/src/main/java/tech/jhipster/lite/generator/buildtool/maven/domain/Maven.java +++ b/src/main/java/tech/jhipster/lite/generator/buildtool/maven/domain/Maven.java @@ -1,7 +1,6 @@ package tech.jhipster.lite.generator.buildtool.maven.domain; import static tech.jhipster.lite.common.domain.WordUtils.LF; -import static tech.jhipster.lite.common.domain.WordUtils.indent; import java.util.List; import java.util.stream.Collectors; @@ -66,37 +65,36 @@ public static String getParentHeader(Parent parent) { } public static String getParentHeader(Parent parent, int indentation) { - StringBuilder result = new StringBuilder() - .append(PARENT_BEGIN) - .append(LF) - .append(indent(2, indentation)) + String begin = PARENT_BEGIN + LF; + + String content = new StringBuilder() .append(GROUP_ID_BEGIN) .append(parent.getGroupId()) .append(GROUP_ID_END) .append(LF) - .append(indent(2, indentation)) .append(ARTIFACT_ID_BEGIN) .append(parent.getArtifactId()) .append(ARTIFACT_ID_END) - .append(LF); - return result.toString(); + .append(LF) + .toString() + .indent(indentation); + + return begin + content; } public static String getParent(Parent parent, int indentation) { - StringBuilder result = new StringBuilder() - .append(getParentHeader(parent, indentation)) - .append(indent(2, indentation)) + String header = getParentHeader(parent, indentation); + + String additionalContent = new StringBuilder() .append(VERSION_BEGIN) .append(parent.getVersion()) .append(VERSION_END) .append(LF) - .append(indent(2, indentation)) .append("") - .append(LF) - .append(indent(1, indentation)) - .append(PARENT_END); + .toString() + .indent(indentation); - return result.toString(); + return header + additionalContent + PARENT_END; } public static String getDependency(Dependency dependency, int indentation) { @@ -177,72 +175,35 @@ public static String getExclusions(List exclusions, int indentation) return begin + body + EXCLUSIONS_END; } - public static String getPlugin(Plugin plugin) { - return getPlugin(plugin, WordUtils.DEFAULT_INDENTATION, 3); - } - - public static String getPlugin(Plugin plugin, int indentation) { - return getPlugin(plugin, indentation, 3); - } - - public static String getPluginHeader(Plugin plugin) { - return getPluginHeader(plugin, WordUtils.DEFAULT_INDENTATION, 3); - } - public static String getPluginHeader(Plugin plugin, int indentation) { - return getPluginHeader(plugin, indentation, 3); - } + String begin = PLUGIN_BEGIN + LF; - public static String getPluginManagement(Plugin plugin) { - return getPlugin(plugin, WordUtils.DEFAULT_INDENTATION, 4); - } - - public static String getPluginManagement(Plugin plugin, int indentation) { - return getPlugin(plugin, indentation, 4); - } - - public static String getPluginManagementHeader(Plugin plugin) { - return getPluginHeader(plugin, WordUtils.DEFAULT_INDENTATION, 4); - } - - public static String getPluginManagementHeader(Plugin plugin, int indentation) { - return getPluginHeader(plugin, indentation, 4); - } - - public static String getPluginHeader(Plugin plugin, int indentation, int initialIndentation) { - StringBuilder result = new StringBuilder() - .append(PLUGIN_BEGIN) - .append(LF) - .append(indent(initialIndentation + 1, indentation)) + String content = new StringBuilder() .append(GROUP_ID_BEGIN) .append(plugin.getGroupId()) .append(GROUP_ID_END) .append(LF) - .append(indent(initialIndentation + 1, indentation)) .append(ARTIFACT_ID_BEGIN) .append(plugin.getArtifactId()) .append(ARTIFACT_ID_END) - .append(LF); - return result.toString(); + .toString() + .indent(indentation); + + return begin + content; } - public static String getPlugin(Plugin plugin, int indentation, int initialIndentation) { - StringBuilder result = new StringBuilder().append(getPluginHeader(plugin, indentation, initialIndentation)); + public static String getPlugin(Plugin plugin, int indentation) { + String header = getPluginHeader(plugin, indentation); - plugin - .getVersion() - .ifPresent(version -> - result.append(indent(initialIndentation + 1, indentation)).append(VERSION_BEGIN).append(version).append(VERSION_END).append(LF) - ); + StringBuilder additionalBodyBuilder = new StringBuilder(); - //replace '\n' to make the multi-line additionalConfiguration platform specific - plugin - .getAdditionalElements() - .ifPresent(additionalElements -> result.append(plugin.getAdditionalElements().get().indent((initialIndentation + 1) * indentation))); + plugin.getVersion().ifPresent(version -> additionalBodyBuilder.append(VERSION_BEGIN).append(version).append(VERSION_END).append(LF)); - result.append(indent(initialIndentation, indentation)).append(PLUGIN_END); + plugin.getAdditionalElements().ifPresent(additionalBodyBuilder::append); + + String additionalBody = additionalBodyBuilder.toString().indent(indentation); - return result.toString(); + return header + additionalBody + PLUGIN_END; } public static String getProperty(String key, String version) { diff --git a/src/main/java/tech/jhipster/lite/generator/buildtool/maven/domain/MavenDomainService.java b/src/main/java/tech/jhipster/lite/generator/buildtool/maven/domain/MavenDomainService.java index f334f64f77e..27c98344c0b 100644 --- a/src/main/java/tech/jhipster/lite/generator/buildtool/maven/domain/MavenDomainService.java +++ b/src/main/java/tech/jhipster/lite/generator/buildtool/maven/domain/MavenDomainService.java @@ -1,6 +1,6 @@ package tech.jhipster.lite.generator.buildtool.maven.domain; -import static tech.jhipster.lite.common.domain.FileUtils.getPath; +import static tech.jhipster.lite.common.domain.FileUtils.*; import static tech.jhipster.lite.common.domain.WordUtils.*; import static tech.jhipster.lite.generator.buildtool.maven.domain.Maven.*; import static tech.jhipster.lite.generator.project.domain.Constants.POM_XML; @@ -31,12 +31,12 @@ public void addParent(Project project, Parent parent) { int indent = (Integer) project.getConfig(PRETTIER_DEFAULT_INDENT).orElse(DEFAULT_INDENTATION); - String parentNode = Maven.getParent(parent, indent).replace(LF, project.getEndOfLine()); - String parentHeaderNode = Maven.getParentHeader(parent, indent).replace(LF, project.getEndOfLine()); - String parentHeaderRegexp = FileUtils.REGEXP_PREFIX_MULTILINE + parentHeaderNode; + String parentHeaderNode = Maven.getParentHeader(parent, indent).indent(indent); + String parentHeaderRegexp = (REGEXP_PREFIX_MULTILINE + parentHeaderNode).replace(LF, project.getEndOfLine()); if (!projectRepository.containsRegexp(project, "", POM_XML, parentHeaderRegexp)) { - projectRepository.replaceText(project, "", POM_XML, NEEDLE_PARENT, parentNode); + String newParentNode = Maven.getParent(parent, indent).indent(indent).replace(LF, project.getEndOfLine()); + projectRepository.replaceText(project, "", POM_XML, REGEXP_SPACE_STAR + NEEDLE_PARENT + project.getEndOfLine(), newParentNode); } } @@ -68,15 +68,13 @@ private void addDependency(Project project, Dependency dependency, List section and needle (not as it can conflict with section in ) - String pluginRegexp = - FileUtils.REGEXP_PREFIX_DOTALL + PLUGIN_BEGIN + FileUtils.DOT_STAR_REGEX + pluginNodeNode + FileUtils.DOT_STAR_REGEX + NEEDLE_PLUGIN; - - if (!projectRepository.containsRegexp(project, "", POM_XML, pluginRegexp)) { - String pluginWithNeedle = Maven.getPlugin(plugin, indent) + project.getEndOfLine() + indent(3, indent) + NEEDLE_PLUGIN; - - projectRepository.replaceText(project, "", POM_XML, NEEDLE_PLUGIN, pluginWithNeedle); - } + addPlugin(project, plugin, NEEDLE_PLUGIN); } @Override public void addPluginManagement(Project project, Plugin plugin) { + addPlugin(project, plugin, NEEDLE_PLUGIN_MANAGEMENT); + } + + private void addPlugin(Project project, Plugin plugin, String needle) { project.addDefaultConfig(PRETTIER_DEFAULT_INDENT); int indent = (Integer) project.getConfig(PRETTIER_DEFAULT_INDENT).orElse(DEFAULT_INDENTATION); - String pluginNodeNode = Maven.getPluginManagementHeader(plugin, indent); - //Checking for plugin declaration in section - String pluginRegexp = - FileUtils.REGEXP_PREFIX_DOTALL + - PLUGIN_MANAGEMENT_BEGIN + - FileUtils.DOT_STAR_REGEX + - pluginNodeNode + - FileUtils.DOT_STAR_REGEX + - PLUGIN_MANAGEMENT_END; + int level = NEEDLE_PLUGIN_MANAGEMENT.equals(needle) ? 4 : 3; + + String pluginNode = Maven.getPluginHeader(plugin, indent).indent(level * indent); + + String pluginRegexp; + if (NEEDLE_PLUGIN_MANAGEMENT.equals(needle)) { + //Checking for plugin declaration in section + pluginRegexp = + ( + FileUtils.REGEXP_PREFIX_DOTALL + + PLUGIN_MANAGEMENT_BEGIN + + FileUtils.REGEXP_DOT_STAR + + pluginNode + + FileUtils.REGEXP_DOT_STAR + + PLUGIN_MANAGEMENT_END + ); + } else { + //Checking for plugin declaration between section and needle (not as it can conflict with section in ) + pluginRegexp = + FileUtils.REGEXP_PREFIX_DOTALL + PLUGIN_BEGIN + FileUtils.REGEXP_DOT_STAR + pluginNode + FileUtils.REGEXP_DOT_STAR + NEEDLE_PLUGIN; + } - if (!projectRepository.containsRegexp(project, "", POM_XML, pluginRegexp)) { - String pluginWithNeedle = - Maven.getPluginManagement(plugin, indent) + project.getEndOfLine() + indent(4, indent) + NEEDLE_PLUGIN_MANAGEMENT; + if (!projectRepository.containsRegexp(project, "", POM_XML, pluginRegexp.replace(LF, project.getEndOfLine()))) { + String newPluginNode = Maven.getPlugin(plugin, indent).indent(level * indent); + String pluginWithNeedle = (newPluginNode + indent(level, indent) + needle).replace(LF, project.getEndOfLine()); - projectRepository.replaceText(project, "", POM_XML, NEEDLE_PLUGIN_MANAGEMENT, pluginWithNeedle); + projectRepository.replaceText(project, "", POM_XML, REGEXP_SPACE_STAR + needle, pluginWithNeedle); } } diff --git a/src/test/java/tech/jhipster/lite/generator/buildtool/maven/application/MavenApplicationServiceIT.java b/src/test/java/tech/jhipster/lite/generator/buildtool/maven/application/MavenApplicationServiceIT.java index 3e4bc5d1f6a..4ebf7db3520 100644 --- a/src/test/java/tech/jhipster/lite/generator/buildtool/maven/application/MavenApplicationServiceIT.java +++ b/src/test/java/tech/jhipster/lite/generator/buildtool/maven/application/MavenApplicationServiceIT.java @@ -2,6 +2,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import static tech.jhipster.lite.TestUtils.*; +import static tech.jhipster.lite.common.domain.WordUtils.DEFAULT_INDENTATION; import static tech.jhipster.lite.generator.buildtool.maven.application.MavenAssertFiles.*; import static tech.jhipster.lite.generator.project.domain.Constants.POM_XML; @@ -61,7 +62,7 @@ void shouldAddParentOnlyOneTime() throws Exception { mavenApplicationService.addParent(project, parent); mavenApplicationService.addParent(project, parent); - assertFileContentManyTimes(project, POM_XML, Maven.getParentHeader(parent), 1); + assertFileContentManyTimes(project, POM_XML, Maven.getParentHeader(parent).indent(DEFAULT_INDENTATION), 1); } @Test @@ -175,7 +176,12 @@ void shouldAddDependencyOnlyOneTime() throws Exception { mavenApplicationService.addDependency(project, dependency); mavenApplicationService.addDependency(project, dependency); - assertFileContentManyTimes(project, POM_XML, Maven.getDependencyHeader(dependency, 2).indent(4), 1); + assertFileContentManyTimes( + project, + POM_XML, + Maven.getDependencyHeader(dependency, DEFAULT_INDENTATION).indent(2 * DEFAULT_INDENTATION), + 1 + ); } @Test @@ -381,7 +387,7 @@ void shouldAddPluginOnlyOneTime() throws Exception { mavenApplicationService.addPlugin(project, plugin); mavenApplicationService.addPlugin(project, plugin); - assertFileContentManyTimes(project, POM_XML, Maven.getPluginHeader(plugin), 1); + assertFileContentManyTimes(project, POM_XML, Maven.getPluginHeader(plugin, DEFAULT_INDENTATION).indent(3 * DEFAULT_INDENTATION), 1); } @Test @@ -392,7 +398,7 @@ void shouldAddPluginManagementOnlyOneTime() throws Exception { mavenApplicationService.addPluginManagement(project, plugin); mavenApplicationService.addPluginManagement(project, plugin); - assertFileContentManyTimes(project, POM_XML, Maven.getPluginManagementHeader(plugin), 1); + assertFileContentManyTimes(project, POM_XML, Maven.getPluginHeader(plugin, DEFAULT_INDENTATION).indent(4 * DEFAULT_INDENTATION), 1); } @Test @@ -403,8 +409,8 @@ void shouldAddPluginManagementWithExistingPlugin() throws Exception { mavenApplicationService.addPlugin(project, plugin); mavenApplicationService.addPluginManagement(project, plugin); - assertFileContentManyTimes(project, POM_XML, Maven.getPluginHeader(plugin), 1); - assertFileContentManyTimes(project, POM_XML, Maven.getPluginManagementHeader(plugin), 1); + assertFileContentManyTimes(project, POM_XML, Maven.getPluginHeader(plugin, DEFAULT_INDENTATION).indent(3 * DEFAULT_INDENTATION), 1); + assertFileContentManyTimes(project, POM_XML, Maven.getPluginHeader(plugin, DEFAULT_INDENTATION).indent(4 * DEFAULT_INDENTATION), 1); } @Test diff --git a/src/test/java/tech/jhipster/lite/generator/buildtool/maven/domain/maven/MavenTest.java b/src/test/java/tech/jhipster/lite/generator/buildtool/maven/domain/maven/MavenTest.java index e42364551f5..fca037368d9 100644 --- a/src/test/java/tech/jhipster/lite/generator/buildtool/maven/domain/maven/MavenTest.java +++ b/src/test/java/tech/jhipster/lite/generator/buildtool/maven/domain/maven/MavenTest.java @@ -19,11 +19,11 @@ void shouldGetParent() { String expected = """ - org.springframework.boot - spring-boot-starter-parent - 2.5.3 - - """; + org.springframework.boot + spring-boot-starter-parent + 2.5.3 + + """; // @formatter:on Parent parent = Parent.builder().groupId("org.springframework.boot").artifactId("spring-boot-starter-parent").version("2.5.3").build(); @@ -36,11 +36,11 @@ void shouldGetParentWith4Indentations() { String expected = """ - org.springframework.boot - spring-boot-starter-parent - 2.5.3 - - """; + org.springframework.boot + spring-boot-starter-parent + 2.5.3 + + """; // @formatter:on Parent parent = Parent.builder().groupId("org.springframework.boot").artifactId("spring-boot-starter-parent").version("2.5.3").build(); @@ -180,34 +180,34 @@ private Dependency.DependencyBuilder fullDependencyBuilder() { void shouldGetPlugin() { String expected = """ - org.springframework.boot - spring-boot-maven-plugin - """; + org.springframework.boot + spring-boot-maven-plugin + """; Plugin plugin = minimalPluginBuilder().build(); - assertThat(Maven.getPlugin(plugin)).isEqualTo(expected); + assertThat(Maven.getPlugin(plugin, 2)).isEqualTo(expected); } @Test void shouldGetPluginWithVersion() { String expected = """ - org.springframework.boot - spring-boot-maven-plugin - 2.6.0 - """; + org.springframework.boot + spring-boot-maven-plugin + 2.6.0 + """; Plugin plugin = fullPluginBuilder().build(); - assertThat(Maven.getPlugin(plugin)).isEqualTo(expected); + assertThat(Maven.getPlugin(plugin, 2)).isEqualTo(expected); } @Test void shouldGetPluginWith4Indentations() { String expected = """ - org.springframework.boot - spring-boot-maven-plugin - """; + org.springframework.boot + spring-boot-maven-plugin + """; Plugin plugin = minimalPluginBuilder().build(); assertThat(Maven.getPlugin(plugin, 4)).isEqualTo(expected); @@ -218,32 +218,32 @@ void shouldGetPluginWithAdditionalElements() { // @formatter:off String expected = """ - org.springframework.boot - spring-boot-maven-plugin - 2.6.0 - - - default-war - - war - - package - - - - WEB-INF/**,META-INF/** - false - target/classes/static/ - - - src/main/webapp - - WEB-INF/** - - - - - """; + org.springframework.boot + spring-boot-maven-plugin + 2.6.0 + + + default-war + + war + + package + + + + WEB-INF/**,META-INF/** + false + target/classes/static/ + + + src/main/webapp + + WEB-INF/** + + + + + """; // @formatter:on Plugin plugin = fullPluginBuilder() .additionalElements( @@ -273,7 +273,7 @@ void shouldGetPluginWithAdditionalElements() { ) .build(); - assertThat(Maven.getPluginManagement(plugin)).isEqualTo(expected); + assertThat(Maven.getPlugin(plugin, 2)).isEqualTo(expected); } @Test