您的位置:首页 > 其它

Maven笔记——基础知识

2015-07-15 11:55 197 查看

1、maven项目目录结构:
   src
      -main
          -java
              -package
              -test
          -java
              -package
    resources

=======================================================================================
2、maven常用命令:
       mvn -v               查看maven版本
       mvn compile      编译
       mvn test            测试
       mvn package    打包
       mvn clean         删除target
       mvn install         安装jar包到本地仓库中

=======================================================================================

3、maven自动创建目录两种方式:
  1)  mvn archetype:generate  按照提示进行选择
  2)  mvn archetype:generate  -DgroupId=组织名,(一般公司网址的反写+项目名称)
                                                  -DartifactId=项目名-模块名
                                                  -Dversion=版本号
                                                  -Dpackage=代码所在的包名

=======================================================================================

4、maven的坐标和仓库:
 坐标
     构建
 仓库
     本地仓库和远程仓库(http://repo.maven.apache.org/maven2  全球中央仓库地址)
     镜像仓库:(指A和B提供一样的功能,maven中央仓库服务器位于国外,有时因为一些原因无法访问外

                          网,国内有镜像仓库)
     修改镜像仓库:打开maven —— config —— settings.xml 找到mirrors  line146

     配置查询:http://mvnrepository.com/
     

<mirror>
<id>maven.net.cn</id>

<!-- 指定镜像名字central为默认中央仓库名字,也可使用通配符*,匹配所有仓库-->
<mirrorOf>central</mirrorOf>
<name>central mirror in china</name>
<url>http://maven.net.cn/content/groups/public</url>
</mirror>

    更改仓库位置:maven从远程仓库下载构件默认存放当前用户中,例如:C:\Users\sino\.m2
    一般不将仓库放c盘,如遇到重装系统或其它问题,容易丢失。
    打开maven — config — settings.xml 找到 line53

    

<localRepository>/path/to/local/repo</localRepository>

 

    修改路径.同时可复制settings.xml到新仓库所在目录下,这样以后更新maven版本,不必再次修改。

=======================================================================================

5、 maven的生命周期和插件:

      clean、compile、test、package、install

      完整的项目构建过程:清理、编译、测试、打包、集成测试、验证、部署。

     

      maven的生命周期:clean(清理项目)、default(构建项目)、site(生成项目站点)这三个周期相互独立。

      1)clean清理项目:

               pre-clean:执行清理前的工作

               clean:清理上一次构建生成的所有文件

               post-clean:执行清理后的文件

      2)default构建项目(最核心):

               compile、test、package、install

      3)site生成项目站点:

               pre-site:在生成项目站点前要完成的工作

               site:生成项目的站点文档

               post-site:在生成项目站点后要完成的工作

               site-deploy:发布生成的站点到服务器上

=======================================================================================

6、maven常用参数:

 

<!--指定了当前pom的版本-->
<modelVersion>4.0.0</modelVersion>

<groupId>反写的公司网址+项目名</groupId>
<artifactId>项目名+模块名</artifactId>
<!--第一个0表示大版本号
第二个0表示分支版本号
第三个0表示小版本号
0.0.1
snapshot快照
alpha内部测试
beta公测
Release稳定
GA正式发布
-->
<version>0.0.1-SNAPSHOT</version>
<!-- jar var zip pom默认是jar -->
<packaging>jar</packaging>
<!-- 项目描述名 -->
<name>hi</name>
<!-- 项目地址 -->
<url>http://maven.apache.org</url>
<!-- 项目描述 -->
<description></description>
<!-- 开发人员列表 -->
<developers></developers>
<!-- 许可证信息-->
<licenses></licenses>
<!-- 组织信息 -->
<organization></organization>
<!-- 依赖列表 -->
<dependencies>
<!-- 依赖项 -->
<dependency>
<!-- 指定位置 -->
<groupId></groupId>
<artifactId></artifactId>
<version></version>
<type></type>
<!-- 依赖范围 -->
<scope>test</scope>
<!--  设置依赖是否可选,默认为false,两个值true,false-->
<optional></optional>
<!--  排除依赖传递列表  -->
<exclusions>
<exclusion></exclusion>
</exclusions>
</dependency>
</dependencies>
<!-- 依赖的管理 -->
<dependencyManagement>
<dependencies>
<dependency></dependency>
</dependencies>
</dependencyManagement>
<!--  对构建行为提供支持,经常使用标签 plugins-->
<build>
<!-- 插件列表 -->
<plugins>
<plugin>
<groupId></groupId>
<artifactId></artifactId>
<version></version>
</plugin>
</plugins>
</build>
<!--  对父模块继承 -->
<parent></parent>
<!-- 定义多个模块,一起编译 -->
<modules>
<module></module>
</modules>

=======================================================================================

7、依赖范围:

      三种classpath:编译、测试、运行    写在<scope></scope>里

      6种:compile、provided、runtime、test、system、import

               compile:默认的范围,编译测试运行都有效。

               provided:在编译和测试时有效。

               runtime:在测试和运行时有效。

               test:只在测试时有效。

               system:与本机系统相关联,可移植性差,在编译和测试时有效。

               import:导入的范围,它只使用在dependencyManagerment中,表示从其它的pom中导入dependecy的配置。

=======================================================================================

8、依赖传递:

例如:B依赖A,C依赖B,则C也依赖A

=======================================================================================

9、依赖冲突:

      原则:1) 短路优先 例:A->B->X(jar)    A->D->X(jar)    优先解析路径短的版本。

                 2) 先声明先优先:如果 2b09f 路径长度相同,则谁先声明,先解析谁。

=======================================================================================

10、聚合和继承:

聚合:

例如:hongxingA模块、hongxingB模块、hongxingC模块,将该三个放在一个maven中运行,新建maven模块ABC-Aggreation,在pom.xml中将三个模块聚合配置如下:

<modules>
<module>../hongxingA</module>
<module>../hongxingB</module>
<module>../hongxingC</module>
</modules>

 

继承:采用<parent>标签,在pom.xml中配置父模块信息

例如:父类加入了junit 3.8.1依赖配置如下:

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit-version>3.8.1</junit-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

 

子类继承父类,即可将父类依赖的junit3.8.1继承配置如下:

 

<parent>
<groupId>com.imooc.hongxingParent</groupId>
<artifactId>hongxingParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

  

=======================================================================================

11、使用maven创建web项目,放到jetty或tomcat中

创建web项目:

1)右键,新建Project 选择:Maven Project ——Next

2)

 3)

 

4).转换web项目:选择项目,右键,属性,找到Project Facets 如图所示:

 
 

jetty:

<plugins>
<plugin>
<!--jeetty插件-->
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.16.v20140903</version>
<!--在打包时候运行jeetty插件-->
<executions>
<execution>
<phase>package</phase>
<goals><goal>run</goal></goals>
</execution>
</executions>
</plugin>
</plugins>

 

tomcat:
<plugins>
<plugin>
<!-- tomcat插件 -->
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.2</version>
<!-- 在打包时候运行tomacat插件 -->
<executions>
<execution>
<phase>package</phase>
<goals><goal>run</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
 

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: