canonical / craft-parts

https://canonical-craft-parts.readthedocs-hosted.com
GNU Lesser General Public License v3.0
11 stars 36 forks source link

maven plugin should not use staged runtime #814

Open vpa1977 opened 1 month ago

vpa1977 commented 1 month ago

Bug Description

Maven plugin tries to use staged Java runtime instead of the one installed as the build package. This causes a subsequent build failure due to runtime missing necessary modules to compile Java files.

To Reproduce

  1. rockcraft pack Build succeeds
  2. rockcraft pack Build fails

part yaml

name: maven-sample-chisel
version: 0.0.1
summary: Sample Maven-based chiselled rock
description: |
  This image is a sample maven application
  using openjdk-21-jre-headless slices

base: bare
build-base: ubuntu@24.04

run-user: _daemon_

platforms:
  amd64:

services:
  test:
    override: replace
    summary: Service summary
    command: /usr/bin/java -jar /jars/sample-0.0.1-SNAPSHOT.jar
    startup: enabled

parts:
  application:
    plugin: maven
    source: ./sample
    source-type: local
    build-packages:
      - maven
      - openjdk-21-jdk-headless

  runtime:
    plugin: nil
    source: https://github.com/vpa1977/chisel-releases
    source-type: git
    source-branch: 24.04-openjdk-21-jre-headless
    build-packages:
      - ca-certificates-java
    override-build: |
      chisel cut --release ./ --root ${CRAFT_PART_INSTALL} \
        base-files_base openjdk-21-jre-headless_security \
        openjdk-21-jre-headless_management \
        openjdk-21-jre-headless_jfr
      mkdir -p ${CRAFT_PART_INSTALL}/etc/ssl/certs/java/
      cp /etc/ssl/certs/java/cacerts ${CRAFT_PART_INSTALL}/etc/ssl/certs/java/cacerts
      java_bin=$(find ${CRAFT_PART_INSTALL} -name java -type f -executable)
      ln -s --relative $java_bin ${CRAFT_PART_INSTALL}/usr/bin/java

Relevant log output

2024-08-16 15:15:30.892 :: ## Plugin environment
2024-08-16 15:15:30.892 :: ## User environment
2024-08-16 15:15:30.892 :: 2024-08-16 15:15:29.753 plugin validation command: 'mvn --version'
2024-08-16 15:15:30.892 :: 2024-08-16 15:15:29.861 executed mvn --version with output Apache Maven 3.8.7
2024-08-16 15:15:30.892 :: Maven home: /usr/share/maven
2024-08-16 15:15:30.892 :: Java version: 21.0.4, vendor: Ubuntu, runtime: /root/stage/usr/lib/jvm/java-21-openjdk-amd64
2024-08-16 15:15:30.892 :: Default locale: en, platform encoding: UTF-8
2024-08-16 15:15:30.892 :: OS name: "linux", version: "6.8.0-40-generic", arch: "amd64", family: "unix"
2024-08-16 15:15:30.892 :: 2024-08-16 15:15:29.861 Executing PosixPath('/root/parts/application/run/build.sh')
vpa1977 commented 1 month ago

See https://github.com/canonical/craft-parts/issues/813 for the sample project.

lengau commented 1 month ago

I'm not sure we can do this without breaking backwards compatibility, but maybe we can provide an option about staging a JRE (defaulting to True)?

@cmatsuoka what do you think?

vpa1977 commented 1 month ago

The issue with the staged runtime is that the error message from the build is misleading - release X is not supported. and there is no way to make maven use the desired JDK. It would be nice to have java_home property to point it to the right Java that should default to build-package JDK if present.

vpa1977 commented 2 weeks ago

The root cause is stage path being prepended to the PATH:

/root/stage/usr/bin:/snap/rockcraft/x1/libexec/rockcraft:/snap/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

This forces Java plugins to use staged runtime.