Package and run a Java application with spring dep

2019-06-23 16:47发布

I built a stand-alone Java application that has a bunch of dependencies (Apache Commons libs, etc) as well as a dependency on the Spring framework, which in turn has a bunch of dependencies.

I built this in Eclipse, where it runs fine. Now I need to deploy it to production and so I'm trying to figure out the best way to package it with all dependencies and also how to even invoke the thing (it will be invoked from the command line).

The brute-force way would be to export my project as a jar, find all the dependent jars (and their dependencies!), copy them to a common directory, write a shell script that includes every one on the classpath and run it. Obviously that seems stupid and tedious.

Should I use Ant, Maven? Should I package all of the dependent Spring jars into one big file? Any tips on dealing with this would be helpful.

10条回答
【Aperson】
2楼-- · 2019-06-23 17:04

You can use a launcher that sets the classpath correctly. Then, you'll have much more flexibility to package your JARs. You could easily put them all in a directory, which is much cleaner than creating a "bastard" ueber jar ...

One of such launcher is commons-launcher.

查看更多
Emotional °昔
3楼-- · 2019-06-23 17:05

All this Maven love and no one mentions Ivy? Oh dear!

Well, here's Ant Ivy and here's comparison to Maven2. Please take a look at it too before going with almost everyone is suggesting.

查看更多
对你真心纯属浪费
4楼-- · 2019-06-23 17:09

You could also use Gradle and here is how you can do it there:

mainClassName = "my.App"

task runnableJar(type: Jar, dependsOn: [':assemble']) {
    from files(sourceSets.main.output.classesDir)
    from files(sourceSets.main.output.resourcesDir)
    from configurations.runtime.asFileTree.files.collect { zipTree(it) }

    manifest {
        attributes 'Main-Class': mainClassName
    }
}
查看更多
SAY GOODBYE
5楼-- · 2019-06-23 17:11

A lot of your concerns disappear if you make your JAR executable and add a manifest including ClassPath. You do have to ZIP the JAR and its dependencies together, unfortunately, but your CLASSPATH problems are taken care of.

What about creating an OSGi bundle? I haven't looked into it myself yet, but it's supposed to be the next thing for packaging Java apps. It's used in app servers like Spring's dm Server. Perhaps it can help you as well.

查看更多
We Are One
6楼-- · 2019-06-23 17:17

With Ant you could do something along the following:

  1. Group all dependencies together.
  2. Package a JAR-file with your application, and embed references to the dependencies in the MANIFEST.MF-file with the "Class-Path"-directive, inside the jar. That should eliminate the need for constructing the classpath at launch.
  3. Bundle it all together as an archive, and distribute it. It should be sufficient to run the jar directly with "java -jar your.jar".

For managing your dependencies, you could go with Ivy. Much like Maven, it is able to retrieve dependencies at build time from whichever repositories you configure, including Maven repositories or your own private repository.

查看更多
神经病院院长
7楼-- · 2019-06-23 17:17

We are using Maven 2 with m2eclipse plugin; works pretty good. Plugin has a dependency graph view, will show conflicts etc.

Now, if you want to package all of your dependencies into the single jar, you can check out Jar Jar Links (no kidding!).

查看更多
登录 后发表回答