首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于picocli和GraalVM本机图像的问题

关于picocli和GraalVM本机图像的问题
EN

Stack Overflow用户
提问于 2022-08-16 20:30:11
回答 1查看 109关注 0票数 0

我正在尝试构建一个Java应用程序,该应用程序将使用GraalVM提供的本地映像构建到Linux命令行应用程序中。我有以下代码:

代码语言:javascript
复制
@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包构建JAR,这个jar有所有需要的依赖项。然后,我使用以下方法构建本机应用程序:

代码语言:javascript
复制
native-image -jar my-application-1.0-SNAPSHOT-jar-with-dependencies.jar -H:+ReportExceptionStackTraces --no-fallback

在运行应用程序时:

代码语言:javascript
复制
./my-application-1.0-SNAPSHOT-jar-with-dependencies inputfile.txt outputfile1.txt outputfile2.txt

我得到以下错误:

代码语言:javascript
复制
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中的非匹配参数”或规范中的NullPointer (用于输出输出)。

在引入picocli之前,我能够成功地运行本机应用程序并使用ObjectMapper操作文件,因此我相信打包和图像构建过程是正确的。

试着跟着这里的提示:

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

但我不知道该怎么做,试着添加了这样的东西

代码语言:javascript
复制
@Reflections({
        @Reflection(scanClass = CommandLine.Mixin.class),
        @Reflection(scanClass = CommandLine.HelpCommand.class)
})

但什么都没变。

如能提供任何帮助,将不胜感激:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-17 12:01:42

我刚刚发现我失去了一个依赖关系:

代码语言:javascript
复制
<!-- 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/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73379986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档