Skip to content

Commit

Permalink
FORGE-2746: Check if a module exists before creating again
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Jun 20, 2017
1 parent 26fb8d3 commit 7b14a7d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Logger;

import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
Expand Down Expand Up @@ -44,10 +45,17 @@ public void associate(final Project project, final Resource<?> parentResource)
.substring(parent.getRoot().getFullyQualifiedName().length());
if (moduleDir.startsWith(File.separator))
moduleDir = moduleDir.substring(1);

parentPom.addModule(moduleDir);
parentMavenFacet.setModel(parentPom);

// If the module is already there, don't add
if (parentPom.getModules().contains(moduleDir))
{
Logger.getLogger(getClass().getName())
.warning("Module '" + moduleDir + "' is already declared in the parent pom.xml");
}
else
{
parentPom.addModule(moduleDir);
parentMavenFacet.setModel(parentPom);
}
MavenFacet projectMavenFacet = project.getFacet(MavenFacet.class);
Model pom = projectMavenFacet.getModel();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package org.jboss.forge.addon.maven;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;

import org.apache.maven.model.Model;
Expand Down Expand Up @@ -40,6 +42,7 @@ public class MavenMultiModuleProviderTest
@AddonDependency(name = "org.jboss.forge.addon:resources"),
@AddonDependency(name = "org.jboss.forge.addon:projects"),
@AddonDependency(name = "org.jboss.forge.addon:maven"),
@AddonDependency(name = "org.jboss.forge.addon:assertj"),
@AddonDependency(name = "org.jboss.forge.furnace.container:simple")
})
public static AddonArchive getDeployment()
Expand Down Expand Up @@ -124,6 +127,29 @@ public void testCreateNestedProjectWithParentThatHasInheritedVersion() throws Ex
Assert.assertEquals(parentProject.getFacet(MetadataFacet.class).getProjectVersion(), version);
Model subModel = subProject.getFacet(MavenFacet.class).getModel();
Assert.assertEquals(parentModel.getGroupId(), subModel.getParent().getGroupId());
}

@Test
public void testCreateNestedProjectWithExistingModuleInPom() throws Exception
{
Project parentProject = projectFactory.createTempProject(buildSystem);
Assert.assertNotNull(parentProject);

parentProject.getFacet(PackagingFacet.class).setPackagingType("pom");

MetadataFacet metadata = parentProject.getFacet(MetadataFacet.class);
metadata.setProjectName("parent");
metadata.setProjectGroupName("com.project.parent");

MavenFacet mavenFacet = parentProject.getFacet(MavenFacet.class);
Model parentModel = mavenFacet.getModel();
parentModel.addModule("sub");
mavenFacet.setModel(parentModel);

DirectoryResource subProjectDir = parentProject.getRoot().reify(DirectoryResource.class).getChildDirectory("sub");
projectFactory.createProject(subProjectDir, buildSystem);

List<String> modules = mavenFacet.getModel().getModules();
assertThat(modules).containsOnly("sub");
}
}

0 comments on commit 7b14a7d

Please sign in to comment.