Skip to content

Commit

Permalink
Fixes #531 Skip shamrock:dev on projects that do not have the build g…
Browse files Browse the repository at this point in the history
…oal configured

This allows Shamrock to work in multi-module project, the dev goal will only
be run for the actual shamrock app, not parent poms or support libraries.
  • Loading branch information
stuartwdouglas committed Jan 18, 2019
1 parent c47abe5 commit ec8929e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ public class CreateProjectMojo extends AbstractMojo {
public static final String VERSION_PROP = "shamrock-version";
public static final String PLUGIN_VERSION_PROPERTY_NAME = "shamrock.version";
public static final String PLUGIN_VERSION_PROPERTY = "${" + PLUGIN_VERSION_PROPERTY_NAME + "}";
public static final String PLUGIN_GROUPID = "org.jboss.shamrock";
public static final String PLUGIN_ARTIFACTID = "shamrock-maven-plugin";
public static final String PLUGIN_KEY = PLUGIN_GROUPID + ":" + PLUGIN_ARTIFACTID;
public static final String PLUGIN_KEY = MavenConstants.PLUGIN_GROUPID + ":" + MavenConstants.PLUGIN_ARTIFACTID;

/**
* The Maven project which will define and configure the shamrock-maven-plugin
Expand Down Expand Up @@ -139,7 +137,7 @@ public void execute() throws MojoExecutionException {
private void addBom(Model model) {
Dependency bom = new Dependency();
bom.setArtifactId(MojoUtils.get("bom-artifactId"));
bom.setGroupId(PLUGIN_GROUPID);
bom.setGroupId(MavenConstants.PLUGIN_GROUPID);
bom.setVersion("${shamrock.version}");
bom.setType("pom");
bom.setScope("import");
Expand All @@ -166,7 +164,7 @@ private void addNativeProfile(Model model) {
Profile profile = new Profile();
profile.setId("native");
BuildBase buildBase = new BuildBase();
Plugin plg = plugin(PLUGIN_GROUPID, PLUGIN_ARTIFACTID, PLUGIN_VERSION_PROPERTY);
Plugin plg = plugin(MavenConstants.PLUGIN_GROUPID, MavenConstants.PLUGIN_ARTIFACTID, PLUGIN_VERSION_PROPERTY);
PluginExecution exec = new PluginExecution();
exec.addGoal("native-image");
MojoUtils.Element element = new MojoUtils.Element("enableHttpUrlHandler", "true");
Expand All @@ -178,13 +176,13 @@ private void addNativeProfile(Model model) {
}

private void addMainPluginConfig(Model model) {
Plugin plugin = plugin(PLUGIN_GROUPID, PLUGIN_ARTIFACTID, PLUGIN_VERSION_PROPERTY);
Plugin plugin = plugin(MavenConstants.PLUGIN_GROUPID, MavenConstants.PLUGIN_ARTIFACTID, PLUGIN_VERSION_PROPERTY);
if (isParentPom(model)) {
addPluginManagementSection(model, plugin);
//strip the shamrockVersion off
plugin = plugin(PLUGIN_GROUPID, PLUGIN_ARTIFACTID);
plugin = plugin(MavenConstants.PLUGIN_GROUPID, MavenConstants.PLUGIN_ARTIFACTID);
} else {
plugin = plugin(PLUGIN_GROUPID, PLUGIN_ARTIFACTID, PLUGIN_VERSION_PROPERTY);
plugin = plugin(MavenConstants.PLUGIN_GROUPID, MavenConstants.PLUGIN_ARTIFACTID, PLUGIN_VERSION_PROPERTY);
}
PluginExecution pluginExec = new PluginExecution();
pluginExec.addGoal("build");
Expand Down
20 changes: 20 additions & 0 deletions maven/src/main/java/org/jboss/shamrock/maven/DevMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import java.util.zip.ZipOutputStream;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginExecution;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoFailureException;
Expand Down Expand Up @@ -107,6 +109,24 @@ public class DevMojo extends AbstractMojo {

@Override
public void execute() throws MojoFailureException {

boolean found = false;
for(Plugin i : project.getBuildPlugins()) {
if(i.getGroupId().equals(MavenConstants.PLUGIN_GROUPID)
&& i.getArtifactId().equals(MavenConstants.PLUGIN_ARTIFACTID)) {
for(PluginExecution p : i.getExecutions()) {
if(p.getGoals().contains("build")) {
found = true;
break;
}
}
}
}
if(!found) {
getLog().warn("The shamrock-maven-plugin build goal was not configured for this project, skipping shamrock:dev as this is assumed to be a support library. If you want to run shamrock dev on this project make sure the shamrock-maven-plugin is configured with a build goal.");
return;
}

if (! sourceDir.isDirectory()) {
throw new MojoFailureException("The `src/main/java` directory is required, please create it.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import java.util.List;
import java.util.Optional;

import static org.jboss.shamrock.maven.CreateProjectMojo.PLUGIN_GROUPID;
import static org.jboss.shamrock.maven.MavenConstants.PLUGIN_GROUPID;

/**
* @author <a href="http://escoffier.me">Clement Escoffier</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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.MavenConstants;
import org.jboss.shamrock.maven.CreateProjectMojo;
import org.jboss.shamrock.maven.it.verifier.RunningInvoker;
import org.jboss.shamrock.maven.utilities.MojoUtils;
Expand Down Expand Up @@ -102,7 +103,7 @@ public void testProjectGenerationFromMinimalPom() throws Exception {
setup(new Properties());
assertThat(new File(testDir, "pom.xml")).isFile();
assertThat(FileUtils.readFileToString(new File(testDir, "pom.xml"), "UTF-8"))
.contains(CreateProjectMojo.PLUGIN_ARTIFACTID, CreateProjectMojo.PLUGIN_VERSION_PROPERTY, CreateProjectMojo.PLUGIN_GROUPID);
.contains(MavenConstants.PLUGIN_ARTIFACTID, CreateProjectMojo.PLUGIN_VERSION_PROPERTY, MavenConstants.PLUGIN_GROUPID);
assertThat(new File(testDir, "src/main/java")).isDirectory();

assertThat(new File(testDir, "src/main/resources/META-INF/microprofile-config.properties")).doesNotExist();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.maven.shared.utils.StringUtils;
import org.jboss.shamrock.maven.MavenConstants;
import org.jboss.shamrock.maven.CreateProjectMojo;
import org.jboss.shamrock.maven.utilities.MojoUtils;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -34,8 +35,8 @@ public static void init() {
assertThat(VERSION).isNotNull();

VARIABLES = ImmutableMap.of(
"@project.groupId@", CreateProjectMojo.PLUGIN_GROUPID,
"@project.artifactId@", CreateProjectMojo.PLUGIN_ARTIFACTID,
"@project.groupId@", MavenConstants.PLUGIN_GROUPID,
"@project.artifactId@", MavenConstants.PLUGIN_ARTIFACTID,
"@project.version@", VERSION,
"@rest-assured.version@", MojoUtils.get("restAssuredVersion"));
}
Expand Down Expand Up @@ -95,26 +96,26 @@ public static File initProject(String name, String output) {
}

public static void installPluginToLocalRepository(File local) {
File repo = new File(local, CreateProjectMojo.PLUGIN_GROUPID.replace(".", "/") + "/"
+ CreateProjectMojo.PLUGIN_ARTIFACTID + "/" + MojoTestBase.VERSION);
File repo = new File(local, MavenConstants.PLUGIN_GROUPID.replace(".", "/") + "/"
+ MavenConstants.PLUGIN_ARTIFACTID + "/" + MojoTestBase.VERSION);
if (!repo.isDirectory()) {
boolean mkdirs = repo.mkdirs();
Logger.getLogger(MojoTestBase.class.getName())
.log(Level.FINE, repo.getAbsolutePath() + " created? " + mkdirs);
}

File plugin = new File("target", CreateProjectMojo.PLUGIN_ARTIFACTID + "-" + MojoTestBase.VERSION + ".jar");
File plugin = new File("target", MavenConstants.PLUGIN_ARTIFACTID + "-" + MojoTestBase.VERSION + ".jar");
if (!plugin.isFile()) {
File[] files = new File("target").listFiles(
file -> file.getName().startsWith(CreateProjectMojo.PLUGIN_ARTIFACTID) && file.getName().endsWith(".jar"));
file -> file.getName().startsWith(MavenConstants.PLUGIN_ARTIFACTID) && file.getName().endsWith(".jar"));
if (files != null && files.length != 0) {
plugin = files[0];
}
}

try {
FileUtils.copyFileToDirectory(plugin, repo);
String installedPomName = CreateProjectMojo.PLUGIN_ARTIFACTID + "-" + MojoTestBase.VERSION + ".pom";
String installedPomName = MavenConstants.PLUGIN_ARTIFACTID + "-" + MojoTestBase.VERSION + ".pom";
FileUtils.copyFile(new File("pom.xml"), new File(repo, installedPomName));
} catch (IOException e) {
throw new RuntimeException("Cannot copy the plugin jar, or the pom file, to the local repository", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.jboss.shamrock.maven.MavenConstants;
import org.jboss.shamrock.maven.CreateProjectMojo;
import org.jboss.shamrock.maven.utilities.MojoUtils;

Expand Down Expand Up @@ -58,8 +59,8 @@ public static void verifySetup(File pomFile) throws Exception {
// Check plugin is set
Plugin plugin = maybe.orElseThrow(() -> new AssertionError("Plugin expected"));
assertThat(plugin).isNotNull().satisfies(p -> {
assertThat(p.getArtifactId()).isEqualTo(CreateProjectMojo.PLUGIN_ARTIFACTID);
assertThat(p.getGroupId()).isEqualTo(CreateProjectMojo.PLUGIN_GROUPID);
assertThat(p.getArtifactId()).isEqualTo(MavenConstants.PLUGIN_ARTIFACTID);
assertThat(p.getGroupId()).isEqualTo(MavenConstants.PLUGIN_GROUPID);
assertThat(p.getVersion()).isEqualTo(CreateProjectMojo.PLUGIN_VERSION_PROPERTY);
});

Expand Down

0 comments on commit ec8929e

Please sign in to comment.