沉沦人生

---^_^

统计

最新评论

[导入]比ant更强大的工具maven2之自动生成工程


网站:JavaEye 作者:danlley 发表时间: 2007-07-17 13:38 此文章来自于 http://www.JavaEye.com
声明:本文系JavaEye网站原创文章,未经JavaEye网站或者作者本人书面许可,任何其他网站严禁擅自发表本文,否则必将追究法律责任!
原文链接: http://danlley.javaeye.com/blog/101979

maven2是在ant的基础上发展起来的,并对ant的功能进行了进一步扩充,下面将说明如何利用maven2自动生成工程结构。

 

要使用maven2,首先需要在apache的官方网站下载最新版的工具:

 


 

 

 

 

接下来就是配置环境变量




设置 MAVEN2_HOME --------》D:\maven2\maven2(指向你的maven2的安装后根目录)

设置path ----------------》D:\maven2\maven2\bin;




 

 

接下来准备开始建立项目:

 

新建一个文件夹作为工作目录: D:\mywork

 

1.在命令行执行如下指令:




mvn archetype:create 

  -DarchetypeGroupId=org.apache.maven.archetypes 

  -DgroupId=com.mycompany.app 

  -DartifactId=my-app




 

切换到工作目录,这时你就会发现我们的工程框架已经建立好了




my-app

|-- pom.xml

`-- src

    |-- main

    |   `-- java

    |       `-- com

    |           `-- mycompany

    |               `-- app

    |                   `-- App.java

    `-- test

        `-- java

            `-- com

                `-- mycompany

                    `-- app

                        `-- AppTest.java





 

 

 

2.生成的pom(pom.xml)文件如下






http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  4.0.0

  com.mycompany.app

  my-app

 

jar

  1.0-SNAPSHOT

  my-app

  http://maven.apache.org

 

   

      junit

      junit

      3.8.1

      test

   

 





 

 

 

 

 

3.下面用maven2开始编译工程:

 

mvn compile

 

 

运行结果如下:




[INFO] ----------------------------------------------------------------------------

[INFO] Building Maven Quick Start Archetype

[INFO]    task-segment: [compile]

[INFO] ----------------------------------------------------------------------------

[INFO] artifact org.apache.maven.plugins:maven-resources-plugin: \

  checking for updates from central

...

[INFO] artifact org.apache.maven.plugins:maven-compiler-plugin: \

  checking for updates from central

...

[INFO] [resources:resources]

...

[INFO] [compiler:compile]

Compiling 1 source file to /my-app/target/classes

[INFO] ----------------------------------------------------------------------------

[INFO] BUILD SUCCESSFUL

[INFO] ----------------------------------------------------------------------------

[INFO] Total time: 3 minutes 54 seconds

[INFO] Finished at: Fri Sep 23 15:48:34 GMT-05:00 2005

[INFO] Final Memory: 2M/6M

[INFO] ----------------------------------------------------------------------------







 

 

 

 

 

4.开始为工程打包

 

mvn package

 

执行结果如下:




D:\mywork\my-app>mvn package

[INFO] Scanning for projects...

[INFO] -------------------------------------------------------------------------

---

[INFO] Building my-app

[INFO]    task-segment: [package]

[INFO] -------------------------------------------------------------------------

---

[INFO] [resources:resources]

[INFO] Using default encoding to copy filtered resources.

Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-compiler-m

anager/1.5.3/plexus-compiler-manager-1.5.3.pom

634b downloaded

Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-compiler/1

.5.3/plexus-compiler-1.5.3.pom

1K downloaded

Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-compiler-a

pi/1.5.3/plexus-compiler-api-1.5.3.pom

624b downloaded

Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-compiler-j

avac/1.5.3/plexus-compiler-javac-1.5.3.pom

797b downloaded

Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-compilers/

1.5.3/plexus-compilers-1.5.3.pom

7K downloaded

Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-compiler-a

pi/1.5.3/plexus-compiler-api-1.5.3.jar

19K downloaded

Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-compiler-j

avac/1.5.3/plexus-compiler-javac-1.5.3.jar

13K downloaded

Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-compiler-m

anager/1.5.3/plexus-compiler-manager-1.5.3.jar

5K downloaded

[INFO] [compiler:compile]

[INFO] Compiling 1 source file to D:\mywork\my-app\target\classes

[INFO] Compiling 1 source file to D:\mywork\my-app\target\classes

[INFO] [resources:testResources]

[INFO] Using default encoding to copy filtered resources.

[INFO] [compiler:testCompile]

[INFO] Compiling 1 source file to D:\mywork\my-app\target\test-classes

Downloading: http://repo1.maven.org/maven2/org/apache/maven/surefire/surefire-bo

oter/2.3/surefire-booter-2.3.pom

1K downloaded

Downloading: http://repo1.maven.org/maven2/org/apache/maven/surefire/surefire-ap

i/2.3/surefire-api-2.3.pom

887b downloaded

Downloading: http://repo1.maven.org/maven2/org/apache/maven/surefire/surefire-bo

oter/2.3/surefire-booter-2.3.jar

28K downloaded

Downloading: http://repo1.maven.org/maven2/org/apache/maven/surefire/surefire-ap

i/2.3/surefire-api-2.3.jar

49K downloaded

[INFO] [surefire:test]

Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-container-

default/1.0-alpha-8/plexus-container-default-1.0-alpha-8.jar

190K downloaded

Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-utils/1.1/

plexus-utils-1.1.jar

164K downloaded

Downloading: http://repo1.maven.org/maven2/org/apache/maven/surefire/surefire-ju

nit/2.3/surefire-junit-2.3.pom

583b downloaded

Downloading: http://repo1.maven.org/maven2/org/apache/maven/surefire/surefire-pr

oviders/2.3/surefire-providers-2.3.pom

1K downloaded

Downloading: http://repo1.maven.org/maven2/org/apache/maven/surefire/surefire-ju

nit/2.3/surefire-junit-2.3.jar

10K downloaded

[INFO] Surefire report directory: D:\mywork\my-app\target\surefire-reports

-------------------------------------------------------

 T E S T S

-------------------------------------------------------

Running com.mycompany.app.AppTest

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.203 sec

Results :

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

Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-archiver/2.2/m

aven-archiver-2.2.pom

1K downloaded

Downloading: http://repo1.maven.org/maven2/org/apache/maven/shared/maven-shared-

components/3/maven-shared-components-3.pom

1K downloaded

Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-archiver/2.2/m

aven-archiver-2.2.jar

9K downloaded

[INFO] [jar:jar]

[INFO] Building jar: D:\mywork\my-app\target\my-app-1.0-SNAPSHOT.jar

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESSFUL

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 2 minutes 52 seconds

[INFO] Finished at: Sun Apr 15 12:16:44 CST 2007

[INFO] Final Memory: 4M/10M

[INFO] ------------------------------------------------------------------------





 

 

生成文件如下:    my-app-1.0-SNAPSHOT.jar

 

 

5.将jar包放入资源库中执行如下命令:mvn install

执行结果如下:




[INFO] Scanning for projects...

[INFO] -------------------------------------------------------------------------

---

[INFO] Building my-app

[INFO]    task-segment: [install]

[INFO] -------------------------------------------------------------------------

---

[INFO] artifact org.apache.maven.plugins:maven-install-plugin: checking for upda

tes from central

Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-instal

l-plugin/2.1/maven-install-plugin-2.1.pom

981b downloaded

Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-plugin

-parent/2.0/maven-plugin-parent-2.0.pom

6K downloaded

Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-instal

l-plugin/2.1/maven-install-plugin-2.1.jar

8K downloaded

[INFO] [resources:resources]

[INFO] Using default encoding to copy filtered resources.

[INFO] [compiler:compile]

[INFO] Nothing to compile - all classes are up to date

[INFO] [resources:testResources]

[INFO] Using default encoding to copy filtered resources.

[INFO] [compiler:testCompile]

[INFO] Nothing to compile - all classes are up to date

[INFO] [surefire:test]

[INFO] Surefire report directory: D:\mywork\my-app\target\surefire-reports

-------------------------------------------------------

 T E S T S

-------------------------------------------------------

Running com.mycompany.app.AppTest

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.141 sec

Results :

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

[INFO] [jar:jar]

[INFO] Building jar: D:\mywork\my-app\target\my-app-1.0-SNAPSHOT.jar

[INFO] [install:install]

[INFO] Installing D:\mywork\my-app\target\my-app-1.0-SNAPSHOT.jar to C:\Document

s and Settings\danlley\.m2\repository\com\mycompany\app\my-app\1.0-SNAPSHOT\my-a

pp-1.0-SNAPSHOT.jar

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESSFUL

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 27 seconds

[INFO] Finished at: Sun Apr 15 12:26:32 CST 2007

[INFO] Final Memory: 5M/9M

[INFO] ------------------------------------------------------------------------




 

 

其中的关键部分如下:




Installing D:\mywork\my-app\target\my-app-1.0-SNAPSHOT.jar to C:\Documents and Settings\danlley\.m2\repository\com\mycompany\app\my-app\1.0-SNAPSHOT\my-app-1.0-SNAPSHOT.jar




 

说明你刚刚生成的jar包已经放到了%userhome%\.m2\repository\






《 比ant更强大的工具maven2之自动生成工程 》 的评论也很精彩,欢迎您也添加评论。查看详细 >>





JavaEye推荐
上海:优秀公司德比:高薪诚聘 资深Java工程师
广州:优易公司:诚聘Java工程师,开发经理
上海:尤恩斯国际集团:诚聘开发工程师
北京:优秀公司NHNChina招聘:WEB开发,系统管理,JAVA开发, DBA



文章来源: http://danlley.javaeye.com/blog/101979

posted on 2007-07-17 13:38 伏羲狂徒 阅读(406) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: