Uso de Oracle GraalVM en DevOps Pipelines de compilación
Los pipelines de compilación DevOps permiten utilizar Oracle GraalVM para crear aplicaciones Java de alto rendimiento.
En la etapa Creación gestionada, puede instalar y utilizar componentes de Oracle GraalVM como Native Image y Java Development Kit (JDK) mediante la adición de un comando de gestor de paquetes YUM simple en el archivo de especificación de creación.
Actualización del archivo de especificación de compilación
Para instalar y utilizar Oracle GraalVM en el pipeline de creación DevOps, actualice el archivo de especificación de creación de la siguiente manera:
- Agregue el siguiente comando para instalar uno o más componentes necesarios de Oracle GraalVM. Por ejemplo, este comando instala Native Image junto con JDK y otras dependencias necesarias.
steps: - type: Command name: "Install the latest Oracle GraalVM for JDK 20 - JDK and Native Image" command: | yum -y install graalvm-20-native-image
- Defina la variable de entorno
JAVA_HOME
.env: variables: "JAVA_HOME" : "/usr/lib64/graalvm/graalvm-java20"
- Defina la variable de entorno
PATH
.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
Ejemplo1:
Archivo de especificación de creación de ejemplo para crear un ejecutable nativo a partir de una aplicación Java mediante Maven y Oracle GraalVM para 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
Ejemplo2:
Archivo de especificación de creación de ejemplo para crear un ejecutable nativo a partir de una aplicación Micronaut mediante Maven y Oracle GraalVM para 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}
Ejemplo3:
Archivo de especificación de compilación de ejemplo para crear un ejecutable nativo a partir de una aplicación Java mediante Maven y GraalVM Enterprise 22.x para 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
Ejemplo4:
Archivo de especificación de compilación de ejemplo para crear un ejecutable nativo a partir de una aplicación Micronaut mediante Maven y GraalVM Enterprise 22.x para 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}