private static void addModules(Model model, List<Model> modules) throws IOException { if (!"pom".equals(model.getPackaging())) return; for (String module : model.getModules()) { final Path modulePom = model.getProjectDirectory().toPath().resolve(module).resolve("pom.xml"); if (exists(modulePom)) { final Model child = Model.readFrom(modulePom); final String relativePath = modulePom.getParent().relativize(model.getPomFile().toPath()).toString(); child.setParent( new Parent(model.getGroupId(), model.getArtifactId(), model.getVersion()) .setRelativePath(relativePath)); modules.add(child); addModules(child, modules); } } }
/** * Get groupId of artifact. If artifact doesn't have groupId this method checks parent artifact * for groupId. */ public static String getGroupId(Model model) { String groupId = model.getGroupId(); if (groupId == null) { final Parent parent = model.getParent(); if (parent != null) { groupId = parent.getGroupId(); } } return groupId; }
@Override public String toString() { return getId(); }
/** * Sets parent relative path * * <p>If {@code relativePath} is {@code null} and this parent instance is associated with xml * element then {@code relativePath} will be removed from model as well as from xml * * @param relativePath new parent relative path * @return this parent instance */ public Parent setRelativePath(String relativePath) { this.relativePath = relativePath; if (!isNew()) { if (relativePath == null) { parentElement.removeChild("relativePath"); } else if (parentElement.hasSingleChild("relativePath")) { parentElement.getSingleChild("relativePath").setText(relativePath); } else { parentElement.appendChild(createElement("relativePath", relativePath)); } } return this; }
/** * Get version of artifact. If artifact doesn't have version this method checks parent artifact * for version. */ public static String getVersion(Model model) { String version = model.getVersion(); if (version == null) { final Parent parent = model.getParent(); if (parent != null) { version = parent.getVersion(); } } return version; }
/** * Sets the location of the parent project, if one exists. * * <p>Values from the parent project will be the default for this project if they are left * unspecified. The location is given as a group ID, artifact ID and version. If {@code parent} is * {@code null} then it will be removed from model and xml as well. * * @param parent new project parent * @return this model instance */ public Model setParent(Parent parent) { this.parent = parent; if (parent == null) { root.removeChild("parent"); } else if (root.hasSingleChild("parent")) { // replace parent parent.parentElement = root.getSingleChild("parent").replaceWith(parent.asXMLElement()); } else { // add parent root.insertChild(this.parent.asXMLElement(), after("modelVersion").or(inTheBegin())); parent.parentElement = root.getSingleChild("parent"); } return this; }
model.packaging = root.getChildText("packaging"); if (root.hasSingleChild("parent")) { model.parent = new Parent(root.getSingleChild("parent"));
/** * Sets the group id of the parent project to inherit from * * <p>If {@code groupId} is {@code null} and this parent instance is associated with xml element * then {@code groupId} will be removed from model as well as from xml. * * @param groupId new parent group identifier * @return this parent instance */ public Parent setGroupId(String groupId) { this.groupId = groupId; if (!isNew()) { if (groupId == null) { parentElement.removeChild("groupId"); } else if (parentElement.hasSingleChild("groupId")) { parentElement.getSingleChild("groupId").setText(groupId); } else { parentElement.insertChild(createElement("groupId", groupId), inTheBegin()); } } return this; }
/** * Sets the version of the parent project to inherit * * <p>If {@code version} is {@code null} and this parent instance is associated with xml element * then {@code version} will be removed from model as well as from xml * * @param version new parent version * @return this parent instance */ public Parent setVersion(String version) { this.version = version; if (!isNew()) { if (version == null) { parentElement.removeChild("version"); } else if (parentElement.hasSingleChild("version")) { parentElement.getSingleChild("version").setText(version); } else { parentElement.insertChild( createElement("version", version), before("relativePath").or(inTheEnd())); } } return this; }
/** * Sets the artifact id of the parent project to inherit from * * <p>If {@code artifactId} is {@code null} and this parent instance is associated with xml * element then {@code artifactId} will be removed from model as well as from xml. * * @param artifactId new parent artifact identifier * @return this parent instance */ public Parent setArtifactId(String artifactId) { this.artifactId = artifactId; if (!isNew()) { if (artifactId == null) { parentElement.removeChild("artifactId"); } else if (parentElement.hasSingleChild("artifactId")) { parentElement.getSingleChild("artifactId").setText(artifactId); } else { parentElement.insertChild( createElement("artifactId", artifactId), after("groupId").or(inTheBegin())); } } return this; }