之路 on it Road.com

build.xml 文件

这是构建文件示例,其中包含将应用程序打包为 jar 文件所需的所有参数。
根据要求随意修改它。

<project name="MyApplication" default="dist" basedir=".">
	<description>
    simplest example build file
  </description>
	<!-- set global properties for this build -->
	<property name="src" location="src" />
	<property name="build" location="build" />
	<property name="dist" location="dist" />
	<property name="version" value="1.0" />
	<target name="init">
		<!-- Create the time stamp -->
		<tstamp />
		<!-- Create the build directory structure used by compile -->
		<mkdir dir="${build}" />
	</target>
	<target name="compile" depends="init" description="compile the source">
		<!-- Compile the java code from ${src} into ${build} -->
		<javac srcdir="${src}" destdir="${build}" />
	</target>
	<target name="dist" depends="compile" description="generate the distribution">
		<buildnumber />
		<!-- Create the distribution directory -->
		<mkdir dir="${dist}/lib" />
		<!-- Put everything in ${build} into the MyApplication-${version}.${build.number}.jar -->
		<jar destfile="${dist}/lib/MyApplication-${version}.${build.number}.jar" basedir="${build}" />
	</target>
	<target name="clean" description="clean up">
		<!-- Delete the ${build} and ${dist} directory trees -->
		<delete dir="${build}" />
		<delete dir="${dist}" />
	</target>
</project>

运行ANT构建

要运行构建,我们有两个选择

  • 从命令提示符运行。要运行的命令是:
$ ant -f build.xml
  • 从eclipse运行。选择该菜单选项。

右键点击 build.xml -> Run as -> Ant build

使用任何或者以上方法将运行构建脚本,我们将在控制台/命令提示符中看到此构建过程输出。

Buildfile: C:\Users\lokesh\workspace\MyApplication\build.xml
init:
    [mkdir] Created dir: C:\Users\lokesh\workspace\MyApplication\build
compile:
    [javac] C:\Users\lokesh\workspace\MyApplication\build.xml:21: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 1 source file to C:\Users\lokesh\workspace\MyApplication\build
dist:
    [mkdir] Created dir: C:\Users\lokesh\workspace\MyApplication\dist\lib
      [jar] Building jar: C:\Users\lokesh\workspace\MyApplication\dist\lib\MyApplication-1.0.0.jar
BUILD SUCCESSFUL
Total time: 4 seconds

我们可以在 dist/lib文件夹中验证生成的 jar 文件(或者如果我们更改了路径,则检查该位置)。

创建要构建的应用程序

我在 eclipse 中创建了一个带有单个类文件的 java 项目。
这个类只有 main()方法,它打印“Hello World !!” 作为控制台的输出。

package com.onitroad.demo;
public class MyClass {
	public static void main(String[] args) {
		System.out.println("Hello World !!");
	}
}
使用ANT为 Java 项目构建 jar 文件

安装ANT

  • 从 ant.apache.org 下载 Apache Ant。
  • 将 zip 文件解压缩到我们选择的目录结构中。
  • ANT_HOME环境变量设置为此位置。
  • 在你的 PATH环境变量中包含 %ANT_HOME%/bin目录。
  • 通过打开命令行并在推荐行中输入“ant -version”来检查安装。
$ ant -version
Apache Ant(TM) version 1.10.2 compiled on February 3 2018
日期:2020-09-17 00:10:39 来源:oir作者:oir