Maven使用笔记

Maven是一个项目管理工具,它包含了一个项目对象模型 (Project Object Model),
一组标准插件(生命周期各阶段是绑定插件中的目标来完成项目构建工作)
一个项目生命周期(Project Lifecycle),
一个依赖管理系统(Dependency Management System).
 
一、Maven私服搭建(管理jar包依赖的JavaEE Web系统):
    nexus下载地址:http://www.sonatype.org/nexus/
    1. 安装私服。
       a. 解压nexus-2.10.0-02-bundle.zip。
          nexus-2.10.0-02:核心目录。
          -- bin/jsw:存放了操作Nexus应用的命令。
          -- conf :nexus.properties 配置文件可更改port、host、nexus-work、runtime。
 sonatype-work:工作目录。
    sonatype-work exusstorage:所有仓库存储目录。
 
       b. 配置启动nexus应用
          nexus-2.10.0-02/bin/jsw/ windows-x86-32:
 - console-nexus.bat :控制台运行nexus
 - start-nexus.bat:启动nexus服务
 - stop-nexus.bat:停止nexus服务
 - install-nexus.bat :安装nexus到window服务中
 - uninstall-nexus.bat:从window服务中删除nexus
 cmd启动nexus:sc start nexus-webapp
 cmd停止nexus: sc stop nexus-webapp
 
    2. nexus应用使用。
      a. 启动进入Nexus主界面:
         - http://localhost:8081/nexus/index.html
      b. 登录Nexus:admin/admin123
      c. 点击左边菜单Repositories查看所有仓库:
         - Public Repositories:仓库组。
         - 3rd party:第三方的仓库。
         - Maven Central:Maven中央仓库。
         - Releases:发行版存放仓库。
         - Snapshots:快照版存放仓库。
         - itcast Central: 自定义的仓库。
 
说明:四种类型的仓库:group(组)、proxy(代理)、hosted(本地)、virtual(虚拟)
 
      d. 创建自己的仓库。
         -- Add按钮后在configuration选项中进行配置.
 
         maven中央库地址: http://repo1.maven.org/maven2/
maven中央库搜索地址:http://search.maven.org/
    
 
二、Maven下载与安装:
   a. maven 官网地址:http://maven.apache.org/
 
   b. 安装maven: 
      -- 设置M2_HOME 变量  E:apache-maven-3.2.1
      -- 设置系统环境变量path.  E:apache-maven-3.2.1in
      -- 在E:apache-maven-3.2.1/conf/settings.xml文件中更改本地仓库路径:
         <localRepository>E:/apache-maven-3.2.1/repo</localRepository>
 
   c. 配置私服(镜像)。
        在conf/settings.xml文件中给定仓库下载的镜像:
-- id : 镜像唯一标识符,用来区分不同的mirror
        -- mirrorOf : 镜像哪些仓库 *:代表所有的仓库 central: 代表仓库id为central的仓库.
-- name : 镜像名称
        -- url : 镜像的URL
<mirror>
  <id>public</id>
  <mirrorOf>*</mirrorOf>
  <name>public Mirror</name>
  <url>http://localhost:8081/nexus/content/groups/public</url>
</mirror>
 
 
三、MyEclipse配置关联自己的Maven: 
 
     Window --> Preferences --> MyEclipse --> Maven4MyEclipse --> User setting 配置关联自已的Maven.
 
 
四、 Maven的核心概念
 
    1. Maven 插件和目标  (Plugins and Goals) 
    -- Maven构建项目由一组标准插件组成。
    -- 一个maven插件是由一个或者多个目标组成的。
    -- 一个目标是一个明确的任务,它可以作为单独的目标运行,
或者作为一个大的构建的一部分和其它目标一起运行。
一个目标是Maven中的一个“工作单元”。
目标的例子包括Compiler插件中的compile目标,它用来编译项目中的所有源文件,
或者Surefire插件中的test目标,用来运行单元测试。
 
    2. Maven 生命周期 (Lifecycle) mvn 
    -- Clean Lifecycle 在构建项目之前进行一些清理工作。
        pre-clean
clean
post-clean 
 
    -- Default Lifecycle 构建的核心部分,初始化、编译、测试、打包、验证、部署和站点生成等。
       validate    验证项目是否正确,以及所有为了完整构建必要的信息是否可用
       generate-sources     生成所有需要包含在编译过程中的源代码
       process-sources    处理源代码,比如过滤一些值
       generate-resources   生成所有需要包含在打包过程中的资源文件
       process-resources    复制并处理资源文件至目标目录,准备打包
       compile            编译项目的源代码
       process-classes    后处理编译生成的文件,例如对Java类进行字节码增强(bytecode enhancement)
       generate-test-sources 生成所有包含在测试编译过程中的测试源码
       process-test-sources 处理测试源码,比如过滤一些值
       generate-test-resources 生成测试需要的资源文件
       process-test-resources 复制并处理测试资源文件至测试目标目录
       test-compile        编译测试源码至测试目标目录
       test                使用合适的单元测试框架运行测试。这些测试应该不需要代码被打包或发布
       prepare-package        在真正的打包之前,执行一些准备打包必要的操作。这通常会产生一个包的展开的处理过的版本(将会在Maven 2.1+中实现)
       package                将编译好的代码打包成可分发的格式,如JAR,WAR,或者EAR
       pre-integration-test 执行一些在集成测试运行之前需要的动作。如建立集成测试需要的环境
       integration-test 如果有必要的话,处理包并发布至集成测试可以运行的环境
       post-integration-test 执行一些在集成测试运行之后需要的动作。如清理集成测试环境。
       verify                执行所有检查,验证包是有效的,符合质量规范
       install                安装包至本地仓库,以备本地的其它项目作为依赖使用
       deploy                复制最终的包至远程仓库,共享给其它开发人员和项目(通常和一次正式的发布相关)
 
    -- Site Lifecycle 生成项目报告,站点,发布站点。
       pre-site
       site
       post-site
       site-deploy
 
       说明:生命周期各阶段Phase由一个或多个插件的Goal目标集合而成。
             用mvn命令直接执行生命周期中的指定阶段(phase)
             mvn phase(阶段名称)
        
 
    3. Maven 坐标 (Coordinates) 
      坐标解释(groupId、artifactId、version):
      groupId:    创建这个项目的组织名称(一般为组织的域名反写)
      artifactId: 表示一个单独项目的唯一标识符(项目名或jar包)
      version:    项目的版本号
 
      注意:groupId、artifactId、version它们标识了一个唯一的项目。
 
    4. Maven 资源库(Repositories)  
        什么是Maven资源库? 资源存储的地方,即所有项目的jar库,插件库。
Maven库中有三种类型
        -- local : 本地库  
  <localRepository>C:/MyLocalRepository</localRepository>
        -- remote  : 远程仓库(代理仓库也就是自已的私服)
-- central : 中央存储库(http://repo1.maven.org/maven2/)
Maven的依赖jar包搜索序列:
第一种:本地库 ---> 中央库
第二种:本地库 ---> 远程库 ---> 中央库
 
 
    5. Maven 依赖管理 (Dependency Management) 
 
      a、Transitive Dependency (传递依赖)
          你的项目依赖于A,A又依赖于B。你的项目是否要声明你依赖于B? 
 Maven的回答是它帮你自动管理这种依赖的传递,
 你不需要声明你依赖于B,由Maven来做。
 
      b、Dependency Scope (依赖范围)
 
         举例来说,你开发时需要做测试,你需要依赖于junit的jar,但是部署应用时并不需要它,
因为单元测试不会在生产环境上跑,也就是说最终打包的jar或者war不包含junit的jar。
又如你开发web程序,你的servlet/jsp进行编译需要依赖于servlet-jsp的标准api(J2EE的jar),
但是部署时也是不需要它的,因为你的应用服务器肯定有这些东西。
 
         因此,Maven考虑了6中可能的scope供选择:
         - compile: 默认的scope。编译、测试、打包全都需要。compile参与依赖传递,就是说,
你的项目A依赖于B(依赖scope是compile),项目C依赖于你的项目A,那么C也就依赖于B。
 
         - provided: 表示JDK或者容器会在Runtime时提供这些(jar),
如上面说到的servlet api。provided的东西在编译和测试时会用到,不参与传递依赖。
 
         - runtime: 表示编译时不需要,但测试和运行时需要,最终打包时会包含进去。
 
         - test: 只用于测试阶段(测试的编译和测试的运行),典型的就是junit的jar。
 
         - system: 和provided类似,但要求jar是你的系统里已有的,
   不会在repository里找,如rt.jar,tools.jar这些。
 
         - import: 简单的说,你的项目的pom可以继承另一个项目的pom,
  从而继承了父项目的依赖关系,但是因为之后single inheritance的限制,
  所以创造了import,使得你可以“导入”或者说“继承”任何一到多个项目的依赖关系。
 
      c. Dependency Classier (依赖jdk版本)
         在jdk1.4下编译的还是jdk1.5 
 
      d. 解决依赖冲突(排除指定的依赖)
         <exclusions>
    <exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
    </exclusion>
        </exclusions>
 
    6. 站点生成和报告  (Site Generation and Reporting)
       -- mvn site 命令
          运行site生命周期阶段
 
五、Maven标准插件说明(-D指定属性):
 
    注意:标准插件的位置(第一次使用时它会去Maven中央库下载):
          Maven本地仓库orgapachemavenplugins
 
   mvn 插件:goal -D属性名=属性值 ……
 
   1. 编译源代码: mvn compiler:compile.
 
   2. 清除项目:mvn clean:clean (只留下源文件).
 
   3. 测试命令: 
      -- mvn surefire:test : 运行测试代码
 
   4. 安装:mvn install:install
      
   5. 只打jar包: mvn jar:jar (java项目)
 
   6. 只打war包: mvn war:war (web项目)
 
   7. 部署最终的包至远程仓库: mvn deploy:deploy
 
   8. 生成站点目录:mvn site:site   
 
   说明:Maven标准插件使用地址: http://maven.apache.org/plugins/index.html
 
六、Maven创建项目:
    1.创建web项目的两种方式: 
 
       第一种方式:选择原型目录创建项目:
mvn 插件名:目标 -D参数
mvn archetype:generate -DarchetypeCatalog=internal (10种原型目录来创建项目)
 说明:
 -- archetypeCatalog属性:internal(内部10), remote(远程1千多种)
 -- 7:代表java项目、10: 代表web项目
 
        第二种方式:通过myeclipse创建web项目(掌握).
 
 
    2.Maven创建出来的Web项目结构:
       src/main/java       java源文件
       src/main/resources  java资源文件
       src/main/webapp     webapp根目录
 
       src/test/java       test源文件
       src/test/resources  test资源文件
 
 
七、myeclipse创建Web项目(maven-archetype-webapp):
    1. mvn compile 编译java项目.
    2. mvn test 运行测试用例.
    3. mvn package 编译java项目并打成war包.
    4. mvn clean  清理java项目.
 
    a. 配置web项目依赖的jar包
       <dependency>
      <groupId>org.apache.tomcat</groupId>
      <artifactId>tomcat-jsp-api</artifactId>
      <version>7.0.65</version>
      <scope>provided</scope>
       </dependency>
       <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
      <scope>runtime</scope>
       </dependency>
    b. 配置web项目运行web容器插件
       <plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
    <port>8080</port>
    </configuration>
    </plugin>
     
     c. 设置jdk版本:
         1. 配置编译插件
         <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
<encoding>utf-8</encoding>
    </configuration>
    </plugin>
         2. 更新Maven项目
    
   
八、POM(Project Object Model)项目对象模型(pom.xml)配置:
 
    a、基础元素配置:
        <!-- 指定POM对象模型的版本号 -->
  <modelVersion>4.0.0</modelVersion>
  <!-- 指定组织ID(公司域名反写) -->
  <groupId>cn.itcast</groupId>
  <!-- 指定项目名 -->
  <artifactId>Struts2-MyBatis3</artifactId>
  <!-- 指定项目的版本  RELEASE(发行版)、SNAPSHOT(快照版) -->
  <version>1.0.0-RELEASE</version>
  <!-- 指定项目打包的产物(jar、ear、war、pom) -->
  <packaging>war</packaging>
 
    b、全局属性配置:
  <properties>
  <currentVersion>${project.version}</currentVersion>
<struts2.version>2.3.16.3</struts2.version>
<mybatis.version>3.2.7</mybatis.version>
<mysql.version>5.1.27</mysql.version>
<tomcat.version>7.0.47</tomcat.version>
  </properties>
  
    c、依赖关系配置:
  <dependencies>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
<type>jar</type>
   <classifier>jdk15</classifier>
      </dependency>
  </dependencies>
 
    d、构建元素配置:
        <!-- 构建项目元素 -->
  <build>
  <finalName>hrm</finalName>
  <!-- 定义默认的目标 -->
  <defaultGoal>package</defaultGoal>
  <!-- 源码目录 -->
  <sourceDirectory>${basedir}/src/main/java</sourceDirectory>
  <!-- 测试代码目录 -->
  <testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
  <!-- 资源文件 -->
  <resources>
 <resource>
 <!-- 源目录 -->
 <directory>${basedir}/src/main/resources</directory>
 </resource>
 </resources>
<plugins>
<plugin>
</plugin>
...
</plugins>
  </build>
 
   e、常用插件配置:
 
      1. 编译插件: 
        <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>utf-8</encoding>
</configuration>
        </plugin>
    
      2. 生成API文档插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<!-- 指定插件需要的配置信息 -->
<configuration>
<!-- 源文件编码 -->
<encoding>UTF-8</encoding> 
<!-- 生成时的编码 -->
<charset>UTF-8</charset> 
<!-- 文档的编码 -->
<docencoding>UTF-8</docencoding>
<!-- 文档标题 -->
<doctitle>传智播客</doctitle>
<!-- 窗口标题 -->
<windowtitle>OA办公管理系统</windowtitle>
</configuration>
<executions>
<execution>
<!-- 指定目标 -->
<goals><goal>javadoc</goal></goals>
<!-- 生命周期哪个阶段时,执行上面javadoc目标 -->
<phase>compile</phase>
</execution>
</executions>
 </plugin>
       3. tomcat插件:
 <plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<!-- 指定插件需要的配置信息 -->
<configuration> 
<!-- 设置端口 -->
<port>8080</port>
<!-- 设置支持热部署 -->
<contextReloadable>true</contextReloadable>
</configuration>  
 </plugin> 
       4. jetty插件:
 <plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.6.16.v20140903</version>
<configuration>
  <!-- 设置web项目配置信息 -->
  <webApp>
     <contextPath>/${project.artifactId}</contextPath>
  </webApp>
  <!-- 设置支持热部署 -->
  <scanIntervalSeconds>1</scanIntervalSeconds>
              <connectors>
                     <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                         <port>9090</port>
                     </connector>
                   </connectors>       
    </configuration>
 </plugin>
 
 注意:需换成jre路径(不要引用jdk路径)
 
   f、站点元素配置:
      
        <!-- 指定项目名称,一般用于文档生成 -->
<name>ssh</name>
<!-- 指定公司网站地址 -->
<url>http://maven.apache.org</url>
<!-- 指定项目描述 -->
<description>java架构项目</description>
<!-- 指定项目的开始时间 -->
<inceptionYear>2015</inceptionYear>





原文地址:https://www.cnblogs.com/dantefung/p/8465eb8cb95b6bba348ec22982eed5c3.html