Ant Getting Started sample

Source: Internet
Author: User
Tags json
In the previous blog post "Ant's Download and installation--mybatis" Preparation of learning notes (i), the author introduces the download and installation of Ant. This article will show you how to use ant to create a simple project as an example of getting started with Ant.

Here first to share with you the author of the new technology to learn a little. Each technology will involve a lot of knowledge points, it is easy to create greater psychological pressure for beginners. If you start to set the goal too high, write more complex procedures, there will be a great disadvantage: because it is beginner, the basic knowledge is not strong, but also lack of experience, make mistakes more likely. At this time, the lack of solid basic knowledge and experience will naturally add a lot of difficulties to the wrong, because you have too many uncertainties. This not only delays time, but also a blow to self-confidence, at least will aggravate their psychological pressure. However, if you only use it to write a smaller program, you will actually use only a small part of the knowledge. After this error (which is unavoidable), there are fewer places to be sure, and it is easy to debug. Therefore, the author's approach is usually to first write a simple procedure, through the process, first firmly grasp the basic knowledge and start to accumulate experience. Then, on the basis of this, gradually expand the complexity of programming (such a small span). After this error, due to the accumulation of prior knowledge and experience, as well as the smaller span, the difficulty of troubleshooting naturally greatly reduced. This can alleviate the psychological pressure, gradually establish self-confidence, more easily gradually grasp more and more knowledge. As a result, this article and the following blog post will follow the same principle: from the simplest start, gradually increase the complexity. This article first describes how to write a simple ant project, later MyBatis's study notes, all use ant to organize the project.

A directory is required to store all files for ant projects, and the author creates a new directory under the E:\DemoPrograms on its own machine Antdemo, which is used to create the ant project for this article, and all other directories are based on this directory. In this directory, you need three subdirectories, which are SRC, lib, and classes, with the following functions:

SRC: Store The source code files and the required configuration files for the project;

LIB: The jar package used in the project;

Classes: the Compiled. class file is exported to this directory, and the required configuration files for the project are also copied to this directory (we can see, in fact, this directory we can have ant create for us).

This example creates a user object and outputs it as a JSON string (for the knowledge of JSON, the reader can refer to http://www.json.org). The program is very simple and is purely an example. The user class is a simple Java Bean with the following code:

Package com.abc.people;

Publicclass User {

Privateint ID;

private String name;

Privateint age;

Private String gender;

Publicint GetId () {

return ID;

}

Publicvoid setId (int id) {

This.id = ID;

}

Public String GetName () {

return name;

}

Publicvoid setName (String name) {

THIS.name = name;

}

Publicint Getage () {

return age;

}

Publicvoid setage (int age) {

This.age = age;

}

Public String Getgender ()

{

return gender;

}

Publicvoid Setgender (String gender)

{

This.gender = gender;

}

}

According to the package declaration of this type, create a subdirectory under SRC com\abc\people (the level of packages corresponds to the directory level one by one), the User.java file is stored in this directory. Then the program that creates the user object and transforms it into a JSON string, here is the Antdemo class, with the following code:

Package Com.demo;

Import Com.abc.people.User;

Import Net.sf.json.JSONObject;

Publicclass Antdemo

{

Publicstaticvoid Main (string[] args)

{

User user = new user ();

User.setid (10);

User.setname ("General Patton");

User.setage (60);

User.setgender ("male");

Create a Jsonobject object

Jsonobject JSON = jsonobject.fromobject (user);

Print as a JSON string

System.out.println (Json.tostring ());

}

}

Similar to User.java, we create a directory Com\demo under the SRC directory based on the Antdemo package, which is used to store Antdemo.java files. The jar package that this example needs to use is as follows: Commons-beanutils-1.8.3.jar,commons-collections-3.2.1.jar,commons-lang-2.6.jar,commons-logging-1.1.1.jar,e Zmorph-1.0.6.jar,json-lib-2.4-jdk15.jar. Naturally, we need to copy these jar packages into the Lib directory.

The next thing to do is to create the core of the Ant project and write the build file. The generated file is named Build.xml, which is typically stored in the top-level directory of the project, in this case under Antdemo. The build file contains a project element, which is a top-level element that represents a project. The project element has at least one target element, and each target element contains one to more task elements. Executing ant is the execution of these target elements, and executing each target element is executing the task element it contains sequentially. The Build.xml files of this project are as follows:

<?xmlversion= "1.0" encoding= "Iso-8859-1"?>

<!--project elements. The Name property specifies the title of the project, the default property

Specifies the target that ant performs by default, that is, when you do not specify a target to execute, the ant

The target to execute. BASEDIR Specifies the base directory for this project, specified here as

The current directory. -

<projectname= "Antdemo" default= "Compile" basedir= "." >

<!--property element, which defines two elements to be referenced later in the form of the ${element name}. -

<propertyname= "TARGETDIR" value= "Classes"/>

<propertyname= "Srcdir" value= "src"/>

<!--define a path element that specifies the id attribute, which is referenced later--

<pathid= "Library" >

<!--defines a fileset element that specifies the files that need to be used. The Dir property specifies which directory the file is under. -

<filesetdir= "Lib" >

<!--specify all the jar packages that contain the Lib directory--

<includename= "*.jar"/>

</fileset>

</path>

<!--defines a target element, the Name property is required and is used to identify this target. The description attribute is used to describe this target, which is not significant. The depends property specifies the other target on which this target is to be executed. This is specified in order to run the target compile, you need to first execute the clean,copy-resources two target. -

<targetname= "Compile" description= "compiles the Task" depends= "clean" >

<!--Javac is the core task of ant (Task) for compiling Java source programs. The Srcdir property specifies the directory where the source program resides, and ${srcdir} indicates that the value of the property element named Srcdir defined above is referenced. DESTDIR Specifies the output directory of the. class file that is generated after compilation. Similarly, the value of the property element named Targetdir, defined above, is referenced here using the form ${targetdir}. Classpathref indicates that the reference to the ID defined above is the path element of the library. Here, you can load the. jar package it contains into classpath without having to manually run the Setclasspath command yourself as I did before. -

<javacsrcdir= "${srcdir}" destdir= "${targetdir}"

classpathref= "library" includeantruntime= "no"/>

</target>

<!--define a target named clean. -

<targetname= "Clean" >

<!--delete is a core ant task for deleting directories or text

Thing This is used to delete the classes directory. Delete the previously created classes directory, lest the old file cause interference--

<deletedir= "${targetdir}"/>

<!--mkdir is also a core task for creating catalogs. Here to

Newly created classes directory--

<mkdirdir= "${targetdir}"/>

</target>

<targetname= "Copy-resources" >

<!--copy is also a core ant task for copying directories or text

Thing TODIR Specifies the destination directory for replication. This example does not use this task--

<copytodir= "${targetdir}" >

<!--fileset element specifies the set of files to be copied, the Dir property specifies the source directory to replicate, and the exclude element specifies that all Java source files in this directory be excluded, that is, all files except those files--

<filesetdir= "${srcdir}" >

<excludename= "**/*.java"/>

</fileset>

</copy>

</target>

<!--depends property specifies that running this target should run first with a target--> named compile

<targetname= "Run" depends= "compile" >

<!--Java is the core task of ant to execute a Java class. The ClassName property is used to specify the class to run, where the full name of the class fork is set to true to indicate that another JVM is used to run our Java class, rather than using the JVM that runs Ant. The classpathref is the same as in the javac above.

<javafork= "true" Classname= "Com.demo.AntDemo" classpathref= "Library" >

<!--loading the classes directory into Classpath--

<classpathpath= "${targetdir}"/>

</java>

</target>

</project>

Open a command-line window and go to the E:\DemoPrograms\AntDemo directory. If you execute the ant command directly, Ant will search the current directory for the Build.xml file (which is the default) and execute the compile target based on the default value of the project element. To run this target, execute the command: Ant run. Here we analyze the process of executing this target: In the run target, the depends property specifies that the target should run compile first, and compile specifies that the clean target should be run first. Therefore, the whole process is: run clean, delete the old classes directory, avoid interfering with the old files it contains, and then create a new classes directory, and then run compile target. Compile the source program and output the. class file to the classes directory, and run target to execute the compiled program. Of course, when compiling and executing the program, ant will load the corresponding jar and classes directories into classpath based on values such as Classpath and Classpathref. The execution process is shown in the following figure:

As shown in the figure above, ant prints out the approximate steps that are performed. As we can see, ant first finds the makefile and then executes the corresponding target and the task within it, according to our definition in the makefile.

To have ant print out the details of the execution, execute the command: Ant-verbose run. The following figure shows the detailed steps when executing clean target:

The following figure shows that Ant uses the-classpath parameter when invoking the native Java compiler, which loads our jar package into the classpath.

(Click on the "attachment download" below to download the ant project for this article.) )

preparation for the "MyBatis Study Notes" series: Ant Download and installation

preparation of the "MyBatis study Notes" Series II: Ant Getting Started example

one of the "MyBatis Study Notes" series: MyBatis Getting Started sample

"MyBatis Study Notes" series bis: MyBatis additions and deletions example

"MyBatis Study Notes" Series III: MyBatis's Association example

"MyBatis Study Notes" series Four: two forms of MyBatis Association

"MyBatis Study notes" series of Five: MyBatis and spring Integration examples

"MyBatis Study Notes" Series VI: MyBatis and Spring integration examples continued

"MyBatis Study notes" series VII: MyBatis one-to-many bidirectional association

"MyBatis Study notes" series of Eight: MyBatis mapperscannerconfigurer configuration

"MyBatis Study Notes" Series IX: Two forms of MyBatis collection

"MyBatis Study Notes" series of ten: MyBatis log log4j Example

xi. "MyBatis Study Notes" series: an example of annotated mode of MyBatis multi-parameter transfer

"MyBatis Study Notes" series 12: mybatis example of the default naming method for multi-parameter delivery

"MyBatis Study Notes" series 13: Example of map mode of MyBatis multi-parameter transfer

"MyBatis Study Notes" series 14: N+1 Problems in MyBatis

the "MyBatis Study Notes" series: a hybrid approach to mybatis multi-parameter transfer

"MyBatis Study Notes" series 16: Spring Declarative Transaction Management Example

"MyBatis Study Notes" series 17: MyBatis Many-to-many save example

This article is from the "Xiaofan column" blog, be sure to keep this source http://legend2011.blog.51cto.com/3018495/898736

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.