MAVEN详述

MAVEN 配置
1. 环境变量

  • 在path中输入%MAVEN_HOME%\bin;

MAVEN详述_第1张图片

  • 新建环境变量MAVEN_HOME 添加变量地址

MAVEN详述_第2张图片

  • 测试 输出 mvn -v

MAVEN详述_第3张图片

Maven自定义本地目录(Maven中央库)

默认存jar包的中央库,本地仓库。
该值表示构建系统本地仓库的路径,默认值为$(user.home)/.m2/repository,也就是C盘下的admin下的用户下进行存储会影响c盘的性能所以一般都会修改中央库存储位置。

  • 更改中央库存储位置

修改Maven配置文件,在apache-maven-3.0.5\conf\settings.xml,也就是Maven的全局变量
MAVEN详述_第4张图片

测试Mavne的配置 cmd中出入 mvn -version显示正常则表示配置完成

Myecplise配置Maven

打印所有java系统属性和环境变量
cmd 中运行 mvn help:system(可自动根据setting中配置的本地库路径创建文件)

2. myeclipse配置Maven中央库路径

MAVEN详述_第5张图片

MAVEN详述_第6张图片

3. Myeclipse配置jdk

MAVEN详述_第7张图片
然后在Window->Preference->Java->Installed JREs->Edit 在Default VM arguments中设置 -Dmaven.multiModuleProjectDirectory=$MAVEN_HOME
这里的MAVEN_HOME是计算机配置环境变量的名字MAVEN_HOME=F:\apache-maven-3.0.5

4.创建maven项目中 group id和artifact id填写规范

groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找。
  groupId一般分为多个段,这里我只说两段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。举个apache公司的tomcat项目例子:这个项目的groupId是org.apache,它的域是org(因为tomcat是非营利项目),公司名称是apache,artigactId是tomcat。
  比如我创建一个项目,我一般会将groupId设置为cn.lqg,cn表示域为中国,pq是我个人姓名缩写,artifactId设置为testProj,表示你这个项目的名称是testProj,依照这个设置,你的包结构最好是cn.lqg.testProj打头的,如果有个StudentPeng,它的全路径就是cn.lqg.testProj.StudentPeng

4.seeting.xml详解

maven的配置文件settings.xml存在于两个地方:
1.安装的地方: M2HOME/conf/settings.xml2. M 2 H O M E / c o n f / s e t t i n g s . x m l 2. 用 户 的 目 录 : {user.home}/.m2/settings.xml

前者又被叫做全局配置,对操作系统的所有使用者生效;后者被称为用户配置,只对当前操作系统的使用者生效。如果两者都存在,它们的内容将被合并,并且用户范围的settings.xml会覆盖全局的settings.xml。

Maven安装后,用户目录下不会自动生成settings.xml,只有全局配置文件。如果需要创建用户范围的settings.xml,可以将安装路径下的settings复制到目录${user.home}/.m2/。Maven默认的settings.xml是一个包含了注释和例子的模板,可以快速的修改它来达到你的要求。

全局配置一旦更改,所有的用户都会受到影响,而且如果maven进行升级,所有的配置都会被清除,所以要提前复制和备份${M2_HOME}/conf/settings.xml文件,一般情况下不推荐配置全局的settings.xml。

代码详解

  


  - xsi全名:xml schema instance
  - version是版本的意思
  - xmlns是web.xml文件用到的命名空间
  -  xmlns:xsi是指web.xml遵守xml规范
  -  xsi:schemaLocation是指具体用到的schema资源

<settings   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/settings-1.0.0.xsd">  


      
    <localRepository>usr/local/mavenlocalRepository>  

      
    <interactiveMode>trueinteractiveMode>  

      
      
      
    <usePluginRegistry>falseusePluginRegistry>  

      
      
    <offline>falseoffline>  

      
      
      


      
    <pluginGroups>  

          
        <pluginGroup>org.codehaus.mojopluginGroup>  

    pluginGroups>  

  • 代理

      
    <proxies>  

          
        <proxy>  

              
            <id>myproxyid>  

              
            <active>trueactive>  

              
            <protocol>http://…protocol>  

              
            <host>proxy.somewhere.comhost>  

              
            <port>8080port>  

               
            <username>proxyuserusername>  

              
            <password>somepasswordpassword>  

              
            <nonProxyHosts>*.google.com|ibiblio.orgnonProxyHosts>  

        proxy>  

    proxies>  

  • 配置服务端设置

      
    <servers>  
          
        <server>  

              
            <id>server001id>  

              
            <username>my_loginusername>  

              
            <password>my_passwordpassword>  

              
              
            <privateKey>${usr.home}/.ssh/id_dsaprivateKey>  

              
            <passphrase>some_passphrasepassphrase>  

              
              
            <filePermissions>664filePermissions>  

              
            <directoryPermissions>775directoryPermissions>  

              
            <configuration>configuration>  

        server>  

    servers>  
  • 下载镜像列表
      
    <mirrors>  

          
        <mirror>  

              
            <id>planetmirror.comid>  

              
            <name>PlanetMirror Australianame>  

              
            <url>http://downloads.planetmirror.com/pub/maven2url>  

              
              
            <mirrorOf>centralmirrorOf>  

        mirror>  

    mirrors>  
  • 调整构建配置的列表

      
      
      
      
    <profiles>  

          
        <profile>  

              
            <id>testid>  

              
              
              
              
            <activation>  

                  
                <activeByDefault>falseactiveByDefault>  

                  
                <jdk>1.7jdk>  

                  
                <os>  

                      
                    <name>Windows XPname>  

                      
                    <family>Windowsfamily>  

                      
                    <arch>x86arch>  

                      
                    <version>5.1.2600version>  

                os>  

                  
                  
                <property>  

                      
                    <name>mavenVersionname>  

                      
                    <value>2.0.3value>  

                property>  

                  
                  
                <file>  

                      
                    <exists>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/exists>  

                      
                    <missing>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/missing>  

                file>  

            activation>  

               
              
               
              
              
              
              
              
            <properties>  

                  
                <user.install>usr/local/winner/jobs/maven-guideuser.install>  

            properties>  
  • 远程仓库列表

              
            <repositories>  

                  
                <repository>  

                      
                    <id>codehausSnapshotsid>  

                      
                    <name>Codehaus Snapshotsname>  

                      
                    <releases>  

                          
                        <enabled>falseenabled>  

                          
                          
                        <updatePolicy>alwaysupdatePolicy>  

                          
                          
                        <checksumPolicy>warnchecksumPolicy>  

                    releases>  

                      
                      
                    <snapshots>  

                        <enabled />  
                        <updatePolicy />  
                        <checksumPolicy />  

                    snapshots>  

                      
                    <url>http://snapshots.maven.codehaus.org/maven2url>  

                      
                      
                    <layout>defaultlayout>  

                repository>  

            repositories>  
  • 发现插件的远程仓库列表

              
              
              
            <pluginRepositories>  

                  
                <pluginRepository>  
                    <releases>  
                        <enabled />  
                        <updatePolicy />  
                        <checksumPolicy />  
                    releases>  

                    <snapshots>  
                        <enabled />  
                        <updatePolicy />  
                        <checksumPolicy />  
                    snapshots>  

                    <id />  
                    <name />  
                    <url />  
                    <layout />  
                pluginRepository>  

            pluginRepositories>  
  • 手动激活profiles的列表

              
              
              
              
            <activeProfiles>  

                <activeProfile>env-testactiveProfile>  

            activeProfiles>  

        profile>  

    profiles>  

settings>  

profile节点

节点包含了激活(activation),仓库(repositories),插件仓库(pluginRepositories)和属性(properties)共四个子元素元素。profile元素仅包含这四个元素是因为他们涉及到整个的构建系统,而不是个别的项目级别的POM配置。

profile可以让maven能够自动适应外部的环境变化,比如同一个项目,在linux下编译linux的版本,在win下编译win的版本等。一个项目可以设置多个profile,也可以在同一时间设置多个profile被激活(active)的。自动激活的 profile的条件可以是各种各样的设定条件,组合放置在activation节点中,也可以通过命令行直接指定。如果认为profile设置比较复杂,可以将所有的profiles内容移动到专门的profiles.xml 文件中,不过记得和pom.xml放在一起。

activation节点是设置该profile在什么条件下会被激活,常见的条件有如下几个:

1. os

判断操作系统相关的参数,它包含如下可以自由组合的子节点元素

message - 规则失败之后显示的消息

arch - 匹配cpu结构,常见为x86

family -
匹配操作系统家族,常见的取值为:dos,mac,netware,os/2,unix,windows,win9x,os/400等

name - 匹配操作系统的名字

version - 匹配的操作系统版本号

display - 检测到操作系统之后显示的信息

2. jdk

检查jdk版本,可以用区间表示。

3. property

检查属性值,本节点可以包含name和value两个子节点。

4. file

检查文件相关内容,包含两个子节点:exists和missing,用于分别检查文件存在和不存在两种情况。

如果settings中的profile被激活,那么它的值将覆盖POM或者profiles.xml中的任何相等ID的profiles。

如果想要某个profile默认处于激活状态,可以在中将该profile的id放进去。这样,不论环境设置如何,其对应的
profile都会被激活。

4.pool.xml配置文件详解

"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/maven-v4_0_0.xsd "> 
 
    <parent> 
         
        <artifactId>xxxartifactId>

        
        <groupId>xxxgroupId> 

         
        <version>xxxversion>

         
        <relativePath>xxxrelativePath> 
    parent>
     
    <modelVersion> 4.0.0 modelVersion> 

     
    <groupId>xxxgroupId> 

     
    <artifactId>xxxartifactId> 

     
    <packaging> jar packaging> 

     
    <version> 1.0-SNAPSHOT version> 

     
    <name> xxx-maven name> 

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

     
    <description> A maven project to study maven. description> 

     
    <prerequisites> 
         
        <maven>maven> 
    prerequisites> 
     
    <issueManagement> 
         
        <system> jira system> 

         
        <url> http://jira.baidu.com/banseon url> 
    issueManagement> 
     
    <ciManagement> 
         
        <system>system> 

         
        <url>url> 

         
        <notifiers> 
             
            <notifier> 
                 
                <type>type> 

                 
                <sendOnError>sendOnError> 

                 
                <sendOnFailure>sendOnFailure> 

                 
                <sendOnSuccess>sendOnSuccess> 

                 
                <sendOnWarning>sendOnWarning> 

                 
                <address>address> 

                 
                <configuration>configuration> 
            notifier> 
        notifiers> 
    ciManagement> 
     
    <inceptionYear /> 
     
    <mailingLists> 
         
        <mailingList> 
             
            <name> Demo name> 

             
            <post> [email protected] post> 

             
            <subscribe> [email protected] subscribe> 

             
            <unsubscribe> [email protected] unsubscribe> 

             
            <archive> http:/hi.baidu.com/banseon/demo/dev/ archive> 
        mailingList> 
    mailingLists> 
     
    <developers> 
         
        <developer> 
             
            <id> HELLO WORLD id>

             
            <name> banseon name> 

             
            <email> [email protected] email> 

             
            <url>url> 

             
            <roles> 
                <role> Project Manager role> 
                <role> Architect role> 
            roles> 

             
            <organization> demo organization> 

             
            <organizationUrl> http://hi.baidu.com/xxx organizationUrl> 

             
            <properties> 
                <dept> No dept> 
            properties> 

             
            <timezone> -5 timezone> 
        developer> 
    developers> 
     
    <contributors> 
         
        <contributor> 
             
            <name>name>

            
            <email>email>

            
            <url>url>

            
            <organization>organization>

            
            <organizationUrl>organizationUrl>

             
            <roles> 
                <role> Project Manager role> 
                <role> Architect role> 
            roles>

             
            <timezone>timezone>

             
            <properties> 
                <dept> No dept> 
            properties>  
        contributor> 
    contributors> 
     
    <licenses> 
         
        <license> 
             
            <name> Apache 2 name> 

             
            <url> http://www.baidu.com/banseon/LICENSE-2.0.txt url> 

             
            <distribution> repo distribution> 

             
            <comments> A business-friendly OSS license comments> 
        license> 
    licenses> 
     
    <scm> 
         
        <connection> 
            scm:svn:http://svn.baidu.com/banseon/maven/banseon/banseon-maven2-trunk(dao-trunk)
        connection> 

         
        <developerConnection> 
            scm:svn:http://svn.baidu.com/banseon/maven/banseon/dao-trunk 
        developerConnection> 

         
        <tag>tag> 

         
        <url> http://svn.baidu.com/banseon url> 
    scm> 
  • 描述项目所属组织的各种属性
     
    <organization> 
         
        <name> demo name> 

         
        <url> http://www.baidu.com/banseon url> 
    organization> 
  • 构建项目需要的信息
     
    <build> 
         
        <sourceDirectory>sourceDirectory> 

         
        <scriptSourceDirectory>scriptSourceDirectory> 

         
        <testSourceDirectory>testSourceDirectory> 

         
        <outputDirectory>outputDirectory> 

         
        <testOutputDirectory>testOutputDirectory> 

         
        <extensions> 
             
            <extension> 
                 
                <groupId>groupId> 

                 
                <artifactId>artifactId> 

                 
                <version>version> 
            extension> 
        extensions> 

         
        <defaultGoal>defaultGoal> 

         
        <resources> 
             
            <resource> 
                 
                <targetPath>targetPath> 

                 
                <filtering>filtering>

                 
                <directory>directory>

                 
                <includes>
                    <include>include>
                includes>

                
                <excludes>
                    <exclude>exclude>
                excludes>
            resource> 
        resources> 

         
        <testResources> 
             
            <testResource> 
                 
                <targetPath>targetPath> 

                 
                <filtering>filtering>

                 
                <directory>directory>

                 
                <includes>
                    <include>include>
                includes>

                
                <excludes>
                    <exclude>exclude>
                excludes> 
            testResource> 
        testResources> 

         
        <directory>directory> 

         
        <finalName>finalName> 

         
        <filters>filters> 

         
        <pluginManagement> 
             
            <plugins> 
                 
                <plugin> 
                     
                    <groupId>groupId> 

                     
                    <artifactId>artifactId> 

                     
                    <version>version> 

                     
                    <extensions>true/falseextensions> 

                     
                    <executions> 
                         
                        <execution> 
                             
                            <id>id>

                             
                            <phase>phase>

                             
                            <goals>goals> 

                             
                            <inherited>true/falseinherited> 

                             
                            <configuration>configuration>
                        execution> 
                    executions> 

                     
                    <dependencies>
                         
                        <dependency> 
                        dependency> 
                    dependencies> 

                     
                    <inherited>true/falseinherited>

                     
                    <configuration>configuration>
                plugin> 
            plugins> 
        pluginManagement> 

         
        <plugins> 
             
            <plugin> 
                 
                <groupId>groupId> 

                 
                <artifactId>artifactId> 

                 
                <version>version> 

                 
                <extensions>true/falseextensions> 

                 
                <executions> 
                     
                    <execution> 
                         
                        <id>id>

                         
                        <phase>phase>

                         
                        <goals>goals> 

                         
                        <inherited>true/falseinherited> 

                         
                        <configuration>configuration>
                    execution> 
                executions> 

                 
                <dependencies>
                     
                    <dependency> 
                    dependency> 
                dependencies> 

                 
                <inherited>true/falseinherited> 

                 
                <configuration>configuration> 
            plugin> 
        plugins>
    build> 
  • 构建profile
     
    <profiles> 
         
        <profile> 
             
            <id>id>

             
            <activation> 
                 
                <activeByDefault>true/falseactiveByDefault> 

                 
                <jdk>jdk版本,如:1.7jdk> 

                 
                <os> 
                     
                    <name> Windows XP name> 

                     
                    <family> Windows family> 

                     
                    <arch> x86 arch> 

                     
                    <version> 5.1.2600 version> 
                os> 

                 
                <property> 
                     
                    <name> mavenVersion name> 

                     
                    <value> 2.0.3 value> 
                property> 

                 
                <file> 
                     
                    <exists> /usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/ exists> 

                     
                    <missing> /usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/ missing> 
                file> 
            activation> 

             
            <build> 
                <defaultGoal /> 
                <resources> 
                    <resource> 
                        <targetPath>targetPath>
                        <filtering>filtering>
                        <directory>directory>
                        <includes>
                            <include>include>
                        includes>
                        <excludes>
                            <exclude>exclude>
                        excludes>
                    resource> 
                resources> 
                <testResources> 
                    <testResource> 
                        <targetPath>targetPath>
                        <filtering>filtering>
                        <directory>directory>
                        <includes>
                            <include>include>
                        includes>
                        <excludes>
                            <exclude>exclude>
                        excludes> 
                    testResource> 
                testResources> 
                <directory>directory>
                <finalName>finalName>
                <filters>filters> 
                <pluginManagement> 
                    <plugins> 
                         
                        <plugin> 
                            <groupId>groupId>
                            <artifactId>artifactId>
                            <version>version>
                            <extensions>true/falseextensions> 
                            <executions> 
                                <execution> 
                                    <id>id>
                                    <phase>phase>
                                    <goals>goals>
                                    <inherited>true/falseinherited>
                                    <configuration>configuration> 
                                execution> 
                            executions> 
                            <dependencies> 
                                 
                                <dependency> 
                                dependency> 
                            dependencies> 
                            <goals>goals>
                            <inherited>true/falseinherited>
                            <configuration>configuration>
                        plugin> 
                    plugins> 
                pluginManagement> 
                <plugins> 
                     
                    <plugin> 
                        <groupId>groupId>
                        <artifactId>artifactId>
                        <version>version>
                        <extensions>true/falseextensions> 
                        <executions> 
                            <execution> 
                                <id>id>
                                <phase>phase>
                                <goals>goals>
                                <inherited>true/falseinherited>
                                <configuration>configuration> 
                            execution> 
                        executions> 
                        <dependencies> 
                             
                            <dependency> 
                            dependency> 
                        dependencies> 
                        <goals>goals>
                        <inherited>true/falseinherited>
                        <configuration>configuration> 
                    plugin> 
                plugins> 
            build> 

             
            <modules>
                
                <module>module>
            modules> 

             
            <repositories> 
                 
                <repository> 
                    <releases> 
                        <enabled><enabled>
                        <updatePolicy>updatePolicy>
                        <checksumPolicy>checksumPolicy> 
                    releases> 
                    <snapshots> 
                        <enabled><enabled>
                        <updatePolicy>updatePolicy>
                        <checksumPolicy>checksumPolicy> 
                    snapshots> 
                    <id>id>
                    <name>name>
                    <url>url>
                    <layout>layout> 
                repository> 
            repositories> 

             
            <pluginRepositories> 
                 
                <pluginRepository> 
                    <releases> 
                        <enabled><enabled>
                        <updatePolicy>updatePolicy>
                        <checksumPolicy>checksumPolicy> 
                    releases> 
                    <snapshots> 
                        <enabled><enabled>
                        <updatePolicy>updatePolicy>
                        <checksumPolicy>checksumPolicy>
                    snapshots> 
                    <id>id>
                    <name>name>
                    <url>url>
                    <layout>layout> 
                pluginRepository> 
            pluginRepositories> 

             
            <dependencies> 
                 
                <dependency> 
                dependency> 
            dependencies> 

             
            <reports>reports> 

             
            <reporting>reporting> 

             
            <dependencyManagement> 
                <dependencies> 
                     
                    <dependency> 
                    dependency> 
                dependencies> 
            dependencyManagement> 

             
            <distributionManagement> 
            distributionManagement> 

             
            <properties /> 
        profile> 
    profiles> 
  • 子项目 模块的目录的相对路径
     
    <modules>
        
        <module>module>
    modules> 
  • 远程仓库
     
    <repositories> 
         
        <repository> 
             
            <releases> 
                 
                <enabled><enabled> 

                 
                <updatePolicy>updatePolicy> 

                 
                <checksumPolicy>checksumPolicy> 
            releases> 

             
            <snapshots> 
                <enabled><enabled>
                <updatePolicy>updatePolicy>
                <checksumPolicy>checksumPolicy> 
            snapshots> 

             
            <id> banseon-repository-proxy id> 

             
            <name> banseon-repository-proxy name> 

             
            <url> http://192.168.1.169:9999/repository/ url> 

             
            <layout> default layout> 
        repository> 
    repositories> 
  • 远程插件仓库的信息
     
    <pluginRepositories> 
         
        <pluginRepository> 
        pluginRepository> 
    pluginRepositories> 
  • 项目相关的依赖
     
    <dependencies> 
        <dependency> 
             
            <groupId> org.apache.maven groupId> 

             
            <artifactId> maven-artifact artifactId> 

             
            <version> 3.8.1 version> 

             
            <type> jar type> 

             
            <classifier>classifier> 

             
            <scope> test scope> 

             
            <systemPath>systemPath> 

             
            <exclusions> 
                <exclusion> 
                    <artifactId> spring-core artifactId> 
                    <groupId> org.springframework groupId> 
                exclusion> 
            exclusions> 

             
            <optional> true optional> 
        dependency> 
    dependencies> 

Maven忽略该元素

     
    <reports>reports> 
  • 报表插件产生报表的规范
     
    <reporting> 
         
        <excludeDefaults /> 

         
        <outputDirectory /> 

         
        <plugins> 
             
            <plugin> 
                 
                <groupId>groupId> 
                 
                <artifactId>artifactId> 

                 
                <version>version> 

                 
                <inherited>true/falseinherited> 

                 
                <configuration>configuration> 

                 
                <reportSets> 
                     
                    <reportSet> 
                         
                        <id>id> 

                         
                        <configuration>configuration> 

                         
                        <inherited>true/falseinherited> 

                         
                        <reports>reports> 
                    reportSet> 
                reportSets> 
            plugin> 
        plugins> 
    reporting> 
  • 子项目的默认依赖信息
     
    <dependencyManagement> 
        <dependencies> 
             
            <dependency> 
            dependency> 
        dependencies> 
    dependencyManagement> 
  • 项目分发信息
     
    <distributionManagement> 
         
        <repository> 
             
            <uniqueVersion /> 
            <id> banseon-maven2 id> 
            <name> banseon maven2 name> 
            <url> file://${basedir}/target/deploy url> 
            <layout>layout> 
        repository> 

         
        <snapshotRepository> 
            <uniqueVersion /> 
            <id> banseon-maven2 id> 
            <name> Banseon-maven2 Snapshot Repository name> 
            <url> scp://svn.baidu.com/banseon:/usr/local/maven-snapshot url> 
            <layout>layout>
        snapshotRepository> 

         
        <site> 
             
            <id> banseon-site id> 

             
            <name> business api website name> 

             
            <url> 
                scp://svn.baidu.com/banseon:/var/www/localhost/banseon-web 
            url> 
        site> 

         
        <downloadUrl /> 

         
        <relocation> 
             
            <groupId>groupId> 

             
            <artifactId>artifactId> 

             
            <version>version> 

             
            <message>message> 
        relocation> 

         
        <status>status> 
    distributionManagement> 
  • 以值替代名称
     
    <properties>
        <name>valuename>
    properties> 
project> 

本文部分转自他出,只为相互交流。

你可能感兴趣的:(maven)