Use ANT and Maven to create executable jar with dependent library

本文详细介绍了如何使用ANT和Maven工具来生成Java JAR文件,包括配置示例和关键步骤解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值