`

初学 Maven

阅读更多
一,使用maven命令生成项目
mvn archetype:generate
-DgroupId=xxxx
-DartifactId=xxx
-DpackageName=xxxx
-Dversion=xxx
goal是maven中最小的执行单元, 如此例的goal是  generate
一些编号(版本3.0.5):
306- simple web


二, pom文件结构
执行 mvn help:effective-pom 来查看effective pom
所有的pom都继承自super pom,而effective pom 就包含super pom

1) Project是所有pom.xml的根元素,并且在里面定义了命名空间和xsd元素
<project xmlns="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/xsd/maven-4.0.0.xsd">

2)ModelVersion 当前pom模型的版本
<modelVersion>4.0.0</modelVersion>

3)GroupId定义当前maven项目隶属的实际项目
<groupId>com.test.mvn</groupId>

4)ArtifactId定义项目中的某个模块名称
<artifactId>MavenTest</artifactId>

5)Version 定义maven项目当前所处的版本号
<version>0.0.1-SNAPSHOT</version>

6)Packaging定义maven项目的打包方式。
<packaging>jar</packaging>
如:
<groupId>org.pango.user</groupId>
<artifactId>user </artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
在空间中的格式:groupId:artifactId:packaging:version,
如: org.pango.user:user:jar:0.0.1-SNAPSHOT

7)Dependencies 元素底下就是加入依赖包的地方

8)每个dependency都是一个依赖包,依赖包也就是在dependency里面定义各个依赖包的坐标,这样maven就会从网上下载依赖包到你本地仓库中,有所不同的是dependency元素包含了一个子元素,这个就是对maven生命周期的一个说明,当然除了上面四个子元素外,还包含几个其他的元素:
(1) type说明依赖的类型
(2) optional标记依赖是否可选
(3) exclusions 用来排斥传递依赖
<dependency>
<groupId>实际项目</groupId>
<artifactId>模块</artifactId>
<version>版本</version>
<type>依赖类型</type>
<scope>依赖范围</scope>
<optional>依赖是否可选</optional> <!—主要用于排除传递性依赖-->
<exclusions>
  <exclusion>
   <groupId>…</groupId>
   <artifactId>…</artifactId>
  </exclusion>
</exclusions>
</dependency>

三, MVN常用命令
1)mvn clean -->表示运行清理操作(会默认把target文件夹中的数据清理)
2)mvn clean compile-->表示先运行清理之后运行编译,会见代码编译到target文件夹中
3)mvn clean test-->运行清理和测试
4)mvn clean package-->运行清理和打包
5)mvn clean install-->运行清理和安装,会将打好的包安装到本地仓库中,以便其他的项目可以调用
6)mvn clean deploy-->运行清理和发布(发布到私服上面)

四,传递依赖规则
1) 最短路径原则:如果A对于依赖路径中有两个相同的jar包,那么选择路径短的那个包;
2) 第一声明优先原则:如果A对于依赖路径中有两个相同的jar包,路径也相同,那么依赖写在前面的优先;
3)可选依赖不会传递,如A->B,B->C,B->D,A对B直接依赖,B对C和D是可选依赖,那么在A中不会引入C和D;

查看依赖列表:
1, mvn dependency:resolve   平滑的打印出你的项目所有依赖包
2, dependency:tree     依赖树的形式打印出
3, mvn install –X  全部显示,包含被reject了的artifact文件

五,依赖范围(<scope>)
1)test范围指的是测试编译和执行期间有效,在编译和打包时都不会使用这个依赖
2)compile范围指的是编译范围有效,在编译和打包时都会将依赖存储进去
3)provided依赖:在编译和测试的过程有效,最后生成war包时不会加入,诸如:servlet-api,因为servlet-api,tomcat等web服务器已经存在了,如果再打包会冲突
4)runtime在运行的时候依赖,在编译的时候不依赖

scope      编译期  测试期   运行期 说明
*compile V V V     默认scope
test V       只在测试期依赖,如junit包
provided V V       运行期由容器提供,如servlet-api包
runtime V V       编译期间不需要直接引用
system V V       编译和测试时由本机环境提供


六,聚合
<modelVersion>4.0.0</modelVersion>
<groupId>org.pango.user</groupId>
<artifactId>user-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>     ///必须是pom

<modules>
<module>../user-core</module>
<module>../user-log</module>
<module>../user-dao</module>
<module>../user-service</module>
</modules>

七,继承
假设前面已经有写好的一个父pom.xml, 可以用过这样来继承
<parent>
<groupId>org.pango.user</groupId>
<artifactId>user-parent</artifactId>    //父模块的gid和aid
<version>0.0.1-SNAPSHOT</version>
<relativePath>../user-parent/pom.xml</relativePath>
</parent>

选择继承:
pom中pluginManagement(dependencyManagement)标签下的配置只是定义, 并不会被使用.子模块中需要显式的激活.
将依赖放入dependencyManagement
<dependencyManagement>
<dependencies>
  <dependency>
    ...
  </dependency>
</dependencies>
</dependencyManagement>
把继承的配置加入到dependencyManagement中,对于继承的项目,我需要引入依赖的声明,但不需要进行版本声明,如下:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>

可继承的POM元素:
1.groupId :项目组ID ,项目坐标的核心元素;
2.version :项目版本,项目坐标的核心元素;
3.description :项目的描述信息;
4.organization :项目的组织信息;
5.inceptionYear :项目的创始年份;
6.url :项目的url 地址
7.developers :项目的开发者信息;
8.contributors :项目的贡献者信息;
9.distributionManagerment :项目的部署信息;
10.issueManagement :缺陷跟踪系统信息;
11.ciManagement :项目的持续继承信息;
12.scm:项目的版本控制信息;
13.mailingListserv :项目的邮件列表信息;
14.properties :自定义的Maven 属性;
15.dependencies :项目的依赖配置;
16.dependencyManagement:醒目的依赖管理配置;
17.repositories :项目的仓库配置;
18.build :包括项目的源码目录配置、输出目录配置、插件配置、插件管理配置等;
19.reporting :包括项目的报告输出目录配置、报告插件配置等。

八, 生命周期
生命周期包含项目的清理、初始化、编译、测试、打包、集成测试、验证、部署、站点生成等几乎所有的过程。
maven包括三套生命周期,分别是:
- Clean Lifecycle 在进行真正的构建之前进行一些清理工作。
- Default Lifecycle 构建的核心部分,编译,测试,打包,部署等等。
- Site Lifecycle 生成项目报 告,站点,发布站点。

1,Clean生命周期一共包含了三个阶段:
- pre-clean 执行一些需要在clean之前完成的工作
- clean 移除所有上一次构建生成的文件
- post-clean  执行一些需要在clean之后立刻完成的工作
如果我们运行mvn post-clean,那么 pre-clean,clean 都会被运行 (向前包含)。

2,Site生命周期的各个阶段:
- pre-site 执行一些需要在生成站点文档之前完成的工作
- site 生成项目的站点文档
- post-site 执行一些需要在生成站点文档之后完成的工作,并且为部署做准备
- site-deploy 将生成的站点文档部署到特定的服务器上

3,Default生命周期:
- validate
- generate-sources
- process-sources
- generate-resources
- process-resources 复制并处理资源文件,至目标目录,准备打包。
- compile 编译项目的源代码。
- process-classes
- generate-test-sources 
- process-test-sources
- enerate-test-resources
- process-test-resources 复制并处理资源文件,至目标测试目录。
- test-compile 编译测试源代码。
- process-test-classes
- test 使用合适的单元测试框架运行测试。这些测试代码不会被打包或部署。
- prepare-package
- package 接受编译好的代码,打包成可发布的格式,如JAR。
- pre-integration-test
- integration-test
- post-integration-test
- verify
- install 将包安装至本地仓库,以让其它项目依赖。
- deploy

九,插件(以测试插件surefire为例)
http://maven.apache.org/plugins/index.html 插件地址。
对于匹配测试类,maven会去src/test/java 下面去寻找类,此文件下面的类需要符合以下
的规则:
- Test*.java:任何目录以Test开头的类
- *Test.java:任何以Test结尾的类
- *TestCase.java:任何以TestCase结尾的类

1,Surefire的test goal,它默认会执行src/test/java下面的 所有/Test*.java, /Test.java和/*/*TestCase.java文件;并且会生成报告目录 surefire-reports.
配置surefire插件:
<build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>   //surefire plugin
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.15</version>
    <configuration>
<testFailureIgnore>true</testFailureIgnore>  //错误仍然继续执行
    </configuration>
<executions>        //executions用于配置测试阶段
           <execution>
              <id>default-test</id>
              <phase>test</phase>        //test阶段
              <goals>
                 <goal>test</goal>      
              </goals>
            <!-- Disable unit test execution by setting skip=true -->           
              <configuration>
                <skip>true</skip>         //跳过测试步骤
              </configuration>
           </execution>
            <execution>
               <id>default-cli</id>
               <phase>integration-test</phase>
               <goals>
                  <goal>test</goal>
               </goals>
               <configuration>
                  <skip>false</skip>
                  <systemPropertyVariables>
                    <webdriver.port>${webdriver.port}</webdriver.port>
                    <webdriver.driver>${webdriver.driver}</webdriver.driver>
                    <webdriver.url>${webdriver.url}</webdriver.url>
                  </systemPropertyVariables>
                  <includes>
                    <include>**/CC/AA/*.java</include>
                    <include>**/BB/*.java</include>
                  </includes>
                </configuration>
              </execution>
    </executions>
   </plugin>
<plugins>
</build>

2,打包生成jar 的plugin:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>   //assembly plugin
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
或使用  mvn install assembly:assembly

2,将assembly goal 加到package阶段:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>      //assembly plugin
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>     
<execution>
<id>simple-command</id>
<phase>package</phase>
<goals>
<goal>attached</goal>     //assembly:attached 在package会被执行
</goals>
</execution>
</executions>
</plugin>

3,使用compile pulgin:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>     // compile plugin
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration> 
</plugin>

4,配置jetty plugin:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
</plugin>
这里需要先配置一下servlet依赖:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>   //表示这个包已被servlet容器提供了,不需要再去下载它
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
使用mvn jetty:run 来启动jetty服务来启动服务, 然后http://localhost:8080/simple-webapp/simple 看看成功没

十,公共依赖
1,在父模块中使用,将公共的依赖放进<dependencyManagement>:
<dependencyManagement>     //公共模块放入dependencyManagement
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.0.7</version>
</dependency>
     ...
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.3.0.ga</version>
</dependency>
</dependencies>
</dependencyManagement>
同样也可以将公共的插件放到父模块中, 使用pluginManagement

2, 分析插件冲突
mvn dependency:analyze

十一,远程仓库配置
<repositories>    //repositories下声明一个或多个repository子元素
<repository> 
      <id>jboss</id>                  //id必须唯一
      <name>JBoss Repository</name> 
      <url>http://repository.jboss.com/maven2/</url>    //地址
      <releases> 
        <enabled>true</enabled> 
      </releases> 
    <snapshots> 
      <enabled>false</enabled>        //快照版的下载支持
      <updatePolicy>daily</updatePolicy>   //远程仓库更新的频率如daily,always等 
      <checksumPolicy>ignore</checksumPolicy> //配置检验文件的策略,下载构件的时候会校验文件,如果失败的时候会怎么办,如果为warn 会警告,fail就失败,ignore忽略
    </snapshots> 
<layout>default</layout> 
</repository> 
  </repositories>

十二, 设置代理
<settings> 
  ... 
  <proxies> 
    <proxy> 
      <id>my-proxy</id> 
      <active>true</active> 
      <protocol>http</protocol> 
      <host>127.0.0.1</host> 
      <port>8087</port> 
      <!-- 
        <username>***</username> 
        <password>***</password> 
        <nonProxyHosts> 
          repository.mycom.com|*.google.com 
        </nonProxyHosts> 
      --> 
    </proxy> 
  </proxies> 
  ... 
</settings>

十三, 手动注册jar包到本地库
mvn install:install-file
-DgroupId=com.danga
-DartifactId=memcached
-Dversion=2.0.1
-Dfile=java_memcached-release_2.0.1.jar
-Dpackaging=jar
-DgeneratePom=true

十四, maven提示内存溢出
找到文件%M2_HOME%\bin\mvn.bat ,这就是启动Maven的脚本文件
加上 set MAVEN_OPTS=-XX:MaxPermSize=128M
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics