Maven总结

来源:互联网 发布:安徽网络大学马鞍山 编辑:程序博客网 时间:2024/06/08 14:04
一。Maven常用命令: 
1. 创建Maven的普通java项目(所有的原型(DarchetypeArtifactId指的什么类型的项目比如jar或者war项目)参考 http://maven.apache.org/guides/introduction/introduction-to-archetypes.html): 
   mvn archetype:generate
   -DgroupId=packageName 
   -DartifactId=projectName  
2. 创建Maven的Web项目:   
    mvn archetype:generate
    -DgroupId=packageName    
    -DartifactId=webappName 
    -DarchetypeArtifactId=maven-archetype-webapp    
3. 编译源代码: mvn compile 
4. 编译测试代码:mvn test-compile    
5. 运行测试:mvn test   
6. 产生site(在target/site目录下生成该文档的一个帮助api网页):mvn site   
7. 打包:mvn package   
8. 在本地Repository中安装jar:mvn install 
9. 清除产生的项目:mvn clean   
10. 生成eclipse项目:mvn eclipse:eclipse  
11. 生成intellij idea:mvn idea:idea  
12. 组合使用goal命令,如只打包不测试:mvn -Dtest package   
13. 编译测试的内容:mvn test-compile  
14. 只打jar包: mvn jar:jar  
15. 只测试而不编译,也不测试编译:mvn test -skipping compile -skipping test-compile 
      ( -skipping 的灵活运用,当然也可以用于其他组合命令)  

16. 清除eclipse的一些系统设置:mvn eclipse:clean 

二 常用单词解释及pom.xnl

 SNAPSHOT表示项目正在一个开发版本 对应的RELEASE  一般开发版本定义是 x.y-SNAPSHOT    比如 1.0-SNAPSHOT  开发一个新版本后y++

1》pom.xml讲解

  》》属性定义

   

[html] view plain copy
  1. <properties>  
  2.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  3.     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
  4.     <java.version>1.7</java.version>  
  5.     <spring-cloud.version>Dalston.SR3</spring-cloud.version>  
  6. lt;/properties>  
 定义一些属性信息 该属性可以在后续引用 

  》》依赖控制

[html] view plain copy
  1. <dependency>  
  2.             <groupId>org.springframework.boot</groupId>  
  3.             <artifactId>spring-boot-starter-test</artifactId>  
  4.                         <version>${属性名}</version>  
  5.             <scope>test</scope>  
  6. </dependency>  

添加依赖后  自动寻找本地仓库jar包  如果无法找到 查找远程仓库

介绍 scope各个值的参考

[html] view plain copy
  1. compile:默认的scope。任何定义在compile scope下的依赖将会在所有的class paths下可用。maven工程会将其打包到最终的arifact中。如果你构建一个WAR类型的artefact,那么在compile scope下引用的JAR文件将会被集成到WAR文件内。  
  2.   
  3. provided:这个scope假定对应的依赖会由运行这个应用的JDK或者容器来提供。最好的例子就是servlet API。任何在provided scope下定义的依赖在构建时的类路径里是可用的,但是不会被打包到最终的artifact中。如果是一个WAR的文件,servlet API在构建时的类路径里是可用的,但是并不会被打包到WAR文件中。  
  4.   
  5. runtime:在runtime scope下定义的依赖只会在运行期可用,而在构建期的类路径下不可用。这些依赖将会被打包到最终的artifact中。比如你有一个基于web的应用需要在运行时访问MySQL数据库。你的代码没有任何MySQL数据库驱动的硬依赖。你的代码仅仅是基于JDBC API来编写,在构建期并不需要MySQL数据库驱动。然而,在运行期,就需要相应的驱动来操作MySQL数据库了。因此,这个驱动应该被打包到最终的artifact中。  
  6.   
  7. test:只用于测试变异的依赖(比如JUnit),execution必须定义在test scope下。这些依赖不会被打包到最终的artefact中。  
  8.   
  9. system:于provided scope很像。唯一的区别在于,在system scope中,你需要告诉Mave如何去找到这个依赖。如果你要引用的依赖在Maven仓库中不存在时,就可以用这个scope。不推荐使用system依赖。  
  10.   
  11. import:从其它的pom文件中导入依赖设置。  




三 mvn插件列表

   如果需要自己构建maven项目 你需要在pom.xml中配置参见 插件就是mvn 后的命令名称 参考(http://maven.apache.org/plugins/)

四 开发自己的插件(开发插件参考官网http://maven.apache.org/guides/plugin/guide-java-plugin-development.html)

  1>生成插件项目 

      命令行:   使用 mvn archetype:generate  生成插件 

[html] view plain copy
  1. Choose archetype:  
  2.    1: internal -> org.apache.maven.archetypes:maven-archetype-archetype (An archety  
  3.     pe which contains a sample archetype.)  
  4.    2: internal -> org.apache.maven.archetypes:maven-archetype-j2ee-simple (An arche  
  5.     type which contains a simplifed sample J2EE application.)  
  6.    3: internal -> org.apache.maven.archetypes:maven-archetype-plugin (An archetype  
  7.     which contains a sample Maven plugin.)  
  8.    4: internal -> org.apache.maven.archetypes:maven-archetype-plugin-site (An arche  
  9.     type which contains a sample Maven plugin site.  


  输入数字3 即可输入groupid和项目ip  生成插件项目

   eclipse生成插件项目(新建maven项目选择第三项)

     

使用eclipse项目时 会出现两个编译异常 mojo-descriptor和helpmojo 这里可以忽略检查这个异常 不影响插件的编译和安装


点击 Permanebtly mark goal helpmojo in pom.xml 即可忽略

maven希望你将插件项目名称命名为 插件名称-maven-plugin  最终 就可以通过插件别名来运行该插件 比如我生成的插件pom。xml如下

[html] view plain copy
  1. <groupId>cn.et.plugin</groupId>  
  2. <artifactId>gen-maven-plugin</artifactId>  
  3. <version>0.0.1-SNAPSHOT</version>  
  4. <packaging>maven-plugin</packaging>  


实现插件的代码 必须添加类 实现AbstractMojo该抽象类 实现execute方法 该方法就是插件运行时执行的方法

这个插件用于统计当前项目中各类型文件的个数

[html] view plain copy
  1. package cn.et.plugin.gen_maven_plugin;  
  2.   
  3.   
  4. import java.io.File;  
  5. import java.util.HashMap;  
  6. import java.util.Map;  
  7.   
  8.   
  9. /*  
  10.  * Copyright 2001-2005 The Apache Software Foundation.  
  11.  *  
  12.  * Licensed under the Apache License, Version 2.0 (the "License");  
  13.  * you may not use this file except in compliance with the License.  
  14.  * You may obtain a copy of the License at  
  15.  *  
  16.  *      http://www.apache.org/licenses/LICENSE-2.0  
  17.  *  
  18.  * Unless required by applicable law or agreed to in writing, software  
  19.  * distributed under the License is distributed on an "AS IS" BASIS,  
  20.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  21.  * See the License for the specific language governing permissions and  
  22.  * limitations under the License.  
  23.  */  
  24.   
  25.   
  26. import org.apache.maven.plugin.AbstractMojo;  
  27. import org.apache.maven.plugin.MojoExecutionException;  
  28. import org.apache.maven.plugins.annotations.LifecyclePhase;  
  29. import org.apache.maven.plugins.annotations.Mojo;  
  30. import org.apache.maven.plugins.annotations.Parameter;  
  31.   
  32. /**  
  33.  * 统计总共拥有的文件分类总个数  
  34.  *   txt 5个  
  35.  *   java 6个  
  36.  *   jpg  10个  
  37.  @Mojo 注解中的name表示插件运行时的类   
  38.   如果一个插件项目中存在两个类都是继承AbstractMojo 每个类需要一个mojo name  
  39.   运行时  mvn 插件名称:mojoname 就可以运行到具体的类  
  40.  * @author jiaozi  
  41.  *  
  42.  */  
  43. @Mojo( name = "filecount"defaultPhase = LifecyclePhase.PROCESS_SOURCES )  
  44. public class MyMojo  
  45.     extends AbstractMojo  
  46. {  
  47.     /**  
  48.        @Parameter表示引用该插件时可以传递参数  
  49.      * defaultValue可以是el表达式  使用javabean的特性  
  50.      * 注解中给定了 api路径 指定可以使用的表达式内容  
  51.      * <a href="/ref/current/maven-core/apidocs/org/apache/maven/plugin/PluginParameterExpressionEvaluator.html">  
  52.           
  53.      */  
  54.     @Parameter( defaultValue = "${project.build.directory}"property = "outputDir"required = true )  
  55.     private File outputDirectory;  
  56.     /**  
  57.      * 获取执行该插件的当前项目的根目录  
  58.      */  
  59.     @Parameter(defaultValue="${basedir}",property="filecount.baseDir")  
  60.     private String baseDir;  
  61.     /**  
  62.      * 键 后缀名  
  63.      * 值 该文件的个数  
  64.      */  
  65.     private Map<String,Integer> fileCount=new HashMap<String,Integer>();  
  66.     /**  
  67.      * 递归统计每个目录下的不同后缀文件个数  
  68.      * @param curFile 当前目录  
  69.      */  
  70.     public void calc(File curFile){  
  71.     File[] listFile=curFile.listFiles();  
  72.     for(File myFile:listFile){  
  73.     if(myFile.isDirectory()){  
  74.     calc(myFile);  
  75.     }else{  
  76.     String fileName=myFile.getName();  
  77.     String fileKey="no_suffix";  
  78.     if(fileName.indexOf(".")>0){  
  79.     fileKey=fileName.split("\\.")[1];  
  80.     }  
  81.     int count=1;  
  82. if(fileCount.containsKey(fileKey)){  
  83. count=fileCount.get(fileName)+1;  
  84. }  
  85. fileCount.put(fileName,count);  
  86.     }  
  87.     }  
  88.     }  
  89.     /**  
  90.      * 必须实现execute方法表示插件入口  
  91.      */  
  92.     public void execute()  
  93.         throws MojoExecutionException  
  94.     {  
  95.         File baseFile=new File(baseDir);  
  96.         calc(baseFile);  
  97.         for(Map.Entry<String,Integer> me:fileCount.entrySet()){  
  98.         System.out.println("文件"+me.getKey()+" 个数:"+me.getValue());  
  99.         }  
  100.     }  
  101. }  

 开发完成后 mvn clean install 自动打包 安装到本地的maven库

 创建一个maven jar项目 测试该插件是否能正常运行

 执行  mvn groupid:artifactId:version:mojoname 就可以运行当前插件  版本号可以省略(这里aaa必须是个maven项目)

[html] view plain copy
  1.    C:\Users\jiaozi\aaa>mvn cn.et.plugin:gen-maven-plugin:0.0.1-SNAPSHOT:filecount  
  2. [INFO] Scanning for projects...  
  3. [INFO]  
  4. [INFO] ------------------------------------------------------------------------  
  5. [INFO] Building aaa 1.0-SNAPSHOT  
  6. [INFO] ------------------------------------------------------------------------  
  7. [INFO]  
  8. [INFO] --- gen-maven-plugin:0.0.1-SNAPSHOT:filecount (default-cli) @ aaa ---  
  9. 文件App.java 个数:1  
  10. 文件AppTest.java 个数:1  
  11. 文件pom.xml 个数:1  


将当前groupid添加到maven的settings.xml中可以通过简写运行

配置如下 添加

  

[html] view plain copy
  1. <pluginGroups>  
  2. lt;pluginGroup>cn.et.plugin</pluginGroup>  
  3.  </pluginGroups>  

这里gen是插件  gen-maven-plugin的第一个单词 满足maven规范 所以可以直接简写了

[html] view plain copy
  1.  C:\Users\jiaozi\aaa>mvn gen:filecount  
  2. [INFO] Scanning for projects...  
  3. [INFO]  
  4. [INFO] ------------------------------------------------------------------------  
  5. [INFO] Building aaa 1.0-SNAPSHOT  
  6. [INFO] ------------------------------------------------------------------------  
  7. [INFO]  
  8. [INFO] --- gen-maven-plugin:0.0.1-SNAPSHOT:filecount (default-cli) @ aaa ---  
  9. 文件App.java 个数:1  
  10. 文件AppTest.java 个数:1  
  11. 文件pom.xml 个数:1  

其他的maven项目中引用该插件 在pom。xml中配置如下

[html] view plain copy
  1. <build>  
  2.     <plugins>  
  3.       <plugin>  
  4.         <groupId>cn.et.plugin</groupId>  
  5.   <artifactId>gen-maven-plugin</artifactId>  
  6.   <version>0.0.1-SNAPSHOT</version>  
  7.         <configuration>  
  8.           <baseDir>C:/app/jiaozi/oradata/orcl</baseDir>  <!--这里配置的参数就是前面mojo类中的属性 带有@Parameter注解的参数-->  
  9.         </configuration>  
  10.       </plugin>  
  11.     </plugins>  
  12. </build>  

如果希望maven插件在maven执行的生命周期里自动执行 比如编译的时候自动执行 

[html] view plain copy
  1. <build>  
  2.         <plugins>  
  3.             <plugin>  
  4.                 <groupId>cn.et.plugin</groupId>  
  5.                  <artifactId>gen-maven-plugin</artifactId>  
  6.                  <version>0.0.1-SNAPSHOT</version>  
  7.                  <executions>  
  8.                     <execution>  
  9.                         <phase>compile</phase> #在哪个周期执行  
  10.                         <goals>  
  11.                             <goal>filecount</goal> #执行插件的pojoname  
  12.                         </goals>  
  13.                     </execution>  
  14.                  </executions>  
  15.             </plugin>  
  16.   
  17.         </plugins>  
  18.     </build>  
如果xml编辑有错误可以忽略 当该maven项目在任意阶段只要包含编译就会在编译 自动调用filecount插件
插件的生命周期 :

[html] view plain copy
  1. default生命周期是最核心的,它包含了构建项目时真正需要执行的所有步骤。  
  2. validate  
  3. initialize  
  4. generate-sources  
  5. process-sources  
  6. generate-resources  
  7. process-resources    :复制和处理资源文件到target目录,准备打包;  
  8. compile    :编译项目的源代码;  
  9. process-classes  
  10. generate-test-sources  
  11. process-test-sources  
  12. generate-test-resources  
  13. process-test-resources  
  14. test-compile    :编译测试源代码;  
  15. process-test-classes  
  16. test    :运行测试代码;  
  17. prepare-package  
  18. package    :打包成jar或者war或者其他格式的分发包;  
  19. pre-integration-test  
  20. integration-test  
  21. post-integration-test  
  22. verify  
  23. install    :将打好的包安装到本地仓库,供其他项目使用;  
  24. deploy    :将打好的包安装到远程仓库,供其他项目使用;  


这里插件参数的el表达式用法可以查看http://maven.apache.org/ref/3.5.0/apidocs/index.html



五 上传插件到私服


这里参考了http://www.cnblogs.com/luotaoyeah/p/3817465.html

1 . 修改私服中仓库的部署策略

  Release版本的项目应该发布到Releases仓库中,对应的,Snapshot版本应该发布到Snapshots仓库中。Maven根据pom.xml文件中版本号<version>节点的属性是否包含-SNAPSHOT,来判断该项目是否是snapshot版本。如果是snapshot版本,在执行mvn deploy部署命令时,maven会自动将项目发布到Snapshots仓库。要发布项目,首先需要将Releases仓库和Snapshots仓库的“Deployment Policy”设置为“Allow Redeploy”:

2 . 配置项目的部署仓库

在pom.xml中分别对Release版本和Snapshot版本配置部署仓库,其中id唯一,url分别对应私服中Releases和Snapshots仓库的Repository Path:

<uniqueVersion>表示是否为Snapshot版本分配一个包含时间戳的构建号,效果如下:

复制代码
    <distributionManagement>        <snapshotRepository>            <id>user-snapshot</id>            <name>User Porject Snapshot</name>            <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>            <uniqueVersion>true</uniqueVersion>        </snapshotRepository>        <repository>            <id>user-release</id>            <name>User Porject Release</name>            <url>http://localhost:8081/nexus/content/repositories/releases/</url>        </repository>    </distributionManagement>
复制代码

3 . 启用Deployment用户

Nexus默认有三个用户,其中Deployment用户用于部署项目:

Deployment用户默认密码为deployment123,右键菜单可修改或重置密码:

在settings.xml中分别为上面配置的部署仓库配置server,其中id需要分别对应上面的部署仓库id:

复制代码
    <servers>        <server>            <id>user-release</id>            <username>deployment</username>            <password>deployment123</password>        </server>        <server>            <id>user-snapshot</id>            <username>deployment</username>            <password>deployment123</password>        </server></servers>
复制代码

4 . 发布项目

  右键pom.xml - Run As - 2 Maven build...

发布成功后,在私服的仓库中就能看到了:

5 . 在Nexus中手动上传项目构件

  在Nexus仓库的Artifact Upload选项卡中,填写相关信息,可以手动的方式上传项目构件:

我这里通过私服 查看 开发版本库的结果  正常上传

Index of /repositories/snapshots/cn/et/plugin

NameLast ModifiedSizeDescriptionParent Directorygen-maven-plugin/Wed May 10 01:42:19 CST 2017  maven-metadata.xmlWed May 10 01:42:19 CST 2017251 maven-metadata.xml.md5Wed May 10 01:42:19 CST 201732 maven-metadata.xml.sha1Wed May 10 01:42:19 CST 201740 六 搭建多模块项目

  maven项目多之间存在两种关系 继承和聚合

  继承关系一般用于定义统一的项目版本号 以及依赖的jar

   

[html] view plain copy
  1. <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>4.0.0</modelVersion>  
  3.   <groupId>cn.et</groupId>  
  4.   <artifactId>myshop-parent</artifactId>  
  5.   <version>0.0.1-SNAPSHOT</version>  
  6.   <packaging>pom</packaging>  
  7.   <!-- 开发项目时 所有用到的项目框架版本号 尽量 统一使用 父项目管理版本号-->  
  8.   <properties>  
  9.     <spring-webmvc-version>4.3.10.RELEASE</spring-webmvc-version>  
  10.     <mybatis-spring-version>1.3.1</mybatis-spring-version>  
  11.     <mybatis-version>3.4.5</mybatis-version>  
  12.   </properties>  
  13.   <!-- 在父项目中 使用了 dependencyManagement 添加的依赖 所有子项目都不需要在添加版本号-->  
  14.   <dependencyManagement>  
  15.     <dependencies>  
  16.         <dependency>  
  17.           <groupId>org.springframework</groupId>  
  18.           <artifactId>spring-webmvc</artifactId>  
  19.           <version>${spring-webmvc-version}</version>  
  20.         </dependency>  
  21.           
  22.         <dependency>  
  23.           <groupId>org.mybatis</groupId>  
  24.           <artifactId>mybatis</artifactId>  
  25.           <version>${mybatis-version}</version>  
  26.         </dependency>  
  27.           
  28.         <dependency>  
  29.           <groupId>org.mybatis</groupId>  
  30.           <artifactId>mybatis-spring</artifactId>  
  31.           <version>${mybatis-spring-version}</version>  
  32.         </dependency>  
  33.           
  34.     </dependencies>  
  35.       
  36.   </dependencyManagement>  
  37.   <build>  
  38.     <pluginManagement>  
  39.           
  40.       
  41.     </pluginManagement>  
  42.   </build>  
  43.     
  44.     
  45. </project>  


聚合关系一般是将 不同的模块聚合成一个项目

  1 通过maven项目创建pom的父项目

  2 通过maven module创建jar项目 父项目选择pom项目

  3 实际的web项目 选择war项目 项目之间互相依赖 添加对方的dependencies

比如:

  

原创粉丝点击