开发者

Packaging and deploying a Jython program from Eclipse

开发者 https://www.devze.com 2022-12-29 23:57 出处:网络
So I\'ve been pigeon-holed into writing some Jython code.I\'ve been using the latest version of Eclipse IDE with the PyDev plugin for development.Up until now, things have been moderately tolerable.I\

So I've been pigeon-holed into writing some Jython code. I've been using the latest version of Eclipse IDE with the PyDev plugin for development. Up until now, things have been moderately tolerable. I've gotten all my Python scripts working and I'm successfully including a couple of JAR files and the class directory of another Java project as external dependencies. Everything seems to run fine through the Eclipse IDE.

Now I need to package everything up and deploy it. From what I can gather, the best way to do this would be to package everything up in a JAR file. The Jython documentation suggests starting out with the jython.jar file and adding to it. OK. So I modify my main python module and start adding all my python source to the JAR.

It executes but of course can't find all the external dependencies.

How is one supposed to add the external JAR files so that they are correctly seen by the Jython interpreter? How is one supposed to manage more complex dependencies in a setup like this?

Is there a plugin for Eclipse or maybe something like Ant or Maven that can handle all of these steps for me with the push of a button?

I can't be the first person that has needed to deploy Jython code with co开发者_高级运维mplex dependencies can I?


I've made some headway on getting this all working so I thought I would put some notes here in case they help anyone else out. I'd still like to hear from others on their experiences trying to put together something like this.

It turns out that Eclipse as of 3.5 has a project export option for Java -> Runnable JAR File. If you use this option, you can point to a Java main class in the export wizard. You also have the option to have it repackage all the JARs that you are dependent on in your new JAR file. Make sure to check the box to save the export as an ANT build so that you can repeat the process quickly. NOTE that the first time you do this through the interface, it may fail, but it will still have created a JAR file.

Now here's where it gets strange. To track all the dependencies, I am still using a mostly incomplete Maven build in my project. I create the Maven .POM file. And I told Maven what my external JAR dependency was. I then told Maven to do a dependency update for me. It pulled everything into my Maven repository as expected.

Now when I do my ANT build, it appears that it is getting its list of JARs to include in the final build from Maven. I'm not really sure if it is supposed to work that way. I'm also not 100% sure that it is working. I guess I'll find out when I have to add another external JAR to the project.

Anyways, if you follow this question you'll see that you can take the latest builds of Jython and pull the org.python.util.JarRunner.java file out and use it in your own project. This is you Java.main class that you will need to point your ANT build at. From there, convert your main Python/Jython script to be the run script that was talked about in that question.

Next, build up another copy of the Jython JAR file in your Jython directory. This one should have the /Lib directory pulled into the JAR. Save that off and then point your Eclipse IDE Jave Build option for your PyDev project at that JAR as an external dependency. Your JarRunner will now work and execute the run.py file under Jython.

If all that works, you should then be able to rerun the ANT exported build from earlier and you will end up with a single JAR file that you can execute on the command line as:

java -jar {yourjar} args

And distribute to your customers without any additional dependencies.

If that all seems a little bit convoluted, it is. If anyone has a better way of setting this all up using Eclipse, please let me know.


Make your life easier and just use Maven and the mavenjython compile plugin.

See my answer of this question: Using Jython with Maven

You get full automation of the build and deploy process, and the result is a jar that includes jython and all other dependencies.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号