pom文件自我解析

💡 根据 遗忘曲线:如果没有记录和回顾,6天后便会忘记75%的内容

读书笔记正是帮助你记录和回顾的工具,不必拘泥于形式,其核心是:记录、翻看、思考

书名

pom文件自我解析

作者

IT摆烂工程师

状态

摆烂

简介

自我解析。

基本配置

<!-- project:是文件的根标签 -->
<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:指pom文件的标签符合哪个版本(比如maven2,3只能是4.0.0) -->
  <modelVersion>4.0.0</modelVersion>

  <!-- parent:指定父 POM ,然后继承其配置。 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.7.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

  <!-- 模块(可以表示子项目) -->
  <modules>
		<module>cereshop-commons</module>
		<module>cereshop-admin</module>
		<module>cereshop-business</module>
		<module>cereshop-app</module>
	</modules>

	<!-- properties:在标签内可以把版本号作为变量进行声明 -->
 <properties>
     <!-- Environment Settings -->
     <java.version>1.8</java.version>
     <version>1.0</version>
     <spring-cloud.version>xxxx</spring-cloud.version>
 </properties>
  
  <!-- groupId:编写这个系统的组织,一般对应java项目的包结构 -->
  <groupId>xxx.xxx.xxx</groupId> 
  <!-- artifactId:java项目的唯一标识,一个groupId下面可能多个项目,就是靠artifactId来区分的 -->
  <artifactId>xx-xxx</artifactId>
  <!-- version:java项目的特定版本,项目当前版本,格式为:主版本.次版本.增量版本-限定版本号 -->
  <version>1.2.5-SNAPSHOT</version>
  <!-- packaging:项目打包的的输出类型(默认jar,还有pom,maven-plugin,ejb,war,ear,rar,par等) -->
  <packaging>jar</packaging>
  <!-- name:项目名,用于文档产生 -->
  <name>项目名</name>
  <!-- description:项目描述 -->
  <description>项目描述</description>
  <!-- url: 项目url -->
  <url>https://xxxx</url>
  <!-- inceptionYear:项目开发年份 -->
  <inceptionYear>2024</inceptionYear>
  <!--组织信息(如公司、开源组织等)-->
  <organization>
    <name>...</name>
    <url>...</url>
  </organization>

  <!-- dependencies:依赖配置标签,描述了项目相关的所有依赖,自动从项目定义的仓库中下载。 -->
	<dependencies>
    <!-- dependency:从属(代表每一个依赖) -->
    <dependency>
      <!-- 依赖项的组织名 -->
     <groupId>xxx.xxxx.xxxx</groupId>
      <!-- 依赖项的子项目名 -->
      <artifactId>xxx-xxx</artifactId>
      <!-- 依赖项的版本 -->
      <version>1.0</version>
      <!-- 依赖类型:默认可以省略 -->
      <type>jar</type>
      <!-- 依赖的试用范围:包括compile,provided,runtime,test,system,exclusions(本文搜索:试用范围有详细) -->
      <scope>test</scope>
      <!--  防止将此依赖传递到,依赖此项目的其他项目中  -->
      <optional>true</optional>
      <!-- 排除主动从依赖关系树中删除自己 -->
      <exclusions>
        <exclusion>
          <groupId>xxx.xxxxx.xxx</groupId>
          <artifactId>xx-xxx</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    ...
  </dependencies>

  <!-- dependencyManagement声明依赖:但是不引入此依赖,只做声明,如果不在子项目中声明依赖,是不会从父项目中继承下来的;
  只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom,
  如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。-->
  <dependencyManagement>
	<dependencies>
	    <!--spring cloud-->
	    <dependency>
	        <groupId>org.springframework.cloud</groupId>
	        <artifactId>spring-cloud-dependencies</artifactId>
	        <version>${spring-cloud.version}</version>
	        <type>pom</type>
	        <scope>import</scope>
	    </dependency>
	<dependencies>
</dependencyManagement>     
  
</project>

基本信息

其中groupId,artifactId,version,packaging这四项组成了项目的唯一坐标。一般情况下,前面三项就足矣。

版本管理的特殊标识

  • SNAPSHOT - 这个版本一般用于开发过程中,表示不稳定的版本。
  • LATEST - 指某个特定构件的最新发布,这个发布可能是一个发布版,也可能是一个 snapshot 版,具体看哪个时间最后。
  • RELEASE :指最后一个发布版。

依赖配置

scope 依赖项的适用范围

  • compile,缺省值,适用于所有阶段,会随着项目一起发布。
  • provided,类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet.jar。
  • runtime,只在运行时使用,如JDBC驱动,适用运行和测试阶段。
  • test,只在测试时使用,用于编译和运行测试代码。不会随项目发布。
  • system,类似provided,需要显式提供包含依赖的jar,Maven不会在Repository中查找它。
  • optional: 当项目自身被依赖时,标注依赖是否传递。用于连续依赖时使用

依赖关系

有两种方式可以对依赖关系进行,分别是可选依赖(Optional Dependencies)以及依赖排除(Dependency Exclusions)。

可选依赖(Optional Dependencies)

如:

假设以上配置是项目A的配置,即:Project-A <-- Project-B。在编译项目A时,是可以正常通过的。

如果有一个新的项目C依赖A,即:Project-C <-- Project-A <-- Project-B。此时项目C就不会依赖项目B了。

依赖排除(Dependency Exclusions)。

当一个项目A依赖项目B,而项目B同时依赖项目C,如果项目A中因为各种原因不想引用项目C,在配置项目B的依赖时,可以排除对C的依赖。

示例(假设配置的是A的pom.xml,依赖关系为:A<–B<–C):

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>sample.ProjectB</groupId>
      <artifactId>Project-B</artifactId>
      <version>1.0</version>
      <scope>compile</scope>
      <exclusions>
        <exclusion>  <!-- declare the exclusion here -->
          <groupId>sample.ProjectB</groupId>
          <artifactId>Project-B</artifactId>
        </exclusion>
      </exclusions> 
    </dependency>
  </dependencies>
</project>

构建配置

build 可以分为 "project build" 和 "profile build"。

<build>  
  
    <!-- 产生的构件的文件名,默认值是${artifactId}-${version}。 -->  
    <finalName>myPorjectName</finalName>  
  
    <!-- 构建产生的所有文件存放的目录,默认为${basedir}/target,即项目根目录下的target -->  
    <directory>${basedir}/target</directory>  
  
    <!--当项目没有规定目标(Maven2叫做阶段(phase))时的默认值, -->  
    <!--必须跟命令行上的参数相同例如jar:jar,或者与某个阶段(phase)相同例如install、compile等 -->  
    <defaultGoal>install</defaultGoal>  
  
    <!--当filtering开关打开时,使用到的过滤器属性文件列表。 -->  
    <!--项目配置信息中诸如${spring.version}之类的占位符会被属性文件中的实际值替换掉 -->  
    <filters>  
        <filter>../filter.properties</filter>  
    </filters>  
  
    <!--项目相关的所有资源路径列表,例如和项目相关的配置文件、属性文件,这些资源被包含在最终的打包文件里。 -->  
    <resources>  
        <resource>  
  
            <!--描述了资源的目标路径。该路径相对target/classes目录(例如${project.build.outputDirectory})。 -->  
            <!--举个例子,如果你想资源在特定的包里(org.apache.maven.messages),你就必须该元素设置为org/apache/maven/messages。 -->  
            <!--然而,如果你只是想把资源放到源码目录结构里,就不需要该配置。 -->  
            <targetPath>resources</targetPath>  
  
            <!--是否使用参数值代替参数名。参数值取自properties元素或者文件里配置的属性,文件在filters元素里列出。 -->  
            <filtering>true</filtering>  
  
            <!--描述存放资源的目录,该路径相对POM路径 -->  
            <directory>src/main/resources</directory>  
  
            <!--包含的模式列表 -->  
            <includes>  
                <include>**/*.properties</include>  
                <include>**/*.xml</include>  
            </includes>  
  
            <!--排除的模式列表 如果<include>与<exclude>划定的范围存在冲突,以<exclude>为准 -->  
            <excludes>  
                <exclude>jdbc.properties</exclude>  
            </excludes>  
  
        </resource>  
    </resources>  
  
    <!--单元测试相关的所有资源路径,配制方法与resources类似 -->  
    <testResources>  
        <testResource>  
            <targetPath />  
            <filtering />  
            <directory />  
            <includes />  
            <excludes />  
        </testResource>  
    </testResources>  
  
    <!--项目源码目录,当构建项目的时候,构建系统会编译目录里的源码。该路径是相对于pom.xml的相对路径。 -->  
    <sourceDirectory>${basedir}\src\main\java</sourceDirectory>  
  
    <!--项目脚本源码目录,该目录和源码目录不同, <!-- 绝大多数情况下,该目录下的内容会被拷贝到输出目录(因为脚本是被解释的,而不是被编译的)。 -->  
    <scriptSourceDirectory>${basedir}\src\main\scripts  
    </scriptSourceDirectory>  
  
    <!--项目单元测试使用的源码目录,当测试项目的时候,构建系统会编译目录里的源码。该路径是相对于pom.xml的相对路径。 -->  
    <testSourceDirectory>${basedir}\src\test\java</testSourceDirectory>  
  
    <!--被编译过的应用程序class文件存放的目录。 -->  
    <outputDirectory>${basedir}\target\classes</outputDirectory>  
  
    <!--被编译过的测试class文件存放的目录。 -->  
    <testOutputDirectory>${basedir}\target\test-classes  
    </testOutputDirectory>  
  
    <!--项目的一系列构建扩展,它们是一系列build过程中要使用的产品,会包含在running bulid‘s classpath里面。 -->  
    <!--他们可以开启extensions,也可以通过提供条件来激活plugins。 -->  
    <!--简单来讲,extensions是在build过程被激活的产品 -->  
    <extensions>  
  
        <!--例如,通常情况下,程序开发完成后部署到线上Linux服务器,可能需要经历打包、 -->  
        <!--将包文件传到服务器、SSH连上服务器、敲命令启动程序等一系列繁琐的步骤。 -->  
        <!--实际上这些步骤都可以通过Maven的一个插件 wagon-maven-plugin 来自动完成 -->  
        <!--下面的扩展插件wagon-ssh用于通过SSH的方式连接远程服务器, -->  
        <!--类似的还有支持ftp方式的wagon-ftp插件 -->  
        <extension>  
            <groupId>org.apache.maven.wagon</groupId>  
            <artifactId>wagon-ssh</artifactId>  
            <version>2.8</version>  
        </extension>  
  
    </extensions>  
  
    <!--使用的插件列表 。 -->  
    <plugins>  
        <plugin>  
            <groupId></groupId>  
            <artifactId>maven-assembly-plugin</artifactId>  
            <version>2.5.5</version>  
  
            <!--在构建生命周期中执行一组目标的配置。每个目标可能有不同的配置。 -->  
            <executions>  
                <execution>  
  
                    <!--执行目标的标识符,用于标识构建过程中的目标,或者匹配继承过程中需要合并的执行目标 -->  
                    <id>assembly</id>  
  
                    <!--绑定了目标的构建生命周期阶段,如果省略,目标会被绑定到源数据里配置的默认阶段 -->  
                    <phase>package</phase>  
  
                    <!--配置的执行目标 -->  
                    <goals>  
                        <goal>single</goal>  
                    </goals>  
  
                    <!--配置是否被传播到子POM -->  
                    <inherited>false</inherited>  
  
                </execution>  
            </executions>  
  
            <!--作为DOM对象的配置,配置项因插件而异 -->  
            <configuration>  
                <finalName>${finalName}</finalName>  
                <appendAssemblyId>false</appendAssemblyId>  
                <descriptor>assembly.xml</descriptor>  
            </configuration>  
  
            <!--是否从该插件下载Maven扩展(例如打包和类型处理器), -->  
            <!--由于性能原因,只有在真需要下载时,该元素才被设置成true。 -->  
            <extensions>false</extensions>  
  
            <!--项目引入插件所需要的额外依赖 -->  
            <dependencies>  
                <dependency>...</dependency>  
            </dependencies>  
  
            <!--任何配置是否被传播到子项目 -->  
            <inherited>true</inherited>  
  
        </plugin>  
    </plugins>  
  
    <!--主要定义插件的共同元素、扩展元素集合,类似于dependencyManagement, -->  
    <!--所有继承于此项目的子项目都能使用。该插件配置项直到被引用时才会被解析或绑定到生命周期。 -->  
    <!--给定插件的任何本地配置都会覆盖这里的配置 -->  
    <pluginManagement>  
        <plugins>...</plugins>  
    </pluginManagement>  
  
</build> 

<profiles>  
  
    <!--根据环境参数或命令行参数激活某个构建处理 -->  
    <profile>  
        <!--自动触发profile的条件逻辑。Activation是profile的开启钥匙。 -->  
        <activation>  
  
            <!--profile默认是否激活的标识 -->  
            <activeByDefault>false</activeByDefault>  
  
            <!--activation有一个内建的java版本检测,如果检测到jdk版本与期待的一样,profile被激活。 -->  
            <jdk>1.7</jdk>  
  
            <!--当匹配的操作系统属性被检测到,profile被激活。os元素可以定义一些操作系统相关的属性。 -->  
            <os>  
  
                <!--激活profile的操作系统的名字 -->  
                <name>Windows XP</name>  
  
                <!--激活profile的操作系统所属家族(如 'windows') -->  
                <family>Windows</family>  
  
                <!--激活profile的操作系统体系结构 -->  
                <arch>x86</arch>  
  
                <!--激活profile的操作系统版本 -->  
                <version>5.1.2600</version>  
  
            </os>  
  
            <!--如果Maven检测到某一个属性(其值可以在POM中通过${名称}引用),其拥有对应的名称和值,Profile就会被激活。 -->  
            <!-- 如果值字段是空的,那么存在属性名称字段就会激活profile,否则按区分大小写方式匹配属性值字段 -->  
            <property>  
  
                <!--激活profile的属性的名称 -->  
                <name>mavenVersion</name>  
  
                <!--激活profile的属性的值 -->  
                <value>2.0.3</value>  
  
            </property>  
  
            <!--提供一个文件名,通过检测该文件的存在或不存在来激活profile。missing检查文件是否存在,如果不存在则激活profile。 -->  
            <!--另一方面,exists则会检查文件是否存在,如果存在则激活profile。 -->  
            <file>  
  
                <!--如果指定的文件存在,则激活profile。 -->  
                <exists>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/</exists>  
  
                <!--如果指定的文件不存在,则激活profile。 -->  
                <missing>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/</missing>  
  
            </file>  
  
        </activation>  
        <id />  
        <build />  
        <modules />  
        <repositories />  
        <pluginRepositories />  
        <dependencies />  
        <reporting />  
        <dependencyManagement />  
        <distributionManagement />  
        <properties />  
    </profile>  

多环境的配置

// An highlighted block
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        
        <!--打包的时候,将静态资源也都打进去 --> 
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>application.yml</include>
                    <include>bootstrap.yml</include>
                    <include>application-${spring.profiles.active}.yml</include>
                    <include>bootstrap-${spring.profiles.active}.yml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources/conf/${profile.env}</directory>
            </resource>
        </resources>
    </build>

    <profiles>
        <!-- 开发环境 -->
        <profile>
            <id>dev</id>
            <activation>
                <!-- 默认环境-->
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
            </activation>
            <properties>
                <spring.profiles.active>dev</spring.profiles.active>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
                <profile.env>dev</profile.env>
            </properties>
        </profile>
        <!-- 生产环境 -->
        <profile>
            <id>prod</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>prod</spring.profiles.active>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
                <profile.env>prod</profile.env>
            </properties>
        </profile>
         <!-- test环境 -->
        <profile>
            <id>test</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>test</spring.profiles.active>
                <profile.env>test</profile.env>
            </properties>
        </profile>
    </profiles>

  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值