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

Dynamic version from Maven with GraalVM #2018

Open
delanym opened this issue May 5, 2023 · 4 comments
Open

Dynamic version from Maven with GraalVM #2018

delanym opened this issue May 5, 2023 · 4 comments

Comments

@delanym
Copy link

delanym commented May 5, 2023

hi. How can I use the Maven version when compiling on GraalVM?

Noted: #850 (comment)

My current approach doesn't work with GraalVM. When I run the executable with -V there is no output and no error.

@CommandLine.Command(
    versionProvider = CommandLineInterface.ManifestVersionProvider.class,
    //    version = "1.0.0-SNAPSHOT",
    name = "raes2zip",
    mixinStandardHelpOptions = true)
public class CommandLineInterface implements Callable<Integer> {

...

static class ManifestVersionProvider implements CommandLine.IVersionProvider {

    public String[] getVersion() throws Exception {
      Enumeration<URL> resources =
          CommandLine.class.getClassLoader().getResources("META-INF/MANIFEST.MF");
      while (resources.hasMoreElements()) {
        URL url = resources.nextElement();
        try {
          Manifest manifest = new Manifest(url.openStream());
          if (isApplicableManifest(manifest)) {
            Attributes attr = manifest.getMainAttributes();
            return new String[] {
              get(attr, "Implementation-Title")
                  + " version \""
                  + get(attr, "Implementation-Version")
                  + "\""
            };
          }
        } catch (IOException ex) {
          return new String[] {"Unable to read from " + url + ": " + ex};
        }
      }
      return new String[0];
    }

    private boolean isApplicableManifest(Manifest manifest) {
      Attributes attributes = manifest.getMainAttributes();
      return "raes2zip".equals(get(attributes, "Implementation-Title"));
    }

    private static Object get(Attributes attributes, String key) {
      return attributes.get(new Attributes.Name(key));
    }
  }
@remkop
Copy link
Owner

remkop commented May 5, 2023

Did you ensure that the META-INF/MANIFEST.MF resource is included in the executable?

The annotation processor includes the resource bundle specified in the @Command annotation; See https://github.com/remkop/picocli/tree/main/picocli-codegen#generate-graalvm-configurations-with-the-annotation-processor,

But the manifest file is not included by default.

See https://github.com/remkop/picocli/tree/main/picocli-codegen#resourceconfiggenerator
for manually generating the necessary GraalVM configuration file.

The --pattern option can be used to specify Java regular expressions that match additional resource(s) to be included in the image.

@remkop
Copy link
Owner

remkop commented May 5, 2023

Also, I would recommend adding some debug tracing to your version provider implementation (using picocli's Tracing API), to help you zoom in to the cause of the issue.

@remkop
Copy link
Owner

remkop commented Aug 27, 2023

@delanym Did you have a chance to investigate my suggestions?

@delanym
Copy link
Author

delanym commented Aug 27, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants