工程化专题——maven-(1)

认识 maven

  1. 什么是maven?

    Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information

    软件项目管理工具,基于POM管理,构建,发布项目
  2. 优势

    a. **约定优于配 ** b. 简单 c. 测试支持 d. 构建简单 e. CI f. 插件丰富

  3. 下载和安装

    超级pom位置 :\lib\maven-model-builder-***.jar/org/apache/maven/model/pom.xml

    1. 配置 MVN_HONE
      1. widows path
      2. Linux .bash_profile
      3. MAVEN_OPTS
    2. 配置优先级

    maven 运行一个mvn命令时,首先会找 ~/.m2/setting.xml,如果不存在会找安装目录下的conf/setting.xml

    3.配置settiing.xml

    1. : 本地库位置

      1.  <mirrors>
                <mirror>
                    <id>maven-public</id>
                    <mirrorOf>aliyun,central,spring,jboss,thirdparty</mirrorOf>
                    <url>https://maven.aliyun.com/repository/public</url>
                </mirror>
            </mirrors>
        
  4. 项目结构

    my-app
    |-- pom.xml
    `-- src
        |-- main
        |   `-- resource
        |   `-- java
        |       `-- com
        |           `-- mycompany
        |               `-- app
        |                   `-- App.java
        `-- test
            `-- java
                `-- com
                    `-- mycompany
                        `-- app
                            `-- AppTest.java
    
  5. 依赖仲裁

    1. 最短路径原则 2.加载顺序原则
  6. : 排除传递的依赖jar包

  7. scope

    1. compile: 编译需要,会打包部署
    2. test :测试依赖, 不会打包部署 --> junit
    3. provided: 编译需要,不会打包 —> tomcat servelt api
    4. runtime: 运行时,编译不需要,会打包—> mysql-connector
    5. system: 本地的一些jar —> 短信jar
    6. import :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.
  8. scope的依赖传递

    1. 若 A.jar —>B(scope=test) --> C(scope:compile) ,若D依赖A , 则D不会依赖B,C

    2. 间接传递jar的scope

      1. 左侧栏scope为直接依赖项目的scope, 上栏为依赖项目传递依赖的scope, 结果为传递到当前项目的最终scope,
      2. -代表不会传递依赖
      compileprovidedruntimetest
      compilecompile(*)-runtime-
      providedprovided-provided-
      runtimeruntime-runtime-
      testtest-test-
  9. 生命周期

    一个生命周期是由多个phase构成,一个phase有多个gloals组成

    1. lifecycle

      1. clean
        1. phase: pre-clean --> clean --> post-clean
      2. default
        1. validate: validate the project is correct and all necessary information is available(验证工程是否正确并且必要的信息可用)
        2. compile: compile the source code of the project (编译项目的源代码)
        3. package: take the compiled code and package it in its distributable format, such as a JAR. (编译和打包生成特殊格式的文件)
        4. verify :run any checks to verify the package is valid and meets quality criteria (检查和验证包的有效性和是否符合质量标准)
        5. test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed (用一个单元测试框架测试编译后的代码。测试代码不需要被打包和发布)
        6. install: install the package into the local repository, for use as a dependency in other projects locally (安装包到本地仓库,作为其他工程的依赖)
        7. deploy:done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects. (集成发布环境的操作,复制包到远程仓库)
      3. site
        4. pre-site
        5. site
        6. post-site
        7. site-deploy
  10. 命令

    mvn clean dependency:copy-dependencies package
    

    这行命令会产生以下效果

    1. 清理 target下编码的文件

    2. 将项目依赖的jar文件 复制到 相应工程 target/dependency/ 目录下

    3. 打包相应类型的工程到 target下 ,打包文件命名格式: g r o u p I d − {groupId}- groupId{artifactId}-${version}.jar/war

    mvn dependency:tree
    

    查看maven的依赖关系树

  11. 插件
    1. 推荐使用的插件

      1. findbugs : 静态代码检查

        1. 根据字节码操作
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>findbugs-maven-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <!-- <configLocation>${basedir}/springside-findbugs.xml</configLocation> -->
                <threshold>High</threshold>
                <effort>Default</effort>
                <findbugsXmlOutput>true</findbugsXmlOutput>
                <!-- findbugs xml输出路径-->
                <findbugsXmlOutputDirectory>target/site</findbugsXmlOutputDirectory>
            </configuration>
        </plugin>
        
        1. versions :统一修改版本号

          <plugin>
              <groupId>org.codehaus.mojo</groupId>
              <artifactId>versions-maven-plugin</artifactId>
              <version>2.3</version>
          </plugin>
          
        2. source: 生成源码jar文件

          <plugin>
              <artifactId>maven-source-plugin</artifactId>
              <version>2.3</version>
              <executions>
                  <execution>
                      <id>attach-sources</id>
                      <phase>install</phase>
                      <goals>
                          <goal>jar-no-fork</goal>
                      </goals>
                  </execution>
              </executions>
          </plugin>
          

        assembly: 帮助项目打包 war zip

        <plugin>       
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          
          <execution><!-- 配置执行器 -->
          <id>make-assembly</id>
            <phase>package</phase><!-- 绑定到package生命周期阶段上 -->
            (1)
            <goals>
              <goal>single</goal><!-- 只运行一次 -->   
            </goals>
            
            (2)
            <configuration>
              <finalName>${project.name}</finalName>
              <!--主类入口等-->
              ...
              
              <descriptor>src/main/assembly/assembly.xml</descriptor><!--配置描述文件路径--> 
            </configuration>
          
          </execution>
        </executions>
        
      ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值