Maven 学习笔记(一)--搭建

 

       因为厌倦了Ant总是要明确指定要做什么事它才会去做什么事的笨构建方式。这段时间终于下定决心,着手全面转向使用Maven来作项目管理和构建,今天开始也做下使用笔记。

 

    以前好几次都决心把项目管理的工作都转到Maven,但一直没有下决心,也是断断续续的看了Maven的很多资料,是时候发挥下它的价值了。这是第一篇应使笔记,从搭建Maven的第一步开始,今后陆续会把使用的整个过程和碰到的问题写进来。

 

    1.到官方站点  下载Maven (当前版本2.10) 并解压到 $USERHOME 目录下  我使用的SuSe11.1 Linux x86_64 系统,所以我的解压目录是/home/bencmai/MyProgram/apache-maven-2.1.0.如果你使用的是其他系统,你大可以解压到任意你喜欢的地方。

 

     2.创建一个符号链接,这样能更容易使用,升级Maven的时候也不再需要改变环境变量

bencmai@benc-linux:~> su
口令:******
#(这句只需做一此,你可以把符号连接放任何地方,记得相应修改下两句的环境变量输出)
benc-linux:/home/bencmai #  ln -s /home/bencmai/MyProgram/apache-maven-2.1.0 maven
  
benc-linux:/home/bencmai # export M2_HOME=/usr/local/maven

benc-linux:/home/bencmai # export PATH=${M2_HOME}/bin:${PATH}


#如果你想不用每次都得敲输出环境变量,就把这两句放到你相应用户的 .bashrc 文件中,我的是(/home/bencmai/.bashrc)
benc-linux:/home/bencmai # export M2_HOME=/usr/local/maven
benc-linux:/home/bencmai # export PATH=${M2_HOME}/bin:${PATH}

#执行mvn -v命令验证下是否正确安装
 benc-linux:/home/bencmai # mvn -v

Apache Maven 2.1.0 (r755702; 2009-03-19 03:10:27+0800)
Java version: 1.6.0_13
Java home: /usr/lib64/jvm/java-1.6.0-sun-1.6.0/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux" version: "2.6.27.21-0.1-default" arch: "amd64" Family: "unix"
#有反应并输出Maven的一些版本信息,说明安装正确.可以那来干活了,呵呵

 

   3.创建我们的第一个Maven项目,进入我们用户的老家(/home/bencmai/)

 

#回用户老家
bencmai@benc-linux:/> cd

#擦看当前老家目录结构
bencmai@benc-linux:~> ls

bin  Desktop  Documents  example  MyProgram  public_html  SoftWeave 

#用mvn在用户老家创建一个简单的Maven原型项目
bencmai@benc-linux:~> mvn archetype:create -DgroupId=com.yotexs.srp \

>                                          -DartifactId=yotexs \

>                                          -DpackageName=com.yotexs

[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [archetype:create] (aggregator-style)
[INFO] ------------------------------------------------------------------------
Downloading: http://repo1.maven.org/maven2/com/jcraft/jsch/0.1.27/jsch-0.1.27.pom
965b downloaded  (jsch-0.1.27.pom)
[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]
[WARNING] This goal is deprecated. Please use mvn archetype:generate instead
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating OldArchetype: maven-archetype-quickstart:RELEASE
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.yotexs.srp
[INFO] Parameter: packageName, Value: com.yotexs
[INFO] Parameter: package, Value: com.yotexs
[INFO] Parameter: artifactId, Value: yotexs
[INFO] Parameter: basedir, Value: /home/bencmai
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] OldArchetype created in dir: /home/bencmai/yotexs
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3 seconds
[INFO] Finished at: Sun May 24 08:38:55 HKT 2009
[INFO] Final Memory: 16M/168M
[INFO] ------------------------------------------------------------------------

bencmai@benc-linux:~> cd yotexs

bencmai@benc-linux:~/yotexs> tree

./
yotexs


|--
 pom.xml


`-- src
    |-- 


main


    |   `-- java
    |       `-- com
    |           `-- yotexs
    |               `-- App.java
    `--
 test


        `-- java
            `-- com
                `-- yotexs
                    `-- AppTest.java


9 directories, 3 files
bencmai@benc-linux:~/yotexs> 

 就这么简单,我们的第一个HelloWorld级别的(yotexs)项目就建成了,虽然很原始,但所有的配置,目录结构,Maven都按照它自身的所谓的约定优于配置 的原则自动给我们构建好了。

 

archetype:create 称为一个Maven目标 (goal) ,类似于一个Ant目标 (target);它们都描述了将会在构建中完成的工作单元,Maven有许多可用的archetype,上面用到的 archetype:create 目标只是其中最原始的一个,如果你想选其他的目标,删除之前构建的 yotexs项目后,可以用 archetype:generate 代替 archetype:create ,再来构建一此,这样我们就可以选择你所需要的项目结构了,我们再来全程完整测试一遍

bencmai@benc-linux:~> mvn archetype:generate -DgroupId=com.yotexs.srp -DartifactId=yotexs -DpackageName=com.yotex
    #(1)创建项目
[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]
[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: :
 #(2)选这项目原型和配置
Define value for version:  1.0-SNAPSHOT: : 
Confirm properties configuration:
groupId: com.yotexs.srp
artifactId: yotexs
version: 1.0-SNAPSHOT
package: com.yotexs.srp
 Y: : 
 #(3) 确认配置信息
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating OldArchetype: maven-archetype-quickstart:RELEASE
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.yotexs.srp
[INFO] Parameter: packageName, Value: com.yotexs.srp
[INFO] Parameter: package, Value: com.yotexs.srp
[INFO] Parameter: artifactId, Value: yotexs
[INFO] Parameter: basedir, Value: /home/bencmai
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] OldArchetype created in dir: /home/bencmai/yotexs
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL(自动创建)
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5 seconds
[INFO] Finished at: Sun May 24 10:01:04 HKT 2009
[INFO] Final Memory: 16M/168M
[INFO] ------------------------------------------------------------------------

bencmai@benc-linux:~> tree yotexs
      #(4)查看下Maven为我们创建的项目结构
yotexs
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- yotexs
    |               `-- srp
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- yotexs
                    `-- srp
                        `-- AppTest.java

11 directories, 3 files


bencmai@benc-linux:~> cd yotexs
                #(5) 进入刚创建的项目目录中
bencmai@benc-linux:~/yotexs> mvn install
  #(6) 执行项目构建指令
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building yotexs
[INFO]    task-segment: [install]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/bencmai/yotexs/src/main/resources
[INFO] [compiler:compile]
[INFO] Compiling 1 source file to /home/bencmai/yotexs/target/classes
[INFO] [resources:testResources]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/bencmai/yotexs/src/test/resources
[INFO] [compiler:testCompile]
[INFO] Compiling 1 source file to /home/bencmai/yotexs/target/test-classes
[INFO] [surefire:test]
[INFO] Surefire report directory: /home/bencmai/yotexs/target/surefire-reports

-------------------------------------------------------
 T E S T S(自动测试)
-------------------------------------------------------
Running com.yotexs.srp.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.046 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] [jar:jar]
[INFO] Building jar: /home/bencmai/yotexs/target/yotexs-1.0-SNAPSHOT.jar
[INFO] [install:install]
[INFO] Installing /home/bencmai/yotexs/target/yotexs-1.0-SNAPSHOT.jar to /home/bencmai/.m2/repository/com/yotexs/srp/yotexs/1.0-SNAPSHOT/yotexs-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Sun May 24 10:01:20 HKT 2009
[INFO] Final Memory: 26M/173M
[INFO] ------------------------------------------------------------------------
bencmai@benc-linux:~/yotexs> cd
 ..
bencmai@benc-linux:~> tree yotexs
        #(6) 我们查看一下Maven构建完后都干了些什么 

yotexs
|-- pom.xml
|-- src
|   |-- main
|   |   `-- java
|   |       `-- com
|   |           `-- yotexs
|   |               `-- srp
|   |                   `-- App.java
|   `-- test
|       `-- java
|           `-- com
|               `-- yotexs
|                   `-- srp
|                       `-- AppTest.java
`-- target
    |-- classes
    |   `-- com
    |       `-- yotexs
    |           `-- srp
    |               `-- App.class
    |-- maven-archiver
    |   `-- pom.properties
    |-- pom-transformed.xml
    |-- surefire-reports
    |   |-- TEST-com.yotexs.srp.AppTest.xml
    |   `-- com.yotexs.srp.AppTest.txt
    |-- test-classes
    |   `-- com
    |       `-- yotexs
    |           `-- srp
    |               `-- AppTest.class
    `-- yotexs-1.0-SNAPSHOT.jar

22 directories, 10 files

bencmai@benc-linux:~> cd yotexs
          #(6) 进入我们的项目目录,执行一下我们构建好的HelloWorld 程序
bencmai@benc-linux:~/yotexs> java -cp target/yotexs-1.0-SNAPSHOT.jar com.yotexs.srp.App

Hello World!

bencmai@benc-linux:~/yotexs> 

 Maven 让我们的项目从建立到测试到构建发布,都如此的简单。呵呵,方便之极

 

环境搭建好了,下一篇,我们将开始用Maven做点实际的事情了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值