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

Option to skip if Graal already made exe #374

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 @@ -61,6 +61,9 @@
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
import org.apache.maven.model.Build;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginExecution;

/**
* Wraps a jar in a Windows executable.
Expand Down Expand Up @@ -343,6 +346,13 @@ public class Launch4jMojo extends AbstractMojo {
@Parameter(defaultValue = "false")
private boolean skip = false;

/**
* If set to true, make best effort to skip execution if other plugins are
* generating native image / exe file
*/
@Parameter(defaultValue = "false")
private boolean skipIfNativeImage = false;

private File getJar() {
return new File(jar);
}
Expand All @@ -364,6 +374,14 @@ private void doExecute() throws MojoExecutionException {
return;
}

if (this.skipIfNativeImageParam()) {
getLog().info("skipIfNativeImage enabled. Consider rewriting your pom with profiles");
getLog().debug("Checking if any other plugin generates a native image / exe file");
if (isNativeImageGenerationActive()) {
return;
}
}

processRequireAdminRights();

fillSensibleJreDefaults();
Expand Down Expand Up @@ -929,9 +947,62 @@ private String getLaunch4jVersion() throws MojoExecutionException {
*/
private boolean skipExecution() {
getLog().debug("skip = " + this.skip);
getLog().debug("skipLaunch4j = " + System.getProperty("skipLaunch4j"));
return skip || System.getProperty("skipLaunch4j") != null;
String sysProp = System.getProperty("skipLaunch4j");
getLog().debug("skipLaunch4j = " + sysProp);

return skip || (sysProp != null && !sysProp.equalsIgnoreCase("false"));

}

private boolean skipIfNativeImageParam() {
getLog().debug("skipIfNativeImage = " + this.skipIfNativeImage);
String sysProp = System.getProperty("skipLaunch4jIfNativeImage");
getLog().debug("skipLaunch4jIfNativeImage = " + sysProp);

return skipIfNativeImage || (sysProp != null && !sysProp.equalsIgnoreCase("false"));
}

private boolean isNativeImageGenerationActive() {
String graalKey = "org.graalvm.buildtools:native-maven-plugin";
Log log = getLog();
MavenProject curProj = session.getCurrentProject();
Build build = curProj.getBuild();
String packaging = curProj.getPackaging();

if (packaging.equalsIgnoreCase("native-image")) {
log.info("Current project's packaging is set to native-image, skipping.");
return true;
}

// check if there's a Graal plugin and if it has any enabled compile executions
Plugin graalPlugin = build.getPluginsAsMap().get(graalKey);

// check if 1) there's a goal with "compile", 2) with no skip params, 3) and no skip sysprops
if (graalPlugin != null) {
for (PluginExecution pe : graalPlugin.getExecutions()) {
String goalsStr = pe.getGoals().toString().toLowerCase();
String confStr = pe.getConfiguration().toString().toLowerCase();

if (!goalsStr.contains("compile") || pe.getPhase() == null) {
continue;
}
if (confStr.contains("<skip>true") || confStr.contains("<skipNativeBuild>true"))
Comment on lines +983 to +989
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no way to detect that without parsing the whole configuration? What if there be <skip>true but not related to Graal plugin, eg. to skip tests?

Copy link
Contributor Author

@Lilianne-Blaze Lilianne-Blaze Jun 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not parsing the whole configuration, only the fragment from a specific execution, for example:

confStr=>>><?xml version="1.0" encoding="utf-8"?>
<configuration>
  <buildargs combine.children="append">
    <buildarg>--no-fallback</buildarg>
    <buildarg>--no-fallback</buildarg>
    <buildarg>--no-fallback</buildarg></buildargs>
  <imagename>xxxxxxxxxx</imagename>
  <mainclass>com.example.xxxxxxxxxx</mainclass>
  <metadatarepository>
    <enabled>true</enabled>
  </metadatarepository>
  <isdetectionenabled>true</isdetectionenabled>
</configuration><<<

Note it's not checking the file-pom, only xml recreated from already parsed and merged effective pom. Also it ignores executions that don't have "compile" word in them, so it shouldn't interfere with things like metadata gathering / merging.
I'm certain there will be cases when it will fail, but it should suffice for 90%+ of typical format+compile+shade+package projects. Certainly for the ones in which the user just copy-pastes and slightly modifies the parts relevant to Graal and Launch4j.

p.s. I have no idea why "--no-fallback" is repeated, it's just a basic Micronaut project, using their generator, with simplest Launch4j fragment added.

{
continue;
}
String sysProp = System.getProperty("skipNativeBuild");
if (sysProp != null && !sysProp.equalsIgnoreCase("false")) {
continue;
}

log.info("There is an active Graal plugin execution, skipping.");
return true;
}
}

return false;
}


@Override
public String toString() {
Expand Down