13 7月 2009

Maven: Create Eclipse Project

用Maven建立Eclipse Project其實頗為容易,第一步用plugin: archetype建立Maven Project,第二步再用plugin: eclipse建立Eclipse要用的設定檔,再來用Eclipse import即可。

1. run mvn archetype:generate
>$ mvn archetype:generate
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [archetype:generate] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] Preparing archetype:generate
[INFO] No goals needed for project - skipping
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:generate {execution: default-cli}]
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: local -> sampleweb-archetype (sampleweb-archetype)
2: internal -> appfuse-basic-jsf (AppFuse archetype for creating a web application with Hibernate, Spring and JSF)
.........
16: internal -> maven-archetype-quickstart ()
17: internal -> maven-archetype-site-simple (A simple site generation project)
18: internal -> maven-archetype-site (A more complex site project)
19: internal -> maven-archetype-webapp (A simple Java web application)
..........
41: internal -> gmaven-archetype-basic (Groovy basic archetype)
42: internal -> gmaven-archetype-mojo (Groovy mojo archetype)
Choose a number:  (1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41/42) 16: : 

可以見到洋洋灑灑42個Template給你選,若沒合意的就用預設16來建吧。

[INFO] artifact org.apache.maven.archetypes:maven-archetype-quickstart: checking for updates from central
Define value for groupId: : org.elliot.web
Define value for artifactId: : test
Define value for version:  1.0-SNAPSHOT: :
Define value for package:  org.elliot.web: :
Confirm properties configuration:
groupId: org.elliot.web
artifactId: test
version: 1.0-SNAPSHOT
package: org.elliot.web
Y: : y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating OldArchetype: maven-archetype-quickstart:RELEASE
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: org.elliot.web
[INFO] Parameter: packageName, Value: org.elliot.web
[INFO] Parameter: package, Value: org.elliot.web
[INFO] Parameter: artifactId, Value: test
[INFO] Parameter: basedir, Value: /Users/elliot/Documents/workspacetesting
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] OldArchetype created in dir: /Users/elliot/Documents/workspacetesting/test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------

Maven要求你輸入groupId, artifactId, version, package後就會依你指定的artifactId建立一個Project。

2. run mvn eclipse:eclipse
進入該目錄後,執行mvn eclipse:eclipse
>$ cd test
>$mvn eclipse:eclipse
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'eclipse'.
[INFO] ------------------------------------------------------------------------
[INFO] Building test
[INFO]    task-segment: [eclipse:eclipse]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing eclipse:eclipse
[INFO] No goals needed for project - skipping
[INFO] [eclipse:eclipse {execution: default-cli}]
[INFO] Using as WTP server : null
[INFO] Adding default classpath contaigner: org.eclipse.jdt.launching.JRE_CONTAINER
[INFO] Using source status cache: /Users/elliot/Documents/workspacetesting/test/target/mvn-eclipse-cache.properties
[INFO] Not writing settings - defaults suffice
[INFO] Wrote Eclipse project for "test" to /Users/elliot/Documents/workspacetesting/test.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------

再import至Eclipse即可

import import
3. 如果你有裝m2eclipse,那可以略過2改執行mvn eclipse:m2eclipse
>$ mvn eclipse:m2eclipse
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'eclipse'.
[INFO] ------------------------------------------------------------------------
[INFO] Building test
[INFO]    task-segment: [eclipse:m2eclipse]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing eclipse:m2eclipse
[INFO] No goals needed for project - skipping
[INFO] [eclipse:m2eclipse {execution: default-cli}]
[INFO] Using source status cache: /Users/elliot/Documents/workspacetesting/test/target/mvn-eclipse-cache.properties
[INFO] Using as WTP server : null
[INFO] Not writing settings - defaults suffice
[INFO] Wrote Eclipse project for "test" to /Users/elliot/Documents/workspacetesting/test.
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------

一樣,import至Eclipse即可

import

使用Maven久了之後會發現,上列的方式仍有不便,例如Source Code Structure不符你的需求,相關的Plugin及Dependencies都需要一再加入pom.xml中,所以會希望能建立一個自己常用的範本。這時,你需要用的是archetype:create-from-project,然後將該archetype install到你Maven的repository中。簡單來說,用你現有的Project建立一個Template,之後再執行archetype:generate時再選擇你的template即可。

4. run mvn archetype:create-from-project
>$ mvn archetype:create-from-project
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building test
[INFO]    task-segment: [archetype:create-from-project] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] Preparing archetype:create-from-project
[INFO] ------------------------------------------------------------------------
[INFO] Building test
[INFO] ------------------------------------------------------------------------
[INFO] No goals needed for project - skipping
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:create-from-project {execution: default-cli}]
[INFO] Setting default groupId: org.elliot.web
[INFO] Setting default artifactId: test
[INFO] Setting default version: 1.0-SNAPSHOT
[INFO] Setting default package: org.elliot.web
[INFO] Archetype created in target/generated-sources/archetype
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------

Maven會將archetype範本產生在target/generated-sources/archetype目錄下,如果還有需要可以進到該目錄,若沒問題的下可以進行下一步

5. run mvn install
先進入target/generated-sources/archetype後再執行mvn install
>$ cd target/generated-sources/archetype/
>$ mvn install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building test-archetype
[INFO]    task-segment: [install]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 7 resources
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/elliot/Documents/workspacetesting/test/target/generated-sources/archetype/src/test/resources
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:jar {execution: default-jar}]
[INFO] [archetype:add-archetype-metadata {execution: default-add-archetype-metadata}]
[INFO] [archetype:integration-test {execution: default-integration-test}]
[INFO] [install:install {execution: default-install}]
[INFO] Installing /Users/elliot/Documents/workspacetesting/test/target/generated-sources/archetype/target/test-archetype-1.0-SNAPSHOT.jar to /Users/elliot/.m2/repository/org/elliot/web/test-archetype/1.0-SNAPSHOT/test-archetype-1.0-SNAPSHOT.jar
[INFO] [archetype:update-local-catalog {execution: default-update-local-catalog}]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
6. run mvn archetype:generate -DarchetypeCatalog=local
再來就可以試者用自已的Template來建立Project了,不過這時要注意,由於Maven將你的Template加入local端的Catalog(存放 archetype的名稱),所以在run plugin:archetype:generate要加個參數指到local端的Catalog。
>$ mvn archetype:generate -DarchetypeCatalog=local
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [archetype:generate] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] Preparing archetype:generate
[INFO] No goals needed for project - skipping
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:generate {execution: default-cli}]
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: local -> sampleweb-archetype (sampleweb-archetype)
2: local -> test-archetype (test-archetype)
Choose a number:  (1/2): 

這時就能看到剛才加入的Template供選擇了囉!

沒有留言: