mirror of
https://gitee.com/dcren/initializr.git
synced 2025-05-12 08:38:34 +08:00
Clean artifactId and groupId
Maven allows alphanumeric characters and `.`, `_` and `-` for group id and artifact id. This commit cleans the artifact id and group id if invalid characters are found. For artifact id, the invalid characters are replaced with a hyphen and for group id a dot is used. In cases where the base directory matches the artifact id, the base directory is also cleaned. See gh-924
This commit is contained in:
parent
918fe40dd9
commit
10c7a4f32e
@ -37,6 +37,7 @@ import org.springframework.util.StringUtils;
|
||||
* Validates a {@link ProjectRequest} and creates a {@link ProjectDescription} from it.
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
* @author HaiTao Zhang
|
||||
*/
|
||||
public class ProjectRequestToDescriptionConverter {
|
||||
|
||||
@ -49,12 +50,14 @@ public class ProjectRequestToDescriptionConverter {
|
||||
validateDependencyRange(springBootVersion, resolvedDependencies);
|
||||
ProjectDescription description = new ProjectDescription();
|
||||
description.setApplicationName(getApplicationName(request, metadata));
|
||||
description.setArtifactId(determineValue(request.getArtifactId(), () -> metadata.getArtifactId().getContent()));
|
||||
description.setBaseDirectory(request.getBaseDir());
|
||||
description.setArtifactId(determineValue(artificialIdCorrection(request.getArtifactId()),
|
||||
() -> metadata.getArtifactId().getContent()));
|
||||
description.setBaseDirectory(baseDirectoryCorrection(request.getBaseDir(), request.getArtifactId()));
|
||||
description.setBuildSystem(getBuildSystem(request, metadata));
|
||||
description
|
||||
.setDescription(determineValue(request.getDescription(), () -> metadata.getDescription().getContent()));
|
||||
description.setGroupId(determineValue(request.getGroupId(), () -> metadata.getGroupId().getContent()));
|
||||
description.setGroupId(
|
||||
determineValue(groupIdCorrection(request.getGroupId()), () -> metadata.getGroupId().getContent()));
|
||||
description.setLanguage(Language.forId(request.getLanguage(), request.getJavaVersion()));
|
||||
description.setName(determineValue(request.getName(), () -> metadata.getName().getContent()));
|
||||
description.setPackageName(getPackageName(request, metadata));
|
||||
@ -63,6 +66,7 @@ public class ProjectRequestToDescriptionConverter {
|
||||
description.setVersion(determineValue(request.getVersion(), () -> metadata.getVersion().getContent()));
|
||||
resolvedDependencies.forEach((dependency) -> description.addDependency(dependency.getId(),
|
||||
MetadataBuildItemMapper.toDependency(dependency)));
|
||||
|
||||
return description;
|
||||
}
|
||||
|
||||
@ -70,6 +74,38 @@ public class ProjectRequestToDescriptionConverter {
|
||||
return (StringUtils.hasText(candidate)) ? candidate : fallback.get();
|
||||
}
|
||||
|
||||
private String baseDirectoryCorrection(String baseDir, String artifactId) {
|
||||
if (baseDir != null && baseDir.equals(artifactId)) {
|
||||
return coordinateCorrection(baseDir, "-");
|
||||
}
|
||||
return baseDir;
|
||||
}
|
||||
|
||||
private String artificialIdCorrection(String artifactId) {
|
||||
return coordinateCorrection(artifactId, "-");
|
||||
}
|
||||
|
||||
private String groupIdCorrection(String groupId) {
|
||||
return coordinateCorrection(groupId, ".");
|
||||
}
|
||||
|
||||
private String coordinateCorrection(String coordinate, String delimiter) {
|
||||
String[] elements = coordinate.split("[^A-Za-z0-9_\\-.]+");
|
||||
if (elements.length <= 1) {
|
||||
return coordinate;
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String element : elements) {
|
||||
if (!(element.startsWith("-") || element.startsWith("_") || element.startsWith(".")) && sb.length() > 0
|
||||
&& !(sb.charAt(sb.length() - 1) == '-' || sb.charAt(sb.length() - 1) == '_'
|
||||
|| sb.charAt(sb.length() - 1) == '.')) {
|
||||
sb.append(delimiter);
|
||||
}
|
||||
sb.append(element);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private void validate(ProjectRequest request, InitializrMetadata metadata) {
|
||||
validateSpringBootVersion(request);
|
||||
validateType(request.getType(), metadata);
|
||||
|
@ -36,6 +36,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
* @author Stephane Nicoll
|
||||
* @author HaiTao Zhang
|
||||
*/
|
||||
public class ProjectRequestToDescriptionConverterTests {
|
||||
|
||||
@ -270,6 +271,39 @@ public class ProjectRequestToDescriptionConverterTests {
|
||||
assertThat(description.getPackageName()).isEqualTo("com.example.demo");
|
||||
}
|
||||
|
||||
@Test
|
||||
void conventionCorrectionForArtifactIdWithDifferentNameAsBaseDir() {
|
||||
ProjectRequest request = createProjectRequest();
|
||||
String artifactId = "correct ! ID @";
|
||||
String validArtifactId = "correct-ID";
|
||||
request.setArtifactId(artifactId);
|
||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
||||
assertThat(description.getArtifactId()).isEqualTo(validArtifactId);
|
||||
assertThat(description.getBaseDirectory()).isEqualTo(request.getBaseDir());
|
||||
}
|
||||
|
||||
@Test
|
||||
void conventionCorrectionForArtifactIdWithSameNameAsBaseDir() {
|
||||
ProjectRequest request = createProjectRequest();
|
||||
String artifactId = "correct ! ID @";
|
||||
String validArtifactId = "correct-ID";
|
||||
request.setArtifactId(artifactId);
|
||||
request.setBaseDir(artifactId);
|
||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
||||
assertThat(description.getArtifactId()).isEqualTo(validArtifactId);
|
||||
assertThat(description.getBaseDirectory()).isEqualTo(validArtifactId);
|
||||
}
|
||||
|
||||
@Test
|
||||
void conventionCorrectionForGroupId() {
|
||||
ProjectRequest request = createProjectRequest();
|
||||
String groupId = "correct ! ID12 @";
|
||||
String validGroupId = "correct.ID12";
|
||||
request.setGroupId(groupId);
|
||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
||||
assertThat(description.getGroupId()).isEqualTo(validGroupId);
|
||||
}
|
||||
|
||||
private ProjectRequest createProjectRequest() {
|
||||
WebProjectRequest request = new WebProjectRequest();
|
||||
request.initialize(this.metadata);
|
||||
|
Loading…
Reference in New Issue
Block a user