2007年6月1日 星期五

(2005.8月號-139期)_為企業導入開放原始碼解決方案_建構企業專案-使用Maven(2)

自動建立IDE開發環境設定

在「序言」中我們提到,我們很難將IDE開發環境的配置複製一份供其他開發人員使用,又或著說,在同一個開發團隊中的每位開發人員都有自己徧好的IDE,甲用Eclipse、乙用IntelliJ IDEA、丙用JBuilder…等,在這種情況下的環境配置就更難於統一了,若將IDE的配置也加入版本控制中,每位開發人員都有權限對環境做修改,這將造成更大的災難。




Maven解決了這個問題,還記得上述過程中建立的goal.txt檔嗎?開啟該檔並搜尋我們常見的IDE名稱。

列表07

[eclipse] Generate Eclipse project files

[idea] Generate IDEA project files

[jbuilder] Generate JBuilder project files

[jdeveloper] Generate JDeveloper project files

也就是說,我們可以使用Maven協助我們產生各種IDE開發環境的project files。試著執行 maven eclipse 或著 maven idea 等等,產生不同IDE的設定。

04-Eclipse開發環境

05-IntelliJ IDEA開發環境




進行後續的整合

現在我們對照之前使用ANT的資料結構,並依下圖將相關檔案複製到定位。

06

接著將所需的jar檔設定於mavenproject.xml內,而至於有那些jar檔需要被引用,參考表「設定01Ant build.xml檔「017-044」「094-138」行,完整的 project.xml 如下。

設定08-project.xml

<?xml version="1.0" encoding="UTF-8"?>

<project>

<pomVersion>3</pomVersion>

<groupId>jpetstore_maven</groupId>

<id>jpetstore_maven</id>

<name>JPetStore Application</name>

<currentVersion>1.0</currentVersion>


<dependencies>

<dependency>

<id>servletapi</id>

<version>2.3</version>

</dependency>

<dependency>

<groupId>aopalliance</groupId>

<artifactId>aopalliance</artifactId>

<version>1.0</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

<dependency>

<groupId>axis</groupId>

<artifactId>axis</artifactId>

<version>1.2-RC3</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

<dependency>

<groupId>axis</groupId>

<artifactId>axis-jaxrpc</artifactId>

<version>1.2-RC3</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

<dependency>

<groupId>axis</groupId>

<artifactId>axis-saaj</artifactId>

<version>1.2-RC3</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

<dependency>

<groupId>axis</groupId>

<artifactId>axis-wsdl4j</artifactId>

<version>1.2-RC3</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

<dependency>

<groupId>burlap</groupId>

<artifactId>burlap</artifactId>

<version>2.1.7</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

<dependency>

<groupId>hessian</groupId>

<artifactId>hessian</artifactId>

<version>3.0.1</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

<dependency>

<groupId>hsqldb</groupId>

<artifactId>hsqldb</artifactId>

<version>1.7.2.2</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

<dependency>

<groupId>ibatis</groupId>

<artifactId>ibatis-common</artifactId>

<version>2.0.8</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

<dependency>

<groupId>ibatis</groupId>

<artifactId>ibatis-sqlmap</artifactId>

<version>2.0.8</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

<dependency>

<groupId>jstl</groupId>

<artifactId>jstl</artifactId>

<version>1.0.6</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

<dependency>

<groupId>ibatis</groupId>

<artifactId>ibatis-sqlmap</artifactId>

<version>2.0.8</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

<dependency>

<groupId>commons-beanutils</groupId>

<artifactId>commons-beanutils</artifactId>

<version>1.7.0</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

<dependency>

<groupId>commons-collections</groupId>

<artifactId>commons-collections</artifactId>

<version>3.1</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

<dependency>

<groupId>commons-dbcp</groupId>

<artifactId>commons-dbcp</artifactId>

<version>1.2.1</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

<dependency>

<groupId>commons-digester</groupId>

<artifactId>commons-digester</artifactId>

<version>1.5</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

<dependency>

<groupId>commons-discovery</groupId>

<artifactId>commons-discovery</artifactId>

<version>0.2</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

<dependency>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

<version>1.0.4</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

<dependency>

<groupId>commons-pool</groupId>

<artifactId>commons-pool</artifactId>

<version>1.2</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

<dependency>

<groupId>commons-validator</groupId>

<artifactId>commons-validator</artifactId>

<version>1.1.3</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

<dependency>

<groupId>taglibs</groupId>

<artifactId>standard</artifactId>

<version>1.0.6</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

<dependency>

<groupId>struts</groupId>

<artifactId>struts</artifactId>

<version>1.2.4</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

<dependency>

<groupId>springframework</groupId>

<artifactId>spring</artifactId>

<version>1.1.5</version>

<properties>

<war.bundle>true</war.bundle>

</properties>

</dependency>

</dependencies>


<build>

<nagEmailAddress>turbine-maven-dev@jakarta.apache.org</nagEmailAddress>

<sourceDirectory>src/java</sourceDirectory>

<unitTestSourceDirectory>src/test</unitTestSourceDirectory>

<unitTest>

<includes>

<include>**/*Test.java</include>

</includes>

<excludes>

<exclude>**/NaughtyTest.java</exclude>

</excludes>

</unitTest>

<resources>

<resource>

<directory>src/conf</directory>

<includes>

<include>*.properties</include>

</includes>

</resource>

</resources>

</build>

</project>


並在maven.xml加入以下的設定

設定09-maven.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns:j="jelly:core" xmlns:ant="jelly:ant" default="war">

<postGoal name="java:compile">

<copy todir="${maven.build.dest}">

<fileset dir="${pom.build.sourceDirectory}">

<include name="**/*.xml"/>

</fileset>

</copy>

</postGoal>

</project>


完成以上設定後於命令下執行maven重新建立war檔。

__ __

| / |__ _Apache__ ___

| |/| / _` V / -_) ' ~ intelligent projects ~

|_| |___,_|_/___|_||_| v. 1.0.2


build:start:


war:init:


war:war-resources:

[mkdir] Created dir: D:JProjectjpetstore_maventargetxdocletwebdoclet

[mkdir] Created dir: D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INF

[copy] Copying 143 files to D:JProjectjpetstore_maventargetxdocletwebdoclet

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INF


java:prepare-filesystem:

[mkdir] Created dir: D:JProjectjpetstore_maventargetclasses


java:compile:

[echo] Compiling to D:JProjectjpetstore_maven/target/classes

[echo]

==========================================================


NOTE: Targetting JVM 1.4, classes

will not run on earlier JVMs


==========================================================


[javac] Compiling 73 source files to D:JProjectjpetstore_maventargetclasses

Note: D:JProjectjpetstore_mavensrcjavaorgspringframeworksamplesjpetstorewebstrutsBaseActionForm.java uses or overrides a deprecated API.

Note: Recompile with -deprecation for details.

[copy] Copying 7 files to D:JProjectjpetstore_maventargetclasses


java:jar-resources:


test:prepare-filesystem:

[mkdir] Created dir: D:JProjectjpetstore_maventargettest-classes

[mkdir] Created dir: D:JProjectjpetstore_maventargettest-reports


test:test-resources:


test:compile:


test:test:


war:webapp:

[echo] Assembling webapp jpetstore_maven

[mkdir] Created dir: D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[mkdir] Created dir: D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFtld

[mkdir] Created dir: D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFclasses

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[copy] Copying 1 file to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFlib

[copy] Copying 81 files to D:JProjectjpetstore_maventargetxdocletwebdocletWEB-INFclasses


war:war:

[echo] Building WAR jpetstore_maven

[jar] Building jar: D:JProjectjpetstore_maventargetjpetstore_maven.war

BUILD SUCCESSFUL

Total time: 11 seconds

Finished at: Sun May 15 15:18:10 CST 2005


目前為止我們已經成功建立完成一個可佈署的jpetstore_maven.war檔,接著將War檔複製進%TOMCAT_HOME%/wabapps,接著同之前的動作,啟動HSQLDB,再啟動Tomcat

使用瀏覽器鍵入以下網址,http://localhost:8080/jpetstore_maven/

我們已經成功的將建置環境由ANT移轉至Maven了!


07-Maven建置的JPetStore


Maven的自我文件化能力

到目前為止,筆者並未帶出Maven太多細節的觀念,純粹以實作與比較的方式讓讀者了解,Maven的能耐。

而就筆者所知,許多OpenSource的專案由以往使用Ant建置漸漸轉向使用Maven,而新的Project更是直接採用Maven作為專案管理工具。

本次的實作讀者們可清楚的了解以下幾點:

  • Maven使用POM管理專案優於Ant建置人員必須自行建置專案邏輯。

  • Maven設置dependencies tag使函式庫的相依性受到了管理優於Ant必須自行管理相依性與版本

  • Maven將所有需要的函式庫置於localrepository內,優於Ant必須將函式庫一起封裝。

  • Maven可產生大多數IDE開發環境設定。

  • Ant可做到的事,Maven皆可做到,並能做到更多。


在本篇文章的最後,我們來展示Maven的自我文件化能力。

我們在命令列鍵入maven site

D:JProjectjpetstore_maven>maven site

__ __

| / |__ _Apache__ ___

| |/| / _` V / -_) ' ~ intelligent projects ~

|_| |___,_|_/___|_||_| v. 1.0.2


build:start:


site:

中間省略

Validating downloads.html

[echo] Generating D:/JProject/jpetstore_maven/target/docs/linkcheck.html from D:JProjectjpetstore_maventargetlinkcheckdocslinkcheck.xml

BUILD SUCCESSFUL

Total time: 49 seconds

Finished at: Sat May 21 18:27:46 CST 2005

當建置完成後,開啟jpetstore_maven/target/docs/index.html

讀者們看完下圖後,應該可以了解,為什麼稱Maven為專案管理工具,而不是像Ant稱為專案建置工具吧!

08-專案首頁

09-專案管理資訊

10-專案建置報表

11-API

12-原始碼

13-單元測試報表



相關資源

1.Jakarta Ant官方網站:http://ant.apache.org/

2. Jakarta Maven官方網站:http://maven.apache.org/

3.Ant實作手冊(Java Development with Ant)-博碩出版

4.專案管理: Maven 讓事情變得簡單:http://www-128.ibm.com/developerworks/cn/java/j-maven/

1 則留言:

匿名 提到...

Yes indeed, in some moments I can say that I approve of with you, but you may be making allowance for other options.
to the article there is still a suspect as you did in the go over like a lead balloon a fall in love with delivery of this solicitation www.google.com/ie?as_q=absolute video to audio converter 3.06 ?
I noticed the catch-phrase you suffer with not used. Or you use the dark methods of promotion of the resource. I take a week and do necheg