pythonbuckpython-pex

Is it possible to create a pex with a JAR included as dependency?


I'm not much of a Python dev, so I'm sorry if there is not much sense in this question..

I'm currently working in a project that contains a py script that communicates with a server written in Java. A considerable part of the script is dedicated to find where the server jar is located based on the current work directory (cwd) from where the script is being executed. I though that I could pack both, the script and the server jar, in some sort of zip that I could distribute and execute in a standalone fashion, removing the whole logic to fetch the jar from the script. Is it possible to have that?

I've been trying to use BUCK python_binary for this purpose, however, it doesn't seem I can add jar as a dependency for the pex.


EDIT 1

Following @sdwilsh suggestion I was able to use BUCK to create a pex containing the jar, however, whenever I try to access it from the main python script, it fails because the jar cannot be found:

BUCK file

java_library(
  name = 'src_main',
  srcs = glob(['src/main/java/**/*.java']),
  source = '8',
  target = '8',
  visibility = [
    'PUBLIC',
  ],
)

java_binary(
  name = 'sample_jar',
  main_class = 'br.com.samples.jar.Main',
  deps = [':src_main'],
  visibility = [
    'PUBLIC',
  ],
)

python_library (
  name = 'jarlib',
  resources = [':sample_jar'],
)

python_binary(
  name = 'wrapper',
  main = 'wrapper.py',
  deps = [':jarlib'],
)

PEX content

-rw-r--r--  1 staff    1319 Mar 28 13:07 sample_jar
-rw-r--r--  1 staff     259 Mar 28 13:15 wrapper.py
-rw-r--r--  1 staff     308 Mar 28 13:15 wrapper.pyc
-rw-r--r--  1 staff     917 Mar 28 13:15 __main__.pyc
-rw-r--r--  1 staff     737 Mar 28 13:15 __main__.py
-rw-r--r--  1 staff     242 Mar 28 13:15 PEX-INFO

wrapper.py

import sys, subprocess

process = subprocess.Popen(['java', '-jar', 'sample_jar'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print process.communicate()

Output for wrapper.py

('', 'Error: Unable to access jarfile sample_jar\n')

EDIT 2

So it seems we cannot reference the jar file from the PEX env. A workaround is to unpack the pex resources in a tmp folder and execute everything from there.


Solution

  • You can, but you'd need to use a python_library and add the rule that produces the java_binary in the resources argument.