在本教程中,我们将向您展示如何使用Ant构建脚本来创建Jar文件以及如何使用项目的外部库/依赖项。
使用的技术:
- Eclipse 4.2
- JDK 1.7
- 蚂蚁1.9.4
- 蚂蚁常春藤2.4
- 登入1.1.2
- 乔达时间2.5
PS先前的Ant Java项目将被重用。
1.项目结构
图1.1:Eclipse IDE中的最终项目目录结构。
2. Java项目+外部库
在Eclipse IDE中,重新打开以前的Java项目AntDateUtils ,更新源代码以使用logback
和joda-time
。
package com.mkyong.core.utils;
import org.joda.time.LocalDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DateUtils {
private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);
public static void main(String[] args) {
logger.debug("[MAIN] Current Date : {}", getLocalCurrentDate());
System.out.println(getLocalCurrentDate());
}
private static String getLocalCurrentDate() {
LocalDate date = new LocalDate();
return date.toString();
}
}
创建一个logback.xml
并将其放在项目src
文件夹中。 参见图1.1
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
ANT + LogBack : %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
3.常春藤–获取外部库
我们使用Apache Ivy获取项目的外部库/依赖项。
3.1创建此文件ivy.xml
:
<ivy-module version="2.0">
<info organisation="org.apache" module="dateUtilsProject" />
<dependencies>
<dependency org="joda-time" name="joda-time" rev="2.5" />
<dependency org="org.slf4j" name="slf4j-api" rev="1.7.6" />
<dependency org="ch.qos.logback" name="logback-classic" rev="1.1.2" />
</dependencies>
</ivy-module>
3.2更新build.xml
,在其顶部添加ivy名称空间,并执行“ ivy”任务下载ivy模块,并执行“ resolve”任务要求Ivy模块下载外部库。
<project xmlns:ivy="antlib:org.apache.ivy.ant"
name="dateUtilsProject" default="main" basedir=".">
<!-- ivy start -->
<!-- ivy to get dependencies and copy to project lib folder automatically -->
<target name="resolve" description="retrieve dependencies with ivy">
<ivy:retrieve />
</target>
<!-- install ivy -->
<target name="ivy" description="Install ivy">
<mkdir dir="${user.home}/.ant/lib" />
<get dest="${user.home}/.ant/lib/ivy.jar"
src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.4.0-rc1/ivy-2.4.0-rc1.jar" />
</target>
<!-- ivy end -->
</project>
第一次,将Ivy模块从Maven中心存储库下载到本地${user.home}/.ant/lib/ivy.jar
。
$ ant ivy
要下载外部库,请运行任务“ resolve”。 声明的库将下载到项目lib
文件夹中。
$ ant resolve
4. build.xml
查看更新的build.xml
脚本,阅读注释以进行自我解释。
要点
- 使用Apache Ivy管理项目外部库,在顶部查看ivy名称空间,然后执行任务“ resolve”。
- 要编译源代码,您需要声明类路径。 查看任务“编译”和“ classpathref”属性。
- 在“ jar”任务中,构造外部库的整个列表并将其放入
manifest.mf
文件中。 - 在“ jar”任务中,项目jar将打包到文件夹“ dist”中,并将整个外部库从“ lib”复制到“ dist / lib”。
<project xmlns:ivy="antlib:org.apache.ivy.ant"
name="dateUtilsProject" default="main" basedir=".">
<description>
Create a Java Project (JAR) with Ant build script
</description>
<property name="projectName" value="DateUtils" />
<property name="src.dir" location="src" />
<property name="build.dir" location="bin" />
<property name="dist.dir" location="dist" />
<property name="dist.lib.dir" location="dist/lib" />
<property name="lib.dir" value="lib" />
<property name="main-class" value="com.mkyong.core.utils.DateUtils" />
<!-- ivy start -->
<!-- ivy to get dependencies and copy to project lib folder automatically -->
<target name="resolve" description="retrieve dependencies with ivy">
<ivy:retrieve />
</target>
<!-- install ivy -->
<target name="ivy" description="Install ivy">
<mkdir dir="${user.home}/.ant/lib" />
<get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.4.0-rc1/ivy-2.4.0-rc1.jar" />
</target>
<!-- ivy end -->
<target name="init">
<mkdir dir="${build.dir}" />
</target>
<!-- external libraries classpath, we don't need sources and javadoc -->
<path id="classpath">
<fileset dir="${basedir}/">
<include name="${lib.dir}/*.jar" />
<exclude name="${lib.dir}/*sources.jar"/>
<exclude name="${lib.dir}/*javadoc.jar"/>
</fileset>
</path>
<!-- To work with external libraries, need classpath to compile -->
<target name="compile" depends="init" description="compile the source ">
<javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath" />
</target>
<!-- constructs the external libraries classpath name -->
<pathconvert property="classpath.name" pathsep=" ">
<path refid="classpath" />
<mapper>
<chainedmapper>
<flattenmapper />
<globmapper from="*.jar" to="lib/*.jar" />
</chainedmapper>
</mapper>
</pathconvert>
<target name="copy-dependencies">
<copy todir="${dist.lib.dir}">
<fileset dir="${lib.dir}" includes="**/*.jar" excludes="**/*sources.jar, **/*javadoc.jar" />
</copy>
</target>
<!-- jar it, and declares the ext libraries in manifest.mf file -->
<target name="jar" depends="compile, copy-dependencies" description="package, output to JAR">
<echo message="classpath.name : ${classpath.name} " />
<mkdir dir="${dist.dir}" />
<mkdir dir="${dist.lib.dir}" />
<jar jarfile="${dist.dir}/${projectName}.jar" basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}" />
<attribute name="Class-Path" value="${classpath.name}" />
</manifest>
</jar>
</target>
<target name="clean" description="clean up">
<delete dir="${build.dir}" />
<delete dir="${dist.dir}" />
</target>
<!-- Default, run this -->
<target name="main" depends="clean, compile, jar" />
</project>
5.测试
使用Ant构建脚本测试Java项目。
5.1搅拌一下。
$ pwd
/Users/mkyong/Documents/workspace/AntDateUtils
$ ant
Buildfile: /Users/mkyong/Documents/workspace/AntDateUtils/build.xml
clean:
[delete] Deleting directory /Users/mkyong/Documents/workspace/AntDateUtils/bin
[delete] Deleting directory /Users/mkyong/Documents/workspace/AntDateUtils/dist
init:
[mkdir] Created dir: /Users/mkyong/Documents/workspace/AntDateUtils/bin
compile:
[javac] Compiling 1 source file to /Users/mkyong/Documents/workspace/AntDateUtils/bin
copy-dependencies:
[copy] Copying 12 files to /Users/mkyong/Documents/workspace/AntDateUtils/dist/lib
jar:
[echo] classpath.name : ... lib/joda-time-2.5.jar lib/logback-classic-1.1.2.jar lib/logback-core-1.1.2.jar lib/mail-1.4.jar ...
[jar] Building jar: /Users/mkyong/Documents/workspace/AntDateUtils/dist/DateUtils.jar
main:
BUILD SUCCESSFUL
Total time: 1 second
5.2检查生成的jar文件。
$ jar -tf dist/DateUtils.jar
META-INF/
META-INF/MANIFEST.MF
com/
com/mkyong/
com/mkyong/core/
com/mkyong/core/utils/
com/mkyong/core/utils/DateUtils.class
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.4
Created-By: 1.7.0_05-b05 (Oracle Corporation)
Main-Class: com.mkyong.core.utils.DateUtils
Class-Path: lib/activation-1.1.jar lib/commons-compiler-2.6.1.jar lib/
geronimo-jms_1.1_spec-1.0.jar lib/groovy-all-2.0.7.jar lib/janino-2.6
.1.jar lib/joda-convert-1.2.jar lib/joda-time-2.5.jar lib/logback-cla
ssic-1.1.2.jar lib/logback-core-1.1.2.jar lib/mail-1.4.jar lib/servle
t-api-2.5.jar lib/slf4j-api-1.7.6.jar
5.3运行Jar文件。
$ pwd
/Users/mkyong/Documents/workspace/AntDateUtils
$ java -jar dist/DateUtils.jar
16:28:43.957 [main] DEBUG com.mkyong.core.utils.DateUtils - [MAIN] Current Date : 2014-11-21
2014-11-21
5.4使用logback.xml
再次运行Jar文件。
$ pwd
/Users/mkyong/Documents/workspace/AntDateUtils
$ java -jar -Dlogback.configurationFile=src/logback.xml dist/DateUtils.jar
16:34:43,746 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [src/logback.xml] at [file:/Users/mkyong/Documents/workspace/AntDateUtils/src/logback.xml]
//...
ANT + LogBack : DEBUG com.mkyong.core.utils.DateUtils - [MAIN] Current Date : 2014-11-21
2014-11-21
下载源代码
下载它– AntDateUtils-External-Libraries.zip (8 KB)
参考文献
翻译自: https://mkyong.com/ant/ant-how-to-create-a-jar-file-with-external-libraries/