diff --git a/src/main/java/org/dominokit/cli/PomUtil.java b/src/main/java/org/dominokit/cli/PomUtil.java index cba79c1..5d61b0d 100644 --- a/src/main/java/org/dominokit/cli/PomUtil.java +++ b/src/main/java/org/dominokit/cli/PomUtil.java @@ -27,5 +27,4 @@ public static Model asModel(String path) throws IOException { ModelReader modelReader = new DefaultModelReader(); return modelReader.read(new File(pomPath.toAbsolutePath().toString()), null); } - } diff --git a/src/main/java/org/dominokit/cli/commands/GenerateAppCommand.java b/src/main/java/org/dominokit/cli/commands/GenerateAppCommand.java index 1df4363..475a8ab 100644 --- a/src/main/java/org/dominokit/cli/commands/GenerateAppCommand.java +++ b/src/main/java/org/dominokit/cli/commands/GenerateAppCommand.java @@ -1,15 +1,22 @@ package org.dominokit.cli.commands; +import org.apache.commons.io.FileUtils; +import org.apache.maven.model.Model; +import org.dominokit.cli.PomUtil; +import org.dominokit.cli.model.Module; import org.dominokit.cli.model.Project; import org.dominokit.cli.structure.files.VelocityContentProcessor; import org.dominokit.cli.structure.folders.Folder; import org.dominokit.cli.structure.folders.Folder_MapperImpl; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Paths; import java.util.logging.Level; import java.util.logging.Logger; +import static java.util.Objects.isNull; +import static java.util.Objects.nonNull; import static picocli.CommandLine.*; @Command( @@ -31,8 +38,7 @@ public class GenerateAppCommand implements Runnable { @Option( names = {"-g", "--groupId"}, - description = "The project group ID, this will be used also for root package name", - required = true + description = "The project group ID, this will be used also for root package name" ) private String groupId; @@ -59,18 +65,37 @@ public class GenerateAppCommand implements Runnable { @Override public void run() { + Model parentPom = null; PathUtils.setWorkingDir(workingDire); + try { + parentPom = PomUtil.asModel(""); + } catch (Exception e) { + LOGGER.info("No parent pom was found, creating a root project."); + } Project project = new Project(); + project.setName(name); project.setArtifactId(name); - project.setGroupId(groupId); - project.setVersion(VERSION); + + + if (nonNull(parentPom)) { + project.setVersion(parentPom.getVersion()); + project.setParentArtifactId(parentPom.getArtifactId()); + project.setHasParent(true); + project.setGroupId(parentPom.getGroupId()); + if(isNull(groupId)) { + groupId = parentPom.getGroupId(); + } + } else { + project.setVersion(VERSION); + } + project.setRootPackage(groupId); project.setModuleShortName(name .replace("-", "") - .replace(".","") - .replace(" ","") + .replace(".", "") + .replace(" ", "") ); try { @@ -81,20 +106,40 @@ public void run() { .read(projectTemplateConfig); folder.write(Paths.get(PathUtils.getUserDir()), project); + if(project.isHasParent()){ + addProjectToParent(project, parentPom); + } + } catch (IOException e) { e.printStackTrace(); } } private String getTemplateByType(boolean j2cl, String type) { - String compiler = j2cl?"j2cl":"gwt"; + String compiler = j2cl ? "j2cl" : "gwt"; + + if (type.equalsIgnoreCase("mvp")) { + return "template/project/" + compiler + "/domino-mvp.json"; + } else if (type.equalsIgnoreCase("basic")) { + return "template/project/" + compiler + "/domino-basic.json"; + } + LOGGER.log(Level.SEVERE, "Unrecognized application type : " + type); + throw new IllegalArgumentException("Unrecognized application type : " + type); + } - if(type.equalsIgnoreCase("mvp")){ - return "template/project/"+compiler+"/domino-mvp.json"; - }else if(type.equalsIgnoreCase("basic")){ - return "template/project/"+compiler+"/domino-basic.json"; + private void addProjectToParent(Project project, Model parentPomModel) throws IOException { + + String parentPom; + try { + parentPom = PomUtil.asString(parentPomModel); + if (parentPom.contains("")) { + parentPom = parentPom.replace("", "\t" + project.getArtifactId() + "\n\t"); + } else { + parentPom = parentPom.replace("", "\n\t\n\t\t" + project.getArtifactId() + "\n\t\n"); + } + FileUtils.write(parentPomModel.getPomFile(), parentPom, StandardCharsets.UTF_8); + } catch (Exception e) { + e.printStackTrace(); } - LOGGER.log(Level.SEVERE, "Unrecognized application type : "+type); - throw new IllegalArgumentException("Unrecognized application type : "+type); } } diff --git a/src/main/java/org/dominokit/cli/model/Project.java b/src/main/java/org/dominokit/cli/model/Project.java index de453bd..5bebfaf 100644 --- a/src/main/java/org/dominokit/cli/model/Project.java +++ b/src/main/java/org/dominokit/cli/model/Project.java @@ -12,6 +12,8 @@ public class Project implements IsContext { private String version; private String rootPackage; private String moduleShortName; + private boolean hasParent = false; + private String parentArtifactId; public String getName() { return name; @@ -61,6 +63,22 @@ public void setModuleShortName(String moduleShortName) { this.moduleShortName = moduleShortName; } + public boolean isHasParent() { + return hasParent; + } + + public void setHasParent(boolean hasParent) { + this.hasParent = hasParent; + } + + public String getParentArtifactId() { + return parentArtifactId; + } + + public void setParentArtifactId(String parentArtifactId) { + this.parentArtifactId = parentArtifactId; + } + @Override public VelocityContext asContext() { VelocityContext context = new VelocityContext(); @@ -70,6 +88,8 @@ public VelocityContext asContext() { context.put("version", version); context.put("rootPackage", rootPackage); context.put("moduleShortName", moduleShortName); + context.put("hasParent", hasParent); + context.put("parentArtifactId", parentArtifactId); return context; } } diff --git a/src/main/resources/app/gwt/mvp/pom.xml b/src/main/resources/app/gwt/mvp/pom.xml index 210dd08..5747ff9 100644 --- a/src/main/resources/app/gwt/mvp/pom.xml +++ b/src/main/resources/app/gwt/mvp/pom.xml @@ -2,10 +2,22 @@ 4.0.0 + #if( $hasParent ) + + + ${groupId} + ${parentArtifactId} + ${version} + + ${artifactId} + + #else ${groupId} ${artifactId} ${version} + + #end pom diff --git a/src/main/resources/app/j2cl/basic/__name__-server/src/main/webapp/WEB-INF/web.xml b/src/main/resources/app/j2cl/basic/__name__-server/src/main/webapp/WEB-INF/web.xml index 96015c8..8d77258 100644 --- a/src/main/resources/app/j2cl/basic/__name__-server/src/main/webapp/WEB-INF/web.xml +++ b/src/main/resources/app/j2cl/basic/__name__-server/src/main/webapp/WEB-INF/web.xml @@ -3,30 +3,9 @@ xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> - - - greetServlet - ${rootPackage}.GreetingService - - - - greetServlet - /app/greet - - index.html - - WebjarsServlet - org.webjars.servlet.WebjarsServlet - 2 - - - WebjarsServlet - /webjars/* - -