Maven知识点复习笔记

一、坐标唯一确定一个jar包

   1、坐标确认组成:groupId、artifactId、version

        groupId --- 一般为公司网址倒写、artifactId --- 项目名、version版本号。

     同时maven 中的 插件(plug)也是由其确认。

二:POM文件结构

    1、<project>标签(*标签)

        1、<modelVersion>:指定当前Maven模型的版本号,一般为"4.0.0"

        2、<groupId >:指定当前项目的组名

        3、<artifactId>:指定当前项目名

        4、<version>:指定当前版本号

        5、<packaging>:指定打包的类型 --jar、war、rar、ear、pom,默认是jar(需注意的是如果为父子什么这种继承,父项目的packaging类型为"pom")

        6、<name>:表示当前项目名

        7、<description>:对当前项目的描叙

        8、<properties>:定义配置的属性,可以在本项目或之项目中以${xxx}的方式引用,例如在父项目中定义一些版本号,然后在子项目中直接引用这个来统一版本等。

        9、<modules>:用来引入module,即模块管理,一般是在父POM文件中使用

       10、<build>:与项目构建相关的如:<plugins>、<resources>

       11、<dependecies>:引入<dependecy>依赖

三:一些常见的POM子节点介绍

        1、<dependecy>

              1、<groupId>、<artifactId>、<version>

              2、<type>:这个应该指的是引用的类型(默认为"jar",一般还可以为"pom")

              3、<scope>:本依赖引用使用的范围 -- "compile"(编译阶段)、"runtime"(直到运行时也需要)、"test"(测试阶       段)、"provided"(这个与compile类似,但其表面的是这个包虽然引用了,但这个包引用的在其运行容器也用,例如servlet-api,其 需要的再tomcat容器也用,所以如果你在本地也引用了,就需要设置为这种类型)、"import"、"system"

             4、<optional>:这个就是ProjectA依赖ProjectB,ProjectB依赖projectC,当你在在ProjectB中引用ProjectC,并且在引用的时候将其设置为true(默认为false),那当ProjectA引用ProjectB的时候,不会将ProjectC带入到ProjectA。

                  Maven知识点复习笔记

          5、<classifier>:可以看下这篇文章

           2、<build>

                    1、<resources>:引入<resource>,<resource>用来获取在不是在resoure目录下面的其他类型的文件如:xxx.xml、xx.properties这些(因为maven在例如java/main目录下面只会将.class这样的文件打进包中)

      Maven知识点复习笔记              Maven知识点复习笔记

  然后在pom.xml设置,就会打进包中(需要注意的是如果自定义设置resource,需要再去设置src/main/resources)

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
                <include>**/*.properties</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
</build>

       3、<parent>

              这个项目是用于引入设置父项目

                               Maven知识点复习笔记

  例如这个项目:父项目的POM.xml有设置这个

<modules>
    <module>SpringBootStarterHello</module>
    <module>SpringBootStarterAutoconfigure</module>
    <module>SpringbootHello</module>
</modules>

  其子项目例如:spring-boot-hello

<parent>
    <artifactId>spring-boot-starter-customize</artifactId>
    <groupId>com.fev.springboot</groupId>
    <version>1.0</version>
    <!--<relativePath>../pom.xml</relativePath>-->
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-hello</artifactId>

      这里需要注意的是,在<parent>中可以设置relativePath属性,用来指定其父项目的POM文件位置。

     这里试了下,如果在子项目之间进行了引用,例如spring-boot-starter-autoconfigure有引用spring-boot-hello,如果没有进行父类的install,直接进行spring-boot-starter-autoconfigure的install,就会报"Could not find artifact com.fev.springboot:spring-boot-starter-customize:pom:1.0",这里就需要先进行整个父项目的install,在进行install就能install了,但由于spring-boot-hello,没有引入其他与它同级的项目,测试的实时没有进行父项目的install也能install通过,修改版本号之后。

      再进行install的时候,需要再对父项目进行install。