pom.xml中build标签

本文详细解析Maven项目的构建配置,包括全局配置与配置文件的差异,基本元素的解释,如defaultGoal、directory等,Resources与testResources的配置方式,plugins及pluginManagement的使用方法。

 

1.分类

       (1)全局配置(project build)

                 针对整个项目的所有情况都有效

       (2)配置(profile build)

                 针对不同的profile配置

[html] view plain copy

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  2.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  
  4.                       http://maven.apache.org/maven-v4_0_0.xsd">  
  5.   …  
  6.   <!– "Project Build" contains more elements than just the BaseBuild set –>  
  7.   <build>…</build>  
  8.   <profiles>  
  9.     <profile>  
  10.       <!– "Profile Build" contains a subset of "Project Build"s elements –>  
  11.       <build>…</build>  
  12.     </profile>  
  13.   </profiles>  
  14. </project>  

 

2.配置说明

       (1)基本元素

[html] view plain copy

  1. <build>  
  2.         <defaultGoal>install</defaultGoal>  
  3.         <directory>${basedir}/target</directory>  
  4.         <finalName>${artifactId}-${version}</finalName>  
  5.         <filters>  
  6.                 <filter>filters/filter1.properties</filter>  
  7.         </filters>  
  8.          ...  
  9. </build>  

               1)defaultGoal

                    执行build任务时,如果没有指定目标,将使用的默认值。

                    如上配置:在命令行中执行mvn,则相当于执行mvn install

              2)directory
                     build目标文件的存放目录,默认在${basedir}/target目录

              3)finalName

                     build目标文件的名称,默认情况为${artifactId}-${version}

              4)filter

                     定义*.properties文件,包含一个properties列表,该列表会应用到支持filter的resources中。

                     也就是说,定义在filter的文件中的name=value键值对,会在build时代替${name}值应用到resources中。

                     maven的默认filter文件夹为${basedir}/src/main/filters

 

        (2)Resources配置

                 用于包含或者排除某些资源文件

[html] view plain copy

  1. <build>  
  2.         ...  
  3.        <resources>  
  4.                   <resource>  
  5.                         <targetPath>META-INF/plexus</targetPath>  
  6.                         <filtering>false</filtering>  
  7.             <directory>${basedir}/src/main/plexus</directory>  
  8.             <includes>  
  9.                 <include>configuration.xml</include>  
  10.             </includes>  
  11.             <excludes>  
  12.                 <exclude>**/*.properties</exclude>  
  13.             </excludes>  
  14.          </resource>  
  15.     </resources>  
  16.     <testResources>  
  17.         ...  
  18.     </testResources>  
  19.     ...  
  20. </build>  

              1)resources

                    一个resources元素的列表。每一个都描述与项目关联的文件是什么和在哪里

              2)targetPath

                    指定build后的resource存放的文件夹,默认是basedir。

                    通常被打包在jar中的resources的目标路径是META-INF

             3)filtering

                    true/false,表示为这个resource,filter是否激活
             4)directory

                    定义resource文件所在的文件夹,默认为${basedir}/src/main/resources

             5)includes

                    指定哪些文件将被匹配,以*作为通配符

             6)excludes

                   指定哪些文件将被忽略

             7)testResources

                   定义和resource类似,只不过在test时使用

 

        (3)plugins配置

                  用于指定使用的插件

[html] view plain copy

  1. <build>  
  2.     ...  
  3.     <plugins>  
  4.         <plugin>  
  5.             <groupId>org.apache.maven.plugins</groupId>  
  6.             <artifactId>maven-jar-plugin</artifactId>  
  7.             <version>2.0</version>  
  8.             <extensions>false</extensions>  
  9.             <inherited>true</inherited>  
  10.             <configuration>  
  11.                 <classifier>test</classifier>  
  12.             </configuration>  
  13.             <dependencies>...</dependencies>  
  14.             <executions>...</executions>  
  15.         </plugin>  
  16.     </plugins>  
  17. </build>  

                1)GAV

                      指定插件的标准坐标

                2)extensions

                      是否加载plugin的extensions,默认为false

                3)inherited

                      true/false,这个plugin是否应用到该pom的孩子pom,默认为true

                4)configuration

                      配置该plugin期望得到的properties

                5)dependencies

                      作为plugin的依赖

                6)executions

                      plugin可以有多个目标,每一个目标都可以有一个分开的配置,可以将一个plugin绑定到不同的阶段

                      假如绑定antrun:run目标到verify阶段

[html] view plain copy

  1. <build>  
  2.     <plugins>  
  3.         <plugin>  
  4.             <artifactId>maven-antrun-plugin</artifactId>  
  5.             <version>1.1</version>  
  6.             <executions>  
  7.                 <execution>  
  8.                     <id>echodir</id>  
  9.                     <goals>  
  10.                         <goal>run</goal>  
  11.                     </goals>  
  12.                     <phase>verify</phase>  
  13.                     <inherited>false</inherited>  
  14.                     <configuration>  
  15.                         <tasks>  
  16.                             <echo>Build Dir: ${project.build.directory}</echo>  
  17.                         </tasks>  
  18.                     </configuration>  
  19.                 </execution>  
  20.             </executions>  
  21.         </plugin>  
  22.     </plugins>  
  23. </build>  

                           id:标识,用于和其他execution区分。当这个阶段执行时,它将以这个形式展示[plugin:goal execution: id]。在这里为: [antrun:run execution: echodir]
                           goals:目标列表

                          phase:目标执行的阶段

                          inherit:子类pom是否继承

                          configuration:在指定目标下的配置
 

        (4)pluginManagement配置

                   pluginManagement的配置和plugins的配置是一样的,只是用于继承,使得可以在孩子pom中使用。

                   父pom:

[html] view plain copy

  1. <build>  
  2.     ...  
  3.     <pluginManagement>  
  4.         <plugins>  
  5.             <plugin>  
  6.               <groupId>org.apache.maven.plugins</groupId>  
  7.               <artifactId>maven-jar-plugin</artifactId>  
  8.               <version>2.2</version>  
  9.                 <executions>  
  10.                     <execution>  
  11.                         <id>pre-process-classes</id>  
  12.                         <phase>compile</phase>  
  13.                         <goals>  
  14.                             <goal>jar</goal>  
  15.                         </goals>  
  16.                         <configuration>  
  17.                             <classifier>pre-process</classifier>  
  18.                         </configuration>  
  19.                     </execution>  
  20.                 </executions>  
  21.             </plugin>  
  22.         </plugins>  
  23.     </pluginManagement>  
  24.     ...  
  25. </build>  

                 则在子pom中,我们只需要配置:

[html] view plain copy

  1. <build>  
  2.     ...  
  3.     <plugins>  
  4.         <plugin>  
  5.             <groupId>org.apache.maven.plugins</groupId>  
  6.             <artifactId>maven-jar-plugin</artifactId>  
  7.         </plugin>  
  8.     </plugins>  
  9.     ...  
  10. </build>  

               这样就大大简化了pom的配置

标签: maven

`pom.xml` 文件中的 `<build>` 元素用于配置 Maven 构建生命周期、插件以及构建过程的相关设置。通过合理配置这个元素,可以自定义项目的编译、测试、打包等步骤的行为。 ### 主要组成部分: #### 1. **<plugins>** - 插件是用来扩展Maven功能的组件。你可以在这里添加各种插件来完成特定任务,比如编译源代码 (`maven-compiler-plugin`) 或者生成站点文档 (`maven-site-plugin`) 等。 示例: ```xml <build> <plugins> <!-- 编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <!-- 打包插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <archive> <manifest> <mainClass>com.example.MainApp</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> ``` #### 2. **<resources> 和 <testResources>** - 这两个标签用来指定项目资源文件夹的位置,默认分别是 `src/main/resources` 和 `src/test/resources`。如果需要改变默认路径或加入额外的资源目录,则可以在该部分进行配置。 示例: ```xml <build> <resources> <resource> <directory>src/main/config</directory> <includes> <include>*.properties</include> <include>*.xml</include> </includes> </resource> </resources> <testResources> <testResource> <directory>src/test/mock-data</directory> </testResource> </testResources> </build> ``` #### 3. **<finalName>** - 定义最终生成的JAR/WAR文件名(不包含版本号)。如果不显式声明此属性,那么输出文件将采用 `${artifactId}-${version}.jar/war` 的命名规则。 示例: ```xml <build> <finalName>my-app-name</finalName> </build> ``` #### 4. **<defaultGoal>** - 指定当仅运行 `mvn` 命令而未提供目标时所使用的默认命令。例如,如果你总是希望每次执行的时候都先清理再安装,就可以这么设: 示例: ```xml <build> <defaultGoal>clean install</defaultGoal> </build> ``` --- 以上就是对 `pom.xml` 中 `<build>` 配置的基本介绍。它允许开发者高度定制化其Maven工程的整个构建流程,并且能够很好地支持团队协作和个人工作流需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值