Advertisement
justin_hanekom

Ant sample XML

Feb 1st, 2025
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 10.72 KB | None | 0 0
  1. <!--
  2.  File: build.xml
  3.  SPDX-License-Identifier: Unlicense
  4. -->
  5.  
  6. <project xmlns:ivy="antlib:org.apache.ivy.ant"
  7.    name="Project" default="all" basedir=".">
  8.  
  9.     <description>
  10.         Ant build-file.
  11.     </description>
  12.  
  13.     <property file="build.properties"/>
  14.  
  15.     <!-- ==================== PROPERTIES ==================== -->
  16.  
  17.     <!-- Project Directories -->
  18.     <property name="src.dir"            location="src"/>
  19.     <property name="main.dir"           location="${src.dir}/main"/>
  20.     <property name="main-src.dir"       location="${main.dir}/java"/>
  21.     <property name="main-resources.dir" location="${main.dir}/resources"/>
  22.     <property name="build.dir"          location="target"/>
  23.     <property name="log.dir"            location="${build.dir}/logs"/>
  24.     <property name="build-main.dir"     location="${build.dir}/main"/>
  25.     <property name="main-classes.dir"   location="${build-main.dir}/classes"/>
  26.     <property name="test.dir"           location="${src.dir}/test"/>
  27.     <property name="test-src.dir"       location="${test.dir}/java"/>
  28.     <property name="test-resources.dir" location="${test.dir}/resources"/>
  29.     <property name="build-test.dir"     location="${build.dir}/test"/>
  30.     <property name="test-classes.dir"   location="${build-test.dir}/classes"/>
  31.     <property name="lib.dir"            location="lib"/>
  32.  
  33.     <!-- Compiler Properties -->
  34.     <fileset id="main.libs" dir="${lib.dir}">
  35.         <exclude name="**/common-math*.jar,**/junit*.jar,**/hamcrest-core*.jar"/>
  36.     </fileset>
  37.     <path id="main.classpath">
  38.         <pathelement location="${main-classes.dir}"/>
  39.         <fileset refid="main.libs"/>
  40.     </path>
  41.  
  42.     <!-- Ivy Properties -->
  43.     <!-- https://repo1.maven.org/maven2/org/apache/ivy/ivy/2.5.0/ -->
  44.     <property name="ivy-get.src"
  45.        value="https://org/apache/ivy/ivy/${ivy.version}/ivy-${ivy.version}.jar"/>
  46.     <property name="ivy-get.dest" value="${build.dir}/ivy.jar"/>
  47.  
  48.     <!-- Package Properties -->
  49.     <property name="jar.filename-prefix"       value="${ant.project.name}-${project.version}"/>
  50.     <property name="jar.dest-filename"         value="${jar.filename-prefix}.jar"/>
  51.     <property name="jar.dest-fqfn"             value="${build.dir}/${jar.dest-filename}"/>
  52.     <property name="jar.all-filename"          value="${jar.filename-prefix}-all.jar"/>
  53.     <property name="jar.all-fqfn"              value="${build.dir}/${jar.all-filename}"/>
  54.  
  55.     <!-- Test Properties -->
  56.     <fileset id="test.libs" dir="${lib.dir}"/>
  57.     <path id="test.classpath">
  58.         <pathelement location="${main-classes.dir}"/>
  59.         <pathelement location="${test-classes.dir}"/>
  60.         <fileset refid="test.libs"/>
  61.     </path>
  62.  
  63.     <!-- ==================== TARGETS ==================== -->
  64.  
  65.     <!-- =============== activate-ivy =============== -->
  66.     <target name="activate-ivy" depends="download-ivy">
  67.         <path id="ivy-lib.path">
  68.             <fileset dir="${build.dir}" includes="*.jar"/>
  69.         </path>
  70.         <taskdef resource="org/apache/ivy/ant/antlib.xml"
  71.            uri="antlib:org.apache.ivy.ant"
  72.            classpathref="ivy-lib.path"/>
  73.     </target>
  74.  
  75.     <!-- =============== all =============== -->
  76.     <target name="all" depends="build"
  77.        description="Compiles, tests, and packages the project.">
  78.     </target>
  79.  
  80.     <!-- =============== build =============== -->
  81.     <target name="build" depends="jar"
  82.        description="Packages the class files and any resources into an executable JAR.">
  83.  
  84.         <!--Expand the non-transitive JARs into build/temp -->
  85. <!--
  86.        <mkdir dir="${build.dir}/temp"/>
  87.        <unzip dest="${build.dir}/temp"
  88.            overwrite="true">
  89.            <zipfileset dir="${lib.dir}">
  90.                <exclude name="**/common-math*.jar"/>
  91.                <exclude name="**/lombok*.jar"/>
  92.                <exclude name="**/junit*.jar"/>
  93.                <exclude name="**/hamcrest-core*.jar"/>
  94.                <exclude name="**/*javadoc*"/>
  95.                <exclude name="**/*sources*"/>
  96.            </zipfileset>
  97.        </unzip>
  98. -->
  99.  
  100.         <!-- Expand the project JAR into build/temp -->
  101.         <unzip src="${jar.dest-fqfn}"
  102.            dest="${build.dir}/temp"
  103.            overwrite="true">
  104.         </unzip>
  105.  
  106.         <!-- Remove cruft -->
  107.         <delete>
  108.             <fileset dir="${build.dir}/temp/META-INF"/>
  109.                 <!--            <fileset dir="${build.dir}/temp/OSGI-OPT"/> -->
  110.             <fileset dir="${build.dir}/temp" includes="LICENSE"/>
  111.             <fileset dir="${build.dir}/temp" includes="NOTICE"/>
  112.             <fileset dir="${build.dir}/temp" includes="*.html"/>
  113.         </delete>
  114.  
  115.         <!-- Create the executable JAR file -->
  116.         <jar destfile="${jar.all-fqfn}"
  117.            basedir="${build.dir}/temp"
  118.            compress="${jar.compress}"
  119.            duplicate="${jar.duplicate}"
  120.            index="${jar.index}"
  121.            indexMetaInf="${jar.index-meta-inf}">
  122.             <manifest>
  123.                 <attribute name="Main-Class" value="${project.main-class}"/>
  124.             </manifest>
  125.         </jar>
  126.  
  127. <!--
  128.        <chmod file="${jar.all-fqfn}"
  129.            perm="ug+x"/>
  130.        <delete dir="build/temp"/>
  131. -->
  132.     </target>
  133.  
  134.     <!-- =============== clean =============== -->
  135.     <target name="clean"
  136.        description="Removes generated class files.">
  137.         <delete dir="${build-main.dir}"/>
  138.         <delete dir="${build-test.dir}"/>
  139.     </target>
  140.  
  141.     <!-- =============== clobber =============== -->
  142.     <target name="clobber"
  143.        description="Deletes the build directory.">
  144.         <delete dir="${build.dir}"/>
  145.     </target>
  146.  
  147.     <!-- =============== compile =============== -->
  148.     <target name="compile" depends="compile-main,compile-tests"
  149.        description="Compiles the main and test source code."/>
  150.  
  151.     <!-- =============== compile-main =============== -->
  152.     <target name="compile-main" depends="init"
  153.        description="Compiles the main source code.">
  154.         <javac srcdir="${main-src.dir}"
  155.            destdir="${main-classes.dir}"
  156.            source="${javac.source}"
  157.            target="${javac.target}"
  158.            deprecation="${javac.deprecation}"
  159.            includeantruntime="${javac.ant-runtime}"
  160.            fork="${javac.fork}"
  161.            listfiles="${javac.list-files}">
  162.             <classpath refid="main.classpath"/>
  163.         </javac>
  164.     </target>
  165.  
  166.     <!-- =============== compile-tests =============== -->
  167.     <target name="compile-tests" depends="compile-main"
  168.        description="Compiles the test source code.">
  169.         <javac srcdir="${test-src.dir}"
  170.            destdir="${test-classes.dir}"
  171.            source="${javac.source}"
  172.            target="${javac.target}"
  173.            deprecation="${javac.deprecation}"
  174.            includeantruntime="${javac.ant-runtime}"
  175.            fork="${javac.fork}"
  176.            listfiles="${javac.list-files}">
  177.             <classpath refid="test.classpath"/>
  178.         </javac>
  179.     </target>
  180.  
  181.     <!-- =============== download-dependencies =============== -->
  182.     <target name="download-dependencies" depends="activate-ivy"
  183.        description="Downloads JAR dependencies.">
  184.         <ivy:retrieve/>
  185.     </target>
  186.  
  187.     <!-- =============== download-ivy =============== -->
  188.     <target name="download-ivy" depends="init-dirs">
  189.         <get src="${ivy-get.src}"
  190.            dest="${ivy-get.dest}"
  191.            verbose="${ivy-get.verbose}"
  192.            ignoreerrors="${ivy-get.ignore-errors}"
  193.            usetimestamp="${ivy-get.use-timestamp}"/>
  194.     </target>
  195.  
  196.     <!-- =============== init =============== -->
  197.     <target name="init" depends="init-dirs,download-dependencies"/>
  198.  
  199.     <!-- =============== init-dirs =============== -->
  200.     <target name="init-dirs"
  201.        description="Creates the project directory structure.">
  202.         <mkdir dir="${main-src.dir}"/>
  203.         <mkdir dir="${main-resources.dir}"/>
  204.         <mkdir dir="${main-classes.dir}"/>
  205.         <mkdir dir="${test-src.dir}"/>
  206.         <mkdir dir="${test-resources.dir}"/>
  207.         <mkdir dir="${test-classes.dir}"/>
  208.         <mkdir dir="${log.dir}"/>
  209.         <mkdir dir="${build.dir}"/>
  210.         <mkdir dir="${lib.dir}"/>
  211.     </target>
  212.  
  213.     <!-- =============== jar =============== -->
  214.     <target name="jar" depends="test">
  215.  
  216.         <!-- Copy any resources and files into the main classes directory -->
  217.         <copy todir="${main-classes.dir}"
  218.            preservelastmodified="${copy.preserve-modified}"
  219.            overwrite="${copy.overwrite}"
  220.            force="${copy.force}"
  221.            flatten="${copy.flatten}"
  222.            includeemptydirs="{copy.include-empty-init-dirs}">
  223.             <fileset dir="${main-resources.dir}"/>
  224.         </copy>
  225.         <copy todir="${main-classes.dir}"
  226.            preservelastmodified="${copy.preserve-modified}"
  227.            overwrite="${copy.overwrite}"
  228.            force="${copy.force}"
  229.            flatten="${copy.flatten}"
  230.            includeemptydirs="{copy.include-empty-init-dirs}">
  231.             <fileset file="COPYING"/>
  232.         </copy>
  233.         <copy todir="${main-classes.dir}"
  234.            preservelastmodified="${copy.preserve-modified}"
  235.            overwrite="${copy.overwrite}"
  236.            force="${copy.force}"
  237.            flatten="${copy.flatten}"
  238.            includeemptydirs="{copy.include-empty-init-dirs}">
  239.             <fileset file="README.md"/>
  240.         </copy>
  241.  
  242.         <!-- Now create a JAR of the main classes directory -->
  243.         <jar destfile="${jar.dest-fqfn}"
  244.            basedir="${main-classes.dir}"
  245.            compress="${jar.compress}"
  246.            duplicate="${jar.duplicate}"
  247.            index="${jar.index}"
  248.            indexMetaInf="${jar.index-meta-inf}">
  249.         </jar>
  250.     </target>
  251.  
  252.     <!-- =============== run =============== -->
  253.     <target name="run" depends="build"
  254.        description="Creates and executes the executable JAR.">
  255.         <java jar="${jar.all-fqfn}" fork="${java.fork}"/>
  256.     </target>
  257.  
  258.     <!-- =============== test =============== -->
  259.     <target name="test" depends="compile-tests"
  260.        description="Runs JUnit tests of the projects main Java files.">
  261.         <junit printsummary="${test.print-summary}"
  262.            fork="${test.fork}"
  263.            forkmode="${test.fork-mode}"
  264.            haltonerror="${test.halt-on-error}"
  265.            haltonfailure="${test.halt-on-failure}"
  266.            includeantruntime="${test.include-ant-runtime}"
  267.            showoutput="yes">
  268.             <classpath refid="test.classpath"/>
  269.             <formatter type="${test.formatter}"/>
  270.             <batchtest todir="${log.dir}">
  271.                 <fileset dir="${test-src.dir}">
  272.                     <include name="**/*Test*.java"/>
  273.                 </fileset>
  274.             </batchtest>
  275.         </junit>
  276.     </target>
  277. </project>
  278.  
Tags: xml build Ant
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement