Maven的pom.xml文件結構中的build
在Maven的pom.xml文件中,Build相關配置包含兩個部分,一個是<build>,另一個是<reporting>,這里我們只介紹<build>。
1. 在Maven的pom.xml文件中,存在如下兩種<build>
<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. Profile Build和Project Build共用的基本build元素
1) 示例如下
<build> <defaultGoal>install</defaultGoal> <directory>${basedir}/target</directory> <finalName>${artifactId}-${version}</finalName> ... </build>
說明:
- defaultGoal,執(zhí)行構建時默認的goal或phase,如jar:jar或者package等
- directory,構建的結果所在的路徑,默認為${basedir}/target目錄
- finalName,構建的最終結果的名字,該名字可能在其他plugin中被改變
2) <resources>
資源往往不是代碼,無需編譯,而是一些properties或XML配置文件,構建過程中會往往會將資源文件從源路徑復制到指定的目標路徑。
<resources>給出各個資源在Maven項目中的具體路徑。示例如下:
<build> ... <filters> <filter>filters/filter1.properties</filter> </filters> <resources> <resource> <targetPath>META-INF/plexus</targetPath> <filtering>false</filtering> <directory>${basedir}/src/main/plexus</directory> <includes> <include>configuration.xml</include> </includes> <excludes> <exclude>**/*.properties</exclude> </excludes> </resource> </resources> <testResources> ... </testResources> ... </build>
說明:
- resources,build過程中涉及的資源文件
- targetPath,資源文件的目標路徑
- filtering,構建過程中是否對資源進行過濾,默認false
- directory,資源文件的路徑,默認位于${basedir}/src/main/resources/目錄下
- includes,一組文件名的匹配模式,被匹配的資源文件將被構建過程處理
- excludes,一組文件名的匹配模式,被匹配的資源文件將被構建過程忽略。同時被includes和excludes匹配的資源文件,將被忽略。
- filters,給出對資源文件進行過濾的屬性文件的路徑,默認位于${basedir}/src/main/filters/目錄下。屬性文件中定義若干鍵值對。在構建過程中,對于資源文件中出現(xiàn)的變量(鍵),將使用屬性文件中該鍵對應的值替換。
- testResources,test過程中涉及的資源文件,默認位于${basedir}/src/test/resources/目錄下。這里的資源文件不會被構建到目標構件中
3) <plugins>
<plugins>給出構建過程中所用到的插件。
<build> ... <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <extensions>false</extensions> <inherited>true</inherited> <configuration> <classifier>test</classifier> </configuration> <dependencies>...</dependencies> <executions>...</executions> </plugin> </plugins> </build>
說明:
- groupId
- artifactId
- version
- extensions,是否加載該插件的擴展,默認false
- inherited,該插件的configuration中的配置是否可以被(繼承該POM的其他Maven項目)繼承,默認true
- configuration,該插件所需要的特殊配置,在父子項目之間可以覆蓋或合并
- dependencies,該插件所特有的依賴類庫
- executions,該插件的某個goal(一個插件中可能包含多個goal)的執(zhí)行方式。一個execution有如下設置:
- id,唯一標識
- goals,要執(zhí)行的插件的goal(可以有多個),如<goal>run</goal>
- phase,插件的goal要嵌入到Maven的phase中執(zhí)行,如verify
- inherited,該execution是否可被子項目繼承
- configuration,該execution的其他配置參數(shù)
4) <pluginManagement>
在<build>中,<pluginManagement>與<plugins>并列,兩者之間的關系類似于<dependencyManagement>與<dependencies>之間的關系。<pluginManagement>中也配置<plugin>,其配置參數(shù)與<plugins>中的<plugin>完全一致。只是,<pluginManagement>往往出現(xiàn)在父項目中,其中配置的<plugin>往往通用于子項目。子項目中只要在<plugins>中以<plugin>聲明該插件,該插件的具體配置參數(shù)則繼承自父項目中<pluginManagement>對該插件的配置,從而避免在子項目中進行重復配置。
3. Project Build特有的<...Directory>
往往配置在父項目中,供所有父子項目使用。示例如下:
<build> <sourceDirectory>${basedir}/src/main/java</sourceDirectory> <scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory> <testSourceDirectory>${basedir}/src/test/java</testSourceDirectory> <outputDirectory>${basedir}/target/classes</outputDirectory> <testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory> ... </build> </project>
目錄可以使用絕對路徑,如示例所示。如果使用相對路徑,則所有的相對路徑都是在${basedir}目錄下。
4. Project Build特有的<extensions>
<extensions>是執(zhí)行構建過程中可能用到的其他工具,在執(zhí)行構建的過程中被加入到classpath中。
也可以通過<extensions>激活構建插件,從而改變構建的過程。
通常,通過<extensions>給出通用插件的一個具體實現(xiàn),用于構建過程。
<extensions>的使用示例如下:
? <build> ? ? ... ? ? <extensions> ? ? ? <extension> ? ? ? ? <groupId>org.apache.maven.wagon</groupId> ? ? ? ? <artifactId>wagon-ftp</artifactId> ? ? ? ? <version>1.0-alpha-3</version> ? ? ? </extension> ? ? </extensions> ? ? ... ? </build> </project>
到此這篇關于Maven的pom.xml文件結構中的build的文章就介紹到這了,更多相關Maven pom.xml build內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java多線程之線程,進程和Synchronized概念初解
這篇文章主要介紹了java多線程之線程,進程和Synchronized概念初解,涉及進程與線程的簡單概念,實現(xiàn)多線程的方式,線程安全問題,synchronized修飾符等相關內(nèi)容,具有一定借鑒價值,需要的朋友可以參考下。2017-11-11Java中Lombok @Value注解導致的variable not been initialized問題
本文主要介紹了Java中Lombok @Value注解導致的variable not been initialized問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07一篇文章帶了解如何用SpringBoot在RequestBody中優(yōu)雅的使用枚舉參數(shù)
這篇文章主要介紹了SpringBoot中RequestBodyAdvice使用枚舉參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08