簡體   English   中英

picocli 和 GraalVM native-image 的問題

[英]Issue with picocli and GraalVM native-image

我正在嘗試使用 GraalVM 提供的本機映像構建一個 Java 應用程序,該應用程序將內置到 Linux 命令行應用程序中。 我有以下代碼:

@CommandLine.Command(
        name = "my-application",
        mixinStandardHelpOptions = true,
        helpCommand = true,
        version = "my-application 1.0",
        description = "My file manipulation application."
)
public class MyApplication implements Callable<Integer> {

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(new YAMLFactory()
            .enable(YAMLGenerator.Feature.LITERAL_BLOCK_STYLE)
            .disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)
    );

    @CommandLine.Parameters(index = "0", description = "The source file.")
    private File sourceFile;

    @CommandLine.Parameters(index = "1", description = "The target files.")
    private List<File> targetFiles;

    @CommandLine.Option(names = {"-env", "--environment"}, defaultValue = "TEST")
    private Environment environment; // TEST or PROD

    @CommandLine.Spec CommandLine.Model.CommandSpec spec;

    public static void main(String[] args) {
        int exitCode = new CommandLine(new MyApplication()).execute(args);
        System.exit(exitCode);
    }

    @Override
    public Integer call() throws Exception {
        spec.commandLine()
                .getOut()
                .println("Starting my-application tool execution...");

        // Some file manipulation using Jackson.

        spec.commandLine().getOut().println("Execution finished successfully.");

        return 0;
    }

我用 maven package 構建了 JAR,而這個 Z68995FCBF432492D15484DAC0Z 具有所有需要的依賴項。 然后我構建本機應用程序:

native-image -jar my-application-1.0-SNAPSHOT-jar-with-dependencies.jar -H:+ReportExceptionStackTraces --no-fallback

當運行應用程序時:

./my-application-1.0-SNAPSHOT-jar-with-dependencies inputfile.txt outputfile1.txt outputfile2.txt

我收到以下錯誤:

Exception in thread "main" picocli.CommandLine$InitializationException: picocli.CommandLine$AutoHelpMixin@62037019 is not a command: it has no @Command, @Option, @Parameters or @Unmatched annotations
    at picocli.CommandLine$Model$CommandReflection.validateCommandSpec(CommandLine.java:11680)
    at picocli.CommandLine$Model$CommandReflection.extractCommandSpec(CommandLine.java:11510)
    at picocli.CommandLine$Model$CommandSpec.forAnnotatedObject(CommandLine.java:6236)
    at picocli.CommandLine$Model$CommandSpec.mixinStandardHelpOptions(CommandLine.java:7220)
    at picocli.CommandLine$Model$CommandReflection.extractCommandSpec(CommandLine.java:11505)
    at picocli.CommandLine$Model$CommandSpec.forAnnotatedObject(CommandLine.java:6236)
    at picocli.CommandLine.<init>(CommandLine.java:227)
    at picocli.CommandLine.<init>(CommandLine.java:221)
    at picocli.CommandLine.<init>(CommandLine.java:196)
    at com.test.MyApplication.main(MyApplication.java:42)

當我通過 IntelliJ 或簡單的 java -jar 命令運行該應用程序時,它運行良好。

我嘗試了一些方法,例如刪除 mixinStandardHelpOptions 和 helpCommand,但我開始收到錯誤,例如“索引 0 中的 arguments 不匹配”或帶有規范的 NullPointer(用於打印輸出)。

在介紹 picocli 之前,我已經能夠成功運行原生應用程序並使用 ObjectMapper 操作文件,因此我相信打包和映像構建過程是正確的。

嘗試按照此處的提示進行操作:

https://github.com/remkop/picocli/issues/631

但我不確定如何繼續,嘗試添加類似

@Reflections({
        @Reflection(scanClass = CommandLine.Mixin.class),
        @Reflection(scanClass = CommandLine.HelpCommand.class)
})

但什么都沒有改變。

任何幫助,將不勝感激:)

剛剛發現我缺少一個依賴項:

<!-- https://mvnrepository.com/artifact/info.picocli/picocli-codegen -->
<dependency>
    <groupId>info.picocli</groupId>
    <artifactId>picocli-codegen</artifactId>
    <version>4.6.3</version>
</dependency>

了解更多信息:

https://www.infoq.com/articles/java-native-cli-graalvm-picocli/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM