Oracle GraalVM in DevOps Build-Pipelines verwenden

Mit DevOps-Build-Pipelines können Sie Oracle GraalVM zum Erstellen leistungsstarker Java-Anwendungen verwenden.

In der Phase Verwalteter Build können Sie Oracle GraalVM-Komponenten wie Native Image und Java Development Kit (JDK) installieren und verwenden, indem Sie einen einfachen YUM-Packagemanagerbefehl in der Build-Spezifikationsdatei hinzufügen.

Hinweis

Oracle GraalVM ist ohne zusätzliche Kosten in Oracle Cloud Infrastructure (OCI) verfügbar.

Build-Spezifikationsdatei aktualisieren

Um Oracle GraalVM in der DevOps-Build-Pipeline zu installieren und zu verwenden, aktualisieren Sie die Build-Spezifikationsdatei wie folgt:

  1. Fügen Sie den folgenden Befehl hinzu, um eine oder mehrere erforderliche Oracle GraalVM-Komponenten zu installieren. Beispiel: Mit diesem Befehl wird Native Image zusammen mit JDK und anderen erforderlichen Abhängigkeiten installiert.
    steps:
      - type: Command
        name: "Install the latest Oracle GraalVM for JDK 20 - JDK and Native Image"
        command: |
          yum -y install graalvm-20-native-image
  2. Legen Sie die JAVA_HOME-Umgebungsvariable fest.
    env:
      variables:
        "JAVA_HOME" : "/usr/lib64/graalvm/graalvm-java20"
  3. Legen Sie die Umgebungsvariable PATH fest.
    env:
      variables:
        # PATH is a reserved variable and cannot be defined as a variable.
        # PATH can be changed in a build step and the change is visible in subsequent steps.
     
    steps:
      - type: Command
        name: "Set the PATH here"
        command: |
          export PATH=$JAVA_HOME/bin:$PATH

Beispiel 1:

Beispiel für eine Build-Spezifikationsdatei zum Erstellen einer nativen ausführbaren Datei aus einer Java-Anwendung mit Maven und Oracle GraalVM for JDK 17 - Native Image.

version: 0.1
component: build
timeoutInSeconds: 600
runAs: root
shell: bash
env:
  variables:
    "JAVA_HOME" : "/usr/lib64/graalvm/graalvm-java17"
    # PATH is a reserved variable and cannot be defined as a variable.
    # However, PATH can be changed in a build step and the change is visible in subsequent steps.
steps:
  - type: Command
    name: "Install the latest Oracle GraalVM for JDK 17 - JDK and Native Image"
    command: |
      yum -y install graalvm-17-native-image
  - type: Command
    name: "Set the PATH here. JAVA_HOME already set in env > variables above."
    command: |
      export PATH=$JAVA_HOME/bin:$PATH
  - type: Command
    name: "Build a native executable with Oracle GraalVM for JDK 17 - Native Image"
    command: |
      mvn --no-transfer-progress -Pnative -DskipTests package
outputArtifacts:
  - name: app_native_executable
    type: BINARY
    location: target/my-app

Beispiel 2:

Beispiel für eine Build-Spezifikationsdatei zum Erstellen einer nativen ausführbaren Datei aus einer Micronaut-Anwendung mit Maven und Oracle GraalVM for JDK 17 - Native Image.

version: 0.1
component: build
timeoutInSeconds: 900
runAs: root
shell: bash
env:
  variables:
    "JAVA_HOME" : "/usr/lib64/graalvm/graalvm-java17"
    TAG: "mn-hello-ni:0.0.1"
    APP_FILE: "MnHelloRest"
  exportedVariables:
    - BUILDRUN_HASH
steps:
  - type: Command
    name: "Define unique image tag"
    timeoutInSeconds: 140
    command: |
      echo "OCI_BUILD_RUN_ID: ${OCI_BUILD_RUN_ID}"
      export BUILDRUN_HASH=`echo ${OCI_BUILD_RUN_ID} | rev | cut -c 1-7`
      echo "BUILDRUN_HASH: " $BUILDRUN_HASH
  - type: Command
    name: "Install the latest Oracle GraalVM for JDK 17 - JDK and Native Image"
    command: |
      yum -y install graalvm-17-native-image
  - type: Command
    name: "Set the PATH here. JAVA_HOME already set in env > variables above."
    command: |
      export PATH=$JAVA_HOME/bin:$PATH
  - type: Command
    name: "Build a native executable with Oracle GraalVM for JDK 17 - Native Image"
    command: |
      ./mvnw --no-transfer-progress package -Dpackaging=native-image
  - type: Command
    name: "Package the native executable in a runtime container image"
    command: |
      docker build -f ./Dockerfile \
                  --build-arg APP_FILE=${APP_FILE} \
                  -t ${TAG} .
outputArtifacts:
  - name: app_native_executable
    type: BINARY
    location: target/MnHelloRest
  - name: runtime_image
    type: DOCKER_IMAGE
    location: ${TAG}

Beispiel3:

Beispiel für eine Build-Spezifikationsdatei zum Erstellen einer nativen ausführbaren Datei aus einer Java-Anwendung mit Maven und GraalVM Enterprise 22.x for Java 17 - Native Image.

version: 0.1
component: build
timeoutInSeconds: 600
runAs: root
shell: bash
env:
  variables:
    "JAVA_HOME" : "/usr/lib64/graalvm/graalvm22-ee-java17"
    # PATH is a reserved variable and cannot be defined as a variable.
    # However, PATH can be changed in a build step and the change is visible in subsequent steps.
steps:
  - type: Command
    name: "Install the latest GraalVM Enterprise 22.x for Java 17 - JDK and Native Image"
    command: |
      yum -y install graalvm22-ee-17-native-image
  - type: Command
    name: "Set the PATH here. JAVA_HOME already set in env > variables above."
    command: |
      export PATH=$JAVA_HOME/bin:$PATH
  - type: Command
    name: "Build a native executable with the installed GraalVM Enterprise 22.x for Java 17 - Native Image"
    command: |
      mvn --no-transfer-progress -Pnative -DskipTests package
outputArtifacts:
  - name: app_native_executable
    type: BINARY
    location: target/my-app

Beispiel4:

Beispiel für eine Build-Spezifikationsdatei zur Erstellung einer nativen ausführbaren Datei aus einer Micronaut-Anwendung mit Maven und GraalVM Enterprise 22.x for Java 17 - Native Image.

version: 0.1
component: build
timeoutInSeconds: 900
runAs: root
shell: bash
env:
  variables:
    "JAVA_HOME" : "/usr/lib64/graalvm/graalvm22-ee-java17"
    TAG: "mn-hello-ni:0.0.1"
    APP_FILE: "MnHelloRest"
  exportedVariables:
    - BUILDRUN_HASH
steps:
  - type: Command
    name: "Define unique image tag"
    timeoutInSeconds: 140
    command: |
      echo "OCI_BUILD_RUN_ID: ${OCI_BUILD_RUN_ID}"
      export BUILDRUN_HASH=`echo ${OCI_BUILD_RUN_ID} | rev | cut -c 1-7`
      echo "BUILDRUN_HASH: " $BUILDRUN_HASH
  - type: Command
    name: "Install the latest GraalVM Enterprise 22.x for Java 17 - JDK and Native Image"
    command: |
      yum -y install graalvm22-ee-17-native-image
  - type: Command
    name: "Set the PATH here. JAVA_HOME already set in env > variables above"
    command: |
      export PATH=$JAVA_HOME/bin:$PATH
  - type: Command
    name: "Build a native executable with the installed GraalVM Enterprise 22.x for Java 17 - Native Image"
    command: |
      ./mvnw --no-transfer-progress package -Dpackaging=native-image
  - type: Command
    name: "Package the native executable in a runtime container image"
    command: |
      docker build -f ./Dockerfile \
                  --build-arg APP_FILE=${APP_FILE} \
                  -t ${TAG}
outputArtifacts:
  - name: app_native_executable
    type: BINARY
    location: target/MnHelloRest
  - name: runtime_image
    type: DOCKER_IMAGE
    location: ${TAG}