简体   繁体   English

在 Travis CI 中进行原生镜像编译的 GraalVM

[英]GraalVM with native-image compilation in Travis CI

I have Java project which I compile with GraalVM native-image to executable binary.我有 Java 项目,我使用 GraalVM 本机映像编译为可执行二进制文件。 I'd like to configure continuous integration process for the project with Travis CI and it's ineresting to me - does Travis CI allow that?我想用 Travis CI 为项目配置持续集成过程,这对我来说很有趣 - Travis CI 允许这样做吗? How can I configure.travis.yml file for building with GraalVM native-image?如何配置 .travis.yml 文件以使用 GraalVM 原生映像进行构建?

I was able to configure native-image GraalVM compilation in Travis CI builds using install-jdk.sh from Bach.java - Java Shell Builder.我能够使用来自Bach.java - Java ZEA89B68C34CE4A63C0F77E17413C6E 的install-jdk.sh在 Travis CI 构建中配置本机映像 GraalVM 编译。 Here is .travis-ci.yml :这是.travis-ci.yml

sudo: false
language: java

cache:
  directories:
    - $HOME/.m2

before_install:
- wget https://github.com/sormuras/bach/raw/master/install-jdk.sh

matrix:
  include:
  # GraalVM
    - env: JDK='GraalVM 19'
      install: . ./install-jdk.sh --url "https://github.com/oracle/graal/releases/download/vm-19.2.0/graalvm-ce-linux-amd64-19.2.0.tar.gz"

script:
  - mvn package -Pnative -Dnative-image.docker-build=true

Option 1: GraalVM with native-image compilation directly on Travis CI host选项 1:直接在 Travis CI 主机上进行原生镜像编译的 GraalVM

The first option to install GraalVM (incl. Native Image) on TravisCI: Simply use SDKMAN .在 TravisCI 上安装 GraalVM(包括 Native Image)的第一个选项:只需使用SDKMAN The .travis.yml looks like this: .travis.yml看起来像这样:

language: minimal

install:
  # Install GraalVM with SDKMAN
  - curl -s "https://get.sdkman.io" | bash
  - source "$HOME/.sdkman/bin/sdkman-init.sh"
  - sdk install java 20.0.0.r11-grl

  # Check if GraalVM was installed successfully
  - java -version

  # Install Maven, that uses GraalVM for later builds
  - sdk install maven

  # Show Maven using GraalVM JDK
  - mvn --version

  # Install GraalVM Native Image
  - gu install native-image

  # Check if Native Image was installed properly
  - native-image --version

script:
  # Run GraalVM Native Image compilation
  - native-image \
    --no-server \
    --no-fallback \
    -H:+TraceClassInitialization \
    -H:Name=yourArtifactNameHere \
    -H:+ReportExceptionStackTraces \
    -DremoveUnusedAutoconfig=true \
    -DremoveYamlSupport=true \
    -cp yourClassPathHere yourMainClassHere;

There's one crucial point to notice here: Don't use a language: java or the default linux distros like dist: bionic alone!这里有一个关键点需要注意:不要使用一种language: java或默认的 linux 发行版,例如dist: bionic , because they ship with pre-installed Maven versions - which are configured to use the pre-installed OpenJDK. ,因为它们附带预安装的 Maven 版本 - 配置为使用预安装的 OpenJDK。 But most people will need Maven to use our SDKMAN installed GraalVM to properly compile our Java projects later.但是大多数人以后需要 Maven 来使用我们 SDKMAN 安装的 GraalVM 来正确编译我们的 Java 项目。 Therefore we simply use the language: minimal , which is a simple way of getting our Travis builds based on a basic Travis build environment without pre-installed JDKs or Maven .因此我们简单地使用language: minimal ,这是一种基于基本 Travis 构建环境而不预装 JDK 或 Maven 的 Travis 构建的简单方法 To verify this, we run a mvn --version , which should show something like this inside our Travis build:为了验证这一点,我们运行一个mvn --version ,它应该在我们的 Travis 构建中显示如下内容:

$ mvn --version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /home/travis/.sdkman/candidates/maven/current
Java version: 11.0.6, vendor: Oracle Corporation, runtime: /home/travis/.sdkman/candidates/java/20.0.0.r11-grl
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-1028-gcp", arch: "amd64", family: "unix"

The native-image command inside the script section is just meant as a placeholder here , since you may have quite different parameters depending on your use case. script部分中的native-image命令在此处仅用作占位符,因为根据您的用例,您可能有完全不同的参数。

In my case, I wanted to compile a Spring Boot App, so I also created a 100% comprehensible example project spring-boot-graalvm , where you could also have a look at the TravisCI builds, which install GraalVM and do the native image compilation: https://travis-ci.org/jonashackt/spring-boot-graalvm就我而言,我想编译一个 Spring 引导应用程序,所以我还创建了一个 100% 可理解的示例项目spring-boot-graalvm ,您还可以在其中查看 TravisCI 构建,它安装 GraalVM 并进行本机映像编译: https://travis-ci.org/jonashackt/spring-boot-graalvm

=========================================== ============================================

Option 2: GraalVM with native-image compilation in Docker using TravisCI docker service选项 2:使用 TravisCI docker 服务在 Docker 中进行原生映像编译的 GraalVM

Maybe you're already used to build your Java apps inside Docker containers - then GraalVM native image compilation is no exception.也许您已经习惯在 Docker 容器中构建您的 Java 应用程序 - 那么 GraalVM 原生映像编译也不例外。 Using the TravisCI docker service , the .travis.yml becomes fairly simple:使用TravisCI docker 服务.travis.yml变得相当简单:

dist: bionic
language: minimal

services:
  - docker

script:
  - docker build . --tag=spring-boot-graal

The crucial part is your Dockerfile now ( see this full example, leveraging Docker multi-stage builds ) - and the advantage over Option 1: you can test it locally on your machine.关键部分是您的Dockerfile现在( 请参阅此完整示例,利用 Docker 多阶段构建) - 与选项 1 相比的优势:您可以在机器上本地测试它。 Here's an example Dockerfile :这是一个示例Dockerfile

FROM oracle/graalvm-ce:20.1.0-java11

MAINTAINER Jonas Hecht

ADD . /build
WORKDIR /build

# For SDKMAN to work we need unzip & zip
RUN yum install -y unzip zip

RUN \
    # Install SDKMAN
    curl -s "https://get.sdkman.io" | bash; \
    source "$HOME/.sdkman/bin/sdkman-init.sh"; \
    sdk install maven; \
    # Install GraalVM Native Image
    gu install native-image;

RUN source "$HOME/.sdkman/bin/sdkman-init.sh" && mvn --version

RUN native-image --version

RUN source "$HOME/.sdkman/bin/sdkman-init.sh" && native-image \
    --no-server -J-Xmx4G \
    --no-fallback \
    -H:+TraceClassInitialization \
    -H:Name=yourArtifactNameHere \
    -H:+ReportExceptionStackTraces \
    -DremoveUnusedAutoconfig=true \
    -DremoveYamlSupport=true \
    -cp yourClassPathHere yourMainClassHere;

We're using the official Oracle GraalVM image oracle/graalvm-ce:20.1.0-java11 from DockerHub at https://hub.docker.com/r/oracle/graalvm-ce/ here. We're using the official Oracle GraalVM image oracle/graalvm-ce:20.1.0-java11 from DockerHub at https://hub.docker.com/r/oracle/graalvm-ce/ here. As this lacks native-image command and Maven, we use gu util to install the command and SDKMAN again to install Maven.由于缺少native-image命令和 Maven,我们使用gu util 安装命令并再次使用 SDKMAN 安装 Maven。 Now you can compile GraalVM Native Images both locally and on CloudCI systems like TravisCI.现在,您可以在本地和 CloudCI 系统(如 TravisCI)上编译 GraalVM 原生映像。

Be reminded about one hint: the native image compilation is quite memory hungry.提醒一个提示:native 镜像编译是相当饿的 memory If you need to use the --no-server option, you should limit memory usage with the -J-Xmx4G parameter to 4GB of RAM for Travis builds, since otherwise your builds might fail with Error: Image build request failed with exit status 137 or similar errors.如果您需要使用--no-server选项,您应该使用-J-Xmx4G参数将 memory 的使用限制为 Travis 构建的4GB RAM,否则您的构建可能会失败并出现Error: Image build request failed with exit status 137或类似的错误。

The next logical step would be to push the resulting Docker image to some sort of Docker registry and maybe run the container in some Cloud PaaS.下一个合乎逻辑的步骤是将生成的 Docker 映像推送到某种 Docker 注册表,并可能在某些 Cloud PaaS 中运行容器。 If you need more info, have a look at this fully comprehesible guide here .如果您需要更多信息, 请在此处查看此完全易懂的指南 There's also an example of a fully working Docker multi-stage build enabled Dockerfile in this example project: https://github.com/jonashackt/spring-boot-graalvm/blob/master/Dockerfile在这个示例项目中还有一个完全工作的 Docker 多阶段构建启用Dockerfile的示例: https://github.com/bootbhackt/Dockerfile-vm/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM