一文搞懂 maven 进阶使用

目录

1、pom.xml 文件详解

1.1、modelVersion(在 maven2.0 以后固定 4.0.0)

1.2、项目的gav

1.3、打包方式

1.4、scope 范围

1.5、依赖排除

1.6、依赖继承

1.7、父工程中管理依赖信息

1.8、自定义属性标签

1.9、distributionManagement 标签

1.11、pluginRepositories 标签

1.12、build 标签

1.13、profile 标签(pom 中最后一个标签;可以在 setting.xml 和 pom 配置)

2、super pom 文件内容

3、自定义jar包的gav

4、多模块打包

        所有的 pom 都会继承于一个超级pom(super POM),就像java对象之于Object类,这个超级pom定义了源文件存放位置、测试源文件存放位置、构建输出目录等信息。

1、pom.xml 文件详解
1.1、modelVersion(在 maven2.0 以后固定 4.0.0)
<modelVersion>4.0.0</modelVersion>
1.2、项目的gav
<groupId>com.weilong.maven</groupId>
<artifactId>first-maven-project</artifactId>
<version>1.0.0</version>
1.3、打包方式
<!--有 jar、war、pom打包方式-->
<!--
jar:普通的java工程
war:web项目
pom:父工程的打包方式-->
<packaging>jar</packaging>
1.4、scope 范围

有以下几个范围:compile、test、provided、system、runtime、import

<!--compile-->
<!--不声明scope元素的情况下的默认值,compile表示被依赖包需要参与当前项目的编译,包括后续的测试,运行周期也参与其中,是一个比较强的依赖;打包的时候也包含进去。-->
<!--===============================-->
<!--provided-->
<!--provided 类型的scope只会在项目的编译、测试阶段起作用,可以认为在目标容器中已经提供了这个依赖,无需在提供,但是在编写代码或者编译时可能会用到这个依赖;依赖不会被打入到项目jar包中。(部署不会包含)-->
<!--===============================-->
<!--test-->
<!--在一般的编译和运行时都不需要,它们只有在测试编译和测试运行阶段可用,不会被打包到项目jar包中,同时如果项目A依赖于项目B,项目B中的test作用域下的依赖不会被继承。-->
<!--===============================-->
<!--system-->
<!--表示使用本地系统路径下的jar包,需要和一个systemPath一起使用(基本用不到),如下:-->
<dependency>
    <groupId>xxxx</groupId>
    <artifactId>xxx</artifactId>
    <systemPath>${basedir}/lib/xxxxx.jar</systemPath>
    <scope>system</scope>
    <version>1.4.12</version>
</dependency>
<!--===============================-->
<!--runtime-->
<!--runtime与compile比较相似,区别在于runtime 跳过了编译阶段,打包的时候会包含进去。-->
<!--===============================-->
<!--import-->
<!--import (打包方式是pom)只能在pom文件的<dependencyManagement>中使用,从而引入其他的pom文件中引入依赖,如:在Spring boot 项目的POM文件中,我们可以通过在POM文件中继承 Spring-boot-starter-parent来引用Srping boot默认依赖的jar包,如下:-->
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.0.1.BUILD-SNAPSHOT</version>
</parent>
<!--但是,通过上面的parent继承的方法,只能继承一个 spring-boot-start-parent。实际开发中,用户很可能需要继承自己的标准parent配置,这个时候可以使用 scope=import 来实现多继承。代码如下:-->
<dependencyManagement>
    <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.1.BUILD-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<!--通过上面方式,就可以获取spring-boot-dependencies.2.0.1.BUILD-SNAPSHOT.pom文件中dependencyManagement配置的jar包依赖。如果要继承多个,可以在dependencyManagement中添加,如:-->
<dependencyManagement>
    <dependencies>
        <!-- Override Spring Data release train provided by Spring Boot -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-releasetrain</artifactId>
            <version>Fowler-SR2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.1.BUILD-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
1.5、依赖排除

        有些 dependency 中会导入一些重复的依赖,导致版本冲突,需要进行排除,在<dependency> 标签下使用。

<exclusions>
	<exclusion>
    	<!--在其中写要排除的jar包的gav-->
    </exclusion>
</exclusions>
1.6、依赖继承
<!--父工程打包方式一定是pom-->
<packaging>pom</packaging>
<!--子工程的引入-->
<modules>
	<module>子工程1</module>
    <module>子工程2</module>
    ....
</modules>
<!--子工程引入父工程(如果子工程的的groupId和version和父工程一样可以省略,只保留artifactId)-->

<parent>
	<!--在其中写父工程的gav,父工程继承 springboot 的依赖-->
</parent>
1.7、父工程中管理依赖信息
<dependencyManagement>
    <dependencies>
       <!-- 即使在父工程中配置了依赖管理,子工程还是需要引入 -->
       <!-- 在父工程中配置了依赖管理,子工程可以不写版本(version),有两种情况 -->
       <!-- 第一种:子工程不写版本(version),就使用父工程中定义的版本号 -->
       <!-- 第二种:子工程写版本(version),如果和父工程中的版本不一致,就会覆盖父工程版本,否则还会使用父工程定义的版本 -->
    </dependencies>
</dependencyManagement>
1.8、自定义属性标签
<properties>
	<weilong.test.property>long_ge1.0</weilong.test.property>
</properties>
<!--引用使用 ${weilong.test.property} 方式-->
1.9、distributionManagement 标签

distributionManagement 表示项目打包成库文件后要上传到仓库地址。

mvn install 会将项目生成的构件安装到本地 maven 仓库之后,所有能访问该仓库的用户都能使用你的构件。我们需要配置 pom 的 distributionManagement 来指定 Maven 分发构件的位置。

<distributionManagement>    
    <repository>    
        <id>nexus-releases</id>    
        <name>Nexus Release Repository</name>    
        <url>http://127.0.0.1:8080/nexus/content/repositories/releases/</url>    
    </repository>    
    <snapshotRepository>    
        <id>nexus-snapshots</id>    
        <name>Nexus Snapshot Repository</name>    
        <url>http://127.0.0.1:8080/nexus/content/repositories/snapshots/</url>    
    </snapshotRepository>    
</distributionManagement>   
<!--================================================================================-->
<!-- 在 setting.xml 文件中添加配置认证信息,server的id要与pom文件中repository的id一致。-->
<servers>    
    <server>    
        <id>nexus-releases</id>    
        <username>admin</username>    
        <password>admin123</password>    
    </server>    
    <server>    
        <id>nexus-snapshots</id>    
        <username>admin</username>    
        <password>admin123</password>    
    </server>      
</servers> 
1.10、repositories 标签

repositorie 表示下载项目依赖库文件的 maven 仓库地址。

<repositories>
  <repository>
      <!-- 仓库ID -->
      <id>nexus</id>
      <!-- 仓库名称 -->
      <name>Nexus</name>
      <!-- 仓库地址 -->
      <url>http://192.168.1.x:xxxx/repository/maven-public/</url>
      <!-- 仓库中版本为releases的构件 -->
      <releases>
          <!-- 是否支持更新-->
          <enabled>true</enabled>
<!--构件更新的策略,可选值有daily, always, never, interval:X(其中的X是一个数字,表示间隔的时间,单位min),默认为daily-->
          <updatePolicy>always</updatePolicy>  
          <!-- 校验码异常的策略,可选值有ignore, fail, warn -->
          <checksumPolicy>warn</checksumPolicy>  
      </releases>
       <!-- 仓库版本为snapshots的构件-->
      <snapshots>
           <!-- 是否支持更新-->
          <enabled>true</enabled>  
          <!-- 同上 -->
          <updatePolicy>always</updatePolicy>  
          <!-- 同上 -->
          <checksumPolicy>warn</checksumPolicy>  
      </snapshots>
  </repository>
</repositories>
1.11、pluginRepositories 标签

pluginRepositories 表示插件的下载仓库地址,字段的用法与 repositories 中的 repository 基本一致。

<pluginRepositories>
  <pluginRepository>
      <id>nexus</id>
      <name>Nexus</name>
      <url>http://192.168.1.x:xxxx/repository/maven-public/</url>
      <releases>
          <enabled>true</enabled>
          <updatePolicy>always</updatePolicy>  
          <checksumPolicy>warn</checksumPolicy>  
      </releases>
      <snapshots>
          <enabled>true</enabled>  
          <updatePolicy>always</updatePolicy>  
          <checksumPolicy>warn</checksumPolicy>  
      </snapshots>
  </pluginRepository>
</pluginRepositories>
1.12、build 标签

在Maven的pom.xml文件中,存在如下两种 build:

(1)全局配置(project build): 针对整个项目的所有情况都有效

(2)配置(profile build):针对不同的profile配置

<build>  
  <!--defaultGoal 执行build任务时,如果没有指定,将使用的默认值。如下配置:在命令行中执行mvn,则相当于执行mvn install-->
  <defaultGoal>install</defaultGoal>  
  <!-- directory build目标文件的存放目录,默认在${basedir}/target目录-->
  <directory>${basedir}/target</directory>  
  <!--finalName  build目标文件的名称,默认情况为${artifactId}-${version}-->
  <finalName>${artifactId}-${version}</finalName>   
  <!--filters 定义*.properties文件,包含一个properties列表,该列表会应用到支持filter的resources中。也就是说,定义在filter的文件中的name=value键值对,会在build时代替${name}值应用到resources中。maven的默认filter文件夹为${basedir}/src/main/filters-->
  <filters>   
      <filter>filters/filter1.properties</filter>  
  </filters> 
  <!--resources 一个resources元素的列表。每一个都描述与项目关联的文件是什么和在哪里-->
<!--==============================================================================-->
<!--resources元素:资源往往不是代码,而是properties或xml文件,无需编译,构建过程中往往会将资源文件从源路径复制到指定的目标路径,resources则给出各个资源在maven项目中的具体路径。-->
  <resources>  
          <resource>  
  <!-- targetPath 指定build后的resource存放的文件夹,默认是basedir。通常被打包在jar中的的目标路径是META-INF-->
             <targetPath>META-INF/plexus</targetPath>  
  <!--filtering true/false,表示为这个resource,filter是否激活,上面配置的filters是否对这个resource生效-->
             <filtering>true</filtering> 
  <!-- directory 定义resource文件所在的文件夹,默认为${basedir}/src/main/resources-->
            <directory>${basedir}/src/main/resources</directory> 
  <!--includes 指定哪些文件将被匹配,以*作为通配符-->
            <includes>  
                <include>configuration.xml</include>  
            </includes>  
  <!--excludes 指定哪些文件将被忽略-->
            <excludes>  
                <exclude>**/*.properties</exclude>  
            </excludes>  
         </resource>  
    </resources>  
  <!--testResources 定义和resource类似,只不过在test时使用-->
    <testResources>  
        ...  
    </testResources>  
<!--===============================================================================-->
    <plugins>  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-jar-plugin</artifactId>  
            <version>2.0</version>  
            <!--extensions:是否加载该插件的扩展,默认false。-->
            <extensions>false</extensions>  
            <!--inherited: 该插件的configuration中的配置是否可以被继承(继承该pom中的其他maven项目),默认true。-->
            <inherited>true</inherited>  
            <!--configuration:该插件所需要的特殊配置,在父子项目之间可以覆盖或合并。-->
            <configuration>  
                <classifier>test</classifier>  
            </configuration> 
            <!--dependencies: 该插件所需要的依赖类库。-->
            <dependencies>...</dependencies>  
            <!--executions: 该插件的某个goal的执行方式。-->
            <executions>...</executions>  
        </plugin>  
    </plugins> 
<!--================================================================================-->
<!--pluginManagement的配置和plugins的配置是一样的,只是用于继承,使得可以子pom中使用。-->
    <pluginManagement>  
        <plugins>  
            <plugin>  
              <groupId>org.apache.maven.plugins</groupId>  
              <artifactId>maven-jar-plugin</artifactId>  
              <version>2.2</version>  
                <executions>  
                    <execution>  
                        <id>pre-process-classes</id> 
                        <!--phase: 关联的生命周期阶段,在生命周期的哪个环节执行目标goal-->
                        <phase>compile</phase> 
                        <!--goals:要执行的插件的goal,如run, repackage-->
                        <goals>  
                            <goal>jar</goal>  
                        </goals>
                        <!--inherited: 该execution是否可被子项目继承-->
                        <inherited>false</inherited>
                        <!--configuration:该execution的其他配置参数-->
                        <configuration>  
                            <classifier>pre-process</classifier>  
                            <encoding>UTF-8</encoding>
                            <source>1.8</source>
                            <target>1.8</target>
                        </configuration>  
                    </execution>  
                </executions>  
            </plugin>  
        </plugins>  
    </pluginManagement>  
</build>
1.13、profile 标签(pom 中最后一个标签;可以在 setting.xml 和 pom 配置)
<!-- setting配置只能在本地项目中生效,项目需要部署应该在pom文件中配置-->
<!-- setting.xml -->
<profiles>
    <profile>
        <id>jdk-1.8</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <jdk>1.8</jdk>
        </activation>
        <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
        </properties>
    </profile>
</profiles>
<!--============================================================================-->
<!-- pom.xml -->
<prrofiles>
	<profile>
        <id>my_profile_id</id>
    	<activation>
            <!-- true表示默认激活,false表示默认不激活-->
            <activeByDefault>true</activeByDefault>
          <!--指定激活条件,可以指定多条,在3.2.2之前是或的关系,只要满足一条就能激活,3.2.2之后是且的关系,需要全部满足-->
        	<jdk>1.8</jdk>  <!-- <jdk>[1.8,)</jdk> jdk1.8版本及以上-->
            <property>
                <name>env</name>
                <value>test</value>
            </property>
        </activation>
        <properties>
            <profiles.active>test</profiles.active>
        </properties>
        <!-- 其他标签,就是当前profile被激活后要使用的配置-->
    </profile>
</prrofiles>
<!-- 在使用特定的profile的时候,通过id进行指定(-P(profileId))-->
2、super pom 文件内容

在super pom 中已经定义打包路径等信息。

<!-- START SNIPPET: superpom -->
<project> 
  <modelVersion>4.0.0</modelVersion>  
  <repositories> 
    <repository> 
      <id>central</id>  
      <name>Central Repository</name>  
      <url>http://repo.maven.apache.org/maven2</url>  
      <layout>default</layout>  
      <snapshots> 
        <enabled>false</enabled> 
      </snapshots> 
    </repository> 
  </repositories>  
  <pluginRepositories> 
    <pluginRepository> 
      <id>central</id>  
      <name>Central Repository</name>  
      <url>http://repo.maven.apache.org/maven2</url>  
      <layout>default</layout>  
      <snapshots> 
        <enabled>false</enabled> 
      </snapshots>  
      <releases> 
        <updatePolicy>never</updatePolicy> 
      </releases> 
    </pluginRepository> 
  </pluginRepositories>  
  <build> 
    <directory>${project.basedir}/target</directory>  
    <outputDirectory>${project.build.directory}/classes</outputDirectory>  
    <finalName>${project.artifactId}-${project.version}</finalName>  
    <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>  
    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>  
    <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>  
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>  
    <resources> 
      <resource> 
        <directory>${project.basedir}/src/main/resources</directory> 
      </resource> 
    </resources>  
    <testResources> 
      <testResource> 
        <directory>${project.basedir}/src/test/resources</directory> 
      </testResource> 
    </testResources>  
    <pluginManagement> 
      <!-- NOTE: These plugins will be removed from future versions of the super POM -->  
      <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->  
      <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.1</version> 
        </plugin>  
        <plugin> 
          <artifactId>maven-release-plugin</artifactId>  
          <version>2.0</version> 
        </plugin> 
      </plugins> 
    </pluginManagement> 
  </build>  
  <reporting> 
    <outputDirectory>${project.build.directory}/site</outputDirectory> 
  </reporting>  
  <profiles> 
    <!-- NOTE: The release profile will be removed from future versions of the super POM -->  
    <profile> 
      <id>release-profile</id>  
      <activation> 
        <property> 
          <name>performRelease</name>  
          <value>true</value> 
        </property> 
      </activation>  
      <build> 
        <plugins> 
          <plugin> 
            <inherited>true</inherited>  
            <artifactId>maven-source-plugin</artifactId>  
            <executions> 
              <execution> 
                <id>attach-sources</id>  
                <goals> 
                  <goal>jar</goal> 
                </goals> 
              </execution> 
            </executions> 
          </plugin>  
          <plugin> 
            <inherited>true</inherited>  
            <artifactId>maven-javadoc-plugin</artifactId>  
            <executions> 
              <execution> 
                <id>attach-javadocs</id>  
                <goals> 
                  <goal>jar</goal> 
                </goals> 
              </execution> 
            </executions> 
          </plugin>  
          <plugin> 
            <inherited>true</inherited>  
            <artifactId>maven-deploy-plugin</artifactId>  
            <configuration> 
              <updateReleaseInfo>true</updateReleaseInfo> 
            </configuration> 
          </plugin> 
        </plugins> 
      </build> 
    </profile> 
  </profiles> 
</project>
<!-- END SNIPPET: superpom -->
3、自定义jar包的gav
<!-- 1、将体系外jar包指定自定义的gav -->
mvn install:install-file -Dfile=本地jar包存放目录 -DgroupId=jar包添加依赖的groupId名称 -DartifactId=jar包添加依赖的artifactId名称 -Dversion=jar包添加依赖的version -Dpackaging=jar

<!-- 2、在pom文件中添加依赖 -->
<dependency>
	<groupId>**</groupId>
	<artifactId>**</artifactId>
	<version>**</version>
</dependency>
4、多模块打包
<!--父模块配置-->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.7.5</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <fork>true</fork>
                <addResources>true</addResources>
            </configuration>
        </plugin>
    </plugins>
</build>

<!--当通用模块没有主启动类时,打包会出错,在对应模块中添加如下配置,可以跳过打包-->
<!-- 一般这种情况使用在 common 模块, 没有加主启动类 -->
<!--子模块配置-->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.7.5</version>
            <configuration>
                <skip>true</skip>
            </configuration>
        </plugin>
    </plugins>
</build>

        本人是一个从小白自学计算机技术,对运维、后端、各种中间件技术、大数据等有一定的学习心得,想获取自学总结资料(pdf版本)或者希望共同学习,关注微信公众号:it自学社团。后台回复相应技术名称/技术点即可获得。(本人学习宗旨:学会了就要免费分享)

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

知其_所以然

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值