- 论坛徽章:
- 0
|
Maven 是ant 以外的另外一种高级的构造工具。与Ant 不同的是,它内置了一套完整的构建生命周期,并且有一套严格的依赖管理机制。
从
Apache Maven
网站上下载最的maven,解压到硬盘(这里为/opt/build/maven)。
指定环境变量到maven 安装位置。
[hantsy@localhost ~]$export M2_HOME=/opt/build/maven
把$M2_HOME/bin加入到系统的path中。
[hantsy@localhost ~]$export PATH=$PATH:$M2_HOME/bin
你也可以把这两句写进用户的配置文件~/.bashrc 或系统的配置文件中,如/etc/profile.d/java.sh(参见
Fedora 9 使用手记(2)--安装java开发环境
)。
验证maven 是否安装正确。
[hantsy@localhost ~]$ mvn -v
Maven version: 2.0.9
Java version: 1.6.0_10-rc
OS name: "linux" version: "2.6.25.14-108.fc9.i686" arch: "i386" Family: "unix"
下面用maven创建第一个java 项目。
[hantsy@localhost ~]$mvn archetype:create -DgroupId=com.myapp -DartifactId=helloworld
这里用archetype:create 来创建了一个项目helloworld。后面-DgroupId=com.myapp
-DartifactId=helloworld不难理解,这是标准的java参数输入。groupId
是组织的id,artifactId是项目id。
如果是第一次运行maven , maven 会从中央资源库中下载必要的plugin和依赖jar 包。
进入项目helloworld目录,可以看到如下的目录结构。
helloworld
|-- pom.xml
`-- src
|-- main
| `-- java
| `-- com
| `-- myapp
| `-- App.java
`-- test
`-- java
`-- com
`-- myapp
`-- AppTest.java
src/main/java 是项目的源文件目录,src/test/java是项目的测试代码目录。pom.xml是项目的maven 配置文件,称为Project Object Model。
执行mvn package为项目打包。
[hantsy@localhost helloworld]$mvn package
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building helloworld
[INFO] task-segment: [package]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Compiling 1 source file to /home/hantsy/Projects/helloworld/target/classes
[INFO] [resources:testResources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:testCompile]
[INFO] Compiling 1 source file to /home/hantsy/Projects/helloworld/target/test-classes
[INFO] [surefire:test]
[INFO] Surefire report directory: /home/hantsy/Projects/helloworld/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.myapp.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.061 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] [jar:jar]
[INFO] Building jar: /home/hantsy/Projects/helloworld/target/helloworld-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5 seconds
[INFO] Finished at: Thu Sep 11 19:12:32 CST 2008
[INFO] Final Memory: 7M/18M
[INFO] ------------------------------------------------------------------------
和前面不同的是,这里package没有前缀,它是一个phase,也就是maven生命周期中的一个步骤。
从执行结果来,它执行了下面几个goal。
resources:resources
compiler:compile
resources:testResources
compiler:testCompile
surefire:test
jar:jar
mvn package 会执行package 之前所有phase 的任务。
测试刚刚打好的包。
[hantsy@localhost ~]$java -cp target/helloworld-1.0-SNAPSHOT.jar com.myapp.App
Hello World!
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/1096/showart_1194010.html |
|