Skip to content

Commit

Permalink
Make Quarkus Maven plugin smart create consistant with codestarts
Browse files Browse the repository at this point in the history
  • Loading branch information
ia3andy committed Nov 17, 2020
1 parent fec90d7 commit c281d1b
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@
@Mojo(name = "create", requiresProject = false)
public class CreateProjectMojo extends AbstractMojo {

private static final String DEFAULT_GROUP_ID = "org.acme.quarkus.sample";
private static final String DEFAULT_GROUP_ID = "org.acme";
private static final String DEFAULT_ARTIFACT_ID = "code-with-quarkus";
private static final String DEFAULT_VERSION = "1.0.0-SNAPSHOT";

@Parameter(defaultValue = "${project}")
protected MavenProject project;
Expand Down Expand Up @@ -102,9 +104,22 @@ public class CreateProjectMojo extends AbstractMojo {
@Parameter(property = "path")
private String path;

/**
* This parameter is only working with the RESTEasy and Spring Web extensions and is going to be removed.
* Use packageName instead.
*
* {@code className}
*/
@Parameter(property = "className")
@Deprecated
private String className;

/**
* If not set, groupId will be used
*/
@Parameter(property = "packageName")
private String packageName;

@Parameter(property = "buildTool", defaultValue = "MAVEN")
private String buildTool;

Expand Down Expand Up @@ -228,6 +243,7 @@ public void execute() throws MojoExecutionException {
.version(projectVersion)
.sourceType(sourceType)
.className(className)
.packageName(packageName)
.extensions(extensions)
.legacyCodegen(legacyCodegen)
.noExamples(noExamples);
Expand Down Expand Up @@ -352,15 +368,19 @@ private void askTheUserForMissingValues() throws MojoExecutionException {
// If the user has disabled the interactive mode or if the user has specified the artifactId, disable the
// user interactions.
if (!session.getRequest().isInteractiveMode() || shouldUseDefaults()) {
// Inject default values in all non-set parameters
if (StringUtils.isBlank(projectGroupId)) {
projectGroupId = DEFAULT_GROUP_ID;
}
if (StringUtils.isBlank(projectArtifactId)) {
projectArtifactId = "my-quarkus-project";
// we need to set it for the project directory
projectArtifactId = DEFAULT_ARTIFACT_ID;
}
if (StringUtils.isBlank(projectVersion)) {
projectVersion = "1.0-SNAPSHOT";
if (legacyCodegen) {
// Inject default values in all non-set parameters
if (StringUtils.isBlank(projectGroupId)) {
projectGroupId = DEFAULT_GROUP_ID;
}

if (StringUtils.isBlank(projectVersion)) {
projectVersion = DEFAULT_VERSION;
}
}
return;
}
Expand All @@ -373,28 +393,43 @@ private void askTheUserForMissingValues() throws MojoExecutionException {

if (StringUtils.isBlank(projectArtifactId)) {
projectArtifactId = prompter.promptWithDefaultValue("Set the project artifactId",
"my-quarkus-project");
DEFAULT_ARTIFACT_ID);
}

if (StringUtils.isBlank(projectVersion)) {
projectVersion = prompter.promptWithDefaultValue("Set the project version",
"1.0-SNAPSHOT");
DEFAULT_VERSION);
}

if (StringUtils.isBlank(className)) {
// Ask the user if he want to create a resource
String answer = prompter.promptWithDefaultValue("Do you want to create a REST resource? (y/n)", "no");
if (isTrueOrYes(answer)) {
String defaultResourceName = projectGroupId.replace("-", ".")
.replace("_", ".") + ".HelloResource";
className = prompter.promptWithDefaultValue("Set the resource classname", defaultResourceName);
if (StringUtils.isBlank(path)) {
path = prompter.promptWithDefaultValue("Set the resource path ", CreateUtils.getDerivedPath(className));
if (legacyCodegen) {
if (StringUtils.isBlank(className)) {
// Ask the user if he want to create a resource
String answer = prompter.promptWithDefaultValue("Do you want to create a REST resource? (y/n)", "no");
if (isTrueOrYes(answer)) {
String defaultResourceName = projectGroupId.replace("-", ".")
.replace("_", ".") + ".HelloResource";
className = prompter.promptWithDefaultValue("Set the resource classname", defaultResourceName);
if (StringUtils.isBlank(path)) {
path = prompter.promptWithDefaultValue("Set the resource path ",
CreateUtils.getDerivedPath(className));
}
} else {
className = null;
path = null;
}
} else {
className = null;
path = null;
}
} else {
if (extensions.isEmpty()) {
extensions = Arrays
.stream(prompter.promptWithDefaultValue("What extensions do you wish to add (comma separated list)",
"")
.split(","))
.map(String::trim).filter(StringUtils::isNotEmpty)
.collect(Collectors.toSet());
}
String answer = prompter.promptWithDefaultValue(
"Do you want example code to get started (yes), or just an empty project (no)", "yes");
noExamples = answer.startsWith("n");
}

} catch (IOException e) {
Expand All @@ -417,13 +452,16 @@ private boolean isTrueOrYes(String answer) {
}

private void sanitizeOptions(SourceType sourceType) {
// If className is null, we won't create the REST resource,
if (className != null) {
className = sourceType.stripExtensionFrom(className);

if (!className.contains(".")) {
// No package name, inject one
className = projectGroupId.replace("-", ".").replace("_", ".") + "." + className;
int idx = className.lastIndexOf('.');
if (idx >= 0 && StringUtils.isBlank(packageName)) {
// if it's a full qualified class name, we use the package name part (only if the packageName wasn't already defined)
packageName = className.substring(0, idx);

// And we strip it from the className
className = className.substring(idx + 1);
}

if (StringUtils.isBlank(path)) {
Expand All @@ -432,6 +470,7 @@ private void sanitizeOptions(SourceType sourceType) {
path = "/" + path;
}
}
// if package name is empty, the groupId will be used as part of the CreateProject logic
}

private void sanitizeExtensions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public static Map<String, Object> convertFromLegacy(Map<String, Object> legacy)
}
}

// TODO remove the class_name convertion when its removed
private static String convertClassName(final Map<String, Object> legacyData) {
Optional<String> classNameValue = NestedMaps.getValue(legacyData, "class_name");
if (classNameValue.isPresent()) {
Expand All @@ -119,6 +120,7 @@ private static String convertPackageName(final Map<String, Object> legacyData) {
if (packageNameValue.isPresent()) {
return packageNameValue.get();
}
// TODO remove this block when class_name is removed
Optional<String> classNameValue = NestedMaps.getValue(legacyData, "class_name");
if (classNameValue.isPresent()) {
final String className = classNameValue.get();
Expand All @@ -127,6 +129,12 @@ private static String convertPackageName(final Map<String, Object> legacyData) {
return className.substring(0, idx);
}
}

// Default to cleaned groupId if packageName not set
Optional<String> groupIdValue = NestedMaps.getValue(legacyData, "project_groupId");
if (groupIdValue.isPresent()) {
return groupIdValue.get().replace("-", ".").replace("_", ".");
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public CreateProject resourcePath(String resourcePath) {
return this;
}

/**
* Use packageName instead as this one is only working with RESTEasy and SpringWeb
*/
@Deprecated
public CreateProject className(String className) {
if (className == null) {
return this;
Expand All @@ -106,6 +110,17 @@ public CreateProject className(String className) {
return this;
}

public CreateProject packageName(String packageName) {
if (packageName == null) {
return this;
}
if (!(SourceVersion.isName(packageName) && !SourceVersion.isKeyword(packageName))) {
throw new IllegalArgumentException(packageName + " is not a package name");
}
setValue(PACKAGE_NAME, packageName);
return this;
}

public CreateProject extensions(Set<String> extensions) {
if (extensions == null) {
return this;
Expand Down Expand Up @@ -219,7 +234,6 @@ public QuarkusCommandOutcome execute() throws QuarkusCommandException {
setValue(IS_SPRING, true);
if (containsRESTEasy(extensions)) {
values.remove(CLASS_NAME);
values.remove(PACKAGE_NAME);
values.remove(RESOURCE_PATH);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
invocation.setValue(CLASS_NAME, className);
}

// Default to cleaned groupId if packageName not set
final String pkgName = invocation.getStringValue(PACKAGE_NAME);
final String groupId = invocation.getStringValue(PROJECT_GROUP_ID);
if (pkgName == null && groupId != null) {
invocation.setValue(PACKAGE_NAME, groupId.replace("-", ".").replace("_", "."));
}

final List<AppArtifactCoords> extensionsToAdd = computeCoordsFromQuery(invocation, extensionsQuery);

// extensionsToAdd is null when an error occurred while matching extensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void testProjectGenerationFromScratch() throws MavenInvocationException,
Properties properties = new Properties();
properties.put("projectGroupId", "org.acme");
properties.put("projectArtifactId", "acme");
properties.put("projectVersion", "1.0-SNAPSHOT");
properties.put("projectVersion", "1.0.0-SNAPSHOT");
InvocationResult result = setup(properties);

assertThat(result.getExitCode()).isZero();
Expand Down Expand Up @@ -375,8 +375,8 @@ public void testThatDefaultPackageAreReplaced() throws Exception {
assertThat(result.getExitCode()).isZero();
// As the directory is not empty (log) navigate to the artifactID directory
testDir = new File(testDir, "my-quarkus-project");
check(new File(testDir, "src/main/java/org/acme/quarkus/sample/MyGreatResource.java"),
"package org.acme.quarkus.sample;");
check(new File(testDir, "src/main/java/org/acme/MyGreatResource.java"),
"package org.acme;");
}

private void check(final File resource, final String contentsToFind) throws IOException {
Expand Down Expand Up @@ -419,7 +419,7 @@ public void generateNewProjectAndRun() throws Exception {
String resp = DevModeTestUtils.getHttpResponse();

assertThat(resp).containsIgnoringCase("ready").containsIgnoringCase("application").containsIgnoringCase("org.acme")
.containsIgnoringCase("1.0-SNAPSHOT");
.containsIgnoringCase("1.0.0-SNAPSHOT");

String greeting = DevModeTestUtils.getHttpResponse("/hello");
assertThat(greeting).containsIgnoringCase("hello");
Expand Down

0 comments on commit c281d1b

Please sign in to comment.