Skip to content

Commit

Permalink
fix(plugins): fix CPATH and --print-module-deps in jlink (#1014)
Browse files Browse the repository at this point in the history
* fix: CPATH persistence

* fix: set --print-module-deps as first argument

A bug in jdeps  causes --print-module-deps to ignore recursive settings
when it is present on classpath after --recursive.

---------

Co-authored-by: Vladimir Petko <[email protected]>
  • Loading branch information
yanksyoon and vpa1977 authored Feb 12, 2025
1 parent b417528 commit d8d6836
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 6 deletions.
15 changes: 9 additions & 6 deletions craft_parts/plugins/jlink_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,21 @@ def get_build_commands(self) -> list[str]:
commands.append("CPATH=.")
commands.append(
"""
find ${CRAFT_PART_BUILD}/tmp -type f -name *.jar | while IFS= read -r file; do
CPATH=$CPATH:${file}
for file in $(find "${CRAFT_PART_BUILD}/tmp" -type f -name "*.jar"); do
CPATH="$CPATH:${file}"
done
find ${CRAFT_STAGE} -type f -name *.jar | while IFS= read -r file; do
CPATH=$CPATH:${file}
for file in $(find "${CRAFT_STAGE}" -type f -name "*.jar"); do
CPATH="$CPATH:${file}"
done
"""
)
commands.append(
"""if [ "x${PROCESS_JARS}" != "x" ]; then
deps=$(${JDEPS} --class-path=${CPATH} -q --recursive --ignore-missing-deps \
--print-module-deps --multi-release ${MULTI_RELEASE} ${PROCESS_JARS})
deps=$(${JDEPS} --print-module-deps -q --recursive \
--ignore-missing-deps \
--multi-release ${MULTI_RELEASE} \
--class-path=${CPATH} \
${PROCESS_JARS})
else
deps=java.base
fi
Expand Down
1 change: 1 addition & 0 deletions docs/common/craft-parts/craft-parts.wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Config
ConfigDict
CopyFileNotFound
CopyTreeError
CPATH
CraftCtl
Craftctl
DESTDIR
Expand Down
10 changes: 10 additions & 0 deletions docs/reference/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
Changelog
*********

2.5.1 (2025-02-12)
------------------

Bug fixes:

- Fix CPATH variable scope in the :ref:`jlink plugin<craft_parts_jlink_plugin>`.
- Fix Jdeps parameter ordering in the
:ref:`jlink plugin<craft_parts_jlink_plugin>`.


2.3.1 (2025-02-07)
------------------

Expand Down
66 changes: 66 additions & 0 deletions tests/integration/plugins/test_jlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,72 @@
from craft_parts import LifecycleManager, Step, errors


@pytest.fixture
def build_test_jar(new_dir):
Path("Test.java").write_text(
"""
public class Test {
public static void main(String[] args) {
new Embedded();
}
}
"""
)
Path("Embedded.java").write_text(
"""
import javax.swing.*;
public class Embedded {
public Embedded() {
new JFrame("foo").setVisible(true);
}
}
"""
)
subprocess.run(
["javac", "Test.java", "Embedded.java"], check=True, capture_output=True
)
subprocess.run(
["jar", "cvf", "embedded.jar", "Embedded.class"],
check=True,
capture_output=True,
)
subprocess.run(
["jar", "cvf", "test.jar", "Test.class", "embedded.jar"],
check=True,
capture_output=True,
)


@pytest.mark.usefixtures("build_test_jar")
def test_jlink_plugin_embedded_jar(new_dir, partitions):
parts_yaml = textwrap.dedent(
"""
parts:
my-part:
plugin: jlink
source: .
jlink-jars: ["test.jar"]
after: ["stage-jar"]
stage-jar:
plugin: dump
source: .
"""
)
parts = yaml.safe_load(parts_yaml)

lf = LifecycleManager(
parts, application_name="test_jlink", cache_dir=new_dir, partitions=partitions
)
actions = lf.plan(Step.PRIME)

with lf.action_executor() as ctx:
ctx.execute(actions)

# java.desktop module should be included in the image
assert len(list(Path(f"{new_dir}/stage/usr/lib/jvm/").rglob("libawt.so"))) > 0


def test_jlink_plugin_with_jar(new_dir, partitions):
"""Test that jlink produces tailored modules"""

Expand Down

0 comments on commit d8d6836

Please sign in to comment.