Skip to content

Commit

Permalink
#216 create-runtime-image: Error message on missing artifact
Browse files Browse the repository at this point in the history
Closes #216
  • Loading branch information
bmarwell committed Feb 12, 2024
1 parent 4c39711 commit a793af4
Showing 1 changed file with 26 additions and 2 deletions.
Expand Up @@ -25,9 +25,11 @@
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
Expand Down Expand Up @@ -109,7 +111,7 @@ public class CreateRuntimeImageMojo extends AbstractMojo {
private boolean bindServices;

@Override
public void execute() throws MojoExecutionException {
public void execute() throws MojoExecutionException, MojoFailureException {
Path jmodsDir = getJModsDir();

Set<Path> effectiveModulePath = this.modulePath.stream()
Expand All @@ -118,12 +120,18 @@ public void execute() throws MojoExecutionException {

effectiveModulePath.add(jmodsDir);

final Artifact primaryProjectArtifact = project.getArtifact();

if (primaryProjectArtifact == null || !primaryProjectArtifact.getFile().exists()) {
throw new MojoFailureException(getNoProjectArtifactMessage(primaryProjectArtifact));
}

CreateRuntimeImage createRuntimeImage = new CreateRuntimeImage(
effectiveModulePath,
modules,
jarInclusionPolicy,
DependencyHelper.getDirectAndTransitiveDependencies(project),
project.getArtifact().getFile().toPath(),
primaryProjectArtifact.getFile().toPath(),
launcher != null ? launcher.getName() : null,
launcher != null ? launcher.getModule() : null,
outputDirectory.toPath(),
Expand All @@ -144,6 +152,22 @@ public void execute() throws MojoExecutionException {
}
}

private static String getNoProjectArtifactMessage(Artifact primaryProjectArtifact) {
final StringBuilder message = new StringBuilder();
message.append("No primary project artifact was found to create a runtime image.");

if (primaryProjectArtifact != null && primaryProjectArtifact.getFile() != null) {
message.append(" The file ");
message.append(primaryProjectArtifact.getFile());
message.append(" does not exist.");
}

message.append(" Please make sure the project artifact has been created (usually in the package lifecycle phase).");
message.append(" This goal should usually not run before artifact creation.");

return message.toString();
}

/**
* Returns the directory with the jmod files to be used for creating the image.
* If {@code baseJdk} has been given, the jmod files from the JDK identified that way
Expand Down

0 comments on commit a793af4

Please sign in to comment.