First, I would like to share with you some of my experiences in learning new technologies. Each technology involves many knowledge points, which can easily put a lot of psychological pressure on beginners. If you set the goal too high at the beginning and write more complex programs, there will be a big downside: because it is a beginner, the basic knowledge is not strong, and lack of experience, you are more likely to make mistakes. At this time, the lack of weak basic knowledge and experience will naturally increase the difficulty of troubleshooting, because you are too uncertain. This not only delays time, but also brings a blow to self-confidence, which at least increases your psychological pressure. However, if you only use it to write a small program, you will actually only use a small part of the knowledge. After such an error (which is inevitable), there are few uncertain points and troubleshooting is also easy. Therefore, the author's practice is usually to first write a simple program, through this process, first firmly grasp the most basic knowledge and start to accumulate experience. Then, based on this, gradually expand the complexity of programming (such as a small span ). After such an error occurs, the difficulty of troubleshooting is naturally greatly reduced due to the accumulation of previous knowledge and experience and a small span. In this way, we can reduce the psychological pressure, gradually establish self-confidence, and gradually master more and more knowledge. Therefore, this article and subsequent blog posts will follow the same principle: gradually increase complexity from the simplest start. This article describes how to compile a simple ant project, and then use ant to organize the project for mybatis study notes.
A directory is required to store all the files of the ant project. The author creates a directory AntDemo under E: \ DemoPrograms on his machine to create the ant project in this article, all other directories are based on this directory. There must be three subdirectories in this directory: src, lib, and classes. Their functions are as follows:
1. src: stores source code files and configuration files required by the project;
2. lib: jar package used by the project;
3. classes: compiled. the class file is output to this directory, and the configuration files required by the project will also be copied to this directory (we can see later that we can actually create this directory for ant ).
In this example, a User object is created and output as a JSON string (for details about JSON, refer to www.2cto.com ). The program is very simple and is purely an example. The User class is a simple JAVA Bean with the following code:
1. package com. abc. people;
2.
3. public class User {
4.
5. private int id;
6. private String name;
7. private int age;
8. private String gender;
9.
10. public int getId (){
11. return id;
12 .}
13.
14. public void setId (int id ){
15. this. id = id;
16 .}
17.
18.
19. public String getName (){
20. return name;
21 .}
22.
23. public void setName (String name ){
24. this. name = name;
25 .}
26.
27. public int getAge (){
28. return age;
29 .}
30.
31. public void setAge (int age ){
32. this. age = age;
33 .}
34.
35. public String getGender ()
36 .{
37. return gender;
38 .}
39.
40. public void setGender (String gender)
41 .{
42. this. gender = gender;
43 .}
44.
45 .}
Based on the package declaration of this class, create the sub-directory com \ abc \ people under src (the package level corresponds to the directory level one by one) and store the User. java file in this directory. Then, the program creates a User object and converts it to a JSON string. Here is the AntDemo class, and the code is as follows:
1. package com. demo;
2.
3. import com. abc. people. User;
4. import net. sf. json. JSONObject;
5.
6. public class AntDemo
7 .{
8.
9. public static void main (String [] args)
10 .{
11. User user = new User ();
12. user. setId (10 );
13. user. setName ("General Patton ");
14. user. setAge (60 );
15. user. setGender ("male ");
16.
17. // create a JSONObject
18. JSONObject json = JSONObject. fromObject (user );
19.
20. // print as a JSON string
21. System. out. println (json. toString ());
22 .}
23.
24 .}
Similar to User. java, we create the com \ demo directory under the src directory based on the package where AntDemo is located to store the AntDemo. java file. The jar package to be used in this example 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, ezmorph-1.0.6.jar. Naturally, we need to copy these jar packages to the lib directory.
What needs to be done next is to create the core of the ant project and write the generated file. The generated file is named build. xml by default and is generally stored in the top-level directory of the project. In this example, it is stored in AntDemo. A generated file contains a project element, which is a top-level element and represents a project. The project element has at least one target element, and each target element contains one or more task elements. Execute ant to execute these target elements. Execute each target element to execute the task elements it contains in sequence. The build. xml file of this project is as follows:
1. <? Xml version = "1.0" encoding = "ISO-8859-1"?>
2. <! -- Project element. Name attribute specifies the project name, default attribute
3. Specify the default target to be executed by ant, that is, when the target to be executed is not specified, ant
4. target of the execution. Basedir specifies the baseline directory of the project.
5. Current directory. -->
6. <project name = "AntDemo" default = "compile" basedir = ".">
7.
8. <! -- Property element, which defines two elements for reference in the form of $ {element name. -->
9. <property name = "targetdir" value = "classes"/>
10. <property name = "srcdir" value = "src"/>
11.
12. <! -- Define a path element and specify the id attribute for later reference -->
13. <path id = "library">
14. <! -- Defines a fileset element to specify the files to be used. The dir attribute specifies the file under which the directory is located. -->
15. <fileset dir = "lib">
16. <! -- Specify all jar packages under the lib directory -->
17. <include name = "*. jar"/>
18. </fileset>
19. </path>
20.
21. <! -- Defines a target element. The name attribute is required to identify this target. The description attribute is used to describe this target, which is meaningless. The depends attribute specifies other targets on which the target is executed. To run the compile target, run the clean and copy-resources targets. -->
22. <target name = "compile" description = "Compiles the Task" depends = "clean">
23. <! -- Javac is the core task of ant and is used to compile JAVA source programs. The srcdir attribute specifies the directory where the source program is located. $ {srcdir} indicates that the value of the property element named srcdir defined above is referenced. Destdir specifies the output directory of the. class file generated after compilation. Similarly, $ {targetdir} is used to reference the value of the property element named targetdir defined above. Classpathref indicates that the path element with the id defined above is referenced as library. Here, you can load the. jar package it contains into classpath, instead of manually running the setclasspath command as before. -->
24. <javac srcdir = "$ {srcdir}" destdir = "$ {targetdir }"
25. classpathref = "library" includeantruntime = "no"/>
26. </target>
27.
28. <! -- Define a target named clean. -->
29. <target name = "clean">
30. <! -- Delete is a core task of ant. It is used to delete directories or files.
31. Pieces. Delete the classes directory. That is, first Delete the previously created classes directory to avoid interference with the old file -->
32. <delete dir = "$ {targetdir}"/>
33. <! -- Mkdir is also a core task used to create directories. Used here
34. Create a new classes directory -->
35. <mkdir dir = "$ {targetdir}"/>
36. </target>
37.
38. <target name = "copy-resources">
39. <! -- Copy is also a core task of ant, used to copy directories or files
40. Pieces. Todir specifies the target directory to be copied. This task is not used in this example -->
41. <copy todir = "$ {targetdir}">
42. <! -- The fileset element specifies the file set to be copied, the dir attribute specifies the copied source directory, And the exclude element specifies to exclude all java source files under this directory, that is, copying all files other than these files -->
43. <fileset dir = "$ {srcdir}">
44. <exclude name = "**/*. java"/>
45. </fileset>
46. </copy>
47. </target>
48.
49. <! -- Depends attribute specifies to run this target. The target named compile should be run first -->
50. <target name = "run" depends = "compile">
51. <! -- Java is the core task of ant and is used to execute a java class. The classname attribute is used to specify the class to be run. Setting fork, the full name of the class, to true indicates that another JVM is used to run our JAVA class, instead of the JVM that runs ant. Classpathref is the same as the above javac -->
52. <java fork = "true" classname = "com. demo. AntDemo" classpathref = "library">
53. <! -- Load the classes directory to classpath -->
54. <classpath path = "$ {targetdir}"/>
55. </java>
56. </target>
57. </project>
Open the command line window and go to the E: \ DemoPrograms \ AntDemo directory. If you directly execute the ant command, ant searches for the build. xml file in the current directory (this is the default situation) and runs the compile target Based on the default value of the project element. To run the run target command, run the command ant run. Here we will analyze the process of executing this target: In the run target, the depends attribute specifies that the compile target should be run first, and the compile specifies that the clean target should be run first. Therefore, the entire process is: first run clean to delete the old classes directory to avoid interference with the old files it contains; then create a new classes directory; then run compile target, compile the source program and. the class file is output to the classes directory. run target to execute the compiled program. Of course, when compiling and executing programs, ant will help us to load the corresponding jar package and classpses Directory into classpath according to the values such as classpath and classpathref. The execution process is shown in:
As shown in, ant prints out the general steps. From this we can see that ant first finds the generated file, and then executes the target and the tasks in sequence according to the definition in the generated file.
If you want ant to print the detailed information during execution, run the command ant-verbose run. For example, detailed steps for executing clean target are shown:
However, ant uses the-classpath parameter when calling the java compiler of the Local Machine to load our jar package into classpath.
Author: Xiao fan