1

在 picocli 中,他们给出了如何在构建 Maven 和 Gradle 期间生成 GraalVM 反射配置的示例。

摇篮示例

configurations {
    generateConfig
}
dependencies {
    compile 'info.picocli:picocli:3.9.3'
    generateConfig 'info.picocli:picocli-codegen:3.9.3'
}

任务

task(generateGraalReflectionConfig, dependsOn: 'classes', type: JavaExec) {
    main = 'picocli.codegen.aot.graalvm.ReflectionConfigGenerator'
    classpath = configurations.generateConfig + sourceSets.main.runtimeClasspath
    def outputFile = new File(project.buildDir, 'cli-reflect.json')
    args = ["--output=$outputFile", 'com.your.package.YourCommand1', 'com.your.package.YourCommand2']
}
assemble.dependsOn generateGraalReflectionConfig

我怎么能为 sbt 写一些等效的东西?

这是我到目前为止所得到的,但我不知道我是否朝着正确的方向前进。

import Dependencies._

ThisBuild / scalaVersion := "2.12.8"
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / organization := "com.example"
ThisBuild / organizationName := "example"

lazy val root = (project in file("."))
  .settings(
    name := "poc-cli",
    libraryDependencies += scalaTest % Test,
    libraryDependencies += "info.picocli" % "picocli" % "3.9.3",
    libraryDependencies += "info.picocli" % "picocli-codegen" % "3.9.3",
  )

lazy val graalConfig = inputKey[Unit]("gen-graal-config") 

graalConfig:= {
    val conf = (runMain in Compile).fullInput(" picocli.codegen.aot.graalvm.ReflectionConfigGenerator --output=cli-reflect.json example.Hello").evaluated
}

enablePlugins(GraalVMNativeImagePlugin)

编辑

在我最初的问题中,我有下面的代码,用我当前的代码问题解决了

lazy val graalConfig = taskKey[Unit]("graal-config") := {
    Process("java" :: "-jar" :: "picocli.codegen.aot.graalvm.ReflectionConfigGenerator" :: "--output" :: "cli-reflect.json" :: "???" :: Nil,
        baseDirectory.value / "lib").!
}
4

2 回答 2

1

解决方案是

import Dependencies._


ThisBuild / scalaVersion := "2.12.8"
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / organization := "com.example"
ThisBuild / organizationName := "example"

lazy val root = (project in file("."))
  .settings(
    name := "poc-cli",
    libraryDependencies += scalaTest % Test,
    libraryDependencies += "info.picocli" % "picocli" % "3.9.3",
    libraryDependencies += "info.picocli" % "picocli-codegen" % "3.9.3",
  )

lazy val graalConfig = inputKey[Unit]("gen-graal-config")

graalVMNativeImageOptions += "-H:ReflectionConfigurationFiles=../cli-reflect.json"

graalConfig:= {
    (runMain in Compile).fullInput(" picocli.codegen.aot.graalvm.ReflectionConfigGenerator -o=target/cli-reflect.json example.Hello").evaluated
}

enablePlugins(GraalVMNativeImagePlugin)

然后我可以运行:

$ sbt graalConfig
$ sbt graalvm-native-image:packageBin 
于 2019-02-18T11:54:43.463 回答
1

picocli-codegen 修复 picocli 项目反射。

Graal 反射配置生成器,它会reflection.json自动为我生成并将其附加到生成的jar中,只需将其作为AnnotationProcessor添加到我的依赖项中并注释类,还支持包扫描。

这样,您可以在需要时轻松生成反射配置。如何配置和使用它的片段示例。

构建.gradle

dependencies {
  compileOnly("com.mageddo.nativeimage:reflection-config-generator:2.1.1")
  annotationProcessor("com.mageddo.nativeimage:reflection-config-generator:2.1.1")
}

配置文件

@Reflection(declaredConstructors = true, scanPackage = "com.github.vo")
public class Config {}

还支持 Maven 和 vanilla java,有关更多详细信息,请查看GRCG 文档,也可以查看有关此工具的这篇文章

于 2019-12-01T15:43:07.500 回答