Maven build標籤的使用

前言:

   <build >設置,主要用於編譯設置
 

1.分類

在Maven的pom.xml文件中,存在如下兩種<build>:

(1)全局配置(project build)

         針對整個項目的所有情況都有效

(2)配置(profile build)

           針對不同的profile配置

複製代碼

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
  ...  
  <!-- "Project Build" contains elements of the BaseBuild set and the Build set-->  
  <build>...</build>  
   
  <profiles>  
    <profile>  
      <!-- "Profile Build" contains elements of the BaseBuild set only -->  
      <build>...</build>  
    </profile>  
  </profiles>  
</project>  

複製代碼

說明:

一種<build>被稱爲Project Build,即是<project>的直接子元素。

另一種<build>被稱爲Profile Build,即是<profile>的直接子元素。

Profile Build包含了基本的build元素,而Project Build還包含兩個特殊的元素,即各種<...Directory>和<extensions>。

2. 配置說明

1.基本元素

示例如下

複製代碼

<build>  
  <defaultGoal>install</defaultGoal>  
  <directory>${basedir}/target</directory>  
  <finalName>${artifactId}-${version}</finalName> 
  <filters>
   <filter>filters/filter1.properties</filter>
  </filters> 
  ...
</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配置

                 用於包含或者排除某些資源文件

    

複製代碼

<build>  
        ...  
       <resources>  
          <resource>  
             <targetPath>META-INF/plexus</targetPath>  
             <filtering>true</filtering>  
            <directory>${basedir}/src/main/plexus</directory>  
            <includes>  
                <include>configuration.xml</include>  
            </includes>  
            <excludes>  
                <exclude>**/*.properties</exclude>  
            </excludes>  
         </resource>  
    </resources>  
    <testResources>  
        ...  
    </testResources>  
    ...  
</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配置

                  用於指定使用的插件

       

複製代碼

<build>  
    ...  
    <plugins>  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-jar-plugin</artifactId>  
            <version>2.0</version>  
            <extensions>false</extensions>  
            <inherited>true</inherited>  
            <configuration>  
                <classifier>test</classifier>  
            </configuration>  
            <dependencies>...</dependencies>  
            <executions>...</executions>  
        </plugin>  
    </plugins>  
</build>  

複製代碼

  4  pluginManagement配置

          pluginManagement的配置和plugins的配置是一樣的,只是用於繼承,使得可以在孩子pom中使用。

       父pom:

複製代碼

<build>  
    ...  
    <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>compile</phase>  
                        <goals>  
                            <goal>jar</goal>  
                        </goals>  
                        <configuration>  
                            <classifier>pre-process</classifier>  
                        </configuration>  
                    </execution>  
                </executions>  
            </plugin>  
        </plugins>  
    </pluginManagement>  
    ...  
</build>  

複製代碼

  則在子pom中,我們只需要配置:

複製代碼

<build>  
    ...  
    <plugins>  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-jar-plugin</artifactId>  
        </plugin>  
    </plugins>  
    ...  
</build>  

複製代碼

 這樣大大簡化了孩子pom的配置

轉載:https://www.cnblogs.com/whx7762/p/7911890.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章