美文网首页
在点评的Maven日常

在点评的Maven日常

作者: 即墨灯火 | 来源:发表于2017-08-08 21:29 被阅读54次

    安装

    for macOS
    1. 下载并解压

    2. ~./bash_profile添加环境变量

      export M2_HOME="maven的位置"
      export PATH=$PATH:$M2_HOME/bin

    for Linux
    1. 下载并解压
    wget http://mirror.bit.edu.cn/apache/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.zip 
    unzip apache-maven-3.5.2-bin.zip 
    
    1. 添加环境变量vi /etc/profile
    # 在文件的最后添加
    export MAVEN_HOME= # maven的位置
    export PATH=$MAVEN_HOME/bin:$PATH
    
    1. source /etc/profile

    使用镜像

    传送门

    冲突了怎么办?

    通过<dependencyManagement>来指定版本

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.6.1</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.6.1</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    

    注意,这里只是声明版本,必须要在独立的<dependencies>标签中指定依赖的jar包(可以不指定版本,因为会使用<dependencyManagement>的版本。如果指定了版本,将覆盖<dependencyManagement>的版本)

    该方法主要用于解决传递造成的依赖

    • 深度:由浅到深
    • 同样深度则会取先声明的依赖版本
    • 如果是间接引用,那么会优先取<dependencyManagement>的版本,由叶子节点向上寻找

    依赖的作用域(scope)

    • compile
      这是默认范围。如果没有指定,就会使用该依赖范围。编译依赖对项目所有的classpath都可用。此外,编译依赖会传递到依赖的项目。
    • provided
      和compile范围很类似,但provided范围表明你希望由JDK或者某个容器提供运行时依赖。例如,当使用Java EE构建一个web应用时,你会设置对Servlet API和相关的Java EE APIs的依赖范围为provided,因为web容器提供了运行时的依赖。provided依赖只对编译和测试classpath有效,并且不能传递。
    • runtime
      runtime范围表明编译时不需要依赖,而只在运行时依赖。此依赖范围对运行和测试classpath有效,对编译classpath无效。
    • test
      test范围表明使用此依赖范围的依赖,只在编译测试代码和运行测试的时候需要,应用的正常运行不需要此类依赖。
    • system
      系统范围与provided类似,不过你必须显式指定一个本地系统路径的JAR,此类依赖应该一直有效,Maven也不会去仓库中寻找它。
    • import(Maven2.0.9及以上)
      import范围只适用于pom文件中的<dependencyManagement>部分。表明指定的POM必须使用<dependencyManagement>部分的依赖。因为依赖已经被替换,所以使用import范围的依赖并不影响依赖传递。

    生命周期

    • clean - 清理
    • validate - 检查工程配置是否正确,完成构建过程的所有必要信息是否能够获取到。(validate the project is correct and all necessary information is available)
    • compile - 文件编译( compile the source code of the project)
    • test - 测试(test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed)
    • package - 打包,从仓库获取依赖(take the compiled code and package it in its distributable format, such as a JAR.)
    • verify - 运行检查操作来验证工程包是有效的,并满足质量要求。(run any checks on results of integration tests to ensure quality criteria are met)
    • install - 将打包的文件部署在本地(install the package into the local repository, for use as a dependency in other projects locally)
    • deploy - 发布(done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.)

    Maven Helper 一个IDEA中的maven插件

    在下载并启用后,可以打开pom文件,发现左下角多了一个Dependency Analyzer标签。可以很方便的找到当前依赖的版本、冲突,并解决。

    image.png

    相关文章

      网友评论

          本文标题:在点评的Maven日常

          本文链接:https://www.haomeiwen.com/subject/uluxrxtx.html