The authors of this book have no interest in creating a feud(不和;争执;封地) between Apache Ant and Apache Maven, but we are also cognizant(审理的;已认知的) of the fact that most organizations have to make a decision between the two standard solutions: Apache Ant and Apache Maven. In this section, we compare and contrast the tools.

Ant excels(超过;擅长) at build process, it is a build system modeled after make with targets and dependencies. Each target consists of a set of instructions which are coded in XML. There is a copy task and a javac task as well as a jar task. When you use Ant, you supply Ant with specific instructions for compiling and packaging your output. Look atthe following example of a simple build.xml file:

A Simple Ant build.xml File.

<project name="my-project" default="dist" basedir="."><description>simple example build file</description><!-- set global properties for this build --><property name="src" location="src/main/java"/><property name="build" location="target/classes"/><property name="dist"  location="target"/><target name="init"><!-- Create the time stamp --><tstamp/><!-- Create the build directory structure used by compile --><mkdir dir="${build}"/></target><target name="compile" depends="init"description="compile the source " ><!-- Compile the java code from ${src} into ${build} --><javac srcdir="${src}" destdir="${build}"/></target><target name="dist" depends="compile"description="generate the distribution" ><!-- Create the distribution directory --><mkdir dir="${dist}/lib"/><!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --><jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/></target><target name="clean"description="clean up" ><!-- Delete the ${build} and ${dist} directory trees --><delete dir="${build}"/><delete dir="${dist}"/></target>
</project>

In this simple Ant example, you can see how you have to tell Ant exactly what to do. There is a compile goal which includes the javac task that compiles the source in the src/main/java directory to the target/classes directory. You have to tell Ant exactly where your source is, where you want the resulting bytecode to be stored, and how to package this all into a JAR file. While there are some recent developments that help make Ant less procedural(程序上的), a developer’s experience with Ant is in coding a procedural language written in XML.

Contrast the previous Ant example with a Maven example. In Maven, to create a JAR file from some Java source, all you need to do is createa simple pom.xml, place your source code in${basedir}/src/main/java and then run mvn install from the command line. The example Maven pom.xml that achieves the same results as the simple Ant file listed in A Simple Ant build.xml File is shown in A Sample Maven pom.xml.

A Sample Maven pom.xml. 

<project><modelVersion>4.0.0</modelVersion><groupId>org.sonatype.mavenbook</groupId><artifactId>my-project</artifactId><version>1.0-SNAPSHOT</version>
</project>

That’s all you need in your pom.xml. Running mvn install from the command line will process resources, compile source, execute unit tests, create a JAR, and install the JAR in a local repository for reuse in other projects. Without modification, you can run mvn site and then find an index.html file in target/site that contains links to JavaDoc and a few reports about your source code.

Admittedly, this is the simplest possible example project containing nothing more than some source code and producing a simple JAR. It is a project which closely follows Maven conventions and doesn’t requireany dependencies or customization. If we wanted to start customizing the behavior, our pom.xml is going to grow in size, and in the largest of projects you can see collections of very complex Maven POMs which contain a great deal of plugin customization and dependency declarations. But, even when your project’s POM files become more substantial, they hold an entirely different kind of information from the build file of a similarly sized project using Ant. Maven POMs contain declarations: "This is a JAR project", and "The source code isin src/main/java". Ant build files contain explicit instructions:"This is project", "The source is in src/main/java", "Run javac against this directory", "Put the results in target/classes","Create a JAR from the ….", etc. Where Ant had to be explicit about the process, there was something "built-in" to Maven that just knew where the source code was and how it should be processed.

The differences between Ant and Maven in this example are:

Apache Ant
  • Ant doesn’t have formal conventions like a common project directory structure or default behavior. You have to tell Ant exactly where to find the source and where to put the output. Informal conventions have emerged over time, but they haven’t been codified(编成法典) into the product.
  • Ant is procedural. You have to tell Ant exactly what to do and when to do it. You have to tell it to compile, then copy, then compress.
  • Ant doesn’t have a lifecycle. You have to define goals and goal dependencies. You have to attach a sequence of tasks to each goal manually.
Apache Maven
  • Maven has conventions. It knows where your source code is because you followed the convention. Maven’s Compiler plugin put the bytecode in target/classes, and it produces a JAR file in target.
  • Maven is declarative. All you had to do was create a pom.xml file and put your source in the default directory. Maven took care of the rest.
  • Maven has a lifecycle which was invoked when you executed mvn install. This command told Maven to execute a series of sequential lifecycle phases until it reached the install lifecycle phase. As a side-effect of this journey through the lifecycle, Maven executed a number of default plugin goals which did things like compile and create a JAR.

Maven has built-in intelligence about common project tasks in the form of Maven plugins. If you wanted to write and execute unit tests, all you would need to do is write the tests, place them in ${basedir}/src/test/java, add a test-scoped dependency on either TestNG or JUnit, and run mvn test. If you wanted to deploy a web application and not a JAR, all you would need to do is change your project type to war and put your docroot in${basedir}/src/main/webapp. Sure, you can do all of this with Ant, but you will be writing the instructions from scratch. In Ant,you would first have to figure out where the JUnit JAR file should be. Then you would have to create a classpath that includes the JUnitJAR file. Then you would tell Ant where it should look for test source code, write a goal that compiles the test source to bytecode, and execute the unit tests with JUnit.

Without supporting technologies like antlibs and Ivy (even with these supporting technologies), Ant has the feeling of a custom procedural build. An efficient set of Maven POMs in a project which adheres to Maven’s assumed conventions has surprisingly little XML compared tothe Ant alternative. Another benefit of Maven is the reliance on widely-shared Maven plugins. Everyone uses the Maven Surefire plugin for unit testing, and if someone adds support for a new unit testing framework, you can gain new capabilities in your own build by just incrementing the version of a particular Maven plugin in your project’s POM.

The decision to use Maven or Ant isn’t a binary one, and Ant still has a place in a complex build. If your current build contains some highly customized process, or if you’ve written some Ant scripts to completea specific process in a specific way that cannot be adapted to the Maven standards, you can still use these scripts with Maven. Ant is made available as a core Maven plugin. Custom Maven plugins can be implemented in Ant, and Maven projects can be configured to execute Ant scripts within the Maven project lifecycle.

Maven by Example 1.7. Comparing Maven with Ant相关推荐

  1. Maven学习总结(6)——Maven与Eclipse整合

    2019独角兽企业重金招聘Python工程师标准>>> Maven学习总结(六)--Maven与Eclipse整合 一.安装Maven插件 下载下来的maven插件如下图所示:,插件 ...

  2. Maven详解(二)------ Maven的安装配置

    2019独角兽企业重金招聘Python工程师标准>>> 目录 1.下载 Maven 2.配置 Maven 环境变量 3.查看 Maven 环境变量是否配置成功 4.在 eclipse ...

  3. Intellij Idea 导入多个maven项目展示在左侧栏Maven Projects

    刚刚要开始从eclipse切换成idea,据说idea功能强大,可是刚刚开始使用很多不习惯,导入第二个maven项目时之前的项目就没了,比较苦恼,下面介绍下导入多个maven项目展示在左侧栏Maven ...

  4. Maven学习总结(2)——Maven项目构建过程练习

    Maven学习总结(二)--Maven项目构建过程练习 上一篇只是简单介绍了一下maven入门的一些相关知识,这一篇主要是体验一下Maven高度自动化构建项目的过程 一.创建Maven项目 1.1.建 ...

  5. Maven学习总结(14)——Maven 多模块项目如何分工?

    2019独角兽企业重金招聘Python工程师标准>>> 一.开场白 使用Maven有段时间了,只能感慨真是个好东西,让我从传统模式体会到了严谨.规范.敏捷.方便的特性. 如果你懂Ma ...

  6. 1.Maven+SpringMVC+Eclipse软件安装配置,Maven报插件错误,Eclipse总是卡死的解决办法,导入一个maven工程后 一直显示importing maven project

     使用Maven+SpringMVC+Eclipse软件安装配置过程中的问题: 1.Eclipse总是卡死的解决办法: 一:内存不足所以会卡死,配置一下eclipse.ini修改这几个值就好了-X ...

  7. maven aspectj_使用Spring AspectJ和Maven进行面向方面的编程

    maven aspectj Spring框架附带AOP支持. 实际上,如Spring参考文档中所述 , " Spring的关键组件之一是AOP框架. 尽管Spring IoC容器不依赖于AO ...

  8. maven 打包指定依赖包_[Maven]-Maven基础-01-基础概念

    Maven基础 1.基础概念 Maven基础概念 什么是Maven 什么是理想的项目构建? 高度自动化,跨平台,可重用的组件,标准化的 什么是依赖?为什么要进行依赖管理? 自动下载,统一依赖管理 有哪 ...

  9. idea maven web工程明明添加了maven lib的依赖,但启动web容器时始终报No Class Found?...

    idea maven web工程明明添加了maven lib的依赖,但启动web容器时始终报No Class Found? 很久没用idea搭新工程,最近自己想做个东西,冲心搭个web工程,jar包都 ...

  10. Maven学习记录之maven基本操作命令,maven本地工厂的创建,maven骨架的生成,以及在eclipse中创建maven工程:...

    摘要:今天又学习了一下maven,之前是下载并安装和配置好了maven的环境,今天主要学习的内容包括:maven本地工厂的创建,maven骨架的生成命令,maven在dos下创建maven工程,以及在 ...

最新文章

  1. fibonacci数列前20项_等差数列、等比数列、调和数列等几种常见数列的总结
  2. 如何保证进程间同步工作_软件测试新玩法,看这5家科技巨头如何组织质量保证工作?...
  3. winform datagridview控件使用
  4. Android 亲测源码分享
  5. 计算机无法用telnet,为何我的电脑cmd没法使用telnet命令?
  6. html纵向的跑马灯效果,HTML+CSS入门 如何实现跑马灯/走马灯效果
  7. HikariCP连接池配置
  8. GridView实战一:自定义分页、排序、修改、插入、删除
  9. VS2017+Opencv3.3+Opencv_contribute编译
  10. 【网赚工具】语音转文字神器,无需安装就能使用,支持长语音识别
  11. 论文笔记《Attention Is All You Need》
  12. 机械工程专业英语词汇
  13. Windows无法安装到这个磁盘。选中的磁盘具有MBR分区表。在EFI系统上,Windows只能安装到GPT磁盘
  14. Vue移动端rotate强制横屏
  15. redis 集群详解及搭建过程
  16. Android ANR原理代码分析(三)
  17. vscode中好用的git相关的插件
  18. 【巴迪亲子英语启蒙课堂】会日常单词,会简单对话,自主对话不行怎么办?是否要加强英文对话?
  19. WPS如何快速统计姓名个数
  20. IPFS应用丨Cloudflare的IPFS网关

热门文章

  1. html 图片放大保证不失真,图片放大不失真的几种方法
  2. 麻省理工遍地走,6年经验安卓程序员面试微软,靠这份思维脑图拿下Offer!
  3. Python数据去重
  4. 天线设计-电感计算以及天线匹配
  5. js原生touch事件实现微信语音按住录音,上滑取消。
  6. php7isapi,如何选择PHP套件中ISAPI和FastCGI模式的版本?_护卫神
  7. KMeans 算法(一)
  8. Ubuntu20.04 libcef笔记
  9. 基于matlab的电池管理系统开发,使用 Simulink 和基于模型的设计开发电池管理系统...
  10. MSVCR71.dll is missing from your computer-(Window7 install sqldeveloper for oracle )