Ant 体验笔记
项目结构如图所示:这是个《gradle实战》里的的demo
一般用ant打jar的时候不用指定程序的入口!这个jar一般是给其他app引用的。但是如果该jar就是程序的启动jar。例如:java -jar abc.jar
这个时候需要指定jar的入口类!也就是指定用哪个类的main方法为入口!否则会报错!所以我修改了build.xml。参考连接https://www.iteye.com/blog/pengyan5945-1562387
build.xml如下:
<project name="my-app" default="dist" basedir="."> <!-- 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 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}" classpath="lib/commons-lang3-3.1.jar" includeantruntime="false"/> </target> <target name="dist" depends="compile" description="generate the distribution"> <!-- Create the distribution directory --> <mkdir dir="${dist}"/> <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --> <jar jarfile="${dist}/my-app-${version}.jar" basedir="${build}"> <!--下面两行我加上的,用来运行jar,当然也可以不用Class-Path,而是在java运行是添加 -Djava.ext.dirs=../lib--> <manifest> <attribute name="Main-Class" value="com.mycompany.app.MainAnt"/> <attribute name="Class-Path" value="../lib/commons-lang3-3.1.jar"/> </manifest> </jar> </target> <target name="clean" description="clean up"> <!-- Delete the ${build} and ${dist} directory trees --> <delete dir="${build}"/> <delete dir="${dist}"/> </target> </project>
package com.mycompany.app; import org.apache.commons.lang3.StringUtils; public class MainAnt { public static void main(String[] args) { System.out.println(StringUtils.capitalize("hello")); } }
运行dist任务后,目录如下
测试结果:
bogon:dist lakeslove$ java -jar my-app-1.0.jar
Hello
当然也可以:
bogon:dist lakeslove$ java -jar -Djava.ext.dirs=../lib my-app-1.0.jar
Hello
如果不用ant,怎么编译运行引用jar包的java文件呢?
bogon:java lakeslove$ javac -Djava.ext.dirs=../../../lib com/mycompany/app/MainAnt.java
bogon:build lakeslove$ java -Djava.ext.dirs=../lib com/mycompany/app/MainAnt
Hello
详细解释:-D<name>=<value>
set a system property(设置一个系统属性),而java.ext.dirs这个系统属性代表的含义就是Path of extension directory or directories(扩展目录路径 )
参考https://www.cnblogs.com/crhdyl/p/5523603.html