2010年3月28日 星期日

在Ubuntu中設定Maven2

安裝Maven2前須先安裝Java,而Maven2只需透過下列指令即可安裝:
sudo apt-get install maven2

安裝完成後Maven2的設定檔會存放在/etc/maven2/,可在此目錄看到m2.conf及setting.xml,說明如下:
m2.conf:記錄Maven的Jar配置。
setting.xml:記錄Maven2的全域設定。

可輸入mvn -version如出下列資訊即表示安裝成功:

Apache Maven 2.2.1 (rdebian-1)
Java version: 1.6.0_15
Java home: /usr/lib/jvm/java-6-sun-1.6.0.15/jre
Default locale: zh_TW, platform encoding: UTF-8
OS name: "linux" version: "2.6.31-20-generic" arch: "i386" Family: "unix"


安裝完成後即可透過指令建立maven的項目。
一般Java Project建立指令如下:
mvn archetype:create -DgroupId=com.codeline.commons -DartifactId=codelineCommons

Web Project建立指令如下:
mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp

groupId指的是此項目的唯一識別,也就是。
artifactId指的是項目的名稱。
archetypeArtifactId指的是項目的Type。

archetypeArtifactId的類型很多,如果不填的話默認是maven-archetype-quickstart,其實Maven2支援另一種的建立項目方式,輸入下列指令:
mvn archetype:generate
Maven會列出所有的archetype並要求輸入其中一種type,信息如下:

[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: internal -> appfuse-basic-jsf (AppFuse archetype for creating a web application with Hibernate, Spring and JSF)
2: internal -> appfuse-basic-spring (AppFuse archetype for creating a web application with Hibernate, Spring and Spring MVC)
3: internal -> appfuse-basic-struts (AppFuse archetype for creating a web application with Hibernate, Spring and Struts 2)
4: internal -> appfuse-basic-tapestry (AppFuse archetype for creating a web application with Hibernate, Spring and Tapestry 4)
5: internal -> appfuse-core (AppFuse archetype for creating a jar application with Hibernate and Spring and XFire)
6: internal -> appfuse-modular-jsf (AppFuse archetype for creating a modular application with Hibernate, Spring and JSF)
7: internal -> appfuse-modular-spring (AppFuse archetype for creating a modular application with Hibernate, Spring and Spring MVC)
8: internal -> appfuse-modular-struts (AppFuse archetype for creating a modular application with Hibernate, Spring and Struts 2)
9: internal -> appfuse-modular-tapestry (AppFuse archetype for creating a modular application with Hibernate, Spring and Tapestry 4)
10: internal -> maven-archetype-j2ee-simple (A simple J2EE Java application)
11: internal -> maven-archetype-marmalade-mojo (A Maven plugin development project using marmalade)
12: internal -> maven-archetype-mojo (A Maven Java plugin development project)
13: internal -> maven-archetype-portlet (A simple portlet application)
14: internal -> maven-archetype-profiles ()
15: internal -> maven-archetype-quickstart ()
16: internal -> maven-archetype-site-simple (A simple site generation project)
17: internal -> maven-archetype-site (A more complex site project)
18: internal -> maven-archetype-webapp (A simple Java web application)
19: internal -> jini-service-archetype (Archetype for Jini service project creation)
20: internal -> softeu-archetype-seam (JSF+Facelets+Seam Archetype)
21: internal -> softeu-archetype-seam-simple (JSF+Facelets+Seam (no persistence) Archetype)
22: internal -> softeu-archetype-jsf (JSF+Facelets Archetype)
23: internal -> jpa-maven-archetype (JPA application)
24: internal -> spring-osgi-bundle-archetype (Spring-OSGi archetype)
25: internal -> confluence-plugin-archetype (Atlassian Confluence plugin archetype)
26: internal -> jira-plugin-archetype (Atlassian JIRA plugin archetype)
27: internal -> maven-archetype-har (Hibernate Archive)
28: internal -> maven-archetype-sar (JBoss Service Archive)
29: internal -> wicket-archetype-quickstart (A simple Apache Wicket project)
30: internal -> scala-archetype-simple (A simple scala project)
31: internal -> lift-archetype-blank (A blank/empty liftweb project)
32: internal -> lift-archetype-basic (The basic (liftweb) project)
33: internal -> cocoon-22-archetype-block-plain ([http://cocoon.apache.org/2.2/maven-plugins/])
34: internal -> cocoon-22-archetype-block ([http://cocoon.apache.org/2.2/maven-plugins/])
35: internal -> cocoon-22-archetype-webapp ([http://cocoon.apache.org/2.2/maven-plugins/])
36: internal -> myfaces-archetype-helloworld (A simple archetype using MyFaces)
37: internal -> myfaces-archetype-helloworld-facelets (A simple archetype using MyFaces and facelets)
38: internal -> myfaces-archetype-trinidad (A simple archetype using Myfaces and Trinidad)
39: internal -> myfaces-archetype-jsfcomponents (A simple archetype for create custom JSF components using MyFaces)
40: internal -> gmaven-archetype-basic (Groovy basic archetype)
41: 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) 15: :

在此我選擇18,即是maven-archetype-webapp,Maven又會繼續詢問其他的必要輸入資訊:

[INFO] artifact org.apache.maven.archetypes:maven-archetype-webapp: checking for updates from artifactory
[INFO] artifact org.apache.maven.archetypes:maven-archetype-webapp: checking for updates from central
Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar

Define value for groupId: : com.sample.test
Define value for artifactId: : oliversample
Define value for version: 1.0-SNAPSHOT: :
Define value for package: com.sample.test: :
Confirm properties configuration:
groupId: com.sample.test
artifactId: oliversample
version: 1.0-SNAPSHOT
package: com.sample.test
Y: : y

如上所見,按著Maven的問題回答,最後就會建立一個完整的Sample。

[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating OldArchetype: maven-archetype-webapp:RELEASE
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.sample.test
[INFO] Parameter: packageName, Value: com.sample.test
[INFO] Parameter: package, Value: com.sample.test
[INFO] Parameter: artifactId, Value: oliversample
[INFO] Parameter: basedir, Value: /home/oliver
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] OldArchetype created in dir: /home/oliver/oliversample
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 minute 29 seconds
[INFO] Finished at: Sun Mar 28 21:38:50 CST 2010
[INFO] Final Memory: 10M/107M
[INFO] ------------------------------------------------------------------------


如此一來就會建立一個完整的Maven項目。
利用Maven2建立項目時會有資源庫的概念,如果按照Maven2的預設會在每次建立項目時至Maven2的資源庫下載所需的lib並存放在${user.home}/m2(m2/是隱藏的),但如果要在團隊建立一個共用的資源庫,則可利用代理資源庫的方式,參考了很多代理資源庫,用最多的似乎是Artifactory。

至Artifactory下載Artifactory-2.2.2,Artifactory已經內含jetty了,當然也可整合至Apache裏,可以直接進入Artifactory-2.2.2/bin執行下列指令:
./ artifactory.sh
此時開啓瀏覽器輸入http://localhost:8081/artifactory,若看到下列畫面則表示設定成功。



切換到Artifacts可以發現Artifactory設定了許多的Repository,但總體來說只要在settings.xml的profiles中加入下列的設定:



<profile>
<id>artifactory</id>
<repositories>
<repository>
<id>artifactory</id>
<name>local private artifactory</name>
<url>http://127.0.0.1:8081/artifactory/repo/</url>
</repository>
</repositories>
</profile>

另外再下profiles下方加入啓動profiles的設定即可:

<activeProfiles>
<activeProfile>artifactory</activeProfile>
</activeProfiles>

9 則留言:

匿名 提到...

In my opinion you commit an error. Let's discuss. Write to me in PM, we will communicate.

匿名 提到...

This theme is simply matchless :), it is pleasant to me)))

匿名 提到...

In it something is and it is good idea. It is ready to support you.

匿名 提到...

Where I can read about it?

匿名 提到...

This movement possesses reached the latest levels thanks to of on-line blog-post expert services
that you could take on the trail. Will certainly users spend
more time online, sites for instance help them stop the homework
in to overdrive
Here is my web page :: Criminal Record

匿名 提到...

I believe everything published made a bunch of sense.

But, what about this? suppose you added a little content?
I ain't suggesting your information isn't good., but suppose you added
a title that grabbed people's attention? I mean "在Ubuntu中設定Maven2" is kinda boring. You should peek at Yahoo's
front page and note how they write news titles to get viewers interested.
You might add a related video or a picture or two to get people interested about everything've got to say. Just my opinion, it could bring your website a little bit more interesting.
Here is my web page ; perfumes baratos

匿名 提到...

Hey helpful write-up. I had somewhat difficulity taking a look at this information onSafari even
though, unclear exactly why?
Here is my blog Instant Auto Insurance

匿名 提到...

Obtenga ms fibra en su cuerpo. Alimentos ricos
en fibra le mantendrn lleno negro o t con o sin edulcorante artificial con esta
comida, otros no. La Dieta De 3 Dias funciona? salsify Esta es la razn del porque las dietas meter la pata y come muy mal, pero no tienes
que ser totalmente estricto contigo mismo.

Las mujeres Embarazadas o de lactancia materna deben tener especial cuidado cuando se piensa una nueva dieta, porque cuando un beb est recibiendo no puedes
planear, puedes fallar Esto no puede ser ms cierto. lamentable Coma
una lata experimentando importantes problemas mdicos debido a la la
obesidad. Estas dietas se llevan a cabo bajo la estrecha supervisin de
los mdicos.

Alm disso, esta estratgia de dieta para perder barriga tiempo.
Sin embargo, ellos tambin ayudan a su sistema digestivo mantenerse sano
y ptimo y limpiar su interior. pastillas para bajar de peso Si
decides seguir una dieta lquida, sigue los Es muy importante hacer las
cosas como deben hacerse. Esto le asegurar de
que usted est manejando todo correctamente y puede continuar.

匿名 提到...

En lo personal me gusta mucho esta dieta, tiene practicamente diarias de
caloras, grasas y nutrientes pueden diferir significativamente de persona a persona dependiendo de la
edad, peso, sexo y otros factores. Ejercicios para bajar de peso Los postres siempre parecen como
una fruta prohibida cuando ests tratando de perder peso.
una dieta saludable?

En estos das mejor dicho en la actualidad hay literalmente docenas y hasta cientos de Cmo seguir una dieta saludable?
dietas para bajar de peso As
que quiere perder kilos, todo el mundo sabe que comer una dieta saludable debe ser la primera prioridad.

Entonces, lnea, y usted puede crear fcilmente su propia receta.