maven学习笔记

maven学习笔记

1、maven的内置属性

        ${basedir}表示项目根目录

        ${project.basedir}同${basedir}

        ${project.groupId}表示项目groupId

        ${project.artifactId}表示项目artifactId

        ${version}表示项目版本

        ${project.version}同${version}

        ${project.baseUri}表示项目文件地址

        ${maven.build.timestamp}表示项目开始构建时间

        ${maven.build.timestamp.format}表示时间格式,可以如下配置

<properties>
	<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
</properties>

        ${project.build.directory}-->target目录

        ${project.build.sourceEncoding}-->UTF-8

        ${project.build.sourceDirectory}-->src\main\java目录

        ${project.build.finalName}

        ${project.build.outputDirectory}-->target\classes

        ${env.JAVA_HOME}-->取环境变量的值D:\Java\jdk1.8.0_92

        maven默认的pluginManagement插件(antrun、assembly、dependency、release)

    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.8</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.3.2</version>
        </plugin>
      </plugins>
    </pluginManagement>

        maven默认的plugins插件(clean、resources、jar、compiler、surefire、insatll、deploy、site、

        maven-project-info-reports-plugin)

    <plugins>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <id>default-testResources</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>testResources</goal>
            </goals>
          </execution>
          <execution>
            <id>default-resources</id>
            <phase>process-resources</phase>
            <goals>
              <goal>resources</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <executions>
          <execution>
            <id>default-compile</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
          <execution>
            <id>default-testCompile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.4</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>default-install</id>
            <phase>install</phase>
            <goals>
              <goal>install</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>default-deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.3</version>
        <executions>
          <execution>
            <id>default-site</id>
            <phase>site</phase>
            <goals>
              <goal>site</goal>
            </goals>
            <configuration>
              <outputDirectory>F:\myWorkspace\atest\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
          <execution>
            <id>default-deploy</id>
            <phase>site-deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
            <configuration>
              <outputDirectory>F:\myWorkspace\atest\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <outputDirectory>F:\myWorkspace\atest\target\site</outputDirectory>
          <reportPlugins>
            <reportPlugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-project-info-reports-plugin</artifactId>
            </reportPlugin>
          </reportPlugins>
        </configuration>
      </plugin>
    </plugins>        

2、maven的profile的配置 

        profile激活方式

        (1)、activeProfiles写的profile将总是被激活

  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>

        (2)、默认激活方式,通过测试也总是会被激活,和官网描述的不一样

      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>

        (3)、条件激活方式,激活条件有多个时要同时满足

                    mvn clean install -Dtarget=dev这样子才能激活

      <activation>
        <property>
          <name>target</name>
          <value>dev</value>
        </property>
      </activation>
                    mvn clean install -Dtarget这样子就能激活
      <activation>
        <property>
          <name>target</name>
        </property>
      </activation>

        (4)、当文件存在时激活

            <activation>  
              <file>  
                   <exists>target</exists>  
              </file>  
            </activation>

        (5)、当文件不存在时激活

            <activation>  
              <file>  
                   <missing>target</missing>  
              </file>  
            </activation>  

        (6)、当文件存在且target参数不存在且jdk为1.8时才激活

      <activation>
        <file>
          <exists>${swagger-properties}</exists>
        </file>
        <property>
          <name>!disableSwaggerJsonAutoGeneration</name>
        </property>
	<jdk>1.8</jdk>
      </activation>
        (7)、mvn clean install -Pdev显示激活

3、maven的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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.my.test</groupId>
  <artifactId>atest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>atest</name>
  <url>http://maven.apache.org</url>
  <properties>
    <aaa>hffgf</aaa>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <repositories>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>a</id>
      <name>a</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>b</id>
      <name>b</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </pluginRepository>
  </pluginRepositories>
  <build>
    <sourceDirectory>F:\myWorkspace\atest\src\main\java</sourceDirectory>
    <scriptSourceDirectory>F:\myWorkspace\atest\src\main\scripts</scriptSourceDirectory>
    <testSourceDirectory>F:\myWorkspace\atest\src\test\java</testSourceDirectory>
    <outputDirectory>F:\myWorkspace\atest\target\classes</outputDirectory>
    <testOutputDirectory>F:\myWorkspace\atest\target\test-classes</testOutputDirectory>
    <resources>
      <resource>
        <directory>F:\myWorkspace\atest\src\main\resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>F:\myWorkspace\atest\src\test\resources</directory>
      </testResource>
    </testResources>
    <directory>F:\myWorkspace\atest\target</directory>
    <finalName>atest-0.0.1-SNAPSHOT</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <reporting>
    <outputDirectory>F:\myWorkspace\atest\target\site</outputDirectory>
  </reporting>
  <profiles>
    <profile>
      <id>dev</id>
      <activation>
        <property>
          <name>s</name>
          <value>d</value>
        </property>
        <file>
          <exists>${basedir}/src/main/resources/conf.properties</exists>
        </file>
      </activation>
      <properties>
        <aaa>hffgf</aaa>
      </properties>
    </profile>
  </profiles>
</project>

4、maven的坐标体系

        groupId

        artifactId

        version

        packaging

        classifier该元素用来帮助定义构建输出的一些附属构件,不能直接定义附属构件,因为附属构件不是项目生成的,

        而是由插件生成的。

5、maven的依赖配置

        1、groupId

        2、artifactId

        3、version

        4、type-->对应packaging,默认为jar

        5、scope依赖范围有以下6种-->默认是compile

            (1)compile(对编译、测试、运行三种classpath都有效)

            (2)test(只对测试classpath有效)

            (3)provided(对编译与测试有效)-->servlet-api

            (4)runtime(对测试和运行有效)-->jdbc驱动

            (5)system(对编译和测试有效)主要是引用本地jar文件而不是maven库的jar文件

<dependency>
    <groupId>javabuilder</groupId>
    <artifactId>javabuilder</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/javabuilder.jar</systemPath>
</dependency>

            (6)import(就是把另外一个poom文件拼凑进来生成一个大的pom文件)

                    该范围的依赖只在dependencyManagement元素下才有效果,使用该范围的依赖通常指向一个pom,作用

                    是将目标pom中的dependencyManagement配置导入并合并到当前pom的dependencyManagement元素中。

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>a</groupId>
      <artifactId>b</artifactId>
      <version>3.8</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

        6、optional标记依赖是否可选

        7、exclusions用来排除传递性依赖    

6、传递性依赖

        图中第一列是第一直接依赖范围,第一行是第二直接依赖范围



        maven解析后的依赖中:不可能出现groupId和artifactId相同,但是version不同的两个依赖。

        依赖调解:

        第一原则:最短路径者优先

        第二原则:最先声明者优先 

        可选依赖:

        当第二直接依赖是可选依赖时,不会被传递,所以只能显示的引入为第一直接依赖。

        优化依赖:

        项目中直接用到的依赖最好配置为第一直接依赖;谨慎的删除一些项目中没有用到的依赖。

7、maven的生命周期

        1、clean生命周期

clean生命周期  
pre-clean  
clean maven-clean-plugin:clean
post-clean  

        2、default生命周期

default生命周期阶段  
validate  
initialize  
generate-sources  
process-sources  
generate-resources  
process-resources maven-resources-plugin:resources
compile maven-compiler-plugin:compile
process-classas  
generate-test-sources  
process-test-sources  
generate-test-resources  
process-test-resources maven-resources-plugin:testResources
test-compile maven-compiler-plugin:testCompile
process-test-classes  
test maven-surefire-plugin:test
prepare-package  
package maven-jar-plugin:jar;maven-war-plugin:war
pre-integration-test  
integration-test  
post-integration-test  
verify  
install maven-install-plugin:install
deploy maven-deploy-plugin:deploy

        pom的default的生命周期

pom的default的生命周期  
package maven-site-plugin:attach-descriptor
install maven-install-plugin:install
deploy maven-deploy-plugin:deploy


        3、site生命周期

site生命周期阶段

pre-site
maven-site-plugin:site
site

post-site

site-deploy
maven-site-plugin:depoy
         当多个插件目标绑定到同一个阶段的时候,这些插件声明的先后顺序决定了目标的执行顺序。

8、maven的聚合和继承

        对于聚合模块来说,其打包方式必须是pom,聚合模块就是有modules的模块。

        下图是本人自己思考部署maven-java工程的框架。

      

9、maven的插件配置

      9.1、maven-resources-plugin

                    其中,filter表示需要用该文件的键值对对其他的配置文件进行配置

                    resource表示所需要的资源文件

                    outputDirectory表示资源文件输出目录

                    directory表示资源文件源目录

                    include表示包含哪些资源文件,其余的资源文件都不包括

                    exclude表示不包含哪些资源文件,其余的资源文件都包括

                    filtering资源文件是否需要替换占位符

                    overwrite

  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-resources-plugin</artifactId>
  			<version>3.0.2</version>
  			<executions>
  				<execution>
  					<id>copy-resources</id>
  					<phase>validate</phase>
  					<goals>
  						<goal>copy-resources</goal>
  					</goals>
  					<configuration>
  						<filters>
  							<filter>F:\myWorkspace\atest\src\main\resources\conf.properties</filter>
  						</filters>
						<outputDirectory>${project.build.outputDirectory}</outputDirectory>
						<resources>
							<resource>
								<directory>${basedir}\src\main\resources</directory>
								<include>**/*.xml</include>
								<filtering>true</filtering>
							</resource>
							<resource>
								<directory>${basedir}\src\main\resources</directory>
								<exclude>**/*.xml</exclude>
								<filtering>false</filtering>
							</resource>
						</resources>
  					</configuration>
  				</execution>
  			</executions>
  		</plugin>

        9.2、maven-compiler-plugin

                       其中,source指定源码的jdk版本
                                 target指定编译后的jdk版本
                                 encoding编码
                                 skip是否跳过编译测试代码
  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-compiler-plugin</artifactId>
  			<version>3.3</version>
  			<configuration>
  				<target>1.8</target>
  				<source>1.8</source>
  				<skip>true</skip>
  				<encoding>UTF-8</encoding>
  			</configuration>
  		</plugin>

        9.3、maven-checkstyle-plugin

                       checkStyle链接地址:http://checkstyle.sourceforge.net/index.html

                       从下面的配置可以看出插件不仅要检查代码的风格,而且还要检查文件头的license头信息。

                       dependency:插件中的dependency就是代表该插件可以直接使用该jar包中的配置文件(主要目地就是寻找配                                                 置文件),为什么不直接配置文件路径呢,有两个缺点:

                                            (1)如果配置相对路径,就意味每一个工程目录下都得有这些配置文件,这样就有多份相同的配                                                         置文件,显然不是我们所想要的。

                                            (2)如果配置绝对路径,就意味着换一个环境,路径就得跟着变。

                        configLocation:插件需要的检查代码风格配置文件,可以在上面的链接地址下细看该怎么配置。

                        sourceDirectory:src/main/java的目录,指明要检查的代码的根目录。

                        includeResources:指明是否要检查src/main/resources文件夹下的文件。

                        includeTestSourceDirectory:指明是否要检查src/test/java文件夹下的文件。

                        includeTestResources:指明是否要检查src/test/resources文件夹下的文件。

                        failsOnError:检查出风格错误后,是否直接就构建失败。

                        consoleOutput:是否要输出到控制台。

                        headerLocation:配置文件头必须要写明的内容。

        <plugin>
          <artifactId>maven-checkstyle-plugin</artifactId>
          <version>2.17</version> 
          <dependencies>
            <dependency>
              <groupId>com.mytest.oparent</groupId>
              <artifactId>checkstyle</artifactId>
              <version>1.1.0-SNAPSHOT</version>
            </dependency>
            <dependency>
              <groupId>com.mytest.oparent</groupId>
              <artifactId>openo-license</artifactId>
              <version>1.1.0-SNAPSHOT</version>
            </dependency>
          </dependencies>
          <configuration>
            <configLocation>myJavaStyle.xml</configLocation>
            <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
            <includeResources>true</includeResources>
            <includeTestSourceDirectory>true</includeTestSourceDirectory>
            <includeTestResources>true</includeTestResources>
            <excludes>
            </excludes>
            <failsOnError>false</failsOnError>
            <consoleOutput>true</consoleOutput>
          </configuration>
          <executions>
            <execution>
              <id>check-license</id>
              <goals>
                <goal>check</goal>
              </goals>
              <phase>process-sources</phase>
              <configuration>
                <configLocation>check-license.xml</configLocation>
                <headerLocation>apache-license-2.regexp.txt</headerLocation>
                <includeResources>false</includeResources>
                <includeTestSourceDirectory>true</includeTestSourceDirectory>
                <includeTestResources>false</includeTestResources>
                <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
                <excludes>
                </excludes>
                <failsOnError>false</failsOnError>
                <consoleOutput>true</consoleOutput>
              </configuration>
            </execution>
            <execution>
              <id>check-style</id>
              <goals>
                <goal>check</goal>
              </goals>
              <phase>process-sources</phase>
            </execution>
          </executions>
        </plugin>

         9.4、jacoco-maven-plugin

                        检查测试代码测试覆盖率的插件,通常与单元测试插件maven-surefire-plugin和
                        集成测试插件maven-failsafe-plugin配合使用。

         9.5、maven-surefire-plugin

                        单元测试插件

         9.6、maven-failsafe-plugin

                        集成测试插件

         9.7、sonar-maven-plugin

                        代码分析管理工具,需要与sonar配合使用,必须先安装sonar。

         9.8、build-helper-maven-plugin

         9.9、maven-help-plugin

                        mvn help:effective-pom --> effctive.pom      分析真实起效的pom文件
                        mvn help:describe -Dplugin=compiler -Dgoal=compile -Ddetail      分析插件

                        mvn help:active-profiles      列出起效的profiles

         9.10、maven-dependency-plugin

                            mvn dependency:tree --> tree.pom   输出项目的依赖树
                            mvn dependency:list    列出工程的依赖
                            mvn dependency:analyze      分析依赖
                            dependency的unpack目标,主要把包拷贝到某个目录下,然后解压:
                            (1)artifactItem下包含的依赖必须在dependency下包含
                            (2)outputDirectory解压后的目录所在
                            (3)excludes排除解压后的包中的一些文件
                            (4)overWriteReleases是否覆盖发布版本
                            (5)overWriteSnapshots是否覆盖快照版本
                            (6)outputAbsoluteArtifactFilename输出filename的名字
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>      
              <execution>
                <id>unpack-dependency-file</id>
                <goals>
                  <goal>unpack</goal>
                </goals>
                <!--unzip the redis binary package first-->
                <phase>generate-resources</phase>
                <configuration>
                  <artifactItems>   
                    <artifactItem>
                      <groupId>org.redis.centos.7</groupId>
                      <artifactId>redis</artifactId>
                      <type>tar.gz</type>
                      <outputDirectory>target/assembly/linux64</outputDirectory>        
                    </artifactItem>                                   
                  </artifactItems>
                  <excludes>**/redis-benchmark**,**/*.pdb,**/*.docx</excludes>
                  <overWriteReleases>false</overWriteReleases>
                  <overWriteSnapshots>true</overWriteSnapshots>
                  <outputAbsoluteArtifactFilename>true</outputAbsoluteArtifactFilename>
                </configuration>
              </execution>          
            </executions>
          </plugin> 

         9.11、maven-deploy-plugin

                            主要是为了部署工程到远程仓库当中。

         9.12、wagon-ssh、wagon-ftp、wagon-webdav-jackrabbit

                            以扩展方式部署项目。
  <build>
    <extensions>
        <extension>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-ssh</artifactId>
            <version>2.10</version>
        </extension>
        <extension>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-ftp</artifactId>
            <version>2.10</version>
        </extension>
        <extension>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-webdav-jackrabbit</artifactId>
            <version>2.10</version>
        </extension>
    </extensions>
  </build>

         9.12、maven-site-plugin

                           主要是为了部署项目。

         9.13、maven-project-info-reports-plugin

                           生成项目总报告。

         9.14、maven-clean-plugin

                           清除项目,主要是清理target目录。

         9.15、maven-install-plugin

                           部署项目到本地maven仓库。

         9.16、maven-antrun-plugin

            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-antrun-plugin</artifactId>
              <executions>
                <execution>
                    <id>distribution</id>
                    <phase>package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                       <target name="distribution">
                          <tar destfile="${version.output}/${packageid}-${project.version}-${classifier.linux64}.tar.gz" longfile="posix" compression="gzip">
                              <tarfileset dir="${linux64outputdir}" filemode="0644" dirmode="0755">
                                  <exclude name="**/*.sh"/>
                                  <exclude name="redis/redis-*"/>
                              </tarfileset>
                              <tarfileset dir="${linux64outputdir}" filemode="0755" dirmode="0755">
                                  <include name="**/*.sh"/>
                                  <include name="redis/redis-*"/>
                              </tarfileset>
                          </tar>
                          <attachartifact file="${version.output}/${packageid}-${project.version}-${classifier.linux64}.tar.gz" classifier="${classifier.linux64}" type="tar.gz"/>
                      </target>
                    </configuration>
                </execution>
              </executions>
            </plugin>

         9.17、maven-enforcer-plugin

                           部署项目到本地

         9.18、maven-invoker-plugin

                           主要是解决项目之间引用与依赖关系

         9.19、maven-jar-plugin

                           部署项目到本地

         9.20、maven-javadoc-plugin

                           用来生成javadoc

         9.21、maven-release-plugin

                           可用于构建release版本项目,实现自动打tag、递增版本号、分发release版本jar包到仓库。

         9.22、maven-plugin-plugin

                           开发maven插件使用的插件。

         9.23、maven-shade-plugin

                           打包项目。

         9.24、maven-source-plugin

                           生成项目的源码包。

         9.25、cobertura-maven-plugin

                           测试代码覆盖率。

         9.26、maven-jxr-plugin

                           生成java代码交叉引用和源代码的html格式。

         9.27、maven-pmd-plugin

                           源码分析工具。

         9.28、copy-rename-maven-plugin

                           修改目录或者文件名的插件。


                        






版权声明:本文为erdanyang原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。