From 13f71d526d8179e9328c5852c131ecb30ab5e080 Mon Sep 17 00:00:00 2001 From: Vladimir Petko Date: Wed, 9 Oct 2024 08:04:35 +1300 Subject: [PATCH 01/21] feat: maven rockcraft plugin Add overrides for craft-part maven plugin to allow building Maven projects in rockcraft. Namely: - Do not link /bin/java as it conflicts with the base-files_base slice - Restrict PATH to /usr/bin in order to avoid picking up unwanted JVM. Use can override PATH in the plugin environment settings if /usr/bin is not sufficient. --- rockcraft/plugins/maven_plugin.py | 76 +++++++++++++++++++ rockcraft/plugins/register.py | 3 +- .../rockcraft/plugin-maven/rockcraft.yaml | 35 +++++++++ .../rockcraft/plugin-maven/sample/pom.xml | 30 ++++++++ .../sample/src/main/java/test/Test.java | 7 ++ tests/spread/rockcraft/plugin-maven/task.yaml | 21 +++++ 6 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 rockcraft/plugins/maven_plugin.py create mode 100644 tests/spread/rockcraft/plugin-maven/rockcraft.yaml create mode 100644 tests/spread/rockcraft/plugin-maven/sample/pom.xml create mode 100644 tests/spread/rockcraft/plugin-maven/sample/src/main/java/test/Test.java create mode 100644 tests/spread/rockcraft/plugin-maven/task.yaml diff --git a/rockcraft/plugins/maven_plugin.py b/rockcraft/plugins/maven_plugin.py new file mode 100644 index 000000000..3f295f97d --- /dev/null +++ b/rockcraft/plugins/maven_plugin.py @@ -0,0 +1,76 @@ +# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- +# +# Copyright 2023 Canonical Ltd. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3 as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +"""The Rockcraft Maven plugin.""" +import logging +from textwrap import dedent + +from craft_parts.plugins import maven_plugin +from overrides import override # type: ignore[reportUnknownVariableType] + +logger = logging.getLogger(__name__) + +# Template for the sitecustomize module that we'll add to the payload so that +# the pip-installed packages are found regardless of how the interpreter is +# called. +SITECUSTOMIZE_TEMPLATE = dedent( + """ + # sitecustomize added by Rockcraft. + import site + import sys + + major, minor = sys.version_info.major, sys.version_info.minor + site_dir = f"/lib/python{major}.{minor}/site-packages" + dist_dir = "/usr/lib/python3/dist-packages" + + # Add the directory that contains the venv-installed packages. + site.addsitedir(site_dir) + + if dist_dir in sys.path: + # Make sure that this site-packages dir comes *before* the base-provided + # dist-packages dir in sys.path. + path = sys.path + site_index = path.index(site_dir) + dist_index = path.index(dist_dir) + + if dist_index < site_index: + path[dist_index], path[site_index] = path[site_index], path[dist_index] + + EOF + """ +).strip() + + +class MavenPlugin(maven_plugin.MavenPlugin): + """A MavenPlugin plugin for Rockcraft. + + This plugin extends Craft-parts' vanilla Maven plugin to properly + use and install the Java VM. Specifically: + + - Do not link java executable to /bin/java + - Do not include staging area in the PATH + """ + + @override + def _get_java_link_commands(self) -> list[str]: + """Get the bash commands to provide /bin/java symlink.""" + return [] + + @override + def get_build_environment(self) -> dict[str, str]: + env = super().get_build_environment() + env["PATH"] = "/usr/bin" + return env diff --git a/rockcraft/plugins/register.py b/rockcraft/plugins/register.py index 5434e4cbc..f4387de69 100644 --- a/rockcraft/plugins/register.py +++ b/rockcraft/plugins/register.py @@ -19,6 +19,7 @@ import craft_parts from craft_parts.plugins.plugins import PluginType +from .maven_plugin import MavenPlugin from .poetry_plugin import PoetryPlugin from .python_plugin import PythonPlugin @@ -30,4 +31,4 @@ def register() -> None: def get_plugins() -> dict[str, PluginType]: """Get a dict of Rockcraft-specific plugins.""" - return {"poetry": PoetryPlugin, "python": PythonPlugin} + return {"poetry": PoetryPlugin, "python": PythonPlugin, "maven": MavenPlugin} diff --git a/tests/spread/rockcraft/plugin-maven/rockcraft.yaml b/tests/spread/rockcraft/plugin-maven/rockcraft.yaml new file mode 100644 index 000000000..6dc6833af --- /dev/null +++ b/tests/spread/rockcraft/plugin-maven/rockcraft.yaml @@ -0,0 +1,35 @@ +name: bare-build-2404 +base: bare +build-base: ubuntu@24.04 + +version: '0.1' +summary: A rock that bundles a Maven project. +description: A rock that bundles a Python project. +license: GPL-3.0 +platforms: + amd64: + +services: + test: + command: /usr/lib/jvm/java-21-openjdk-${CRAFT_ARCH_BUILD_FOR}/bin/java -jar /jar/sample-0.0.1-SNAPSHOT.jar + override: replace + startup: enabled + +parts: + maven-sample: + plugin: maven + source: sample + stage-packages: + - openjdk-21-jre-headless_core + - base-files_base + build-packages: + - maven + - openjdk-21-jdk-headless + # replace chiselled image with jlink + override-stage: | + craftctl default + rm -rf ${CRAFT_STAGE}/usr/lib/jvm/java-21-openjdk-${CRAFT_ARCH_BUILD_FOR} + jlink --no-header-files --no-man-pages --strip-debug \ + --add-modules java.base,java.xml,java.logging,jdk.net \ + --output ${CRAFT_STAGE}/usr/lib/jvm/java-21-openjdk-${CRAFT_ARCH_BUILD_FOR} + touch ${CRAFT_STAGE}/usr/lib/jvm/java-21-openjdk-${CRAFT_ARCH_BUILD_FOR}/lib/jvm.cfg-default diff --git a/tests/spread/rockcraft/plugin-maven/sample/pom.xml b/tests/spread/rockcraft/plugin-maven/sample/pom.xml new file mode 100644 index 000000000..9c8a762b6 --- /dev/null +++ b/tests/spread/rockcraft/plugin-maven/sample/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + test + sample + 0.0.1-SNAPSHOT + sample + + 8 + 8 + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.4.2 + + + + test.Test + + + + + + + diff --git a/tests/spread/rockcraft/plugin-maven/sample/src/main/java/test/Test.java b/tests/spread/rockcraft/plugin-maven/sample/src/main/java/test/Test.java new file mode 100644 index 000000000..7871063ae --- /dev/null +++ b/tests/spread/rockcraft/plugin-maven/sample/src/main/java/test/Test.java @@ -0,0 +1,7 @@ +package test; + +public class Test { + public static void main(String[] args) { + System.out.println("hello world"); + } +} diff --git a/tests/spread/rockcraft/plugin-maven/task.yaml b/tests/spread/rockcraft/plugin-maven/task.yaml new file mode 100644 index 000000000..7776a9e12 --- /dev/null +++ b/tests/spread/rockcraft/plugin-maven/task.yaml @@ -0,0 +1,21 @@ +summary: Maven plugin tests +execute: | + SCENARIO="plugin-maven" + ROCK_FILE="${SCENARIO}_0.1_amd64.rock" + IMAGE="${SCENARIO}:0.1" + + # Build the rock & load it into docker + run_rockcraft pack + test -f ${ROCK_FILE} + rockcraft.skopeo copy oci-archive:${ROCK_FILE} docker-daemon:${IMAGE} + docker images + rm ${ROCK_FILE} + + # Run the packaged project, both via the console script and via "python -m" + docker run --rm $IMAGE exec hello | MATCH "hello world" + + docker system prune -a -f + + # Rebuild the java project + touch sample/src/main/java/test/Test.java + rockcraft pack From c76815f063b4e91c2f7d668f9d6d941f22deceed Mon Sep 17 00:00:00 2001 From: Vladimir Petko Date: Wed, 9 Oct 2024 15:56:48 +1300 Subject: [PATCH 02/21] test: only copy modules file --- tests/spread/rockcraft/plugin-maven/rockcraft.yaml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/spread/rockcraft/plugin-maven/rockcraft.yaml b/tests/spread/rockcraft/plugin-maven/rockcraft.yaml index 6dc6833af..8908a8355 100644 --- a/tests/spread/rockcraft/plugin-maven/rockcraft.yaml +++ b/tests/spread/rockcraft/plugin-maven/rockcraft.yaml @@ -1,4 +1,4 @@ -name: bare-build-2404 +name: plugin-maven base: bare build-base: ubuntu@24.04 @@ -25,11 +25,11 @@ parts: build-packages: - maven - openjdk-21-jdk-headless - # replace chiselled image with jlink + # replace chiselled image modules with jlink output override-stage: | craftctl default - rm -rf ${CRAFT_STAGE}/usr/lib/jvm/java-21-openjdk-${CRAFT_ARCH_BUILD_FOR} + rm -rf out jlink --no-header-files --no-man-pages --strip-debug \ - --add-modules java.base,java.xml,java.logging,jdk.net \ - --output ${CRAFT_STAGE}/usr/lib/jvm/java-21-openjdk-${CRAFT_ARCH_BUILD_FOR} - touch ${CRAFT_STAGE}/usr/lib/jvm/java-21-openjdk-${CRAFT_ARCH_BUILD_FOR}/lib/jvm.cfg-default + --add-modules java.base \ + --output out + cp out/lib/modules ${CRAFT_STAGE}/usr/lib/jvm/java-21-openjdk-${CRAFT_ARCH_BUILD_FOR}/lib From 810587a76fe3d5cd3909a01db348aec949d3cd7c Mon Sep 17 00:00:00 2001 From: Vladimir Petko Date: Wed, 9 Oct 2024 17:00:35 +1300 Subject: [PATCH 03/21] chore: fix comment in the test --- tests/spread/rockcraft/plugin-maven/task.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/spread/rockcraft/plugin-maven/task.yaml b/tests/spread/rockcraft/plugin-maven/task.yaml index 7776a9e12..101cc21f5 100644 --- a/tests/spread/rockcraft/plugin-maven/task.yaml +++ b/tests/spread/rockcraft/plugin-maven/task.yaml @@ -11,7 +11,7 @@ execute: | docker images rm ${ROCK_FILE} - # Run the packaged project, both via the console script and via "python -m" + # Run the packaged project docker run --rm $IMAGE exec hello | MATCH "hello world" docker system prune -a -f From 7e864d5faa9fc70dfc62ad35a676592a6fa96995 Mon Sep 17 00:00:00 2001 From: Vladimir Petko Date: Wed, 9 Oct 2024 17:02:07 +1300 Subject: [PATCH 04/21] chore: fix copyright year for maven plugin --- rockcraft/plugins/maven_plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rockcraft/plugins/maven_plugin.py b/rockcraft/plugins/maven_plugin.py index 3f295f97d..6aeed1072 100644 --- a/rockcraft/plugins/maven_plugin.py +++ b/rockcraft/plugins/maven_plugin.py @@ -1,6 +1,6 @@ # -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- # -# Copyright 2023 Canonical Ltd. +# Copyright 2024 Canonical Ltd. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License version 3 as From 81b02fef18322b7a01ad658dd930c5dd322423a9 Mon Sep 17 00:00:00 2001 From: Vladimir Petko Date: Thu, 10 Oct 2024 10:22:16 +1300 Subject: [PATCH 05/21] test: remove maven download spam - use -B to skip printing download logs --- tests/spread/rockcraft/plugin-maven/rockcraft.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/spread/rockcraft/plugin-maven/rockcraft.yaml b/tests/spread/rockcraft/plugin-maven/rockcraft.yaml index 8908a8355..821ded4b5 100644 --- a/tests/spread/rockcraft/plugin-maven/rockcraft.yaml +++ b/tests/spread/rockcraft/plugin-maven/rockcraft.yaml @@ -19,6 +19,7 @@ parts: maven-sample: plugin: maven source: sample + maven-parameters: [ "-B" ] stage-packages: - openjdk-21-jre-headless_core - base-files_base From 4aaa3e47834dbcc5d89329956f6352007e352d99 Mon Sep 17 00:00:00 2001 From: Vladimir Petko Date: Thu, 10 Oct 2024 10:22:40 +1300 Subject: [PATCH 06/21] test: log actual vs expected content Log actual directory content and expected rock file name. --- tests/spread/rockcraft/plugin-maven/task.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/spread/rockcraft/plugin-maven/task.yaml b/tests/spread/rockcraft/plugin-maven/task.yaml index 101cc21f5..eb402c5da 100644 --- a/tests/spread/rockcraft/plugin-maven/task.yaml +++ b/tests/spread/rockcraft/plugin-maven/task.yaml @@ -6,6 +6,8 @@ execute: | # Build the rock & load it into docker run_rockcraft pack + ls + echo expecting ${ROCK_FILE} test -f ${ROCK_FILE} rockcraft.skopeo copy oci-archive:${ROCK_FILE} docker-daemon:${IMAGE} docker images From 30d1320653639109fd6234024324d155134964af Mon Sep 17 00:00:00 2001 From: Vladimir Petko Date: Thu, 10 Oct 2024 14:36:15 +1300 Subject: [PATCH 07/21] test: use insecure-policy in copy args --- tests/spread/rockcraft/plugin-maven/task.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/spread/rockcraft/plugin-maven/task.yaml b/tests/spread/rockcraft/plugin-maven/task.yaml index eb402c5da..6bfd57219 100644 --- a/tests/spread/rockcraft/plugin-maven/task.yaml +++ b/tests/spread/rockcraft/plugin-maven/task.yaml @@ -9,7 +9,7 @@ execute: | ls echo expecting ${ROCK_FILE} test -f ${ROCK_FILE} - rockcraft.skopeo copy oci-archive:${ROCK_FILE} docker-daemon:${IMAGE} + rockcraft.skopeo copy --insecure-policy oci-archive:${ROCK_FILE} docker-daemon:${IMAGE} docker images rm ${ROCK_FILE} From a86542867bb2abcefc0716881b075373d8c687c4 Mon Sep 17 00:00:00 2001 From: Vladimir Petko Date: Thu, 10 Oct 2024 16:32:41 +1300 Subject: [PATCH 08/21] test: drop startup server from the test --- tests/spread/rockcraft/plugin-maven/rockcraft.yaml | 6 ------ tests/spread/rockcraft/plugin-maven/task.yaml | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/tests/spread/rockcraft/plugin-maven/rockcraft.yaml b/tests/spread/rockcraft/plugin-maven/rockcraft.yaml index 821ded4b5..dd9fb607f 100644 --- a/tests/spread/rockcraft/plugin-maven/rockcraft.yaml +++ b/tests/spread/rockcraft/plugin-maven/rockcraft.yaml @@ -9,12 +9,6 @@ license: GPL-3.0 platforms: amd64: -services: - test: - command: /usr/lib/jvm/java-21-openjdk-${CRAFT_ARCH_BUILD_FOR}/bin/java -jar /jar/sample-0.0.1-SNAPSHOT.jar - override: replace - startup: enabled - parts: maven-sample: plugin: maven diff --git a/tests/spread/rockcraft/plugin-maven/task.yaml b/tests/spread/rockcraft/plugin-maven/task.yaml index 6bfd57219..dd859a4bd 100644 --- a/tests/spread/rockcraft/plugin-maven/task.yaml +++ b/tests/spread/rockcraft/plugin-maven/task.yaml @@ -14,7 +14,7 @@ execute: | rm ${ROCK_FILE} # Run the packaged project - docker run --rm $IMAGE exec hello | MATCH "hello world" + docker run --rm $IMAGE exec /usr/lib/jvm/java-21-openjdk-amd64/bin/java -jar /jar/sample-0.0.1-SNAPSHOT.jar | MATCH "hello world" docker system prune -a -f From 26440ec6c060c8bc0bc0cfbe163910f3cf5ce58b Mon Sep 17 00:00:00 2001 From: Vladimir Petko Date: Mon, 14 Oct 2024 08:28:29 +1300 Subject: [PATCH 09/21] doc: add rockcraft-specific maven docs Maven plugin is overriden in rockcraft. Add rockcraft-specific instructions for the plugin. --- docs/reference/plugins.rst | 2 +- docs/reference/plugins/maven_plugin.rst | 51 +++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 docs/reference/plugins/maven_plugin.rst diff --git a/docs/reference/plugins.rst b/docs/reference/plugins.rst index d01219be6..b2235927b 100644 --- a/docs/reference/plugins.rst +++ b/docs/reference/plugins.rst @@ -17,7 +17,7 @@ Rockcraft. /common/craft-parts/reference/plugins/dump_plugin /common/craft-parts/reference/plugins/go_plugin /common/craft-parts/reference/plugins/make_plugin - /common/craft-parts/reference/plugins/maven_plugin + plugins/maven_plugin /common/craft-parts/reference/plugins/meson_plugin /common/craft-parts/reference/plugins/nil_plugin /common/craft-parts/reference/plugins/npm_plugin diff --git a/docs/reference/plugins/maven_plugin.rst b/docs/reference/plugins/maven_plugin.rst new file mode 100644 index 000000000..9f40bdd2e --- /dev/null +++ b/docs/reference/plugins/maven_plugin.rst @@ -0,0 +1,51 @@ +.. _craft_parts_maven_plugin: + +Maven plugin +============ + +The Maven plugin builds Java projects using the Maven build tool. + +After a successful build, this plugin will: + +* Create ``jar/`` directory in ``$CRAFT_PART_INSTALL``. +* Hard link the ``.jar`` files generated in ``$CRAFT_PART_BUILD`` to + ``$CRAFT_PART_INSTALL/jar``. + + +Keywords +-------- + +In addition to the common :ref:`plugin ` and +:ref:`sources ` keywords, this plugin provides the following +plugin-specific keywords: + +maven-parameters +~~~~~~~~~~~~~~~~ +**Type:** list of strings + +Used to add additional parameters to the ``mvn package`` command line. + + +Environment variables +--------------------- + +This plugin reads the ``http_proxy`` and ``https_proxy`` variables from the environment +to configure Maven proxy access. A comma-separated list of hosts that should not be +accessed via proxy is read from the ``no_proxy`` environment variable. + +Please refer to `Configuring Apache Maven `_ for +a list of environment variables used to configure Maven. + + +.. _maven-details-begin: + +Dependencies +------------ + +The plugin expects Maven to be available on the system as the ``mvn`` executable. + +Note that the Maven plugin does not make a Java runtime available in the target +environment. This must be handled by the developer when defining the part, according to +each application's runtime requirements. + +.. _maven-details-end: From d47db083f1fc0ab2a01428eb0ee6ebf4f1abbdac Mon Sep 17 00:00:00 2001 From: Vladimir Petko Date: Mon, 14 Oct 2024 08:35:28 +1300 Subject: [PATCH 10/21] lint(maven_plugin.rst): reformat file - fix linter 'line too long' warnings --- docs/reference/plugins/maven_plugin.rst | 26 ++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/docs/reference/plugins/maven_plugin.rst b/docs/reference/plugins/maven_plugin.rst index 9f40bdd2e..c39fbd4c3 100644 --- a/docs/reference/plugins/maven_plugin.rst +++ b/docs/reference/plugins/maven_plugin.rst @@ -16,8 +16,8 @@ Keywords -------- In addition to the common :ref:`plugin ` and -:ref:`sources ` keywords, this plugin provides the following -plugin-specific keywords: +:ref:`sources ` keywords, this plugin +provides the following plugin-specific keywords: maven-parameters ~~~~~~~~~~~~~~~~ @@ -29,12 +29,13 @@ Used to add additional parameters to the ``mvn package`` command line. Environment variables --------------------- -This plugin reads the ``http_proxy`` and ``https_proxy`` variables from the environment -to configure Maven proxy access. A comma-separated list of hosts that should not be -accessed via proxy is read from the ``no_proxy`` environment variable. +This plugin reads the ``http_proxy`` and ``https_proxy`` variables +from the environment to configure Maven proxy access. A comma-separated +list of hosts that should not be accessed via proxy is read from the +```no_proxy`` environment variable. -Please refer to `Configuring Apache Maven `_ for -a list of environment variables used to configure Maven. +Please refer to `Configuring Apache Maven `_ for a list of +environment variables used to configure Maven. .. _maven-details-begin: @@ -42,10 +43,13 @@ a list of environment variables used to configure Maven. Dependencies ------------ -The plugin expects Maven to be available on the system as the ``mvn`` executable. +The plugin expects Maven to be available on the system as the ``mvn`` +executable. -Note that the Maven plugin does not make a Java runtime available in the target -environment. This must be handled by the developer when defining the part, according to -each application's runtime requirements. +Note that the Maven plugin does not make a Java runtime available in +the target environment. This must be handled by the developer when +defining the part, according to each application's runtime requirements. .. _maven-details-end: + +.. _`mvn`: https://maven.apache.org/configure.html From 84de8ae93c3266db8c8057f3d0d1308bb3c03991 Mon Sep 17 00:00:00 2001 From: Vladimir Petko Date: Mon, 14 Oct 2024 08:37:43 +1300 Subject: [PATCH 11/21] test(plugin-maven): expand test comment --- tests/spread/rockcraft/plugin-maven/rockcraft.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/spread/rockcraft/plugin-maven/rockcraft.yaml b/tests/spread/rockcraft/plugin-maven/rockcraft.yaml index dd9fb607f..59b2b8cac 100644 --- a/tests/spread/rockcraft/plugin-maven/rockcraft.yaml +++ b/tests/spread/rockcraft/plugin-maven/rockcraft.yaml @@ -21,6 +21,8 @@ parts: - maven - openjdk-21-jdk-headless # replace chiselled image modules with jlink output + # in order to ensure that only java.base module is present in + # runtime override-stage: | craftctl default rm -rf out From a73213827bcbfb9a6c3cde9d8a1570f400f814ec Mon Sep 17 00:00:00 2001 From: Vladimir Petko Date: Mon, 14 Oct 2024 08:57:59 +1300 Subject: [PATCH 12/21] doc(maven-plugin): update labels and links --- docs/reference/plugins/maven_plugin.rst | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/docs/reference/plugins/maven_plugin.rst b/docs/reference/plugins/maven_plugin.rst index c39fbd4c3..037b98ea1 100644 --- a/docs/reference/plugins/maven_plugin.rst +++ b/docs/reference/plugins/maven_plugin.rst @@ -1,4 +1,4 @@ -.. _craft_parts_maven_plugin: +.. _rockcraft_maven_plugin: Maven plugin ============ @@ -34,11 +34,12 @@ from the environment to configure Maven proxy access. A comma-separated list of hosts that should not be accessed via proxy is read from the ```no_proxy`` environment variable. -Please refer to `Configuring Apache Maven `_ for a list of +Please refer to `Configuring Apache Maven +`_ for a list of environment variables used to configure Maven. -.. _maven-details-begin: +.. _rockcraft_maven-details-begin: Dependencies ------------ @@ -50,6 +51,6 @@ Note that the Maven plugin does not make a Java runtime available in the target environment. This must be handled by the developer when defining the part, according to each application's runtime requirements. -.. _maven-details-end: +.. _rockcraft_maven-details-end: -.. _`mvn`: https://maven.apache.org/configure.html +.. _`mvn`: From bdaec033dafea0f99c736d213fd64fd436a8cee6 Mon Sep 17 00:00:00 2001 From: Vladimir Petko Date: Mon, 21 Oct 2024 21:47:34 +0200 Subject: [PATCH 13/21] fix: drop unused sitecustomize --- rockcraft/plugins/maven_plugin.py | 32 ------------------------------- 1 file changed, 32 deletions(-) diff --git a/rockcraft/plugins/maven_plugin.py b/rockcraft/plugins/maven_plugin.py index 6aeed1072..0d1a6f1b0 100644 --- a/rockcraft/plugins/maven_plugin.py +++ b/rockcraft/plugins/maven_plugin.py @@ -16,44 +16,12 @@ """The Rockcraft Maven plugin.""" import logging -from textwrap import dedent from craft_parts.plugins import maven_plugin from overrides import override # type: ignore[reportUnknownVariableType] logger = logging.getLogger(__name__) -# Template for the sitecustomize module that we'll add to the payload so that -# the pip-installed packages are found regardless of how the interpreter is -# called. -SITECUSTOMIZE_TEMPLATE = dedent( - """ - # sitecustomize added by Rockcraft. - import site - import sys - - major, minor = sys.version_info.major, sys.version_info.minor - site_dir = f"/lib/python{major}.{minor}/site-packages" - dist_dir = "/usr/lib/python3/dist-packages" - - # Add the directory that contains the venv-installed packages. - site.addsitedir(site_dir) - - if dist_dir in sys.path: - # Make sure that this site-packages dir comes *before* the base-provided - # dist-packages dir in sys.path. - path = sys.path - site_index = path.index(site_dir) - dist_index = path.index(dist_dir) - - if dist_index < site_index: - path[dist_index], path[site_index] = path[site_index], path[dist_index] - - EOF - """ -).strip() - - class MavenPlugin(maven_plugin.MavenPlugin): """A MavenPlugin plugin for Rockcraft. From b68dce76ee1cb1e003f6056bf32ec88e5b25e4f7 Mon Sep 17 00:00:00 2001 From: Vladimir Petko Date: Mon, 21 Oct 2024 21:58:16 +0200 Subject: [PATCH 14/21] Update tests/spread/rockcraft/plugin-maven/rockcraft.yaml Co-authored-by: Tiago Nobrega --- tests/spread/rockcraft/plugin-maven/rockcraft.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/spread/rockcraft/plugin-maven/rockcraft.yaml b/tests/spread/rockcraft/plugin-maven/rockcraft.yaml index 59b2b8cac..d52ec6e60 100644 --- a/tests/spread/rockcraft/plugin-maven/rockcraft.yaml +++ b/tests/spread/rockcraft/plugin-maven/rockcraft.yaml @@ -4,7 +4,7 @@ build-base: ubuntu@24.04 version: '0.1' summary: A rock that bundles a Maven project. -description: A rock that bundles a Python project. +description: A rock that bundles a Maven project. license: GPL-3.0 platforms: amd64: From 5d745eadb343455e6a22e87984c7ad1527c69872 Mon Sep 17 00:00:00 2001 From: Vladimir Petko Date: Tue, 22 Oct 2024 07:51:11 +0200 Subject: [PATCH 15/21] fix: put /usr/bin in front Do not touch path, just put /usr/bin as a priority --- rockcraft/plugins/maven_plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rockcraft/plugins/maven_plugin.py b/rockcraft/plugins/maven_plugin.py index 0d1a6f1b0..c6c852833 100644 --- a/rockcraft/plugins/maven_plugin.py +++ b/rockcraft/plugins/maven_plugin.py @@ -40,5 +40,5 @@ def _get_java_link_commands(self) -> list[str]: @override def get_build_environment(self) -> dict[str, str]: env = super().get_build_environment() - env["PATH"] = "/usr/bin" + env["PATH"] = "/usr/bin:$PATH" return env From 940315a940063391ca5b4340fe73e83f1da6e6c8 Mon Sep 17 00:00:00 2001 From: Vladimir Petko Date: Tue, 22 Oct 2024 07:51:25 +0200 Subject: [PATCH 16/21] test: fix override stage-> build --- tests/spread/rockcraft/plugin-maven/rockcraft.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/spread/rockcraft/plugin-maven/rockcraft.yaml b/tests/spread/rockcraft/plugin-maven/rockcraft.yaml index d52ec6e60..23d54c991 100644 --- a/tests/spread/rockcraft/plugin-maven/rockcraft.yaml +++ b/tests/spread/rockcraft/plugin-maven/rockcraft.yaml @@ -23,10 +23,10 @@ parts: # replace chiselled image modules with jlink output # in order to ensure that only java.base module is present in # runtime - override-stage: | + override-build: | craftctl default rm -rf out jlink --no-header-files --no-man-pages --strip-debug \ --add-modules java.base \ --output out - cp out/lib/modules ${CRAFT_STAGE}/usr/lib/jvm/java-21-openjdk-${CRAFT_ARCH_BUILD_FOR}/lib + cp out/lib/modules ${CRAFT_PART_INSTALL}/usr/lib/jvm/java-21-openjdk-${CRAFT_ARCH_BUILD_FOR}/lib From 53d2e6a4de6d25cbee9f9d1c44f16ae2494189e6 Mon Sep 17 00:00:00 2001 From: Vladimir Petko Date: Wed, 23 Oct 2024 06:59:50 +0200 Subject: [PATCH 17/21] Update tests/spread/rockcraft/plugin-maven/task.yaml Co-authored-by: Tiago Nobrega --- tests/spread/rockcraft/plugin-maven/task.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/spread/rockcraft/plugin-maven/task.yaml b/tests/spread/rockcraft/plugin-maven/task.yaml index dd859a4bd..c017fc8d4 100644 --- a/tests/spread/rockcraft/plugin-maven/task.yaml +++ b/tests/spread/rockcraft/plugin-maven/task.yaml @@ -18,6 +18,6 @@ execute: | docker system prune -a -f - # Rebuild the java project + # Rebuild the java project to confirm that it rebuilds cleanly touch sample/src/main/java/test/Test.java rockcraft pack From bb08f27b692d74ab4dc81140b6b2ac0154c89b60 Mon Sep 17 00:00:00 2001 From: Vladimir Petko Date: Wed, 23 Oct 2024 07:00:09 +0200 Subject: [PATCH 18/21] Update tests/spread/rockcraft/plugin-maven/task.yaml Co-authored-by: Tiago Nobrega --- tests/spread/rockcraft/plugin-maven/task.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/spread/rockcraft/plugin-maven/task.yaml b/tests/spread/rockcraft/plugin-maven/task.yaml index c017fc8d4..fe16b9615 100644 --- a/tests/spread/rockcraft/plugin-maven/task.yaml +++ b/tests/spread/rockcraft/plugin-maven/task.yaml @@ -14,7 +14,7 @@ execute: | rm ${ROCK_FILE} # Run the packaged project - docker run --rm $IMAGE exec /usr/lib/jvm/java-21-openjdk-amd64/bin/java -jar /jar/sample-0.0.1-SNAPSHOT.jar | MATCH "hello world" + docker run --rm $IMAGE exec /usr/bin/java -jar /jar/sample-0.0.1-SNAPSHOT.jar | MATCH "hello world" docker system prune -a -f From d326747a7acd251848d8288bcab115e2b1c513fc Mon Sep 17 00:00:00 2001 From: Vladimir Petko Date: Wed, 23 Oct 2024 08:46:28 +0200 Subject: [PATCH 19/21] Revert "Update tests/spread/rockcraft/plugin-maven/task.yaml" This reverts commit bb08f27b692d74ab4dc81140b6b2ac0154c89b60. Chisel slices do not install /usr/bin symlink as it will cause conflicts between different versions of jre slices. --- tests/spread/rockcraft/plugin-maven/task.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/spread/rockcraft/plugin-maven/task.yaml b/tests/spread/rockcraft/plugin-maven/task.yaml index fe16b9615..c017fc8d4 100644 --- a/tests/spread/rockcraft/plugin-maven/task.yaml +++ b/tests/spread/rockcraft/plugin-maven/task.yaml @@ -14,7 +14,7 @@ execute: | rm ${ROCK_FILE} # Run the packaged project - docker run --rm $IMAGE exec /usr/bin/java -jar /jar/sample-0.0.1-SNAPSHOT.jar | MATCH "hello world" + docker run --rm $IMAGE exec /usr/lib/jvm/java-21-openjdk-amd64/bin/java -jar /jar/sample-0.0.1-SNAPSHOT.jar | MATCH "hello world" docker system prune -a -f From 5bc22b661fbc95c48f4a09f1fb0a4b9a3be8a005 Mon Sep 17 00:00:00 2001 From: Vladimir Petko Date: Wed, 23 Oct 2024 09:13:05 +0200 Subject: [PATCH 20/21] lint: apply black --- rockcraft/plugins/maven_plugin.py | 1 + 1 file changed, 1 insertion(+) diff --git a/rockcraft/plugins/maven_plugin.py b/rockcraft/plugins/maven_plugin.py index c6c852833..b9976ccbd 100644 --- a/rockcraft/plugins/maven_plugin.py +++ b/rockcraft/plugins/maven_plugin.py @@ -22,6 +22,7 @@ logger = logging.getLogger(__name__) + class MavenPlugin(maven_plugin.MavenPlugin): """A MavenPlugin plugin for Rockcraft. From fae438306c2a34f317a575eea73166abb6c5c65c Mon Sep 17 00:00:00 2001 From: Vladimir Petko Date: Thu, 7 Nov 2024 09:10:28 +1300 Subject: [PATCH 21/21] fix: only drop /bin/java symlink Maven plugin in rockcraft will work if we drop /bin/java symlink. The JAVA_HOME variable will be set by java plugin --- rockcraft/plugins/maven_plugin.py | 7 ------- tests/spread/rockcraft/plugin-maven/task.yaml | 6 ------ 2 files changed, 13 deletions(-) diff --git a/rockcraft/plugins/maven_plugin.py b/rockcraft/plugins/maven_plugin.py index b9976ccbd..a3e2d0331 100644 --- a/rockcraft/plugins/maven_plugin.py +++ b/rockcraft/plugins/maven_plugin.py @@ -30,16 +30,9 @@ class MavenPlugin(maven_plugin.MavenPlugin): use and install the Java VM. Specifically: - Do not link java executable to /bin/java - - Do not include staging area in the PATH """ @override def _get_java_link_commands(self) -> list[str]: """Get the bash commands to provide /bin/java symlink.""" return [] - - @override - def get_build_environment(self) -> dict[str, str]: - env = super().get_build_environment() - env["PATH"] = "/usr/bin:$PATH" - return env diff --git a/tests/spread/rockcraft/plugin-maven/task.yaml b/tests/spread/rockcraft/plugin-maven/task.yaml index c017fc8d4..87b97d4d1 100644 --- a/tests/spread/rockcraft/plugin-maven/task.yaml +++ b/tests/spread/rockcraft/plugin-maven/task.yaml @@ -6,18 +6,12 @@ execute: | # Build the rock & load it into docker run_rockcraft pack - ls echo expecting ${ROCK_FILE} test -f ${ROCK_FILE} rockcraft.skopeo copy --insecure-policy oci-archive:${ROCK_FILE} docker-daemon:${IMAGE} - docker images rm ${ROCK_FILE} # Run the packaged project docker run --rm $IMAGE exec /usr/lib/jvm/java-21-openjdk-amd64/bin/java -jar /jar/sample-0.0.1-SNAPSHOT.jar | MATCH "hello world" docker system prune -a -f - - # Rebuild the java project to confirm that it rebuilds cleanly - touch sample/src/main/java/test/Test.java - rockcraft pack