原文: Apache Maven Simplifies the Java Build Process—Even More Than Ant
http://www.devx.com/java/Article/17204
apache maven,一個(gè)潛在的基于java的apache ant的構建工具的替代者.承諾消除維護復雜的構建腳本的爭論.
這個(gè)新工具可以自動(dòng)化java的構建過(guò)程.給maven一點(diǎn)項目的關(guān)于目錄結構的信息讓它來(lái)處理剩余的.所有需要的構建項目的功能已經(jīng)構建到Maven中去了.換一句話(huà)說(shuō),對于一個(gè)標準的構建過(guò)程,你不必創(chuàng )建自定義的構建腳本.
在本文中,我將解釋什么是maven和它將要解決什么問(wèn)題.然后我將帶你用maven自動(dòng)化你自己的構建過(guò)程.作為在java中最流行的構建工具ant,此文件假設讀者有對ant的基本了解.
構建過(guò)程使每天的任務(wù)自動(dòng)化
作為一個(gè)程序員,花大把的時(shí)間在使其他的任務(wù)自動(dòng)化,IDE自動(dòng)化了我們大部分工作,但是許多開(kāi)發(fā)者還是不得不使用外部工具來(lái)完成特別的任務(wù):構建過(guò)程.構建的概念發(fā)展了多年,但是現在的構建工具只是提供了一些開(kāi)發(fā)者在開(kāi)發(fā)過(guò)程中開(kāi)發(fā),編譯,測試和部署任務(wù)的自動(dòng)化.
對于我的工作,我勉強使用多用途的
ant允許你自定義任務(wù),它可以跨項目重用.這也許使ant成功的原因吧,但是即使有可重用的任務(wù),你仍然要
For my work, I am grudgingly using a general-purpose, scripting language more and more with the build tool—not unlike the macro languages built into IDEs. Table 1 shows a few of the build functions that I perform daily.
Table 1. Daily Build Functions I Perform
Compile Run jikes or javac
Copy Copy resource files into the classes directory
Clean Delete the classes directory so that our next compile will be a clean compile
Delete any temporary, app-server-generated files
Recreate the classes directory
Jar Zip up a project in to a .jar file
Javadoc Generate documentation from source code
Test Use JUnit to run the project‘s unit tests
Deploy Copy runtime files to a staging server
Kodo Run Kodo‘s JDO byte-code enhancer (Kodo is a JDO implementation that post-processes class files to make them persistence-capable.)
Move Over Ant, Maven Is Here
When I first started using Ant to automate the above-mentioned tasks, it was a big timesaver. Especially once I learned to use and create custom Ant Tasks. But that was for individual, fairly simple projects. I now use Ant to automate build tasks for multiple, complex projects. A considerable part of my day is spent creating and maintaining complex Ant scripts. Over time, maintaining these scripts became a major thorn in my side.
Specifically, the following Ant limitations led me to start looking for a new build tool:
Cross-project reuse. My 10 Ant build scripts are mostly the same. So I started looking for code reuse features in Ant. As it turns out, Ant has no convenient way to reuse targets across projects. Copy-and-paste is the only choice.
Cross-developer reuse. Most of my Ant scripts are complex but not particularly unique to my project. In other words, since most of my build scripts perform the same functions as other people‘s build scripts, why should I have to create a build script at all—other than for project-specific functionality?
Logic and looping. As mentioned previously, I now use Ant for general-purpose scripting. Therefore, I need general-purpose scripting features, like conditional logic, looping constructs, and reuse mechanisms. Ant was never meant to be a general-purpose scripting tool, however. As such, although possible via the Script Task or a custom Task, adding conditional logic and looping to the Ant build process is awkward.
To be fair, Ant does allow you to create custom Tasks, which are reusable across projects. These are a great help and probably are the reason for Ant‘s success. However, even with reusable Tasks, you still end up with numerous, mostly redundant Targets in each project. If your objective is to simplify or eliminate the build script, you need something more. You need reusable Target Libraries. That‘s where Maven comes in.
Maven addresses these issues. With this new tool, targets (which Maven calls goals) are reusable (See the sidebar "Maven Term and Concept Summary" for a complete listing of Maven terminology.). In fact, for the most common tasks, Maven has already created the goals. This means you don‘t have to create them yourself. For simple projects, you may not need a build file at all. (See the sidebar "Maven Versus Ant" for a comparative analysis of Ant and Maven.)
Maven + POM = Goals Achieved
To achieve its magic, Maven uses a Project Object Model (POM), which describes a project in the form of an XML file, project.xml. Specifically, it describes the project‘s directory structure, its jar file dependencies, and some of its project management details. Think of the POM as project meta-data. Once you describe your project to Maven by creating the POM, you can invoke any of Maven‘s built-in goals (remember, a goal is like an Ant target).
The following sections demonstrate Maven. They walk you step by step through creating a simple project and invoking some of the Maven-provided goals.
安裝Maven
1.下載,安裝 http://maven.apache.org/start/download.html
2.設置環(huán)境變量MAVEN_HOME,如果是下載的exe安裝文件,在windows平臺上安裝程序會(huì )自動(dòng)設置.如果你下載的zip包或其他壓縮包,那么你必須手動(dòng)設置這個(gè)環(huán)境變量.
3.添加%MAVEN_HOME%\bin到PATH中去.
4.為了避免重復下載資源庫,運行install_repo.bat C:\Documents and Settings\Administrator\.maven\repository把%MAVEN_HOME%\lib中的插件安裝到本地資源庫中.否則maven會(huì )到網(wǎng)上去自動(dòng)下載,這樣速度有影響.
記住,第一次運行maven,它要到網(wǎng)上去下載一大堆jar文件.這些jar文件是maven眾多的插件所需要的.因此在等待下載的過(guò)程中,你有時(shí)間去泡杯咖啡了.