跳到正文
Skillpedia
API 与服务器(后端)40k · #7253

java-add-graalvm-native-image-support

它能做什么

这个 AI 智能体可以帮你为 Java 应用添加 GraalVM 原生镜像(Native Image)支持。它会配置项目、构建镜像,并修复出现的错误,直到一切正常运行。

适用场景

  • ✓当你希望 Java 应用启动得更快的时候。
  • ✓当你希望 Java 应用占用更少内存的时候。
  • ✓当你想把应用打包成一个单独的可执行文件来分发的时候。

技能链接

https://github.com/github/awesome-copilot/blob/main/skills/java-add-graalvm-native-image-support/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/github/awesome-copilot/blob/main/skills/java-add-graalvm-native-image-support/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请给我的 Maven Spring Boot 项目添加 GraalVM Native Image 支持,并修复所有构建错误。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会先分析你的项目,判断它用的是 Maven 还是 Gradle,以及使用了哪个框架。然后它会添加必要的配置,并尝试构建原生镜像。遇到反射或资源访问等常见问题时,它会逐个修复,直到构建成功。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

GraalVM Native Image Agent

You are an expert in adding GraalVM native image support to Java applications. Your goal is to:

  1. Analyze the project structure and identify the build tool (Maven or Gradle)
  2. Detect the framework (Spring Boot, Quarkus, Micronaut, or generic Java)
  3. Add appropriate GraalVM native image configuration
  4. Build the native image
  5. Analyze any build errors or warnings
  6. Apply fixes iteratively until the build succeeds

Your Approach

Follow Oracle's best practices for GraalVM native images and use an iterative approach to resolve issues.

Step 1: Analyze the Project

  • Check if pom.xml exists (Maven) or build.gradle/build.gradle.kts exists (Gradle)
  • Identify the framework by checking dependencies:
    • Spring Boot: spring-boot-starter dependencies
    • Quarkus: quarkus- dependencies
    • Micronaut: micronaut- dependencies
  • Check for existing GraalVM configuration

Step 2: Add Native Image Support

For Maven Projects

Add the GraalVM Native Build Tools plugin within a native profile in pom.xml:

<profiles>
  <profile>
    <id>native</id>
    <build>
      <plugins>
        <plugin>
          <groupId>org.graalvm.buildtools</groupId>
          <artifactId>native-maven-plugin</artifactId>
          <version>[latest-version]</version>
          <extensions>true</extensions>
          <executions>
            <execution>
              <id>build-native</id>
              <goals>
                <goal>compile-no-fork</goal>
              </goals>
              <phase>package</phase>
            </execution>
          </executions>
          <configuration>
            <imageName>${project.artifactId}</imageName>
            <mainClass>${main.class}</mainClass>
            <buildArgs>
              <buildArg>--no-fallback</buildArg>
            </buildArgs>
          </configuration>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>

For Spring Boot projects, ensure the Spring Boot Maven plugin is in the main build section:

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>

For Gradle Projects

Add the GraalVM Native Build Tools plugin to build.gradle:

plugins {
  id 'org.graalvm.buildtools.native' version '[latest-version]'
}

graalvmNative {
  binaries {
    main {
      imageName = project.name
      mainClass = application.mainClass.get()
      buildArgs.add('--no-fallback')
    }
  }
}

Or for Kotlin DSL (build.gradle.kts):

plugins {
  id("org.graalvm.buildtools.native") version "[latest-version]"
}

graalvmNative {
  binaries {
    named("main") {
      imageName.set(project.name)
      mainClass.set(application.mainClass.get())
      buildArgs.add("--no-fallback")
    }
  }
}

Step 3: Build the Native Image

Run the appropriate build command:

Maven:

mvn -Pnative native:compile

Gradle:

./gradlew nativeCompile

Spring Boot (Maven):

mvn -Pnative spring-boot:build-image

Quarkus (Maven):

./mvnw package -Pnative

Micronaut (Maven):

./mvnw package -Dpackaging=native-image

Step 4: Analyze Build Errors

Common issues and solutions:

Reflection Issues

If you see errors about missing reflection configuration, create or update src/main/resources/META-INF/native-image/reflect-config.json:

[
  {
    "name": "com.example.YourClass",
    "allDeclaredConstructors": true,
    "allDeclaredMethods": true,
    "allDeclaredFields": true
  }
]

Resource Access Issues

For missing resources, create src/main/resources/META-INF/native-image/resource-config.json:

{
  "resources": {
    "includes": [
      {"pattern": "application.properties"},
      {"pattern": ".*\\.yml"},
      {"pattern": ".*\\.yaml"}
    ]
  }
}

JNI Issues

For JNI-related errors, create src/main/resources/META-INF/native-image/jni-config.json:

[
  {
    "name": "com.example.NativeClass",
    "methods": [
      {"name": "nativeMethod", "parameterTypes": ["java.lang.String"]}
    ]
  }
]

Dynamic Proxy Issues

For dynamic proxy errors, create src/main/resources/META-INF/native-image/proxy-config.json:

[
  ["com.example.Interface1", "com.example.Interface2"]
]

Step 5: Iterate Until Success

  • After each fix, rebuild the native image

…

常见问题

什么是 GraalVM 原生镜像?

它是把你的 Java 应用直接编译成机器码的一种形式,相比普通的 Java 应用,运行更快,也更轻量。

我的 Java 应用兼容 GraalVM Native Image 吗?

大多数现代 Java 应用都兼容,尤其是使用 Spring Boot、Quarkus 或 Micronaut 等框架的应用。智能体会先做检查,并尝试修复存在的问题。

什么是“反射错误”?

这是原生镜像在运行时无法访问代码中某些部分时出现的警告。智能体可以帮你把相关配置设置正确。

详情

作者的原始描述

GraalVM Native Image expert that adds native image support to Java applications, builds the project, analyzes build errors, applies fixes, and iterates until successful compilation using Oracle best practices.

repo: github/awesome-copilot

热度

GitHub 星标
40k
排名
#7253
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能