美文网首页
[Maven专题-02] Maven基础入门

[Maven专题-02] Maven基础入门

作者: ccczyl2006 | 来源:发表于2019-11-05 20:36 被阅读0次

Maven的核心概念

  • 约定的目录结构(通过命令mvn archetype:generate -DarchetypeCatalog=local)交互创建普通Java Web项目。

    hello
        -- pom.xml     #顶层pom.xml文件
        -- src
          -- main      #源代码以及资源目录
            -- java  (下面是包名对应的文件夹 如cn/yuanjianfeng/maven)
            -- resources
            -- webapp(web项目文件夹)
          -- test      #测试代码以及测试使用的资源
            -- java
            -- resources
    
  • pom.xml核心配置文件,位于项目的顶层目录下,运行mvn相关的命令必须在此文件所在的目录执行。pom文件的结构如下图,下面的节点后续补充:

    <project xmlns="http://maven.apache.org/POM/4.0.0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        
    </project>
    

Maven依赖

  • Maven通过坐标来表述依赖的位置,每一个依赖都存在唯一的坐标。Maven中依赖通过dependencies节点来描述,如下:

    <dependencies>
        <!-- dependencies节点管理具体的依赖项,实际导入jar包.主要配置GAV:实际jar包的坐标 -->
        <dependency>
            <!-- 项目依赖坐标的GAV -->
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <!--依赖范围
              - compile:默认值.参与编译 运行 测试.
              - test:只参与测试,不会被打包.
              - provided:等同于compile,但会被打包排除掉.常见的为jsp servlet等依赖,容器会提供.
            -->
            <scope>test</scope>
        </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${org.springframework.version}</version>
                <!-- 当计算传递依赖时,从依赖构件列表里,列出被排除的依赖构件集.
                即告诉maven你只依赖指定的项目,不依赖项目的依赖. 此元素主要用于解决版本冲突问题 -->
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-jcl</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    

Maven仓库

  • 仓库中存储文件布局方式根据其GAV坐标来布局。仓库分为本地仓库和远程仓库,maven在构建优先在本地查找,如没有则会从远程仓库拉取。私有仓库是一种特殊的远程仓库,可减少公司内部对远程仓库的访问压力。

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.0.6.RELEASE</version>
    </dependency>
    <!--
      -- 转化的规则:按groupId/artifactId/version/转化,当groupId中存在.时将.转化为/文件夹
      -- 所以上述依赖的路径为:org/springframework/spring-webmvc/5.0.6.RELEASE/spring-webmvc-5.0.6.RELEASE.jar文件
    -->
    

Maven生命周期及插件

  • Maven的设计理念,抽象生命周期的概念,实际生命周期本省不做任何的实际工作,实际的工作任务都由具体的插件通过绑定到某一个生命周期来完成。

  • Maven存在三套项目独立的生命周期:clean清理,default默认(编译 测试 打包 部署等)和site建立项目站点,具体生命周期的任务执行实际上都是由绑定的插件来执行。

    • clean生命周期:包含三个阶段,pre-clean, clean, post-clean
    • default生命周期:最核心的生命周期阶段,包括validate,initialize,generate-sources,process-sources,generate-resources,process-resources,compile,process-class,generate-test-sources,process-test-sources,generate-test-resources,process-test-resources,test-compile,process-test-class,test,prepare-package,package,verify,install,deploy
  • 命令行运行生命周期,每一个生命周期都是独立的,所以可以运行mvn clean install, 每一个生命周期的阶段有前后依赖关系,如执行mvn test运行default生命周期的test阶段,则它会把defaulttest阶段前的阶段都执行。

  • 插件绑定,具体而言是生命周期的阶段与具体的插件目标进行绑定,在未进行配置的情况下默认绑定为:

  • maven-clean-plugin:clean绑定clean生命周期的clean阶段。

    • maven-resources-plugin:resources绑定defaultprocess-resources阶段,进行项目主资源的处理。
    • maven-compile-plugin:compile绑定defaultcompile阶段,组要进行项目主代码编译工作。
    • maven-resources-plugin:testResources绑定defaultprocess-test-resources阶段,进行项目测试资源的处理。
    • maven-compile-plugin:testCompile绑定defaulttest-compile阶段,组要进行项目测试代码编译工作。
    • maven-surefire-plugin:test绑定defaulttest阶段,进行测试工作。
    • maven-jar-plugin:jar绑定defaultpackage阶段,进行项目打包。
    • maven-install-plugin:install绑定defaultinstall阶段,进行项目包安装到本地仓库。
  • maven-deploy-plugin:deploy绑定defaultdeploy阶段,进行项目包安装到远程仓库。

  • 自定义绑定,进行插件配置,以maven-source-plugin进行源代码打包进行配置说明如下:

    <build>
      <plugins>
          <plugin>
                <!-- 插件配置在build-plugins-plugin节点下 首先配置使用的插件GAV-->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                  <!-- 配置执行任务-->
                    <execution>
                        <id>attach-source</id>
                        <phase>verify</phase>
                        <goals>
                          <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
                
            </plugin>
        </plugins>
    </build>
    
  • Maven命令原理

    #1 mvn 生命周期阶段 运行生命周期阶段,相应的该阶段之前的阶段也会得以执行
    #1.1 mvn clean:    清理项目生成的临时文件.一般在项目的target目录下.
    #1.2 mvn compile:  编译源代码,一般编译src/main/java目录下的java文件.
    #1.3 mvn test:     测试命令.测试src/test下的junit用例.
    #1.4 mvn package:  项目打包工具.会在target目录下生成jar包或者war包.
    #1.5 mvn install:  将生成的jar/war包发布到本地仓库.
    
    #2 mvn 插件:目标 直接运行插件的目标,从而调用相应的生命周期执行。
    #2.1 mvn org.apache.maven.plugins:maven-help-plugin:3.1.0:describe -Dplugin=compiler 
    #2.1 mvn help:describe -Dplugin=compiler   #前面具体的插件可以使用插件前缀来代替
    #2.2 mvn dependency:list:解析当前的已解析依赖.
    #2.3 mvn dependency:tree:树状形式展示依赖关系.
    #2.4 mvn help:effective-pom:在pom.xml所在文件夹运行命令即可查看整合Super POM后形成的pom文件.
    

常用插件使用以及配置

  • 插件命令行通用使用方法:mvn clean:clean -DparameterOption = value (多个参数选项用空格隔开)。

  • 官方插件地址:apache plugins; Codehaus插件地址:codehaus plugins

  • maven-compiler-plugin,默认绑定default生命周期的compiler阶段,提供compiletestCompile目标,分别编译主程序和测试程序。

    <!--maven-compiler-plugin编译插件,配置编译JDK8-->
    <build>
      <plugins>
          <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <!--源代码编译版本-->
                    <source>1.8</source>
                    <!--目标平台编译版本-->
                    <target>1.8</target>
                    <!--字符集编码-->
                    <encoding>UTF-8</encoding>
               </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
      <!--设置编译版本为1.8-->
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    
  • maven-shade-plugin,生成可执行jar包,类中包含main函数。包含唯一的目标shade,执行在package阶段。

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <!-- 配置带main函数的主类-->
                                <mainClass>cn.yuanjianfeng.mvndemo.App</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    
  • maven-source-plugin:源代码打包插件,支持jar test-jar jar-no-fork test-jar-no-fork目标。

    <!--生成源代码jar包配置-->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <version>3.0.1</version>
        <executions>
            <execution>
                <id>attach-source</id>
                <phase>verify</phase>
                <goals>
                    <goal>jar-no-fork</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

相关文章

网友评论

      本文标题:[Maven专题-02] Maven基础入门

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