现在的位置: 首页 > 综合 > 正文

maven基础三

2013年08月09日 ⁄ 综合 ⁄ 共 4037字 ⁄ 字号 评论关闭

实例:

1。创建一个maven项目simple-weather

C:/>mvn archetype:create -DgroupId=cn.vicky.maven.ch04 -DartifactId=simple-weather -DpackageName=cn.vicky.maven -Dversion=1.0   (覆盖掉默认的1.0-SNAPSHOT)

 

2。我们想要做的是添加一些关于项目许可证,组织以及项目相关开发人员的一些信息。

 

3。添加新的依赖

     这个项目,我们将使用到: Dom4J 和 Jaxen ,为了格式化这个命令行程序的输出,我们将会使用 Velocity ,我们还需要加入对 Log4j 的依赖,用来做日志。

     可以使用http://www.mvnrepository.com 然后搜索一些常用的类库,如 Hibernate 或者 Spring Framework 。

 

4。运行mvn install 那么maven将自动下载以来的库文件!

5。编写程序

6。本项目依赖于两个 classpath 资源: Main 类通过 classpath 资源log4j.preoperties 来配置 Log4J , WeatherFormatter 引用了一个在classpath 中的名为output.vm 的 Velocity 模板。为了添加这些资源,我们需要在项目的基础目录下创建一个新的目录——
src/main/resources。由于任务archetype:create 没有创建这个目录,我们需要通过在项目的基础目录下运行下面的命令来创建它:

     $ cd src/main
     $ mkdir resources
     $ cd resources

 

7。运行mvn install。

     如果出现maven错误(1.3不支持泛型)

     请配置pom.xml

     运行项目mvn exec:java -Dexec.mainClass=org.sonatype.mavenbook.weather.Main

     Exec 插件让我们能够在不往 classpath 载入适当的依赖的情况下,运行这个程序。

 

8。打印项目列表

     mvn dependency:resolve

 

com.ibm.icu:icu4j:jar:2.6.1:compile
commons-collections:commons-collections:jar:3.1:compile
commons-lang:commons-lang:jar:2.1:compile
dom4j:dom4j:jar:1.6.1:compile
jaxen:jaxen:jar:1.1.1:compile
jdom:jdom:jar:1.0:compile
junit:junit:jar:3.8.1:test
log4j:log4j:jar:1.2.14:compile
oro:oro:jar:2.0.8:compile
velocity:velocity:jar:1.5:compile
xalan:xalan:jar:2.6.0:compile
xerces:xercesImpl:jar:2.6.2:compile
xerces:xmlParserAPIs:jar:2.6.2:compile
xml-apis:xml-apis:jar:1.0.b2:compile
xom:xom:jar:1.0:compile

     正如你能看到的,我们项目拥有一个很大的依赖集合。虽然我们只是为四个类库引入了直接的依赖,看来我们实际共引入了15 个依赖。 Dom4J 依赖于 Xerces 和 XML解析器 API ,Jaxen 依赖于 Xalan,后者也就在 classpath 中可用了。

 

9。查看详细的依赖树。

     在8中,我们可以查看,4个jar包的依赖jar包,但我们,无法获悉,具体某个jar包依赖那些jar包。如果你想知道你项目的整个依赖树,你可以运行 mvn dependency:tree 目标。

cn.vicky.maven.ch04:simple-weather:jar:1.0
+- log4j:log4j:jar:1.2.14:compile
+- dom4j:dom4j:jar:1.6.1:compile
|  /- xml-apis:xml-apis:jar:1.0.b2:compile
+- jaxen:jaxen:jar:1.1.1:compile
|  +- jdom:jdom:jar:1.0:compile
|  +- xerces:xercesImpl:jar:2.6.2:compile
|  /- xom:xom:jar:1.0:compile
|     +- xerces:xmlParserAPIs:jar:2.6.2:compile
|     +- xalan:xalan:jar:2.6.0:compile
|     /- com.ibm.icu:icu4j:jar:2.6.1:compile
+- velocity:velocity:jar:1.5:compile
|  +- commons-collections:commons-collections:jar:3.1:compile
|  +- commons-lang:commons-lang:jar:2.1:compile
|  /- oro:oro:jar:2.0.8:compile
/- junit:junit:jar:3.8.1:test

     如果你还不满足,或者想要查看完整的依赖踪迹,包含那些因为冲突或者其它原因而被拒绝引入的构件,打开 Maven 的调试标记运行:mvn install -X

 

10。编写单元测试

     Maven 内建了对单元测试的支持,测试是 Maven 默认生命周期的一部分.

     这部分略过……

 

11。添加一个测试范围依赖

     某些库文件,仅仅是对测试使用的,所以可以为这些库文件,添加范围配置。如<scope>test</scope>

 

 12。打包,发布项目

     Maven Assembly 插件是一个用来创建你应用程序特有分发包的插件。要配置 Maven Assembly 插件, 我们需要在pom.xml 中的 build 配置中添加如下的plugin 配置。

    运行 : mvn install assembly:assembly  构建装配

    现在在target/simple-weather-1.0-jar-with-dependencies.jar 装配好了!

 

13。装配好项目后,我们可以运行项目

cd target

C:/simple-weather/target>java -cp simple-weather-1.0-jar-with-dependencies.jar org.sonatype.mavenbook.weather.Main 10002

抱歉!评论已关闭.