maven

1    maven的安装与配置

1.1   maven的安装

 

 

       A:解压压缩包到某一目录中。

 

       B:配置环境变量

             

              M2_HOME:D:\apache-maven-3.2.5(类似Java_Home)

 

              path:加入D:\apache-maven-3.2.5\bin

 

       C:cmd窗口:输出mvn -v:如果有提示信息,表示安装成功。

 

 

更改Maven的本地仓库的路径

       bin

 

       conf目录:setting.xml:Maven的配置文件。

              A:更改Maven的本地仓库的路径:

 

Maven缺省的本地仓库路径为${user.home}/.m2/repository。 
可以通过修改${user.home}/.m2/settings.xml配置本地仓库路径 
Xml代码 
   
 D:\java\repository   
   

1.2   约定大于配置。(目录结构)

 

       Maven提倡“约定优于配置”(Convention Over Configuration),

       这是 Maven最核心的设计理念之一。

 

       原因之一就是使用约定可以大量减少配置。

 

       a)源码目录为 src/main/java/

 

       b)源码资源目录为 src/main/resources/

 

       c)测试目录为 src/test/java/

 

       d)测试资源目录为 src/test/resources/

 

       e)编译输出目录为 target/classes/

 

       f)打包方式为 jar/war

 

       g)包输出目录为 target/

 

       h)超级 pom

 

           超级 pom 定义以上的目录结构、核心插件设定版本。Maven 设定核心插件的原因,

 

           是防止由于插件版本的变化而造成构建的不稳定。

 

           遵循约定虽然损失了一定的灵活性,用户不能随意安排目录结构,但是却能减少配置。

 

           更重要的是,遵循约定能够帮用户遵循构建标准。个性往往意味着牺牲通用性,

 

           意味着增加无谓的复杂度。

 

2    maven与IDE结合:

2.1   maven与myeclipse2014创建一个简单的maven工程。

      

       A:更改MyElipse的默认Maven。Window---->Preferences---->MyEclipse---->Maven4MyEclipse--->Installations

 

       B:更改MyElipse的默认Maven。Window---->Preferences---->MyEclipse---->Maven4MyEclipse--->Installations:更改为Maven目录的settting.xml的路径。

 

       C:更改MyElipse的默认Maven。Preferences---->MyEclipse---->Maven4MyEclipse中的jdk路径。

 

      

 

       工程目录(main中的java/resource与test中的java/resource是源文件夹,不是文件夹。)

 

              src

                     main

                            java:                  放java的源文件

                            resources             放配置文件。

                            webapp

                                   WEB-INF

                                   web.xml

                     test

                            java               放测试javar的源文件

                            resources             放测试的的配置文件。

 

 

                     pom.xml                     工程模型配置文件。

 

                           

              target

                    

                     classes

 

 

 

       pom.xml

             

       坐标定义:

       com.dfrz                          G     公司或者组的定义。

       Maven_01                      A     项目或者模块名称定义

       0.0.1-SNAPSHOT               V: 版本定义

 

       GAV:坐标

 

 

       SNAPSHOT--->Alpha--->Beta--->Release----GA

 

       SNAPSHOT:内部开发版本

 

       Alpha:   内部公测版本

 

       Beta:    对外公开的测试版本

 

       Release:可以使用的版本

 

       GA:     稳定版本

 

       jar                      运行maven的package命令时,该工程会打包为一个java

 

       maven中通过GAV形成坐标。

 

 

       //Maven工程中的依赖:从本地仓库中寻找。如果本地仓库中寻找不到,再找网络仓库(中央仓库)中寻找。

      

             

                     junit

                     junit

                     3.8.1

                     test

             

      

与 

2.2   maven与intellijidea结合

http://blog.csdn.net/zht666/article/details/8673609/

 

idea设置默认maven配置

2.3      scope取值

              maven认为,程序对外部的依赖会随着程序的所处阶段和应用场景而变化,

              所以maven中的依赖关系有作用域(scope)的限制。在maven中,scope包含如下的取值:

 

              compile(编译范围)

                     compile是默认的范围;如果没有提供一个范围,那该依赖的范围就是编译范围。

                     编译范围依赖在所有的classpath中可用,同时它们也会被打包。

 

 

              provided(已提供范围)

                     provided依赖只有在当JDK或者一个容器已提供该依赖之后才使用。

                     例如,如果你开发了一个web应用,

                     你可能在编译classpath中需要可用 的Servlet API来编译一个servlet,

                     但是你不会想要在打包好的WAR中包含这个Servlet API;

                     这个Servlet API JAR由你的应用服务器或者servlet容器提供。

                     已提供范围的依赖在编译classpath(不是运行时)可用。

                     它们不是传递性的,也不会被打包。

 

              runtime(运行时范围)

                     runtime依赖在运行和测试系统的时候需要,但在编译的时候不需要。

                     比如,你可能在编译的时候只需要JDBC API JAR,

                     而只有在运行的时候才需要JDBC驱动实现。

 

              test(测试范围)

                     test范围依赖在一般的 编译和运行时都不需要,它们只有在测试编译和测试运行阶段可用。

                     测试范围依赖在之前的中介绍过。

             

2.4      使用dependency来下载中央仓库中存在的jar

 

2.5      中央仓库中不存在的jar,可以通过手工安装到本地仓库,然后再进行引用。

 

              A:纯手工安装:

                     按照GAV的方式在仓库中建立对应的文件夹。

                    

                     jar文件的命名:A-V.jar

 

              B:使用命令安装。

                    

                     mvninstall:install-file -Dfile=G:\SoftWare\DataBase_Lib\oracle10G的驱动\ojdbc6_11.2.0.3.0.jar -DgroupId=com.driver -DartifactId=oracle-Dversion=11.2 -Dpackaging=jar -DgeneratePom=true -DcreateChecksum=true

 

             

2.6      maven对于工程的命令的操作。

 

              compile

 

              package:对工程打包。默认存放工程的taget目录下。

 

              install:将jar安装到本地仓库中。

 

      

              命令可以组合一起使用 package install

 

2.7   工程中间Jar的引用

      

       工程不同版本的引用

             

              使用GVA来引用工程。/通过版本的控制

       自动判断

 

              不同深度:路径优先级

                     A---->B---->C(1.2.7)

                     D---->C(1.2.15)

                     选1.2.15版本,路径短

 

              相同深度:按依赖的先后顺序

                     A--->C(1.2.7)

                     B--->C(1.2.15)

                            选1.2.7

B--->C(1.2.15)

                     A--->C(1.2.7)

                            选1.2.15

 

       手工判断

       A:排除依赖管理

             

                     com.test

                     maven_02

                     0.0.1-SNAPSHOT

                    

                           

                                   log4j

                                   log4j

                           

                    

             

       C:在pom.xml中指定具体引用的版本。

3    maven中的仓库

 

       中央仓库:maven官方维护的仓库,提供通用的jar与插件下载

 

       默认中央仓库的位置:\maven-3.2.5\lib\maven-model-builder-3.2.5.jar中的pom-4.0.0.xml文件中

 

       在工程中更改中央仓库的位置:

             

                     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

                     4.0.0

                     com.test

                     test_02

                     0.0.1-SNAPSHOT

                     jar

                     test_02

                     http://maven.apache.org

 

                    

                           

                                   JBossRepository

                                  

                                          http://repository.jboss.com/maven2/

                                  

                                  

                                          true

                                  

                                  

                                          true

                                  

                           

                    

                    

                    

                           

                                   JBossRepository

                                   ApacheSnapshots

                                  

                                          http://repository.jboss.com/maven2/

                                  

                                  

                                          true

                                  

                                  

                                          true

                                  

                           

                    

                    

                            UTF-8

                    

                    

                           

                                   dom4j

                                   dom4j

                                   1.5

                           

                    

             

                    

 

              如果多个工程需要同时设置:setting.xml中配置中央仓库:

                 

             

                test_xuewei_profile   

               

                                    //配置仓库

                       nexus

                       Nexus

                      http://maven.oschina.net/content/groups/public/

                      true

                      true

                    

               

               

                              //配置插件仓库

                       nexus

                            Nexus

                            http://maven.oschina.net/content/groups/public/

                            true

                            true

                    

               

             

             

 

             

                  test_xuewei_profile //激活设置。

             

 

       本地仓库:

 

4    插件

      

       Maven中所有的功能都是由插件来完成的。

 

       默认插件的目录:\Maven_Repository\org\apache\maven\plugins

      

       常用的插件:

4.1   javadoc插件(生成JavaDoc文档)

              javadoc:javadoc

              javadoc:jar

 

                           

                    

                            org.apache.maven.plugins

                            maven-javadoc-plugin

                            2.10.3

                           

                                  

                                   UTF-8

                                   UTF-8

                                   UTF-8

                           

                           

                           

                                  

                                          createjavadoc

                                          package

                                         

                                                 jar

                                                 javadoc

                                         

                                  

                           

                    

4.2   Source插件(用来将工程打包成带源代码的jar包)

                    

                            org.apache.maven.plugins

                            maven-source-plugin

                            2.4

 

                           

                                  

                                          package

                                         

                                                 jar-no-fork

                                         

                                  

                           

                    

4.3   RAR插件(生成rar文件)

                    

                            org.apache.maven.plugins

                            maven-rar-plugin

                            2.4

                           

                                   true

                           

                           

                                  

                                          package

                                         

                                                 rar

                                         

                                  

                           

                    

4.4   将工程的jar包拷到lib目录中。(在部署项目时用)

                    

                            org.apache.maven.plugins

                            maven-dependency-plugin

                            2.10

                           

                                  

                                          copy-dependencies

                                          package

                                         

                                                 copy-dependencies

                                         

                                         

                                                 lib

                                                 false

                                                

                                                        true

                                                

                                                 true

                                                 system

                                                

                                                        jstl,jsp-api

                                                

                                                

                                                        jstl,javax.servlet

                                                

                                         

                                  

                           

                    

4.5   main插件

             

                     org.apache.maven.plugins

                     maven-jar-plugin

                    

                           

                                  

                                          true

                                          com.util.DateUtil

                                  

                           

                    

             

4.6   jetty插件(开发时不用频繁重启服务器)

                    

                            org.mortbay.jetty

                            maven-jetty-plugin

                            6.1.5

                           

                                  

                                          WebRoot

                                  

                                   5

                                  

                                         

                                                 implementation="org.mortbay.jetty.nio.SelectChannelConnector">

                                                 8081

                                         

                                  

                           

                    

4.7   tomcat插件(可以远程部署tomcat)

4.7.1           user.xml中配置

                    

                      

                      

 

                      

                      

                      

                      

 

                      

                            roles="tomcat,manager,manager-gui,manager-script,admin-gui"/>

 

                    

 

4.7.2           setting.xml中配置

                        

                       

                           Tomcat6_Server     

                           tomcat

                           tomcat

                        

                      

 

4.7.3           pom.xml中配置

                    

                            org.codehaus.mojo

                            tomcat-maven-plugin

                            1.1

                           

                                   http://127.0.0.1:8080/manager

                                  

                                   Tomcat6_Server

                                   true

                           

                                 执行部署:

                     cleantomcat:redeploy

4.8   运行插件

             

先选择插件,然后运行

 

5    maven中的聚合

 

       Mavne的聚合就是将多个Maven工程合并在一起编译与打包

 

       新建一个Maven_total的工程,只需要编辑pom.xml文件。

      

              xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

              4.0.0

              com.test

              maven_total

              0.0.1-SNAPSHOT

              pom   

             

 

             

                     ../maven_01

                     ../maven_02

                     ../maven_03

                     ../maven_04

                     ../maven_hibernate

             

      

 

 

6    maven中的继承

      

              xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

              4.0.0

              com.ecan

              project_03

              0.0.1-SNAPSHOT

              pom

             

                    

                           

                                  

                                          maven-compiler-plugin

                                          2.5.1

                                         

                                                 1.6

                                                 1.6

                                         

                                  

                                  

                                          maven-war-plugin

                                          2.2

                                         

                                                 ${basedir}/WebRoot

                                                 2.5

                                         

                                  

 

 

                                  

                                          org.codehaus.mojo

                                          tomcat-maven-plugin

                                          1.1

                                         

                                                 http://localhost:8083/manager

                                                 Tomcat6_Server

                                                 true

                                         

                                  

                           

                    

             

 

             

             

                    

                           

                                   javax.servlet

                                   servlet-api

                                   2.5

                                   provided

                           

                           

                                   javax.servlet.jsp

                                   jsp-api

                                   2.1

                                   provided

                           

                           

                                   jstl

                                   jstl

                                   1.2

                           

 

                           

                                   org.apache.struts

                                   struts2-core

                                   2.3.29

 

                                  

                                         

                                                 com.sun

                                                 tools

                                         

                                  

                           

                    

             

      

 

 

       子工程:

      

              xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

 

             

                     com.ecan

                     project_03

                     0.0.1-SNAPSHOT

                     ../pom.xml

             

 

              4.0.0

              com.ecan

              project_04

              0.0.1-SNAPSHOT

 

             

                    

                            javax.servlet

                            servlet-api

                    

 

                    

                            org.apache.struts

                            struts2-core

 

                    

 

             

 

             

                    

                           

                                   org.apache.maven.plugins

                                   maven-compiler-plugin

                           

                    

             

          

你可能感兴趣的:(javaweb)