diff --git a/devtools/maven/src/main/java/io/quarkus/maven/CreateProjectMojo.java b/devtools/maven/src/main/java/io/quarkus/maven/CreateProjectMojo.java index 525dbeb34..48a30ca0a 100644 --- a/devtools/maven/src/main/java/io/quarkus/maven/CreateProjectMojo.java +++ b/devtools/maven/src/main/java/io/quarkus/maven/CreateProjectMojo.java @@ -26,6 +26,8 @@ import java.util.Properties; import java.util.Set; import java.util.stream.Collectors; +import javax.lang.model.SourceVersion; + import org.apache.commons.lang3.StringUtils; import org.apache.maven.execution.DefaultMavenExecutionRequest; import org.apache.maven.execution.MavenExecutionRequest; @@ -192,6 +194,10 @@ public class CreateProjectMojo extends AbstractMojo { final SourceType sourceType = CreateProject.determineSourceType(extensions); sanitizeOptions(sourceType); + if (className != null && !isClassNameValid(className)) { + throw new MojoExecutionException("Unable to create the project, " + className + " is not valid FQCN."); + } + final Map context = new HashMap<>(); context.put("path", path); @@ -383,6 +389,10 @@ public class CreateProjectMojo extends AbstractMojo { } } + private boolean isClassNameValid(String className) { + return SourceVersion.isName(className) && !SourceVersion.isKeyword(className); + } + private void sanitizeExtensions() { extensions = extensions.stream().filter(Objects::nonNull).map(String::trim).collect(Collectors.toSet()); } diff --git a/integration-tests/maven/src/test/java/io/quarkus/maven/it/CreateProjectMojoIT.java b/integration-tests/maven/src/test/java/io/quarkus/maven/it/CreateProjectMojoIT.java index 46d0851eb..2fefe7f62 100644 --- a/integration-tests/maven/src/test/java/io/quarkus/maven/it/CreateProjectMojoIT.java +++ b/integration-tests/maven/src/test/java/io/quarkus/maven/it/CreateProjectMojoIT.java @@ -133,6 +133,23 @@ public class CreateProjectMojoIT extends QuarkusPlatformAwareMojoTestBase { check(new File(testDir, "src/main/java/org/acme/MyResource.java"), "package org.acme;"); } + @Test + public void testProjectGenerationWithInvalidPackage() throws Exception { + testDir = initEmptyProject("projects/project-generation-invalid-package"); + assertThat(testDir).isDirectory(); + invoker = initInvoker(testDir); + + Properties properties = new Properties(); + properties.put("projectGroupId", "org.acme"); + properties.put("projectArtifactId", "acme"); + properties.put("className", "org.acme.invalid-package-name.MyResource"); + + InvocationResult result = setup(properties); + + assertThat(result.getExitCode()).isNotZero(); + assertThat(new File(testDir, "src/main/java/org/acme")).doesNotExist(); + } + @Test public void testProjectGenerationFromMinimalPomWithResource() throws Exception { testDir = initProject("projects/simple-pom-it", "projects/project-generation-from-empty-pom-with-resource");