Compare Maven and ant

Source: Internet
Author: User

Ant is an excellent building system based on tasks and dependencies. Each task contains a set of XML-encoded commands. There are copy tasks, javac tasks, and jar tasks. When you use ant
You provide specific commands for ant to compile and package your output. Let's take a look at the following example. A simple build. xml file:

Example 1.1: A simple ant build. xml file

 <! --  Create the build directory structure used by compile  -->  <  Mkdir  Dir  = "Org. Apache. Maven. model. Build @ d7e661"  />  </  Target  >  < Target  Name  = "Compile"  Depends  = "Init"  Description  = "Compile the source"   >  <! --  Compile the Java code from $ {SRC} into Org. apache. maven. model. build @ d7e661 <javac srcdir = "$ {SRC}" destdir = "org. apache. maven. model. build @ d7e661 "/> </Target> <target name =" Dist "depends =" compile "Description =" generate the distribution "> <! -- Create the distribution directory  -->  < Mkdir  Dir  = "$ {Dist}/lib"  />  <! --  Put everything in org. apache. maven. model. build @ d7e661 into the myproject-$ {<jar jarfile = "$ {Dist}/lib/myproject-$ {dstamp }. jar "basedir =" org. apache. maven. model. </Target> <target name = "clean" Description = "Clean Up"> <! -- Delete the org. apache. maven. model. build @ d7e661 and $ {Dist} directory trees <Delete dir = "org. apache. maven. model. build @ d7e661 "/> <Delete dir =" $ {Dist} "/> </Target> </Project> 

In this simple ant example, you can see that you need to clearly tell ant what you want it to do. There is a compilation target that contains the javac task to compile the source code of SRC/main/Java to target/classes.
Directory. You must clearly tell ant where your source code is, where you want to store the result bytecode, and how to package these bytecode into a jar file. Although there have been some recent progress to help ant decreaseProgramBut a developer
Ant's feeling is that it uses XML to write programming languages.
Compare the maven sample with the ant sample. In Maven, to create a jar file from the Java source code, you only need to create a simple Pom. xml file and put your source code in/usr/local/Hudson/
Hudson-home/jobs/Maven-guide-ZH-to-production/workspace/content-zh/src/main/Java, and then run MVN install from the command line. The following example shows that the maven Pom. xml file can do the same thing as the ant sample file.

Example 1.2: A simple Maven Pom. xml

 <  Project  >  <  Modelversion  > 4.0.0 </ Modelversion  >  <  Groupid  > Org. sonatype. mavenbook </  Groupid  >  <  Artifactid  > My-Project </  Artifactid  >  <  Version > 1.0 </  Version  >  </  Project  > 

This is all about pom. xml. Running MVN install from the command line will process the resource file, compileSource code, Run the unit test, create a jar, and then install the jar to the local repository for other projects
Reusability. Without any modification, you can run MVN site and find an index.html file in the target/site directory. This file links Javadoc and some information about the sourceCode.
It is a simple example. A project that only contains the source code and generates a jar. A Maven-compliant project does not require any dependency or customization. If we want to customize behavior, we
The size of POM. XML will increase. In the largest project, you can see a very complex collection of Maven pom, which contains a large number of plug-in customization and dependency declarations. However, although the POM file of your project becomes
The information they contain is completely different from the information of an ant project-based build file. Maven pom contains the declaration: "This is a jar Project", "source code in src/main/Java
". The ant build file contains explicit commands: "This is a project", "source code in src/main/Java", and "Run javac for this directory ", "Place the result to target/classes", "from ...... Create
Create a jar, and so on. The process required by ANT must be explicit, while Maven has some "built-in" to let it know where the source code is and how to handle it.

In this example, the difference between ANT and Maven is:
Apache ant

• Ant has no formal conventions, such as the directory structure of a general project. You must clearly tell ant where to find the source code and where to place the output. Over time, informal conventions emerged,
They have not been modeled in products.

• Ant is programmatic. You must clearly tell ant what to do and when to do it. You must tell it to compile, copy, and compress.

• Ant has no lifecycle. You must define dependencies between the target and the target. You must manually attach a task sequence for each target.

Apache Maven

• Maven has a convention, because you have followed the Convention and it already knows where your source code is. It places the byte code to the target/classes, and then generates a jar file in the target.

• Maven is declarative. All you need to do is create a pom. xml file and put the source code to the default directory. Maven will help you deal with other things.

• Maven has a lifecycle that is called when you run MVN install. This command tells Maven to execute a series of ordered steps until it reaches your specified lifecycle. One of the impacts of traversing the lifecycle is that Maven runs many default plug-in targets that complete compilation and creation of a jar file.

Maven provides built-in intelligence for some general project tasks in the form of plug-ins. If you want to write and run a unit test, all you need to do is write the test and put it in/usr/local/Hudson-home/jobs/Maven-guide-ZH-to-production/workspace/content-zh/src/ test/Java, add a test scope dependency for testng or JUnit, and then run MVN test. If you want to deploy a web application instead of jar, you need to change the project type to war, set the root directory of your document to/usr/local/Hudson-home/jobs/Maven-guide-ZH-to-production/workspace/content-zh/src/main. /webapp. Of course, you can use ant to do these tasks, but you will need to write these commands from scratch. To use ant, you must first determine where the JUnit JAR file should be stored, and then create a classpath containing the JUnit JAR file, and then tell ant where to find the test source code, compile a objective to compile the test source code as a bytecode and use JUnit to perform the unit test.

without support for technologies such as antlibs and lvy (even with such support technologies), ant gives the impression that it is a custom procedural build. In the project, a set of efficient Maven pom files adhere to the Conventions, and there are only a few XML files compared to ant configuration files. Another advantage of Maven is that it relies on widely used Maven plug-ins. Everyone uses the maven surefire plug-in to run unit tests. If someone adds support for new test frameworks, you can only upgrade the version of a specific plug-in the POM of your project to obtain new functions. Using Maven or ant makes no difference between them. ant still has its location in complicated construction. If your current build contains some highly customized processes, or you have written some ant scripts to complete a specific process in a specific way, this process is not suitable for Maven standards. You can still use these scripts in Maven. As the core plug-in of Maven, ant is still available. Custom plug-ins can be implemented using ant. MAVEN projects can be configured to run ant scripts in the lifecycle.

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.