Package and run the java application with spring dependencies

I built a standalone Java application that contains many dependencies (Apache Commons libs, etc.), as well as a dependency on the Spring framework, which in turn contains many dependencies.

I built this in Eclipse where it works fine. Now I need to deploy it for production, and so I'm trying to find the best way to package it with all the dependencies, as well as how to even call this thing (it will be called from the command line).

The brute force path is to export my project as a jar, find all the dependent jars (and their dependencies!), Copy them to the general directory, write a shell script that includes everything in the class path and run it. Obviously, this seems silly and tedious.

Should I use Ant, Maven? Should I pack all dependent Spring banks in one big file? Any advice on working with this would be helpful.

+5
source share
10 answers

Deploying Java is still stupid and tedious in 2009. So the usual option is to write this little script. If you put all your JARs in one directory (for example, lib\), you can use a generic script that automatically creates the class path (see this blog post ). I suggest adding cd /d %~dp0cd to the script directory at the beginning.

Maven 2 , " JAR" ( mvn: ). , DB2 JCC ( "COM.ibm.", com.ibm. → ClassNotFoundException).

, ANT build.xml, JAR " JAR" .

+4

Ant - :

  • .
  • JAR MANIFEST.MF "Class-Path" . path .
  • . java -jar your.jar.

Ivy. Maven, , Maven .

+2

, Maven Assembly. . , . Spring Integration, . .

+1

Maven ? , !

, Ant Ivy Maven2. , , , .

+1

, . JAR. , , "" ueber jar...

- commons-launcher.

0

Maven 2 m2eclipse ; . , ..


, , Jar Jar Links ( !).

0

, JAR , ClassPath. , , JAR , CLASSPATH .

OSGi? , Java. , Spring dm Server. , .

0

ANT script *.bat *.sh JAR lib.

, :

<path id="jar-classpath">
    <fileset dir="${build.war.dir}/WEB-INF/lib">
        <include name="**/*.jar" />
    </fileset>
</path>

<pathconvert refid="jar-classpath" targetos="unix" property="exec.classpath.unix">
       <map from="${build.war.dir}\WEB-INF\lib\" to="../lib/"/>
</pathconvert>

<pathconvert refid="jar-classpath" targetos="windows" property="exec.classpath.windows">
        <map from="${build.war.dir}\WEB-INF\lib\" to="../lib/"/>
</pathconvert>

${exec.classpath.unix} ${exec.classpath.windows} java.

0
0

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

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
    }
}
0
source

All Articles