Maven使用小结

使用:

第一步:项目团队开发,一定要先更新公共模块,对依赖进行maven clean(本地清理)、 java clean(编译) 、maven install(安装到本地仓库)

第二步:maven clean(本地清理)、maven updateSnapshot(从本地仓库获取公共模块的最新快照)、java clean(编译)、maven install(安装到本地仓库)


Maven的pom.xml文件可分成四部分:

项目基本信息部分、依赖配置部分、构建环境部分、构建设置、其他扩展部分

其他扩展部分有:

repositories中央仓库配置,按顺序进行

distributionManagement,发布提交配置

profiles主要用户重点研究 

Maven主要思想是以生命周期为主线,在每个阶段完成各个目标。Maven一个插件对应多个目标,在每个阶段都是有插件对应的目标进行工作的。

举例来说:就像是一个角色(插件)有多个人(目标),一个任务(生命周期)需要多个角色(插件和插件的目标)配合而完成。Maven这种安排工作的方法是面向对象的。另外,Maven在POM配置文件中重点描述的是要达成的目的而非过程。

Maven的POM文件继承和依赖的继承以及对依赖版本号的统一管理思想也是面向对象的。(这里有几个概念,超级POM、多模块管理parent、在pom中通过定义全局参数统一管理版本号)

超级POM中定义了项目结构的约定,项目生命周期各个阶段的默认插件和目标。

 

 

本地maven私服配置和使用

maven私服配置:

1、下载nexus组件,驱动该组件,在web页面正确配置hosted、proxy、group信息,主要是proxy用来指明代理的仓库地址设置proxy更新本地索引,hosted用来指明第三方插件

2、设置用户名和密码

3、查看插件中央服务的仓库sonatype-work\nexus\storage

maven私服使用:两个位置,一个是pom.xml配置,另一个是setting文件配置

  1. setting文件配置

     

    1. 服务器安全权限:

      <id>my-nexus-releases</id> 
                <username>admin</username> 
                <password>admin123</password> 
              </server> 
              <server> 
                <id>my-nexus-snapshot</id> 
                <username>admin</username> 
                <password>admin123</password> 
              </server> 

    2. 下载地址列表

      <mirror>    
               <id>nexus</id>     
               <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>    
               <mirrorOf>*</mirrorOf>    
             </mirror>

    3. 部署到远程服务器的地址

      <profile>
          <repository>
          <id>central</id>
         <name>central</name>
         <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>
         <layout>default</layout>
         <releases>
          <enabled>true</enabled>
         </releases>
         <snapshots>
          <enabled>true</enabled>
         </snapshots>
          </repository>
         </profile>
          <!-- 执行:$ mvn release:prepare 命令时会打包并发布到该仓库。 -->
          <profile>
            <id>nexus</id>
            <repositories>
              <repository>
                <id>nexus</id>
                <name>local private nexus</name>
                <url>http://127.0.0.1:8081/nexus/content/groups/public</url>
              </repository>
            </repositories>
          </profile>
          <!-- 执行:$ mvn deploy 命令时会打包并发布到该仓库。 -->
          <profile>
            <id>nexus-snapshots</id>
            <repositories>
              <repository>
                <id>nexus-snapshots</id>
                <name>local private nexus snapshots</name>
                <url>http://127.0.0.1:8081/nexus/content/groups/public-snapshots</url>
              </repository>
            </repositories>
          </profile>

    4. 激活部署配置

      <activeProfile>central</activeProfile>
          <activeProfile>nexus</activeProfile>
          <activeProfile>nexus-snapshots</activeProfile>

       

  2. pom.xml文件配置

    1. 仓库地址

      <repositories>
        <repository>
         <id>nexus</id>
         <name>my-nexus-repository</name>
         <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>
         <releases>
          <enabled>true</enabled>
         </releases>
         <snapshots>
          <enabled>true</enabled>
         </snapshots>
        </repository>

    2. 构建发布地址

      <distributionManagement> 
                 <repository> 
                     <id>my-nexus-releases</id> 
                     <url>http://127.0.0.1:8081/nexus/content/repositories/releases/</url> 
                 </repository> 
                  
                 <snapshotRepository> 
                     <id>my-nexus-snapshot</id> 
                     <url>http://127.0.0.1:8081/nexus/content/repositories/snapshots/</url> 
                 </snapshotRepository> 
            </distributionManagement>

       

 

Maven的使用方式有继承和多模块组合管理两种:组合是面向对象管理,继承是面向资源和行为的继承

1、继承采用parent关键字,在子项目中直接采用parent关键字指明父项目的groupid,子项目将继承父项目pom中的所有依赖。可通过依赖管理实现对依赖的统一管理(顶层pom定义依赖管理的版本号,子模块定义依赖,版本号来源于父pom)

2、多模块组合管理采用pom格式,从上向下看就是一种管理上的策略,定义pom管理模块中需要定义子模块<modules>列表,子模块之间可以相互依赖。构建顶级pom时会对所有子模块进行构建。

 

对maven继承和组合的应用:

  1. 如果一个项目小组有自己的类库,可以通过定义一个顶级POM,让所有的子模块进行通过继承实现以来管理,可以继承父POM定义的生命周期

  2. 如果多个项目模块需要每个子模块独立定义自己的依赖和生命周期,同时他们自己可以互相依赖,构建顶级POM会对所有的子模块进行构建。

  3. 模块中引用pom依赖,使用方法是将某一类依赖定义到一个pom文件中去,然后在模块中引用这个pom文件作为依赖。

 

Maven是通过pom文件和插件进行工作的。

Maven是用来管理依赖,项目编译、测试、打包、发布、的工具

Maven可以将编译好的jar更新到本地库,本地其他项目如果涉及到jar的引用,可以直接得到最新的jar引用。

Maven可以从本地私服中获取公共jar信息,可以讲本地jar提交到私服中供项目组人员使用。

Maven会自动管理依赖的依赖。

Maven可以实现项目的模块化管理,通过parent关键字可实现。

 

1)创建一个maven项目
  mvn archetype:create
  -DgroupId=org.sonatype.mavenbook.ch03
  -DartifactId=simple
  -DpackageName=org.sonatype.mavenbook

 

  相关解释说明
  mvn archetype:create  通过archetype插件快速创建一个项目
  -Dname=value  这样的对是将会被传到目标中的参数,java用来设置系统属性的方式
  artifactId          项目的基础目录(simple)
  simple项目下的pom.xml文件 描述了项目,配置了插件,声明了依赖
  src/main/java           java类文件
  src/main/resources  classpath资源文件
  src/test/java            测试java类文件
  src/test/resources    测试classpath资源文件

 

  (2)构建并打包项目(在包含pom.xml文件下运行)
  mvn install     
  运行后在target目录下生成simple-1.0-SNAPSHOT.jar文件(名称根据pom.xml文件配置得来),
  并安装到我们的本地maven仓库。


  (3)maven的pom.xml文件说明:

<groupId>org.sonatype.maven.test</groupId>

<artifactId>simple</artifactId>

<version>1.0-SNAPSHOT</version>

<packaging>jar</packaging>

 以上四个元素是maven的坐标,唯一标识了一个项目。

<name>simple</name>

<url>http://maven.apache.org</url>

 

 以上二个元素是pom提供的描述性元素,给人提供可阅读的名字。

<dependencies>

 <dependency>

 <groupId>junit</groupId>

 <artifactId>junit</artifactId>

 <version>3.8.1</version>

 <scope>test</scope>

</dependency>

</dependencies>

 <dependencies>元素定义了项目的相关依赖,
  <scope>元素为test,说明只有在运行compiler:testCompile和surefire:test时才会被加到classpath中。
  <scope>元素如果为provided,则依赖在编译的时候需要,但是不应该被捆绑在构建的输出中。
                 在开发web应用时很有用。
  maven支持传递性依赖,会隐式地把相关依赖间接依赖的包也加到项目中。

 

(4)验证程序是否工作(java -cp 指定类运行所依赖其他类的路径,多个依赖包用;隔开)
      java -cp target/simple-1.0-SNAPSHOT.jar org.sonatype.maven.App

 

(5)-查看有效的(effective)pom,即maven真正运行根据的pom
      mvn help:effective-pom (在项目的基础目录下simple运行)

 

(6)maven项目打包
  mvn package 运行到打包为止的所有阶段,包含以下一系列插件目标
  mvn resources:resources
         compiler:compile
        resources:testResources
        compiler:testCompile
        surefire:test
        jar:jar
    
(7)浏览maven中央仓库  
        http://repo2.maven.org/maven2
  maven仓库的目录存储格式
      <groupId> / <artifactId> / <version> / <artifactId>-<version>.<packaging>

 

(8)生成站点文档和报告
       mvn site (生成文档目录在target/site目录下)

 

(9)查看相关依赖的groupId和artifactId
       http://mvnrepository.com/ (搜索依赖可查看pom.xml文件中的依赖元素)

 

(10)浏览项目的相关依赖(打印出已解决依赖的列表)
  1.mvn dependency:resolve(在项目目录下执行)
  2.mvn dependency:tree    (浏览项目的整个依赖树,包含间接依赖)


(11)执行单元测试
         mvn test      
  1.忽略单元测试失败,需要设置Surefire的testFailureIgnore的属性为true

<build>

 <plugins>

  <plugin>

   <groupId>org.apache.maven.plugins</groupId>

   <artifactId>maven-surefire-plugin</artifactId>

   <configuration> 

     <testFailureIgnore>true<testFailureIgnore>

   </configuration>

  <plugin>

 </plugins>

</build>

 2.跳过单元测试,需要设置Surefire的skip的属性为true

<build>

 <plugins>

  <plugin>

   <groupId>org.apache.maven.plugins</groupId>

   <artifactId>maven-surefire-plugin</artifactId>

   <configuration>

    <skip>true</skip>

   </configuration>

  </plugin>

 </plugins>

</build>

(12)创建Maven的Web应用(需指定archetypeArtifactId为maven-archetype-webapp,打包成war)
       mvn archetype:create
       -DgroupId=org.sonatype.mavenbook.ch05
       -DartifactId=simple-webapp
       -DpackageName=org.sonatype.mavenbook
       -DarchetypeArtifactId=maven-archetype-webapp

  打包的War文件默认名称为<artifactId>-<version>.war
  如果定义了finalName,则包名为<finalName>.war.
  如:<finalName>simple-webapp</finalName>

 

(13)配置Maven Jetty插件
    1.在pom.xml文件中配置

 <build>

  <finalName>simple-webapp</finalName>

  <plugins>

   <plugin>

    <groupId>org.mortbay.jetty</groupId>

    <artifactId>maven-jetty-plugin</artifactId>  

   </plugin>

  </plugins>

  </build>

2.启动Web项目(调用Jetty插件的run目标)
   mvn jetty:run
  启动完后就可以通过(http://localhost:8080/simple-webapp/)访问。

 

(14)Pom相关
  1.asm包(字节码操作)依赖包如果版本不一致,则项目会出问题。

  2.所有的pom文件都从超级pom继承,超级pom存放于
  {M2_HOME}\lib\maven-2.2.1-uber.jar\org\apache\maven\project

  3.查看项目的有效POM,超级POM和项目POM的合并(打印出XML文档)
  mvn help:effective-pom

  4.版本号格式(例:1.3.5, 1.3-beta-01)
  <主版本>.<次版本>.<增量版本>-<限定版本>

  5.pom的属性引用(${})
  例:${project.groupId}-${project.artifactId}
  maven提供了三个隐式的变量
  1.env 例:${env.PATH},暴露了操作系统,访问系统的环境变量
  2.project 例:${project.groupId},暴露了POM,访问POM的信息。
  3.settings 例:${settings.offline},暴露了Maven Settings的信息,
  引用settings.xml文件中offline元素的值。

 

(15)项目依赖的依赖范围
  1.compile(编译范围),默认的范围,在所有的classpath中可用,同时也会被打包。
  2.provided(已提供范围),只有在容器提供该依赖后才可使用,在编译时可用,但不会被打包。(例:Servlet Api)
  3.runtime(运行时范围),只有在运行和测试系统时需要,编译时不需要。(例:JDBC驱动)
  4.test(测试范围),只有在测试编译和测试运行阶段可用,编译和运行时不需要。
  5.system(系统范围),与provided类似,必须显示的提供一个对于本地系统中JAR文件的路径。不推荐使用。

 

  6.使用依赖的多个版本
  (,) 不包含量词
  [,] 包含量词
  例:<version>[3.8,4.0]</version> 依赖于3.8-4.0之间的版本
  <version>[,3.8.1]</version> 依赖于<=3.8.1的版本

  7排除传递性依赖(配置exclusions元素)

<dependency>

 <groupId>log4j</groupId>

 <artifactId>log4j</artifactId>

 <version>1.2.16</version>

 <exclusions>

  <exclusion>

   <groupId>javax.mail</groupId>

   <artifactId>mail</artifactId>

  </exclusion>

  <exclusion>

   <groupId>javax.jms</groupId>

   <artifactId>jms</artifactId>

  </exclusion>    

 </exclusions>

</dependency>

8.统一依赖版本号
    在子项目中引用依赖而不用显示的列出版本号(dependencyManagement元素),
  版本升级时不用手工的一个个修改依赖的pom.xml文件。

<dependencyManagement>

  <dependencies>

    <dependency>

      <groupId>mysql</groupId>

      <artifactId>mysql-connector-java</artifactId>

      <version>5.1.2</version>

    </dependency>

  </dependencies>

</dependencyManagement>

在子项目中引用依赖,不用指定版本号

<dependencies>

  <dependency>

   <groupId>mysql</groupId>

   <artifactId>mysql-connector-java</artifactId>  

  </dependency>

</dependencies>

  如果子项目中指定了版本,将覆盖顶层POM的dependencyManagement元素的版本号。

 

  9.多模块项目管理
  将很多项目归类在一起,成为一个构建,打包类型总是POM.

<modules>

  <module>sub-gooup-a</module>

  <module>sub-gooup-b</module>

</modules>

10.项目继承(当一个项目声明一个parent的时候,它从父项目的POM中继承信息)

<parent>

  <groupId>com.training.killerapp</groupId>

  <artifactId>a-parent</artifactId>

  <version>1.0-SNAPSHOT</version>

</parent>

(16).maven生命周期
  (1)清理生命周期(删除整个构建目录,target目录)
  mvn clean
  (2)默认生命周期阶段
  1.validate 验证项目是否正确
  2.generate-sources 生成所有需要包含在编译过程中的源代码
  3.process-sources 处理源代码,比如过滤一些值。
  4.generate-resources 生成所有需要包含在打包过程中的资源文件
  5.process-resources  复制并处理资源文件至目标目录,准备打包
  6.compile 编译项目的源代码
  7.process-classes 后处理编译生成的文件,例如对Java类进行字节码增强
  8.generate-test-sources 生成所有包含在测试编译过程中的测试源码
  9.process-test-sources 处理测试源码,比如过滤一些值
  10.generate-test-resources 生成测试需要的资源文件
  11.process-test-resources 复制并处理测试资源文件至测试目标目录
  12.test-compile 编译测试源码至目标目录
  13.test 使用合适的单元测试框架运行测试。这些测试应该不需要代码被打包或发布
  14.prepare-package 在真正的打包之前,执行一些准备打包必要的操作。
   这通常会产生一个包的展开的处理过的版本
  15.package 将编译好的代码打包成可分发的格式 ,如JAR,WAR,或者EAR
  16.pre-integration-test 执行一些在集成测试运行之前需要的动作。如建立集成测试需要的环境
  17.integration-test 如果有必要的话,处理包并发布至集成测试可以运行的环境
  18.post-integration-test 执行一些在集成测试运行之后需要的动作。如清理集成测试环境
  19.verify 执行所有检查,验证包是有效的,符合质量规范
  20.install 安装包至本地仓库,以备本地的其它项目作为依赖使用
  21.deploy 复制最终的包至远程仓库,共享给其它开发人员和项目(通常和一次正式的发布相关)

 

(17).过滤资源,替换属性
  默认的Maven行为会跳过过滤,只是将资源复制到输出目录。需显示地配置资源过滤

<build>

<filters>

 <filter>src/main/filters/default.properties</filter>

</filters>

<rosources>

 <directory>src/main/resources</directory>

 <filtering>true</filtering>

</resources>

</build>

(18).使用Maven Profile覆盖Compiler插件配置

<profiles>

<profile>

<id>production</id>

<build>

<plugins>

  <plugin>

    <inherited>true</inherited>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-compiler-plugin</artifactId>

    <configuration>

     <debug>false</debug>

     <optimize>true</optimize>

    </configuration>

  </plugin>         

</plugins>

</build>

</profile>

</profiles>

  1.profiles通常是pom.xml中最后一个元素
   2.每个profile必须要有一个id元素,通过传给Maven一个-P<profile_id>参数来调用profile.
   3.一个profile元素可以包含很多其它元素,只要这些元素可以出现在pom.xml文件的project元素下。
   4.mvn clean install -Pproduction -X (-X为开启调试输出)
    
(19).使用profile激活动态包含子模块

<profiles>

<profile>

  <id>jdk16</id>

  <activation>

   <jdk>1.6</jdk>

  </activation>

  <modules>

   <module>simple-script</module>

  </modules>

</profile>

</profiles>

 1.如果在jdk1.6下运行,则会构建simple-script项目,否则不会构建。
  2.activation元素列出了所有激活profile需要的条件。
  其它方式:通过属性缺失激活(!表示否定,当没有设置${environment.type}属性时被激活.

<activation>

<property>

 <name>!environment.type</name>

</property>

</activation>

3.如果大量使用Maven Profile,可将profile从POM文件中分离,
  使用一个单独文件,名字为profiles.xml,放到项目目录下(同pom.xml),格式为

<profiles>

<profile>

 ...

</profile>

</profiles>

4.setting profile可以应用到所有使用到Maven构建的项目,可以在两个地方定义
  (1).~/.m2/settings.xml(特定用户)
  (2).${M2_HOME}/conf/settings.xml(全局)

  5.列出活动的profile
  mvn help:active-profiles

 

 (20).创建私服
  下载nexus(http://nexus.sonatype.org/downloads/)
  解压后运行目录下\bin\jsw\windows-x86-32\Nexus.bat,
  访问http://127.0.0.1:8081/nexus(默认admin,admin123)
  1.配置maven settings

<mirrors>    

<mirror>

  <id>Nexus</id>

  <mirrorOf>central</mirrorOf>

  <name>Nexus Public Mirror</name>

  <url>http://localhost:8081/nexus/content/groups/public</url>

</mirror>     

</mirrors>

2.或者

<profiles>
<profile>
<id>artifactory</id>
<repositories>
<repository>
 <id>nexus</id>
 <name>local nexus</name>
 <url>http://localhost:8081/nexus/content/groups/public</url>  
<layout>default</layout>
</repository>    
</repositories>  
<pluginRepositories>
<pluginRepository>
 <id>nexus</id>
 <name>local nexus</name>
 <url>http://localhost:8081/nexus/content/groups/public</url>     
  <layout>default</layout>
 </pluginRepository>
</pluginRepositories>
</profile>
</profiles> 
<activeProfiles>
<activeProfile>artifactory</activeProfile>
</activeProfiles>

 3. 或者在项目pom.xml中加入

 <repositories>
<repository>
<id>nexus</id>
<name>local nexus</name>
<url>http://localhost:8081/nexus/content/groups/public</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
 <id>nexus</id>
 <name>local nexus</name>
 <url>http://localhost:8081/nexus/content/groups/public</url>
</pluginRepository>
</pluginRepositories>

 配置后maven会从本地的nexus安装查阅(可在Public Repositories和Maven Central仓库中查到)
   
 (21)部署第三方构件到nexus 
   在nexus项目管理中
   找到Repositories,这里选择其中一个,例如3rd party,
   可以在下面看到一个Artifact Upload选项卡。
   打开在GAV Definition中选择GAV Parameters,在接下来的Group中选输入组织名,
   Artifact项输入artifactId ,Packaging:这里选择jar,
   之后上传一个自定义的jar文件,
   点击Add Artifact,Upload Artifact(s),上传成功后,
   在上边的3rd party上单击右键——ReIndex,然后刷新下这个列表,
   就可以看到下边多了.index文件夹和我们刚才上传相关的文件夹。

转载于:https://my.oschina.net/u/1458864/blog/287775

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值