Skip to content

Commit

Permalink
[MPLUGIN-450] Require goalPrefix to be valid
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Nov 10, 2023
1 parent b2aa6cd commit 0b27ea3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

Expand Down Expand Up @@ -81,28 +80,24 @@ public void execute() throws MojoExecutionException {
return;
}

String defaultGoalPrefix = getDefaultGoalPrefix(project);

if (goalPrefix == null) {
goalPrefix = defaultGoalPrefix;
} else if (!goalPrefix.equals(defaultGoalPrefix)) {
getLog().warn(LS + LS + "Goal prefix is specified as: '" + goalPrefix + "'. "
+ "Maven currently expects it to be '" + defaultGoalPrefix + "'." + LS);
if (goalPrefix == null || goalPrefix.isEmpty()) {
goalPrefix = getDefaultGoalPrefix(project);
}
if (goalPrefix == null || goalPrefix.isEmpty()) {
throw new MojoExecutionException("You need to specify a goalPrefix as it can not be correctly computed");
}

generate();
}

static String getDefaultGoalPrefix(MavenProject project) {
String defaultGoalPrefix;
if ("maven-plugin-report-plugin".equalsIgnoreCase(project.getArtifactId())) {
defaultGoalPrefix = "plugin-report";
} else if ("maven-plugin".equalsIgnoreCase(project.getArtifactId())) {
defaultGoalPrefix =
project.getGroupId().substring(project.getGroupId().lastIndexOf('.') + 1);
String artifactId = project.getArtifactId();
if (artifactId.endsWith("-maven-plugin")) {
return artifactId.substring(0, artifactId.length() - "-maven-plugin".length());
} else if (artifactId.startsWith("maven-") && artifactId.endsWith("-plugin")) {
return artifactId.substring("maven-".length(), artifactId.length() - "-plugin".length());
} else {
defaultGoalPrefix = PluginDescriptor.getGoalPrefixFromArtifactId(project.getArtifactId());
return null;
}
return defaultGoalPrefix;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ public static Stream<Arguments> goalPrefixes() {
arguments(null, "maven-plugin-plugin", "plugin"),
arguments(null, "maven-plugin-report-plugin", "plugin-report"),
arguments(null, "maven-default-plugin", "default"),
arguments(null, "default-maven-plugin", "default"),
arguments(null, "default-maven-plugin", "default"),
arguments("foo.bar", "maven-plugin", "bar"),
arguments("foo", "maven-plugin", "foo"));
arguments(null, "default-maven-plugin", "default"));
}

@ParameterizedTest
Expand Down

0 comments on commit 0b27ea3

Please sign in to comment.