Eclipse: Package multiple projects into one JAR
I have m开发者_开发问答ultiple projects but only one with a Main class. The one with the Main class depends on the other projects. These projects are referenced in Eclipse, but when I export my JAR, the other projects are not exported with that JAR.
How can I export my Main project and "include" the other projects into that same JAR? I'd rather not have several JARs and have to define them into my Classpath on the command line.
Don't do it the hard way. Use Eclipse's own exporter. First ensure that you've the other projects referenced as Projects in the main project's Build Path. Once done that, just rightclick the main project, choose Export and then Java > Runnable JAR file. Choose the launch configuration (which you used to test the main()
class locally) and then you've 3 Library Handling options to package the JAR:
The first option will just repackage the classes of other projects inside the JAR. Everything is plain thrown together.
The second option will copy other projects as JARs inside the JAR. This does normally not work that way, but Eclipse also adds a special launcher which basically copies the embedded JARs into memory, extracts there, adds the files to the classloader and then invoke the main()
with that classloader.
The third option is something you don't want for this particular case.
If I were doing it I'd 'mavenize' the projects, using a maven dependency for each project dependency and then use
mvn assembly:assembly
see http://maven.apache.org/plugins/maven-assembly-plugin/ for more on this plugin
精彩评论