Hey, I'm trying to evaluate pants for setting up a...
# general
p
Hey, I'm trying to evaluate pants for setting up a monorepo at my work. One hurdle I'm having; I need to build a java
.jar
and call it from a python script. I've got the java part built with
deploy_jar
, but there's no obvious way of combining
resource
or
file
or
relocated_files
to make it accessible from python. Any tips or help would be appreciated
e
I haven't worked with this myself, but I suspect that https://www.pantsbuild.org/stable/reference/targets/experimental_wrap_as_resources may be what you need
👀 1
p
Thanks Luke, found this based on your comment - https://github.com/pantsbuild/pants/discussions/21366
Though, still not quite working - the
shell_command
doesn't find the ".jar" file generated by
deploy_jar
For those who stumble back across this in future, there were some extra steps to get this working:
Copy code
shell_command(
    name="my-jar-file.command",
    command="exit 0",
    workdir="/src.jvm.com.my.namespace",
    execution_dependencies=["src/jvm/com/my/namespace:target"],
    output_files=["my-jar-file.jar"],
    outputs_match_mode="at_least_one",
    root_output_directory=".",
)
relocated_files(
    name="my-jar-file.relocated",
    files_targets=[":my-jar-file.command"],
    src="",
    dest="mypackage/mymodule",
)
experimental_wrap_as_resources(name="my-jar-file.bin", inputs=[":my-jar-file.relocated"])
The key detail was using
relocated_files
with the relative path within
src/python
to my python module. Then I could resolve the file path, and execute java with
Copy code
with importlib.resources.as_file(importlib.resources.files(__name__) / "my-jar-file.jar") as my_jar_file:
   sh.java("-jar", my_jar_file, ....)