Use ANT and Maven to create executable jar with dependent library

来源:互联网 发布:安易数据恢复软件免费 编辑:程序博客网 时间:2024/06/07 00:56

As we all know, we can use java jar command and IDE (Eclipse, Net-Bean and etc) export tools to generate the java jar file. Sometime, it is more convenient and maintainable to use  ANT and Maven to do this task.

ANT demo:

<?xml version="1.0" encoding="UTF-8"?><project name="MyProject" default="jar">  <!-- Name of the output .jar file -->  <property name="jar.name" value="jar_name.jar" />  <!-- Base directory for distribution target -->  <property name="deploy.home" value="." />  <!-- Base directory for compilation targets -->  <property name="build.home" value="." />  <!-- Main class -->  <property name="main.class" value="my.path.to.the.main.Application" />   <!-- The base directory for all libraries (jar) files -->  <property name="lib.home" value="lib" />  <target name="jar" description="Create jar and MANIFEST.MF">    <pathconvert property="libs.project" pathsep=" ">      <mapper>        <chainedmapper>          <!-- remove absolute path -->          <flattenmapper />          <!-- add lib/ prefix -->          <globmapper from="*" to="lib/*" />        </chainedmapper>      </mapper>      <path>        <!-- lib.home contains all jar files,                                         in several subdirectories -->        <fileset dir="${lib.home}">          <include name="**/*.jar" />        </fileset>      </path>    </pathconvert>    <!-- create the jar -->    <jar jarfile="${deploy.home}/${jar.name}" basedir="${build.home}/classes">      <manifest>        <attribute name="Built-By" value="${user.name}" />        <attribute name="Main-Class" value="${main.class}" />        <!-- Finally, use the generated libs path -->        <attribute name="Class-Path" value="${libs.project}" />      </manifest>    </jar>  </target></project>
Maven sample

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.wonder.core</groupId><artifactId>superman</artifactId><packaging>jar</packaging><version>1.0</version><name>superman</name><url>http://maven.apache.org</url> <properties><jdk.version>1.6</jdk.version><log4j.version>1.2.17</log4j.version></properties> <dependencies> <dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>${log4j.version}</version></dependency> </dependencies> <build><plugins>  <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>${jdk.version}</source><target>${jdk.version}</target></configuration>  </plugin>   <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>2.4</version><configuration><archive>  <manifest><addClasspath>true</addClasspath><mainClass>com.wonder.core.App</mainClass><classpathPrefix>dependency-jars/</classpathPrefix>  </manifest></archive></configuration>  </plugin>   <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><version>2.5.1</version><executions>  <execution><id>copy-dependencies</id><phase>package</phase><goals><goal>copy-dependencies</goal></goals><configuration><includeGroupIds>log4j</includeGroupIds><outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory></configuration>  </execution> </executions></plugin> </plugins></build> </project>


原创粉丝点击