ant Hello World

仓库地址

https://gitee.com/bzrj/ant-study/tree/master/ant-hello

参考资料

APACHE ANT编写BUILD.XML的自动提示 ANT DTD

关于ant -javac的 includeAntRuntime 属性

升级Intellij IDEA 2023.3后找回Ant构建工具

信步漫谈之Ant—打包可运行的Jar包(加入第三方jar包)

运行效果

E:\gitee\bzrj\ant-study\ant-hello>dir
 驱动器 E 中的卷是 文档
 卷的序列号是 EDE0-BB33

 E:\gitee\bzrj\ant-study\ant-hello 的目录

2024/07/04  01:07    <DIR>          .
2024/07/04  01:07    <DIR>          ..
2024/07/03  23:52    <DIR>          .externalToolBuilders
2024/07/04  00:07                49 .gitignore
2024/07/04  00:45    <DIR>          .idea
2024/07/04  00:00               557 ant-hello.iml
2024/07/03  23:24           215,327 ant_DTD.dtd
2024/07/04  01:05    <DIR>          build
2024/07/04  01:07             4,390 build.xml
2024/07/04  01:05    <DIR>          dist
2024/07/03  23:30    <DIR>          src
               4 个文件        220,323 字节
               7 个目录 67,938,680,832 可用字节

E:\gitee\bzrj\ant-study\ant-hello>ant exec
Buildfile: E:\gitee\bzrj\ant-study\ant-hello\build.xml

pre-compile:

compile:
    [javac] Compiling 1 source file to E:\gitee\bzrj\ant-study\ant-hello\build\classes

exec:
     [java] thresh is running...

BUILD SUCCESSFUL
Total time: 0 seconds

E:\gitee\bzrj\ant-study\ant-hello>ant
Buildfile: E:\gitee\bzrj\ant-study\ant-hello\build.xml

pre-compile:

compile:
    [javac] Compiling 1 source file to E:\gitee\bzrj\ant-study\ant-hello\build\classes

archive:
      [jar] Building jar: E:\gitee\bzrj\ant-study\ant-hello\dist\thresh.jar

BUILD SUCCESSFUL
Total time: 0 seconds

E:\gitee\bzrj\ant-study\ant-hello>cd dist

E:\gitee\bzrj\ant-study\ant-hello\dist>java -jar thresh.jar
thresh is running...

E:\gitee\bzrj\ant-study\ant-hello\dist>

build.xml

<?xml version="1.0"?>
<!DOCTYPE project [
        <!ENTITY ant_dtd SYSTEM "ant_DTD.dtd">
        ]>
<project name="ant hello world" default="archive">

    <!--
       定义属性
    -->
    <!-- 源码目录 -->
    <property name="src.dir" value="src"/>
    <property name="src.main.dir" value="${src.dir}/main"/>
    <property name="src.main.java.dir" value="${src.main.dir}/java"/>
    <!-- 构建目录 -->
    <property name="build.dir" value="build"/>
    <!-- 编译目录 -->
    <property name="build.classes.dir" value="${build.dir}/classes"/>
    <!-- 发布目录 -->
    <property name="dist.dir" value="dist"/>
    <!-- jar 包名称 -->
    <property name="finalName" value="thresh.jar"/>
    <property name="mainclass" value="com.laolang.thresh.ThreshApplication"/>
    <property name="project-version" value="0.1"/>

    <!-- 一个测试 target -->
    <target name="hello">
        <echo>Hello World - Welcome to Apache Ant!</echo>
    </target>

    <!-- 打印内置属性 -->
    <target name="print-system-property">
        <!-- 用于项目基础的绝对路径 -->
        <echo>basedir:${basedir}</echo>
        <!-- 用于构建文件的绝对路径 -->
        <echo>ant.file:${ant.file}</echo>
        <!-- 用于Ant的版本 -->
        <echo>ant.version:${ant.version}</echo>
        <!-- 它包含当前正在执行的项目的名称 -->
        <echo>ant.project.name:${ant.project.name}</echo>
        <!-- 它包含当前正在执行的项目的默认目标的名称 -->
        <echo>ant.project.default-target:${ant.project.default-target}</echo>
        <!-- 调用当前项目时的目标列表 -->
        <echo>ant.project.invoked-targets:${ant.project.invoked-targets}</echo>
        <!-- 拥有的JVM版本 -->
        <echo>ant.java.version:${ant.java.version}</echo>
        <!-- ant.jar 文件的绝对路径 -->
        <echo>ant.core.lib:${ant.core.lib}</echo>
        <!-- 包含Ant的主目录 -->
        <echo>ant.home:${ant.home}</echo>
        <!-- 包含用于加载Ant的jar的目录 -->
        <echo>ant.library.dir:${ant.library.dir}</echo>
    </target>

    <!-- 打印自定义属性 -->
    <target name="print-custom-property">
        <echo>src.dir:${src.dir}</echo>
        <echo>src.main.dir:${src.main.dir}</echo>
        <echo>src.main.java.dir:${src.main.java.dir}</echo>
        <echo>build.dir:${build.dir}</echo>
        <echo>build.classes.dir:${build.classes.dir}</echo>
        <echo>dist.dir:${dist.dir}</echo>
        <echo>finalName:${finalName}</echo>
    </target>

    <!-- 打印所有属性,展示另一种 target 依赖关系管理方式 -->
    <target name="print-property">
        <antcall target="print-system-property"/>
        <antcall target="print-custom-property"/>
    </target>

    <!-- 打印 ant dtd -->
    <target name="makeantdtd">
        <antstructure output="ant_DTD.dtd"/>
    </target>

    <!-- 预编译,创建目录 -->
    <target name="pre-compile">
        <mkdir dir="${src.dir}"/>
        <mkdir dir="${src.main.dir}"/>
        <mkdir dir="${src.main.java.dir}"/>
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${build.classes.dir}"/>
        <mkdir dir="${dist.dir}"/>
    </target>

    <!-- 编译 -->
    <target name="compile" depends="pre-compile">
        <javac srcdir="${src.dir}" destdir="${build.classes.dir}" includeantruntime="false"/>
    </target>

    <!-- 运行 -->
    <target name="exec" depends="compile">
        <java classname="${mainclass}" classpath="${build.classes.dir}"/>
    </target>

    <!-- 打包 -->
    <target name="archive" depends="compile">
        <!-- 时间戳 -->
        <tstamp>
            <format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss"/>
        </tstamp>

        <!-- 清单文件 -->
        <manifest file="${build.dir}/MANIFEST.MF">
            <attribute name="Built-By" value="${user.name}"/>
            <attribute name="Implementation-Version" value="${project-version}"/>
            <attribute name="Built-Date" value="${TODAY}"/>
            <attribute name="Main-Class" value="${mainclass}"/>
        </manifest>
        <jar destfile="${dist.dir}/${finalName}" basedir="${build.classes.dir}" manifest="${build.dir}/MANIFEST.MF"/>
    </target>

    <!-- 清理 -->
    <target name="clean">
        <delete dir="${build.dir}"/>
        <delete dir="${dist.dir}"/>
    </target>
</project>
posted @ 2024-07-04 01:09  潼关路边的一只野鬼  阅读(8)  评论(0编辑  收藏  举报