ວິທີການເພື່ອເຮັດໃຫ້ຂະບວນການກໍ່ສ້າງຂອງທ່ານໂດຍໃຊ້ Java ແລະ Ant?

Automate build with Ant

ໂດຍອັດໂນມັດການກໍ່ສ້າງທີ່ມີຈໍາພວກກິນມົດ

ພາບ​ລວມ:

ໃນເອກະສານນີ້ພວກເຮົາຈະປຶກສາຫາລືກ່ຽວກັບມົດສ້າງເຄື່ອງມືແລະການນໍາໃຊ້ນີ້ວິທີທີ່ເຮົາສາມາດເຮັດໃຫ້ການກໍ່ສ້າງກົນໄກສໍາລັບຄໍາຮ້ອງສະຫມັກ java ຕາມ. ໃນຈໍາພວກກິນມົດຍຸກປັດຈຸບັນໄດ້ກາຍເປັນສ່ວນຫນຶ່ງຂອງການພັດທະນາ java. ລູກຄ້າສ່ວນຫຼາຍມັກຈະມີການກໍ່ສ້າງ script ອັດຕະໂນມັດເປັນສ່ວນຫນຶ່ງຂອງລະຫັດທີ່ມາສົ່ງໄດ້. ເຫດຜົນສໍາລັບການນີ້ແມ່ນນາທີ່ສະຫນອງໃຫ້ໂດຍພວກກິນມົດໄດ້ດັ່ງນັ້ນໃຜທີ່ມີຄວາມຮູ້ການຂຽນສະຄິບພຽງເລັກນ້ອຍສາມາດສ້າງຄູ່ deployable ໂດຍບໍ່ມີການຮູ້ຄໍາຮ້ອງສະຫມັກໃນລາຍລະອຽດ.

ການ​ນໍາ​ສະ​ເຫນີ:
Apache ant is a generic build tool. ຊື່ ANT ຈໍາຫຍໍ້ສໍາລັບ 'ອີກປະການຫນຶ່ງເຄື່ອງມື Neat'. ເຄື່ອງມືນີ້ແມ່ນຄ້າຍຄືກັນກັບ "ຍີ່ຫໍ້ 'ຜົນປະໂຫຍດໃນ UNIX ແຕ່ຖືກປະຕິບັດນໍາໃຊ້ Java. ມັນຖືກນໍາໃຊ້ຕົ້ນຕໍສໍາລັບການກໍ່ສ້າງໄບນາລີຂອງ java ຕາມລະຫັດທີ່ມາແລະການນໍາໃຊ້ຄູ່ສ້າງຂຶ້ນເພື່ອເປັນເຄື່ອງແມ່ຂ່າຍຂອງຄໍາຮ້ອງສະຫມັກທີ່ເປັນ predefined. It can also be used to generate javadocs for a code base and to execute the unit test suite for the whole codebase. Ant in collaboration with JUNIT helps developer to follow the test driven development approach.

Ant requires Java compiler to be installed having the environment variable JAVA_HOME set with its adequate value. Ant uses an XML file to define the build procedure. ຊື່ໃນຕອນຕົ້ນຂອງເອກະສານນີ້ແມ່ນ build.xml. ການພັດທະນາບາງຄົນຍັງນໍາໃຊ້ຄຸນສົມບັດທີ່ຊື່ໄຟ build.properties ເພື່ອກໍານົດຄຸນສົມບັດບາງ e.g. the build version number and other environmental parameters which are required to change from time to time based on the need.

Installation:
FEDORA: On RHEL, ANT can be installed using the command – yum install ant or it can be installed using the rpm – i command. In this case you should download the ant package first.

Debian: On Ubuntu, ANT can be installed using the command – apt-get install ant.

Windows: On windows environment we need to download the ant_<VERSION_NO>.zip file. This zip bundle should be extracted in a folder. This folder should be defined as an environment variable ANT_HOME.

A sample build.xml file is shown as under:

LISTING 1: Sample build.xml file

[Code]

<?xml version=”1.0″?>

<project name=”Ant-Test” default=”ຕົ້ນ​ຕໍ” basedir=”.”>

<!– Sets variables which can later be used. –>

<!– The value of a property is accessed via ${} –>

<property name=”src.dir” location=”src” />

<property name=”build.dir” location=”bin” />

<property name=”dist.dir” location=”dist” />

<property name=”docs.dir” location=”docs” />

<!– Deletes the existing build, docs and dist directory–>

<target name=”clean”>

<delete dir=”${build.dir}” />

<delete dir=”${docs.dir}” />

<delete dir=”${dist.dir}” />

</target>

<!– Creates the build, docs and dist directory–>

<target name=”makedir”>

<mkdir dir=”${build.dir}” />

<mkdir dir=”${docs.dir}” />

<mkdir dir=”${dist.dir}” />

</target>

<!– Compiles the java code (including the usage of library for JUnit –>

<target name=”compile” depends=”clean, makedir”>

<javac srcdir=”${src.dir}” destdir=”${build.dir}”>

</javac>

</target>

<!– Creates Javadoc –>

<target name=”docs” depends=”compile”>

<javadoc packagenames=”src” sourcepath=”${src.dir}” destdir=”${docs.dir}”>

<!– Define which files / directory should get included, we include all –>

<fileset dir=”${src.dir}”>

<include name=”**” />

</fileset>

</javadoc>

</target>

<!–Creates the deployable jar file –>

<target name=”jar” depends=”compile”>

<jar destfile=”${dist.dir}\in.kolkalta.build.test.ant.jar” basedir=”${build.dir}”>

<manifest>

<attribute name=”Main-Class” value=”test.Main” />

</manifest>

</jar>

</target>

<target name=”ຕົ້ນ​ຕໍ” depends=”compile, jar, docs”>

<description>Main target</description>

</target>

</project>

[/Code]

ອັດຕະໂນມັດການນໍາໃຊ້ ANT:
Ant ໃຫ້ການຊ່ວຍເຫຼືອທີ່ຍິ່ງໃຫຍ່ເພື່ອການພັດທະນາ java. ເກືອບທຸກການພັດທະນາ java ຈະຕ້ອງການທີ່ຈະມີ script ທີ່ສາມາດໃຊ້ເວລາດູແລຂອງແລ່ນທົດສອບຫນ່ວຍບໍລິການແລະການສ້າງເອກະສານແຈກຢາຍຄູ່ທີ່ສາມາດໄດ້ຮັບການປະຕິບັດກ່ຽວກັບເຄື່ອງແມ່ຂ່າຍຂອງຄໍາຮ້ອງສະຫມັກທີ່ເປັນ. Ant ຍັງໄດ້ສະຫນອງວິທີການເພື່ອດໍາເນີນການອັກອັດຕະໂນມັດທີ່ຖືກນໍາໃຊ້ເພື່ອປະຕິບັດການທົດສອບຫນ່ວຍເທິງຖານລະຫັດ. Thus, ມັນໃຫ້ການຊ່ວຍເຫຼືອການພັດທະນາຜູ້ທີ່ນໍາໃຊ້ການທົດສອບການຂັບເຄື່ອນການພັດທະນາ. As we know in test driven development methodology JUNIT is a compulsion, ມັນເປັນສະເຫມີເປັນປະໂຫຍດເພີ່ມຂຶ້ນຖ້າຫາກວ່າພວກເຮົາມີ script ອັດຕະໂນມັດທີ່ເຮັດວຽກທັງຫມົດກໍລະນີການທົດສອບ junit ໃນຫນຶ່ງໄປ. ບໍ່ພຽງແຕ່ວ່າ script ນີ້ຍັງສາມາດຖືກນໍາໃຊ້ຖ້າຫາກວ່າພວກເຮົາມີມີການເຊື່ອມໂຍງຢ່າງຕໍ່ເນື່ອງ tooled e.g. Hudson. Our following sample code illustrates how to use ANT with JUNIT.

LISTING 2: A Sample java file

[Code]

package com.home.junit;

ສາ​ທາ​ລະ​ນະ ທີ່ຫ້ອງຮຽນ SampleAdd {

ສາ​ທາ​ລະ​ນະ int ຕື່ມ​ການ(int x, int y) {

return x + y;

}

}

[/Code]

LISTING 3: The corresponding JUNIT file

[Code]

package com.home.junit;

import org.junit.After;

import org.junit.Assert;

import org.junit.Before;

import org.junit.Test;

public class SampleAddTest {

private SampleAdd sampleClass;

@Before

public void setUp() {

sampleClass = new SampleAdd();

}

@Test

public void testAdd() {

Assert.assertEquals(5, sampleClass.add(3, 2));

}

@After

public void settleDown() {

sampleClass = null;

}

}

[/Code]

LISTING 4: The ANT script to compile and run the JUNIT test

[Code]

<?xml version=”1.0″?>

<project name=”JUNIT” default=”ຕົ້ນ​ຕໍ” basedir=”..”>

<!– Sets variables which can later be used. –>

<!– The value of a property is accessed via ${} –>

<property name=”src.dir” location=”src” />

<property name=”test.dir” location=”tests” />

<property name=”build.dir” location=”bin” />

<property name=”build.test.dir” location=”bin/tests” />

<!– Variables used for JUnit testin –>

<property name=”test.report.dir” location=”testreport” />

<!– Define the classpath which includes the junit.jar and the classes after compiling–>

<path id=”junit.class.path”>

<pathelement location=”lib/junit-4.11.jar” />

<pathelement location=”lib/hamcrest-core-1.3.jar” />

<pathelement location=”${build.dir}” />

</path>

<!– Deletes the existing build, docs and dist directory–>

<target name=”clean”>

<delete dir=”${build.dir}” />

<delete dir=”${test.report.dir}” />

</target>

<!– Creates the build, docs and dist directory–>

<target name=”makedir”>

<mkdir dir=”${build.dir}” />

<mkdir dir=”${build.test.dir}” />

<mkdir dir=”${test.report.dir}” />

</target>

<!– Compiles the java code (including the usage of library for JUnit –>

<target name=”compile” depends=”clean, makedir”>

<javac srcdir=”${src.dir}” destdir=”${build.dir}”>

<classpath refid=”junit.class.path” />

</javac>

<javac srcdir=”${test.dir}” destdir=”${build.test.dir}”>

<classpath refid=”junit.class.path” />

</javac>

</target>

<!– Run the JUnit Tests –>

<!– Output is XML, could also be plain–>

<target name=”junit” depends=”compile”>

<junit printsummary=”ສຸດ” fork=”ຄວາມ​ຈິງ” haltonfailure=”yes”>

<classpath refid=”junit.class.path” />

<classpath>

<pathelement location=”${build.test.dir}”/>

</classpath>

<formatter type=”xml” />

<batchtest todir=”${test.report.dir}”>

<fileset dir=”${test.dir}”>

<include name=”**/*Test*.java” />

</fileset>

</batchtest>

</junit>

</target>

<target name=”ຕົ້ນ​ຕໍ” depends=”compile, junit”>

<description>Main target</description>

</target>

</project>

[/Code]

When we run this script, ມັນຄັ້ງທໍາອິດລວບລວມຖານລະຫັດແລະຈາກນັ້ນທີ່ເນັ້ນການທົດສອບ junit ໃສ່ລະຫັດການລວບລວມແລະສຸດທ້າຍໄດ້ສ້າງບົດລາຍງານການທົດສອບ. The test report can be generated in any format. For convenience we have generated this in XML format.

LISTING 5: The Generated test report in XML format

[Code]

<?xml version=”1.0″ encoding=”UTF-8″?>

-<testsuite timestamp=”2013-10-26T18:52:45″ time=”0.077″ tests=”1″ name=”com.home.junit.SampleAddTest” hostname=”INDTAPPL132-001″ failures=”0″ errors=”0″>-<properties><property name=”java.vendor” value=”Oracle Corporation”/><property name=”sun.java.launcher” value=”SUN_STANDARD”/><property name=”eclipse.pdebuild.templates” value=”/F:/Utpal/Softs/Softs/eclipse-java-indigo-win32/eclipse/plugins/org.eclipse.pde.build_3.7.0.v20110512-1320/templates/”/><property name=”sun.management.compiler” value=”HotSpot Client Compiler”/><property name=”os.name” value=”Windows 7″/><property name=”sun.boot.class.path” value=”C:\Program FilesJavajre7libresources.jar;C:\Program FilesJavajre7librt.jar;C:\Program FilesJavajre7libsunrsasign.jar;C:\Program FilesJavajre7libjsse.jar;C:\Program FilesJavajre7libjce.jar;C:\Program FilesJavajre7libcharsets.jar;C:\Program FilesJavajre7libjfr.jar;C:\Program FilesJavajre7classes”/><property name=”sun.desktop” value=”windows”/><property name=”java.vm.specification.vendor” value=”Oracle Corporation”/><property name=”ant.home” value=”F:\UtpalSoftsSoftseclipse-java-indigo-win32eclipsepluginsorg.apache.ant_1.8.2.v20110505-1300″/><property name=”java.runtime.version” value=”1.7.0_45-b18″/><property name=”user.name” value=”utpalb”/><property name=”build.dir” value=”F:\WorkRnDSpaceJunitbin”/><property name=”user.language” value=”en”/><property name=”test.report.dir” value=”F:\WorkRnDSpaceJunittestreport”/><property name=”sun.boot.library.path” value=”C:\Program FilesJavajre7bin”/><property name=”ant.project.default-target” value=”ຕົ້ນ​ຕໍ”/><property name=”ant.project.name” value=”JUNIT”/><property name=”java.version” value=”1.7.0_45″/><property name=”user.timezone” value=””/><property name=”sun.arch.data.model” value=”32″/><property name=”java.endorsed.dirs” value=”C:\Program FilesJavajre7libendorsed”/><property name=”sun.cpu.isalist” value=”pentium_pro mmx pentium_pro pentium mmx pentium i486 i386 i86″/><property name=”sun.jnu.encoding” value=”Cp1252″/><property name=”file.encoding.pkg” value=”sun.io”/><property name=”file.separator” value=”\”/><property name=”java.specification.name” value=”Java Platform API Specification”/><property name=”java.class.version” value=”51.0″/><property name=”user.country” value=”US”/><property name=”java.home” value=”C:\Program FilesJavajre7″/><property name=”java.vm.info” value=”mixed mode, sharing”/><property name=”ant.file” value=”F:\WorkRnDSpaceJunitbuildbuild.xml”/><property name=”os.version” value=”6.1″/><property name=”path.separator” value=”;”/><property name=”java.vm.version” value=”24.45-b08″/><property name=”user.variant” value=””/><property name=”ant.library.dir” value=”F:\UtpalSoftsSoftseclipse-java-indigo-win32eclipsepluginsorg.apache.ant_1.8.2.v20110505-1300lib”/><property name=”java.awt.printerjob” value=”sun.awt.windows.WPrinterJob”/><property name=”sun.io.unicode.encoding” value=”UnicodeLittle”/><property name=”ant.file.type.JUNIT” value=”ໄຟລ໌”/><property name=”awt.toolkit” value=”sun.awt.windows.WToolkit”/><property name=”user.script” value=””/><property name=”ant.file.JUNIT” value=”F:\WorkRnDSpaceJunitbuildbuild.xml”/><property name=”eclipse.pdebuild.scripts” value=”/F:/Utpal/Softs/Softs/eclipse-java-indigo-win32/eclipse/plugins/org.eclipse.pde.build_3.7.0.v20110512-1320/scripts/”/><property name=”user.home” value=”C:\Usersutpalb”/><property name=”java.specification.vendor” value=”Oracle Corporation”/><property name=”test.dir” value=”F:\WorkRnDSpaceJunittests”/><property name=”java.library.path” value=”C:\Program FilesJavajre7bin;C:\WindowsSunJavabin;C:\Windowssystem32;C:\Windows;C:/Program Files/Java/jre7/bin/client;C:/Program Files/Java/jre7/bin;C:/Program Files/Java/jre7/lib/i386;C:\Windowssystem32;C:\Windows;C:\WindowsSystem32Wbem;C:\WindowsSystem32WindowsPowerShellv1.0;C:\Program FilesJavajdk1.6.0_10bin;ມັນ:\apache-ant-1.8.2-binapache-ant-1.8.2bin;C:\Program FilesLiquid TechnologiesLiquid XML Studio 2011XmlDataBinder9Redist9cppwin32bin;C:\Program FilesTortoiseSVNbin;ມັນ:\Program FilesAndroidandroid-sdkplatform-tools;ມັນ:\Program FilesAndroidandroid-sdktools;C:\Program FilesQuickTimeQTSystem;D:\SunAppServerbin;9;F:\UtpalSoftsSoftseclipse-java-indigo-win32eclipse;;.”/><property name=”java.vendor.url” value=”http://java.oracle.com/”/><property name=”java.vm.vendor” value=”Oracle Corporation”/><property name=”java.runtime.name” value=”ເກາະ​ຊາ​ວາ(TM) SE Runtime Environment”/><property name=”sun.java.command” value=”org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner com.home.junit.SampleAddTest filtertrace=true haltOnError=false haltOnFailure=true formatter=org.apache.tools.ant.taskdefs.optional.junit.SummaryJUnitResultFormatter showoutput=false outputtoformatters=true logfailedtests=true logtestlistenerevents=false formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,F:\WorkRnDSpaceJunittestreportTEST-com.home.junit.SampleAddTest.xml crashfile=F:\WorkRnDSpaceJunitjunitvmwatcher6288340937110531134.properties propsfile=F:\WorkRnDSpaceJunitjunit7583515827572177542.properties”/><property name=”java.class.path” value=”F:\WorkRnDSpaceJunitlibjunit-4.11.jar;F:\WorkRnDSpaceJunitlibhamcrest-core-1.3.jar;F:\WorkRnDSpaceJunitbin;F:\WorkRnDSpaceJunitbintests;F:\WorkRnDSpaceJunit;C:\Program FilesJavajdk1.6.0_10jrelibrt.jar;F:\UtpalSoftsSoftseclipse-java-indigo-win32eclipsepluginsorg.apache.ant_1.8.2.v20110505-1300libant-launcher.jar;F:\UtpalSoftsSoftseclipse-java-indigo-win32eclipsepluginsorg.apache.ant_1.8.2.v20110505-1300libant.jar;F:\UtpalSoftsSoftseclipse-java-indigo-win32eclipsepluginsorg.apache.ant_1.8.2.v20110505-1300libant-junit.jar;F:\UtpalSoftsSoftseclipse-java-indigo-win32eclipsepluginsorg.apache.ant_1.8.2.v20110505-1300libant-junit4.jar”/><property name=”ant.version” value=”Apache Ant(TM) version 1.8.2 compiled on December 20 2010″/><property name=”java.vm.specification.name” value=”Java Virtual Machine Specification”/><property name=”java.vm.specification.version” value=”1.7″/><property name=”sun.cpu.endian” value=”little”/><property name=”sun.os.patch.level” value=”Service Pack 1″/><property name=”eclipse.pdebuild.home” value=”/F:/Utpal/Softs/Softs/eclipse-java-indigo-win32/eclipse/plugins/org.eclipse.pde.build_3.7.0.v20110512-1320/./”/><property name=”java.io.tmpdir” value=”C:\UsersutpalbAppDataLocalTemp”/><property name=”java.vendor.url.bug” value=”http://bugreport.sun.com/bugreport/”/><property name=”os.arch” value=”x86″/><property name=”java.awt.graphicsenv” value=”sun.awt.Win32GraphicsEnvironment”/><property name=”java.ext.dirs” value=”C:\Program FilesJavajre7libext;C:\WindowsSunJavalibext”/><property name=”user.dir” value=”F:\WorkRnDSpaceJunitbuild”/><property name=”build.test.dir” value=”F:\WorkRnDSpaceJunitbintests”/><property name=”line.separator” value=” “/><property name=”java.vm.name” value=”Java HotSpot(TM) Client VM”/><property name=”basedir” value=”F:\WorkRnDSpaceJunit”/><property name=”ant.java.version” value=”1.7″/><property name=”ant.core.lib” value=”F:\UtpalSoftsSoftseclipse-java-indigo-win32eclipsepluginsorg.apache.ant_1.8.2.v20110505-1300libant.jar”/><property name=”file.encoding” value=”Cp1252″/><property name=”java.specification.version” value=”1.7″/><property name=”src.dir” value=”F:\WorkRnDSpaceJunitsrc”/><property name=”ant.project.invoked-targets” value=”ຕົ້ນ​ຕໍ”/></properties><testcase time=”0.003″ name=”testAdd” classname=”com.home.junit.SampleAddTest”/>-<system-out>

<![CDATA[]]>

</system-out>-<system-err>

<![CDATA[]]>

</system-err></testsuite>

[/Code]

ສະ​ຫຼຸບ:

  • APACHE ANT ຜົນປະໂຫຍດ java ທີ່ຖືກນໍາໃຊ້ໂດຍການພັດທະນາ Java ສໍາລັບຈຸດປະສົງດັ່ງຕໍ່ໄປນີ້
    • To generate the binaries from the source code
    • To generate javadocs for the source code
    • To run JUNIT test cases on a code base
  • ANT in collaboration with JUNIT, helps the java developers to follow the Test Driven Development methodology

 

============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share