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)文件如下
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
执行结果如下:
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
生成文件如下: 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\
|