maven的配置、参数说明、依赖管理

一、建maven项目(eclipse ee版本)

选这两项就可以建maven项目了

二、设置maven

一是修改setting.xml(maven的conf目录)

修改本地仓库地址:<localRepository>d:/.m2/repository</localRepository>

在mirrors加上阿里云的镜像:

<mirror>
        <!--This sends everything else to /public -->
        <id>nexus-aliyun</id>
        <mirrorOf>*</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

三、查询maven包:

http://mvnrepository.com/

找到相关包,比如junit:

<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
</dependency>
在maven项目的pom.xml的dependencies加上这个depend即可

四、建立测试类小技巧:

选中主类,建立junit类:

然后拖到test目录下。

五、maven命令

1,mvn -v 查看版本信息,

2,mvn compile 编译  在项目根目录下

3,mvn test运行测试文件

4,mvn package 对项目进行打包

5,mvn clean 删除target目录

6,mvn install 安装项目jar包到本地仓库

7,archetype 插件:创建符合maven规定的项目骨架

mvn archetype:generate  会在空的目录中建立maven项目

Choose a number or apply filter,7建立quick start项目

然后填入group-id等,也可以如下方式直接指定

D:\workspace\m3>mvn archetype:generate -DgroupId=commaven -DartifactId=
maven-test01 -Dversion=0.0.1-snapshot -Dpackage=com.maven.test

六、参数说明(pom.xml)

modelVersion:当前pom的版本号

groupId:组织名,公司名网址反过来写+项目名称

artifactId:项目名+模块名

name:也是项目名,如果不填,默认是artifactId

version:版本号  

   0.0.1-SNAPSHOT  第一个数表示大版本好,第二个数表示分支版本号,第三个数表示小版本号

   SNAPSHOT  :快照版本,开发阶段

   alpha:内部测试版本

   beta:公测版本

   release:稳定版本

   package:代码的包路径

   GA:正式发布

dependency scope:作用域(http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

Dependency Scope

Dependency scope is used to limit the transitivity of a dependency, and also to affect the classpath used for various build tasks.

There are 6 scopes available:

  • compile    默认范围,整个编译测试运行阶段有效
    This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
  • provided     编译测试  有效
    This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
  • runtime    运行  测试有效
    This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
  • test          只在测试阶段  有效
    This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. This scope is not transitive.
  • system      跟本机系统相关联   可一致性差
    This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.
  • import       导入的范围,在 dependencyManagement中,表示从其他的pom中导入dependency的配置作用
    This scope is only supported on a dependency of type pom in the <dependencyManagement> section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM's <dependencyManagement> section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

packaging :有jar、war、zip、pom等方式打包

url:项目地址

description:项目描述

optiona :设置依赖是否可选

exclusions:排除依赖传递列表

dependencyManagement:进行依赖管理

plugins:插件管理

parent:父级

modules、module:模块

scope:依赖作用域范围

七,maven的生命周期

clean compile test package intall

完成的项目构建过程

清理  编译  测试  打包  继承测试     --部署到服务器

Clean  清理项目

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

     Clean:清理上一次构建产生的所有文件

     Post-clean:执行清理后的工作

Default   构建项目(核心)

     Compile   \  test   、 package 、instal

site   生成站点  进行访问

    Pre-site:项目生成站点的工作

    Site:生成网站文件

    Post-site:生成后的工作

    Site-deploy:发布到服务器

    

八、maven的依赖冲突及管理

1,谁离得近,谁优先,

           a)son  ->mother  ->  father  -> x-jar包

                    如果father和mother都有x.jar,则继承mother的包

           b)son  ->mother  ->x.jar包

2,谁先声明谁优先

         谁在前,用谁的依赖

        son  ->mother ->x.jar包

        son  ->father ->x.jar包

九、maven的聚合和继承

1,maven的聚合,就是多模块的组合。聚合的项目的pom注意配置两个地方:

  <!-- 聚合项目需要用pom -->
  <packaging>pom</packaging>

<!-- 模块部分 -->
  <modules>
      <module>../yilai-mother</module>
      <module>../father</module>
      <module>../son</module>
  </modules>

配置完后,我个人的感觉就是一导包全都能导进来

2,依赖的继承,如下语句即为继承:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

用eclipse依次点开,会找到一堆的依赖版本。个人感觉parent的主要作用就在于统一版本信息。继承的依赖可以从父依赖的<dependencyManagement>版本信息

猜你喜欢

转载自blog.csdn.net/qq_22059611/article/details/82892399