返回

Maven——项目构建完整配置

发布时间:2023-08-29 00:01:54 289


 我的工具包在哪里,我的插件在哪里?哎,我的项目怎么缺了这个又缺了哪个,咦,这两个动动版本不配合?不起效了!怎么办!怎么办!在Maven没有诞生前,这一系列的问题,总能困住一群又一群的开发人员,而今天的我们,因为有了maven,再也不必担心依赖于插件的问题啦。所以,我们有必要好好来了解一下Maven。

 

一、Maven简介

(一)什么是Maven

      Maven是apache下的开源项目,项目管理工具,管理java项目。

1、项目对象模型(Project Object Model)

      POM对象模型,每个maven工程中都有一个pom.xml文件,定义工程所依赖的jar包、本工程的坐标、打包运行方式。。

2、依赖管理系统(基础核心 )

       maven通过坐标对项目工程所依赖的jar包统一规范管理。

3、maven定义一套项目生命周期

清理、初始化、编译、测试、报告、打包、部署、站点生成

4、一组标准集合

       强调:maven工程有自己标准的工程目录结构、定义坐标有标准。

5、maven 管理项目生命周期过程都是基于插件完成的

(二)Maven仓库

1、中央仓库

       就是远程仓库,仓库中jar由专业团队(maven团队)统一维护。

       中央仓库的地址:http://repo1.maven.org/maven2/

2、本地仓库

        相当于缓存,工程第一次会从远程仓库(互联网)去下载jar 包,将jar包存在本地仓库(在程序员的电脑上)。第二次不需要从远程仓库去下载。先从本地仓库找,如果找不到才会去远程仓库找。

3、私服

       在公司内部架设一台私服,其它公司架设一台仓库,对外公开。

二、Maven常用操作

Maven依赖查询:
​​​http://mvnrepository.com/​​

导入jar到本地仓库:

​​javascript:void(0)​​

Maven常用命令:
1. 创建Maven的普通java项目:
   mvn archetype:create
   -DgroupId=packageName
   -DartifactId=projectName 
2. 创建Maven的Web项目:  
    mvn archetype:create
    -DgroupId=packageName   
    -DartifactId=webappName
    -DarchetypeArtifactId=maven-archetype-webapp   
3. 编译源代码: mvn compile  (将项目中java文件编译为class,放在target目录)
4. 编译测试代码:mvn test-compile   
5. 运行测试:mvn test  
6. 产生site:mvn site  
7. 打包:mvn package      (先执行compile,然后将class文件打包成jar或war包到本项目target目录)
8. 在本地Repository中安装jar:mvn install (先执行compile与package,然后将jar包安装到本地仓库) 
9. 清除产生的项目:mvn clean  
10. 生成eclipse项目:mvn eclipse:eclipse 
11. 生成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 只打包不测试:mvn package -Dmaven.test.skip=true -P prepare

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

18.安装到远程仓库:mvn deploy

三、Maven的Setting.xml

顶级元素概览

下面列举了settings.xml中的顶级元素

[html]  ​​view plain​​  ​​copy​​

1. 2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
3. xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
4. >
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.


LocalRepository

作用:该值表示构建系统本地仓库的路径。
其默认值:~/.m2/repository。

${user.home}/.m2/repository

InteractiveMode

作用:表示maven是否需要和用户交互以获得输入。
如果maven需要和用户交互以获得输入,则设置成true,反之则应为false。默认为true。

true

UsePluginRegistry

作用:maven是否需要使用plugin-registry.xml文件来管理插件版本。
如果需要让maven使用文件~/.m2/plugin-registry.xml来管理插件版本,则设为true。默认为false。

false

Offline

作用:表示maven是否需要在离线模式下运行。
如果构建系统需要在离线模式下运行,则为true,默认为false。
当由于网络设置原因或者安全因素,构建服务器不能连接远程仓库的时候,该配置就十分有用。

false

PluginGroups

作用:当插件的组织id(groupId)没有显式提供时,供搜寻插件组织Id(groupId)的列表。
该元素包含一个pluginGroup元素列表,每个子元素包含了一个组织Id(groupId)。
当我们使用某个插件,并且没有在命令行为其提供组织Id(groupId)的时候,Maven就会使用该列表。默认情况下该列表包含了org.apache.maven.plugins和org.codehaus.mojo。



[html]  ​​view plain​​  ​​copy​​

1. 2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0                     https://maven.apache.org/xsd/settings-1.0.0.xsd">  
3.
4. ...
5.
6.
7.
8.
9. org.codehaus.mojo
10.
11.

12.
13.


Servers

作用:一般,仓库的下载和部署是在pom.xml文件中的repositories和distributionManagement元素中定义的。然而,一般类似用户名、密码(有些仓库访问是需要安全认证的)等信息不应该在pom.xml文件中配置,这些信息可以配置在settings.xml中。



[html]  ​​view plain​​  ​​copy​​



1. 2. xmlns="http://maven.apache.org/SETTINGS/1.0.0"  
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
4.
5. ...
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17. server001
18.
19.
20.
21. my_login
22.
23.
24.
25. my_password
26.
27.
28.
29. ${usr.home}/.ssh/id_dsa
30.
31.
32.
33. some_passphrase
34.
35.
36.
37. 664
38.
39.
40.
41. 775
42.
43.

44.
45.

46.
47.


Mirrors

作用:为仓库列表配置的下载镜像列表。



[html]  ​​view plain​​  ​​copy​​




  1. xmlns="http://maven.apache.org/SETTINGS/1.0.0"  
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0                      https://maven.apache.org/xsd/settings-1.0.0.xsd">  
  3.   
  4.   ...  
  5.   
  6.   
  7.   
  8.   
  9.   
  10.   
  11.   
  12.   
  13.   
  14. planetmirror.com  
  15.   
  16.   
  17.   
  18. PlanetMirrorAustralia  
  19.   
  20.   
  21.   
  22. http://downloads.planetmirror.com/pub/maven2  
  23.   
  24.   
  25.   
  26. central  
  27.   
  28.   
  29.   
  30.   
  31.   
  32.   



Proxies

作用:用来配置不同的代理。



[html]  ​​view plain​​  ​​copy​​




  1. xmlns="http://maven.apache.org/SETTINGS/1.0.0"  
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0                     https://maven.apache.org/xsd/settings-1.0.0.xsd">  
  3.   
  4.   ...  
  5.   
  6.   
  7.   
  8.   
  9.   
  10.   
  11.   
  12.   
  13.   
  14. myproxy  
  15.   
  16.   
  17.   
  18. true  
  19.   
  20.   
  21.   
  22. http  
  23.   
  24.   
  25.   
  26. proxy.somewhere.com  
  27.   
  28.   
  29.   
  30. 8080  
  31.   
  32.   
  33.   
  34. proxyuser  
  35.   
  36.   
  37.   
  38. somepassword  
  39.   
  40.   
  41.   
  42. *.google.com|ibiblio.org  
  43.   
  44.   
  45.   
  46.   
  47.   
  48.   



Profiles

作用:根据环境参数来调整构建配置的列表。
settings.xml中的profile元素是pom.xml中profile元素的裁剪版本。
它包含了id、activation、repositories、pluginRepositories和 properties元素。这里的profile元素只包含这五个子元素是因为这里只关心构建系统这个整体(这正是settings.xml文件的角色定位),而非单独的项目对象模型设置。如果一个settings.xml中的profile被激活,它的值会覆盖任何其它定义在pom.xml中带有相同id的profile。



[html]  ​​view plain​​  ​​copy​​




  1. xmlns="http://maven.apache.org/SETTINGS/1.0.0"  
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0                      https://maven.apache.org/xsd/settings-1.0.0.xsd">  
  3.   
  4.  ...  
  5.   
  6.   
  7.   
  8.   
  9.   
  10.   
  11.   
  12. test  
  13.   
  14.   
  15.   
  16.   
  17.   
  18.   
  19.   
  20.   
  21.   
  22.   
  23.   
  24.   
  25.   
  26.   
  27.   
  28.   
  29.   
  30.   
  31.   
  32.   
  33.   
  34.   



 

Activation

作用:自动触发profile的条件逻辑。
如pom.xml中的profile一样,profile的作用在于它能够在某些特定的环境中自动使用某些特定的值;这些环境通过activation元素指定。
activation元素并不是激活profile的唯一方式。settings.xml文件中的activeProfile元素可以包含profile的id。profile也可以通过在命令行,使用-P标记和逗号分隔的列表来显式的激活(如,-P test)。



[html]  ​​view plain​​  ​​copy​​




  1.   
  2.   
  3.   
  4.   
  5. false  
  6.   
  7.   
  8.   
  9. 1.5  
  10.   
  11.   
  12.   
  13.   
  14.   
  15.   
  16.   
  17. Windows XP  
  18.   
  19.   
  20.   
  21. Windows  
  22.   
  23.   
  24.   
  25. x86  
  26.   
  27.   
  28.   
  29. 5.1.2600  
  30.   
  31.   
  32.   
  33.   
  34.   
  35.   
  36.   
  37.   
  38.   
  39. mavenVersion  
  40.   
  41.   
  42.   
  43. 2.0.3  
  44.   
  45.   
  46.   
  47.   
  48.   
  49.   
  50.   
  51.   
  52.   
  53. ${basedir}/file2.properties  
  54.   
  55.   
  56.   
  57. ${basedir}/file1.properties  
  58.   
  59.   



注:在maven工程的pom.xml所在目录下执行mvn help:active-profiles命令可以查看中央仓储的profile是否在工程中生效。

properties

作用:对应profile的扩展属性列表。
maven属性和ant中的属性一样,可以用来存放一些值。这些值可以在pom.xml中的任何地方使用标记${X}来使用,这里X是指属性的名称。属性有五种不同的形式,并且都能在settings.xml文件中访问。

 ${user.home}/our-project

注:如果该profile被激活,则可以在pom.xml中使用${user.install}。

Repositories

作用:远程仓库列表,它是maven用来填充构建系统本地仓库所使用的一组远程仓库。



[html]  ​​view plain​​  ​​copy​​




  1.   
  2.   
  3.   
  4.   
  5.   
  6.   
  7.   
  8.   
  9. codehausSnapshots  
  10.   
  11.   
  12.   
  13. Codehaus Snapshots  
  14.   
  15.   
  16.   
  17.   
  18.   
  19.   
  20.   
  21. false  
  22.   
  23.   
  24.   
  25. always  
  26.   
  27.   
  28.   
  29. warn  
  30.   
  31.   
  32.   
  33.   
  34.   
  35.   
  36.   
  37.   
  38.   
  39.   
  40.   
  41.   
  42.   
  43.   
  44.   
  45.   
  46.   
  47. http://snapshots.maven.codehaus.org/maven2  
  48.   
  49.   
  50.   
  51. default  
  52.   
  53.   



pluginRepositories

作用:发现插件的远程仓库列表。
和repository类似,只是repository是管理jar包依赖的仓库,pluginRepositories则是管理插件的仓库。
maven插件是一种特殊类型的构件。由于这个原因,插件仓库独立于其它仓库。pluginRepositories元素的结构和repositories元素的结构类似。每个pluginRepository元素指定一个Maven可以用来寻找新插件的远程地址。



[html]  ​​view plain​​  ​​copy​​




  1.   
  2.   
  3.   
  4.   
  5.   
  6.   
  7.   
  8.   
  9.   
  10.   
  11.   
  12.   
  13.   
  14.   
  15.   
  16.   
  17.   
  18.   
  19.   
  20.   
  21.   
  22.   
  23.   
  24.   
  25.   
  26.   
  27.   
  28.   
  29.   
  30.   
  31.   
  32.   
  33.   
  34.   
  35.   



ActiveProfiles

作用:手动激活profiles的列表,按照profile被应用的顺序定义activeProfile。
该元素包含了一组activeProfile元素,每个activeProfile都含有一个profile id。任何在activeProfile中定义的profile id,不论环境设置如何,其对应的 profile都会被激活。如果没有匹配的profile,则什么都不会发生。
例如,env-test是一个activeProfile,则在pom.xml(或者profile.xml)中对应id的profile会被激活。如果运行过程中找不到这样一个profile,Maven则会像往常一样运行。



[html]  ​​view plain​​  ​​copy​​




  1.   
  2.   
  3.   ...  
  4.   
  5.   
  6.   
  7.   
  8.   
  9. env-test  
  10.   
  11.   
  12.   
  13.   



四、Maven的Pom.xml



原文地址:http://blog.csdn.net/zhuxinhua/article/details/5788546



什么是pom?
    pom作为项目对象模型。通过xml表示maven项目,使用pom.xml来实现。主要描述了项目:包括配置文件;开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的url,项目的依赖性,以及其他所有的项目相关因素。

快速察看:



[html]  ​​view plain​​  ​​copy​​




 



 



  1.   
  2. 4.0.0  
  3.   
  4.   
  5. ...  
  6. ...  
  7. ...  
  8. ...  
  9. ...  
  10. ...  
  11. ...  
  12. ...  
  13. ...  
  14. ...  
  15. ...  
  16.   
  17.   
  18. ...  
  19. ...  
  20.   
  21.   
  22. ...  
  23. ...  
  24. ...  
  25. ...  
  26. ...  
  27. ...  
  28. ...  
  29. ...  
  30.   
  31.   
  32. ...  
  33. ...  
  34. ...   
  35. ...  
  36. ...  
  37. ...  
  38. ...  
  39. ...  
  40. ...  
  41.   



基本内容:

POM包括了所有的项目信息

groupId:项目或者组织的唯一标志,并且配置时生成路径也是由此生成,如org.myproject.mojo生成的相对路径为:/org/myproject/mojo

artifactId:项目的通用名称

version:项目的版本

packaging:打包机制,如pom,jar,maven-plugin,ejb,war,ear,rar,par

name:用户描述项目的名称,无关紧要的东西,可选

url:应该是只是写明开发团队的网站,无关紧要,可选

classifer:分类

其中groupId,artifactId,version,packaging这四项组成了项目的唯一坐标。一般情况下,前面三项就可以组成项目的唯一坐标了。

 

POM关系:主要为依赖,继承,合成

依赖关系:

 



[html]  ​​view plain​​  ​​copy​​




 



 



  1.   
  2.   
  3. junit  
  4. junit  
  5. 4.0  
  6. jar  
  7. test  
  8. true  
  9.   
  10.    
  11.   
  12. com.alibaba.china.shared  
  13. alibaba.apollo.webx  
  14. 2.5.0  
  15.   
  16.   
  17. org.slf4j.slf4j-api  
  18. com.alibaba.external  
  19.   
  20.           ....  
  21.   
  22. ......  
  23.   



其中groupId, artifactId, version这三个组合标示依赖的具体工程,而且这个依赖工程必需是maven中心包管理范围内的,如果碰上非开源包,maven支持不了这个包,那么则有三种方法处理:

1.本地安装这个插件install plugin

例如:mvn install:intall-file -Dfile=non-maven-proj.jar -DgroupId=som.group -DartifactId=non-maven-proj -Dversion=1

2.创建自己的repositories并且部署这个包,使用类似上面的deploy:deploy-file命令,

3.设置scope为system,并且指定系统路径。

 

dependency里属性介绍:

type:默认为jar类型,常用的类型有:jar,ejb-client,test-jar...,可设置plugins中的extensions值为true后再增加新的类型,

scope:是用来指定当前包的依赖范围,​​maven的依赖范围​​

optional:设置指依赖是否可选,默认为false,即子项目默认都继承,为true,则子项目必需显示的引入,与dependencyManagement里定义的依赖类似 。

exclusions:如果X需要A,A包含B依赖,那么X可以声明不要B依赖,只要在exclusions中声明exclusion.

exclusion:是将B从依赖树中删除,如上配置,alibaba.apollo.webx不想使用com.alibaba.external  ,但是alibaba.apollo.webx是集成了com.alibaba.external,r所以就需要排除掉.

 

如果一个工程是parent或者aggregation(即mutil-module的)的,那么必须在packing赋值为pom,child工程从parent继承的包括:dependencies,developers,contributors,plugin lists,reports lists,plugin execution with matching ids,plugin configuration

parent的使用方法如下:

 



[html]  ​​view plain​​  ​​copy​​




 



 



  1.    
  2. org.codehaus.mojo   
  3. my-parent   
  4. 2.0   
  5. ../my-parent   
  6.   



relativePath是可选的,maven会首先搜索这个地址,在搜索本地远程repositories之前.

dependencyManagement:是用于帮助管理chidren的dependencies的。例如如果parent使用dependencyManagement定义了一个dependencyon junit:junit4.0,那么它的children就可以只引用 groupId和artifactId,而version就可以通过parent来设置,这样的好处就是可以集中管理依赖的详情

 

modules:对于多模块的project,outer-module没有必需考虑inner-module的dependencies,当列出modules的时候,modules的顺序是不重要的,因为maven会自动根据依赖关系来拓扑排序,

modules例子如下 :

my-project

other-project

 

properties:是为pom定义一些常量,在pom中的其它地方可以直接引用。

定义方式如下:

 



[html]  ​​view plain​​  ​​copy​​




 



 



  1.   
  2. UTF-8  
  3. 1.5  
  4. 1.5  
  5.   



使用方式 如下 :

${file.encoding}

 

还可以使用project.xx引用pom里定义的其它属性:如$(project.version} 

 

build设置:

defaultGoal:默认的目标,必须跟命令行上的参数相同,如:jar:jar,或者与时期parse相同,例如install

directory:指定build target目标的目录,默认为$(basedir}/target,即项目根目录下的target

finalName:指定去掉后缀的工程名字,例如:默认为${artifactId}-${version}

filters:用于定义指定filter属性的位置,例如filter元素赋值filters/filter1.properties,那么这个文件里面就可以定义name=value对,这个name=value对的值就可以在工程pom中通过${name}引用,默认的filter目录是${basedir}/src/main/fiters/

resources:描述工程中资源的位置 

 



[html]  ​​view plain​​  ​​copy​​




 



 



  1.    
  2. META-INF/plexus   
  3. false   
  4. ${basedir}/src/main/plexus   
  5.    
  6. configuration.xml   
  7.    
  8.    
  9. **/*.properties   
  10.    
  11.   



targetPath:指定build资源到哪个目录,默认是base directory

filtering:指定是否将filter文件(即上面说的filters里定义的*.property文件)的变量值在这个resource文件有效,例如上面就指定那些变量值在configuration文件无效。

directory:指定属性文件的目录,build的过程需要找到它,并且将其放到targetPath下,默认的directory是${basedir}/src/main/resources

includes:指定包含文件的patterns,符合样式并且在directory目录下的文件将会包含进project的资源文件。

excludes:指定不包含在内的patterns,如果inclues与excludes有冲突,那么excludes胜利,那些符合冲突的样式的文件是不会包含进来的。

testResources:这个模块包含​​测试​​资源元素,其内容定义与resources类似,不同的一点是默认的测试资源路径是${basedir}/src/test/resources,测试资源是不部署的。

 

plugins配置:

 



[html]  ​​view plain​​  ​​copy​​




 



 



  1.    
  2. org.apache.maven.plugins   
  3. maven-jar-plugin   
  4. 2.0   
  5. false   
  6. true   
  7.    
  8. test   
  9.    
  10. ...   
  11. ...   
  12.   



extensions:true or false, 决定是否要load这个plugin的extensions,默认为true.

inherited:是否让子pom继承,ture or false 默认为true.

configuration:通常用于私有不开源的plugin,不能够详细了解plugin的内部工作原理,但使plugin满足的properties

dependencies:与pom基础的dependencies的结构和功能都相同,只是plugin的dependencies用于plugin,而pom的denpendencies用于项目本身。在plugin的dependencies主要用于改变plugin原来的dependencies,例如排除一些用不到的dependency或者修改dependency的版本等,详细请看pom的denpendencies.

executions:plugin也有很多个目标,每个目标具有不同的配置,executions就是设定plugin的目标,

 



[html]  ​​view plain​​  ​​copy​​




 



 



  1.    
  2. echodir   
  3.    
  4. run   
  5.    
  6. verify   
  7. false   
  8.    
  9.    
  10. Build Dir: ${project.build.directory}   
  11.    
  12.    
  13.    



id:标识符

goals:里面列出一系列的goals元素,例如上面的run goal

phase:声明goals执行的时期,例如:verify

inherited:是否传递execution到子pom里。

configuration:设置execution下列表的goals的设置,而不是plugin所有的goals的设置

 

pluginManagement配置:

pluginManagement的作用类似于denpendencyManagement,只是denpendencyManagement是用于管理项目jar包依赖,pluginManagement是用于管理plugin。与pom build里的plugins区别是,这里的plugin是列出来,然后让子pom来决定是否引用。

例如:

 



[html]  ​​view plain​​  ​​copy​​




 



 



  1.    
  2.    
  3.    
  4. org.apache.maven.plugins   
  5. maven-jar-plugin   
  6. 2.2   
  7.    
  8.    
  9. pre-process-classes   
  10. compile   
  11.    
  12. jar   
  13.    
  14.    
  15. pre-process   
  16.    
  17.    
  18.    
  19.    
  20.    
  21.    




子pom引用方法: 
在pom的build里的plugins引用: 



[html]  ​​view plain​​  ​​copy​​




 



 



  1.    
  2.    
  3. org.apache.maven.plugins   
  4. maven-jar-plugin   
  5.    
  6.   



build里的directories:

 



[html]  ​​view plain​​  ​​copy​​




 



 



  1. ${basedir}/src/main/java   
  2. ${basedir}/src/main/scripts   
  3. ${basedir}/src/test/java   
  4. ${basedir}/target/classes   
  5. ${basedir}/target/test-classes  



这几个元素只在parent build element里面定义,他们设置多种路径结构,他们并不在profile里,所以不能通过profile来修改

 

build 里面的Extensions: 
它们是一系列build过程中要使用的产品,他们会包含在running bulid‘s classpath里面。他们可以开启extensions,也可以通过提供条件来激活plugins。简单来讲,extensions是在build过程被激活的产品 



[html]  ​​view plain​​  ​​copy​​




 



 



  1.    
  2.    
  3. org.apache.maven.wagon   
  4. wagon-ftp   
  5. 1.0-alpha-3   
  6.    
  7.    



reporting设置:

reporting包含site生成阶段的一些元素,某些maven plugin可以生成reports并且在reporting下配置。例如javadoc,maven site等,在reporting下配置的report plugin的方法与build几乎一样,最不同的是build的plugin goals在executions下设置,而reporting的configures goals在reporttest。

excludeDefaults:是否排除site generator默认产生的reports

outputDirectory,默认的dir变成:${basedir}/target/site

report sets:设置execution goals,相当于build里面的executions,不同的是不能够bind a report to another phase,只能够是site

 



[html]  ​​view plain​​  ​​copy​​




 



 



  1.    
  2.    
  3.    
  4.         ...   
  5.    
  6.    
  7. sunlink   
  8.    
  9. javadoc   
  10.    
  11. true   
  12.    
  13.    
  14. http://java.sun.com/j2se/1.5.0/docs/api/   
  15.    
  16.    
  17.    
  18.    
  19.    
  20.    
  21.    



reporting里面的reportSets和build里面的executions的作用都是控制pom的不同粒度去控制build的过程,我们不单要配置plugins,还要配置那些plugins单独的goals。

更多项目信息:

name:项目除了artifactId外,可以定义多个名称
description: 项目描述
url: 项目url
inceptionYear:创始年份

Licenses



[html]  ​​view plain​​  ​​copy​​




 



 



  1.   
  2.   
  3. Apache 2  
  4. http://www.apache.org/licenses/LICENSE-2.0.txt  
  5. repo  
  6. A business-friendly OSS license  
  7.   
  8.   



列出本工程直接的licenses,而不要列出dependencies的licenses

配置组织信息:



[html]  ​​view plain​​  ​​copy​​




 



 



  1.   
  2. Codehaus Mojo  
  3. http://mojo.codehaus.org  
  4.   



很多工程都受到某些组织运行,这里设置基本信息

 

配置开发者信息:

例如:一个开发者可以有多个roles,properties是 



[html]  ​​view plain​​  ​​copy​​




 



 



  1.   
  2.   
  3. eric  
  4. Eric  
  5. eredmond@codehaus.org  
  6. http://eric.propellors.NET  
  7. Codehaus  
  8. http://mojo.codehaus.org  
  9.   
  10. architect  
  11. developer  
  12.   
  13. -6  
  14.   
  15. http://tinyurl.com/prv4t  
  16.   
  17.   
  18.   
  19.    



环境设置:

issueManagement:bug跟踪管理系统,定义defect tracking system缺陷跟踪系统,比如有(bugzilla,testtrack,clearquest等).

例如:

 



[html]  ​​view plain​​  ​​copy​​




 



 



  1.    
  2. Bugzilla   
  3. http://127.0.0.1/bugzilla/   
  4.    



仓库:

Repositories:pom里面的仓库与setting.xml里的仓库功能是一样的。主要的区别在于,pom里的仓库是个性化的。比如一家大公司里的setting文件是公用 的,所有项目都用一个setting文件,但各个子项目却会引用不同的第三方库,所以就需要在pom里设置自己需要的仓库地址。

repositories:要成为maven2的repository artifact,必须具有pom文件在$BASE_REPO/groupId/artifactId/version/artifactId-version.pom 
BASE_REPO可以是本地,也可以是远程的。repository元素就是声明那些去查找的repositories 
默认的central Maven repository在http://repo1.maven.org/maven2/

 



[html]  ​​view plain​​  ​​copy​​




 



 



  1.    
  2.    
  3.    
  4. false   
  5. always   
  6. warn   
  7.    
  8.    
  9. true   
  10. never   
  11. fail   
  12.    
  13. codehausSnapshots   
  14. Codehaus Snapshots   
  15. http://snapshots.maven.codehaus.org/maven2   
  16. default   
  17.    
  18.    



release和snapshots:是artifact的两种policies,pom可以选择那种政策有效。 
enable:本别指定两种类型是否可用,true or false 
updatePolicy:说明更新发生的频率always 或者 never 或者 daily(默认的)或者 interval:X(X是分钟数) 

checksumPolicy:当Maven的部署文件到仓库中,它也部署了相应的校验和文件。您可以选择忽略,失败,或缺少或不正确的校验和警告。

layout:maven1.x与maven2有不同的layout,所以可以声明为default或者是legacy(遗留方式maven1.x)。

 

插件仓库:

pluginRepositories:与Repositories具有类似的结构,只是Repositories是dependencies的home,而这个是plugins 的home。

 

分发管理:

distributionManagement :管理distribution和supporting files。 

downloadUrl:是其他项目为了抓取本项目的pom’s artifact而指定的url,就是说告诉pom upload的地址也就是别人可以下载的地址。 
status:这里的状态不要受到我们的设置,maven会自动设置project的状态,有效的值:none:没有声明状态,pom默认的;converted:本project是管理员从原先的maven版本convert到maven2的;partner:以前叫做synched,意思是与partner repository已经进行了同步;deployed:至今为止最经常的状态,意思是制品是从maven2 instance部署的,人工在命令行deploy的就会得到这个;verified:本制品已经经过验证,也就是已经定下来了最终版。 
repository:声明deploy过程中current project会如何变成repository,说明部署到repository的信息。 



[html]  ​​view plain​​  ​​copy​​




 



 



  1.    
  2. false   
  3. corp1   
  4. Corporate Repository   
  5. scp://repo1/maven2   
  6. default   
  7.    
  8.    
  9. true   
  10. propSnap   
  11. Propellors Snapshots   
  12. sftp://propellers.Net/maven   
  13. legacy   
  14.    



id, name::唯一性的id,和可读性的name 
uniqueVersion:指定是否产生一个唯一性的version number还是使用address里的其中version部分。true or false 
url:说明location和transport protocol 
layout:default或者legacy

 

profiles:pom4.0的一个新特性就是具有根据environment来修改设置的能力

它包含可选的activation(profile的触发器)和一系列的changes。例如test过程可能会指向不同的​​数据库​​(相对最终的deployment)或者不同的dependencies或者不同的repositories,并且是根据不同的JDK来改变的。那么结构如下: 



[html]  ​​view plain​​  ​​copy​​




 



 



  1.    
  2.    
  3. test   
  4. ...   
  5. ...   
  6. ...   
  7. ...   
  8. ...   
  9. ...   
  10. ...   
  11. ...   
  12. ...   
  13.    
  14.    



Activation: 
触发这个profile的条件配置如下例:(只需要其中一个成立就可以激活profile,如果第一个条件满足了,那么后面就不会在进行匹配。 



[html]  ​​view plain​​  ​​copy​​




 



 



  1.    
  2. test   
  3.    
  4. false   
  5. 1.5   
  6.    
  7. Windows XP   
  8. Windows   
  9. x86   
  10. 5.1.2600   
  11.    
  12.    
  13. mavenVersion   
  14. 2.0.3   
  15.    
  16.    
  17. ${basedir}/file2.properties   
  18. ${basedir}/file1.properties   
  19.    
  20.    



激活profile的方法有多个:setting文件的activeProfile元素明确指定激活的profile的ID,在命令行上明确激活Profile用-P flag 参数 

查看某个build会激活的profile列表可以用:mvn help:active-profiles 

五、Maven私服

(一)nexus安装

1. 安装nexus

下载nexus至本地目录

在命令提示符下,进入本地资源目录,敲入命令

nexus install

如有下列提示则证明安装成功。

Maven——项目构建完整配置_运维

如果想卸载,命令为:

nexus uninstall

2. 启动nexus

在服务中找到nexus ,右键启动

Maven——项目构建完整配置_操作系统_02

3. 进入nexus 

打开浏览器输入地址:​​http://localhost:8081/nexus​​

Maven——项目构建完整配置_操作系统_03

点击右上角的Log In  ,在弹出窗口中输入username为admin ,password为admin123 ,点击Log In按钮

Maven——项目构建完整配置_运维_04

(二)构建索引

刚刚安装好的nexus是无法直接搜索到jar包的,必须下载索引才可以正常使用。

我们到中央仓库地址 ​​http://repo.maven.apache.org/maven2/.index/​​下载以下两个索引压缩文件





这两个文件一般在此页面的最后的位置,另外还需要在网上下载一个名为indexer-cli-5.1.1.jar 的jar包 

将以下三个文件放入一个文件夹

Maven——项目构建完整配置_java_05

cmd执行:

java -jar indexer-cli-5.1.1.jar -u nexus-maven-repository-index.gz -d indexer

执行后生成目录indexer,目录下的文件是索引文件,如下图

Maven——项目构建完整配置_运维_06

将D:\nexus-2.12.0-01-bundle\sonatype-work\nexus\indexer\central-ctx 目录下的文件删除,将上边的索引文件拷贝到此目录下。

拷贝后重新启动nexus,然后进入nexus搜索jar包发现可以进行搜索了。

Maven——项目构建完整配置_xml_07

(三)仓库类型

1、hosted:宿主仓库,自己项目的jar要放到hosted类型的仓库中。

2、proxy:代理仓库,代理中央仓库

3、virtual:虚拟仓库,存储了maven1的构件,一般不用。

4、group:仓库组,将一些仓库组成一个组,自己项目连接仓库组去下载jar包。

       平常开发中都去连接仓库组,仓库组中包括:hosted、proxy等。

Maven——项目构建完整配置_运维_08

(四)镜像配置

nexus已经安装好了,我们要开始使用它了。现在我们需要在maven的配置文件settings.xml中配置镜像,让maven找私服,而不是直接到中央仓库下载。

打开maven的配置文件settings.xml,添加如下配置:

    

    

      nexus

      *

      http://localhost:8081/nexus/content/groups/public/

    

mirrors为镜像组,可以配置多个mirror(镜像),我们这里配置的是nexus中的中央代理仓库。

配置好后,我们需要创建一个项目来测试一下,看看是不是可以通过nexus来下载jar包。

测试步骤:

创建maven工程(jar),在pom.xml中添加依赖,观察nexus中是否下载了相应的jar包,目录为 nexus-2.12.0-01-bundle\sonatype-work\nexus\storage\central

(五)发布自己的工程jar到nexus

1. 创建maven工程itcastutil (jar) 

坐标信息:  Group Id :cn.itcast.util  

           Artifact Id: itcastutil

创建包cn.itcast.util

创建类DateUtil 

package cn.itcast.util;

 

import java.util.Calendar;

import java.util.Date;

/**

 * 日期工具类

 * @author Administrator

 *

 */

publicclass DateUtil {

    

    /**

     * 获取当前年份

     * @return

    

    publicstaticint getYear(){

    

        Calendar calendar=Calendar.getInstance();

        calendar.setTime(new Date());

        return  calendar.get(Calendar.YEAR);

    }

}

在pom.xml中引入如下配置信息 

    

        releases

        

             ​​http://localhost:8081/nexus/content/repositories/releases/​​

       

    

    

        snapshots

        

            ​​http://localhost:8081/nexus/content/repositories/snapshots/​​

        

    

  

2. 在Maven中配置

将以下配置信息放入maven的settings.xml中

  

    

      releases

      admin

      admin123

       

    

      snapshots

      admin

      admin123

    

  

注意:settings.xml 配置 和 pom.xml 对应!

然后执行命令  deploy  ,有如下提示表示部署成功

Maven——项目构建完整配置_运维_09

我们在私服中进行查询,发现可以查到刚才我们上传的jar

Maven——项目构建完整配置_操作系统_10

(六)发布第三方的工程jar到nexus

有很多jar包由于版权等问题,并不会出现在中央仓库中,比如oracle的驱动,那如何将这类jar包放在nexus中呢?我们看下列操作步骤:

(1)选择左侧菜单的Repositories,  在Repositories窗口中选择3rd party

Maven——项目构建完整配置_xml_11

(2)在3rd party窗口中选择Artifact Upload 

Maven——项目构建完整配置_xml_12

(3)在Artifact Upload选项卡中填写坐标,并上传jar包。

 

Maven——项目构建完整配置_java_13

上传jar包选择oracle的驱动。

Maven——项目构建完整配置_操作系统_14

填写坐标

Maven——项目构建完整配置_xml_15

有下列提示则为上传成功

Maven——项目构建完整配置_运维_16

上传成功后可以在3rd party库中找到此jar包

Maven——项目构建完整配置_运维_17

(七)从nexus中下载jar包

  1、对于所有项目,设置setting.xml



[html]  ​​view plain​​  ​​copy​​




  1.        
  2.     
  3. dev       
  4.        
  5.       
  6.     
  7. nexus       
  8.     
  9. http://localhost:8081/nexus/content/groups/public/       
  10.     
  11.        
  12. true       
  13.        
  14.     
  15.        
  16. true       
  17.        
  18.        
  19.       
  20.       
  21.     
  22.       
  23.     
  24. public      
  25. Public Repositories      
  26. http://localhost:8081/nexus/content/groups/public/      
  27.       
  28.       
  29.       



  2、对于单个项目,设置pom.xml



[html]  ​​view plain​​  ​​copy​​


1.     
2.
3.
4. true
5.

6.
7. true
8.

9. nexus
10. http://localhost:8081/nexus/content/groups/public/
11.

12.
13.
14. false
15.

16. central
17. Central Repository
18. https://repo.maven.apache.org/maven2
19.

20.

21.
22.
23. public
24. Public Repositories
25. http://localhost:8081/nexus/content/groups/public/
26.

27.
28.
29. never
30.

31.
32. false
33.

34. central
35. Central Repository
36. https://repo.maven.apache.org/maven2
37.

38.


六、Maven常见问题


特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
精选文章
thumb 中国研究员首次曝光美国国安局顶级后门—“方程式组织”
thumb 俄乌线上战争,网络攻击弥漫着数字硝烟
thumb 从网络安全角度了解俄罗斯入侵乌克兰的相关事件时间线
下一篇
Java工程师常见面试题分享 2023-08-28 20:51:14