我正在尝试构建一个Java应用程序,该应用程序将使用GraalVM提供的本地映像构建到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包构建JAR,这个jar有所有需要的依赖项。然后,我使用以下方法构建本机应用程序:
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中的非匹配参数”或规范中的NullPointer (用于输出输出)。
在引入picocli之前,我能够成功地运行本机应用程序并使用ObjectMapper操作文件,因此我相信打包和图像构建过程是正确的。
试着跟着这里的提示:
https://github.com/remkop/picocli/issues/631
但我不知道该怎么做,试着添加了这样的东西
@Reflections({
@Reflection(scanClass = CommandLine.Mixin.class),
@Reflection(scanClass = CommandLine.HelpCommand.class)
})但什么都没变。
如能提供任何帮助,将不胜感激:)
发布于 2022-08-17 12:01:42
我刚刚发现我失去了一个依赖关系:
<!-- 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/
https://stackoverflow.com/questions/73379986
复制相似问题