Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use bom in generated projects #347

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,36 @@ public void execute() throws MojoExecutionException {
createDirectories();
templates.generate(project, root, path, className, getLog());
Optional<Plugin> maybe = MojoUtils.hasPlugin(project, PLUGIN_KEY);

if (maybe.isPresent()) {
printUserInstructions(pomFile);
return;
}

// The plugin is not configured, add it.
addVersionProperty(model);
addBom(model);
addMainPluginConfig(model);
addExtensions(model, extensions, getLog());
addNativeProfile(model);
save(pomFile, model);
}

private void addBom(Model model) {
Dependency bom = new Dependency();
bom.setArtifactId(MojoUtils.get("bom-artifactId"));
bom.setGroupId(PLUGIN_GROUPID);
bom.setVersion("${shamrock.version}");
bom.setType("pom");
bom.setScope("import");

DependencyManagement dm = model.getDependencyManagement();
if (dm == null) {
dm = new DependencyManagement();
}
dm.addDependency(bom);
model.setDependencyManagement(dm);
}

private void printUserInstructions(File pomFile) {
getLog().info("");
getLog().info("========================================================================================");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public List<String> labels() {
return list;
}

public Dependency toDependency() {
public Dependency toDependency(boolean stripVersion) {
Dependency dependency = new Dependency();
dependency.setGroupId(groupId);
dependency.setArtifactId(artifactId);
Expand All @@ -136,7 +136,7 @@ public Dependency toDependency() {
if (classifier != null && !classifier.isEmpty()) {
dependency.setClassifier(classifier);
}
if (version != null && ! version.isEmpty()) {
if (version != null && ! version.isEmpty() && ! stripVersion) {
dependency.setVersion(version);
}
return dependency;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,23 @@ public static boolean addExtensions(Model model, List<String> extensions, Log lo
List<Extension> exts = Extensions.get();
for (String dependency : extensions) {
Optional<Extension> optional = exts.stream()
.filter(d ->{
.filter(d -> {
boolean hasTag = d.labels().contains(dependency.trim().toLowerCase());
boolean machName = d.getName().toLowerCase().contains(dependency.trim().toLowerCase());
return hasTag || machName;
return hasTag || machName;
})
.findAny();

if (optional.isPresent()) {
if (! MojoUtils.hasDependency(model, optional.get().getGroupId(), optional.get().getArtifactId())) {
if (!MojoUtils.hasDependency(model, optional.get().getGroupId(), optional.get().getArtifactId())) {
log.info("Adding extension " + optional.get().toCoordinates());
model.addDependency(optional.get().toDependency());

if (containsBOM(model)) {
model.addDependency(optional.get().toDependency(true));
} else {
model.addDependency(optional.get().toDependency(false));
}

updated = true;
} else {
log.info("Extension already present - skipping");
Expand All @@ -113,4 +119,14 @@ public static boolean addExtensions(Model model, List<String> extensions, Log lo
return updated;
}

private static boolean containsBOM(Model model) {
List<Dependency> dependencies = model.getDependencyManagement().getDependencies();
return dependencies.stream()
// Find bom
.filter(dependency -> "import".equalsIgnoreCase(dependency.getScope()))
.filter(dependency -> "pom".equalsIgnoreCase(dependency.getType()))
// Does it matches the bom artifact name
.anyMatch(dependency -> dependency.getArtifactId().equalsIgnoreCase(MojoUtils.get("bom-artifactId")));
}

}
3 changes: 2 additions & 1 deletion maven/src/main/resources/shamrock-maven-plugin.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ maven-compiler-plugin-version=3.8.0
maven-jar-plugin-version=3.1.0
maven-resources-plugin=3.1.0
shamrock-version=${shamrock.version}
doc-root=http://10.8.247.58/nfs/protean
doc-root=http://10.8.247.58/nfs/protean
bom-artifactId=shamrock-bom
18 changes: 12 additions & 6 deletions maven/src/main/templates/templates/pom-template.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,32 @@
<shamrock.version>${shamrockVersion}</shamrock.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-bom</artifactId>
<version>${r"${shamrock.version}"}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-jaxrs-deployment</artifactId>
<scope>provided</scope>
<version>${r"${shamrock.version}"}</version>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-arc-deployment</artifactId>
<scope>provided</scope>
<version>${r"${shamrock.version}"}</version>
</dependency>

<!-- Test extensions -->
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-junit</artifactId>
<version>${r"${shamrock.version}"}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import org.apache.commons.io.FileUtils;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.shared.invoker.*;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.jboss.shamrock.maven.CreateProjectMojo;
import org.jboss.shamrock.maven.it.verifier.RunningInvoker;
import org.jboss.shamrock.maven.utilities.MojoUtils;
import org.junit.After;
import org.junit.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.Properties;
import java.util.stream.Collectors;

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

Expand Down Expand Up @@ -65,6 +66,32 @@ public void testProjectGenerationFromScratch() throws MavenInvocationException,
assertThat(index).contains(VERSION);

assertThat(new File(testDir, "src/main/docker/Dockerfile")).isFile();

Model model = load(testDir);
assertThat(model.getDependencyManagement().getDependencies().stream().anyMatch(d ->
d.getArtifactId().equalsIgnoreCase(MojoUtils.get("bom-artifactId"))
&& d.getVersion().equalsIgnoreCase("${shamrock.version}")
&& d.getScope().equalsIgnoreCase("import")
&& d.getType().equalsIgnoreCase("pom"))).isTrue();

assertThat(model.getDependencies().stream().anyMatch(d ->
d.getArtifactId().equalsIgnoreCase("shamrock-jaxrs-deployment")
&& d.getVersion() == null)).isTrue();
}

private Model load(File directory) {
File pom = new File(directory, "pom.xml");
assertThat(pom).isFile();
MavenXpp3Reader reader = new MavenXpp3Reader();
try (FileReader fr = new FileReader(pom)) {
return reader.read(fr);
} catch (FileNotFoundException e) {
throw new IllegalArgumentException("Cannot find the pom.xml file", e);
} catch (IOException e) {
throw new IllegalArgumentException("Cannot read the pom.xml file", e);
} catch (XmlPullParserException e) {
throw new IllegalArgumentException("Malformed pom.xml file", e);
}
}

@Test
Expand All @@ -80,6 +107,17 @@ public void testProjectGenerationFromMinimalPom() throws Exception {

assertThat(new File(testDir, "src/main/resources/META-INF/microprofile-config.properties")).doesNotExist();
assertThat(new File(testDir, "src/main/resources/META-INF/resources/index.html")).doesNotExist();

assertThat(FileUtils.readFileToString(new File(testDir, "pom.xml"), "UTF-8")).containsIgnoringCase(MojoUtils.get("bom-artifactId"));

Model model = load(testDir);
assertThat(model.getDependencyManagement().getDependencies().stream().anyMatch(d ->
d.getArtifactId().equalsIgnoreCase(MojoUtils.get("bom-artifactId"))
&& d.getVersion().equalsIgnoreCase("${shamrock.version}")
&& d.getScope().equalsIgnoreCase("import")
&& d.getType().equalsIgnoreCase("pom"))).isTrue();

assertThat(model.getDependencies()).isEmpty();
}

@Test
Expand Down Expand Up @@ -134,6 +172,23 @@ public void testProjectGenerationFromScratchWithExtensions() throws Exception {
assertThat(new File(testDir, "src/main/java/org/acme/ShamrockApplication.java")).isFile();
assertThat(FileUtils.readFileToString(new File(testDir, "pom.xml"), "UTF-8"))
.contains("shamrock-jaxrs-deployment", "shamrock-metrics-deployment").doesNotContain("missing");

Model model = load(testDir);
assertThat(model.getDependencyManagement().getDependencies().stream().anyMatch(d ->
d.getArtifactId().equalsIgnoreCase(MojoUtils.get("bom-artifactId"))
&& d.getVersion().equalsIgnoreCase("${shamrock.version}")
&& d.getScope().equalsIgnoreCase("import")
&& d.getType().equalsIgnoreCase("pom"))).isTrue();

System.out.println(model.getDependencies().stream().map(d -> d.getManagementKey() + "[" + d.getVersion() + "]").collect(Collectors.toList()));

assertThat(model.getDependencies().stream().anyMatch(d ->
d.getArtifactId().equalsIgnoreCase("shamrock-jaxrs-deployment")
&& d.getVersion() == null)).isTrue();

assertThat(model.getDependencies().stream().anyMatch(d ->
d.getArtifactId().equalsIgnoreCase("shamrock-metrics-deployment")
&& d.getVersion() == null)).isTrue();
}

@Test
Expand All @@ -152,6 +207,21 @@ public void testProjectGenerationFromScratchWithCustomDependencies() throws Exce
assertThat(new File(testDir, "src/main/java/org/acme/ShamrockApplication.java")).isFile();
assertThat(FileUtils.readFileToString(new File(testDir, "pom.xml"), "UTF-8"))
.contains("commons-io");

Model model = load(testDir);
assertThat(model.getDependencyManagement().getDependencies().stream().anyMatch(d ->
d.getArtifactId().equalsIgnoreCase(MojoUtils.get("bom-artifactId"))
&& d.getVersion().equalsIgnoreCase("${shamrock.version}")
&& d.getScope().equalsIgnoreCase("import")
&& d.getType().equalsIgnoreCase("pom"))).isTrue();

assertThat(model.getDependencies().stream().anyMatch(d ->
d.getArtifactId().equalsIgnoreCase("shamrock-jaxrs-deployment")
&& d.getVersion() == null)).isTrue();

assertThat(model.getDependencies().stream().anyMatch(d ->
d.getArtifactId().equalsIgnoreCase("commons-io")
&& d.getVersion().equalsIgnoreCase("2.5"))).isTrue();
}

@Test
Expand Down