1.Maven聚合
我們在平時的開發中,項目往往會被劃分為好幾個模塊,比如common公共模塊、system系統模塊、log日志模塊、reports統計模塊、monitor監控模塊等等。這時我們肯定會出現這麼一個需要,我們需要一次構件多個模塊,而不用每個模塊都去mvn clean install一次,Maven聚合就是用來實現這個需求的。
我們需要構建另外一個模塊,假設是UidpWeb,然後通過該模塊來構件整個項目的所有模塊,POM結構如下:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 5 <groupId>com.uidp</groupId> 6 <artifactId>UidpWeb</artifactId> 7 <packaging>pom</packaging> 8 <version>0.0.1-SNAPSHOT</version> 9 <name>UidpWeb</name> 10 <url>http://maven.apache.org</url> 11 12 <modules> 13 14 <!-- parent pom --> 15 <module>parent</module> 16 17 <!-- 公共資源文件,第三方js庫,圖片等媒體資源 --> 18 <module>ThirdParty</module> 19 20 <!-- 公共依賴,工具包 --> 21 <module>common</module> 22 23 <!-- 日志模塊 --> 24 <module>log/pom-pack.xml</module> 25 26 <!-- 第三方客戶定制服務模塊 --> 27 <module>biz/pom-pack.xml</module> 28 29 <!-- 客戶管理 --> 30 <module>customer/pom-pack.xml</module> 31 32 <!-- 策略模塊--> 33 <module>strategy/pom-pack.xml</module> 34 35 <!-- 統計報表 --> 36 <module>reports/pom-pack.xml</module> 37 38 <!-- 監控統計 --> 39 <module>monitor/pom-pack.xml</module> 40 41 <!-- 系統管理模塊 --> 42 <module>sysmgr/pom-pack.xml</module> 43 44 <!-- 統一打war包 --> 45 <module>/pom-app.xml</module> 46 47 </modules> 48 49 </project>
注釋:
<packaging>pom</packaging>:對於聚合模塊UidpWeb來說,packaging的類型必須為pom,否則無法構建。
<name>UidpWeb</name>:提供一個更容易閱讀的名稱,沒什麼其他太大作用。
<modules>......</modules>:這是實現聚合的標簽,其下可包含多個module元素。
<module>...</module>:用來指定實現聚合的模塊或者實現聚合的POM。
為了方便用戶構建項目,通常將聚合模塊放在項目目錄的最頂層,其他模塊則作為聚合模塊的子目錄存在,這時聚合的時候便可如我這般指定路徑:
<module>parent</module>
<module>log/pom-pack.xml</module>
這就表示聚合模塊下面的parent目錄,聚合模塊下面的log目錄下的pom-pack.xml。
聚合模塊下的內容只需要POM文件,它不像其他模塊那樣有src/main/java、src/test/java等目錄。他只是用來幫助聚合其他模塊構建,他本身並不會有過多的實質內容。
關於目錄結構要注意的是,聚合模塊既可以作為其他模塊的父目錄,也可以與其他模塊處於平行的目錄,如圖:
如果使用平行目錄,聚合模塊的POM要做相應的修改,以指向正確的模塊目錄:
<module>../parent</module>
<module>../log/pom-pack.xml</module>
最後運行mvn clean install命令,Maven會分析聚合模塊的POM、分析要構建的模塊、並計算出一個反應堆構建順序,然後根據這個順序依次構建各個模塊,這樣便可以一次性構建所有聚合的模塊。
2.Maven繼承
如果多個模塊出現相同的依賴包,這樣在pom.xml文件的內容出現了冗余、重復的內容,解決這個問題其實使用Maven的繼承機制即可,就像Java的繼承一樣,父類就像一個模板,子類繼承自父類,那麼有些通用的方法、變量都不必在子類中再重復聲明了。Maven的繼承機制類似,在一個父級別的Maven的pom文件中定義了相關的常量、依賴、插件等等配置後,實際項目模塊可以繼承此父項目 的pom文件,重復的項不必顯示的再聲明一遍了,相當於父Maven項目就是個模板,等著其他子模塊去繼承。不過父Maven項目要高度抽象,高度提取公共的部分(交集),做到一處聲明,多處使用。
與聚合一樣我們需要構建另外一個模塊,假設是parent,在聚合模塊UidpWeb下面創建parent模塊,然後通過該模塊來作為所有模塊的父POM,POM結構如下:
1 <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"> 2 <modelVersion>4.0.0</modelVersion> 3 4 <groupId>com.uidp</groupId> 5 <artifactId>UidpParent</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 <packaging>pom</packaging> 8 9 <name>UidpParent</name> 10 <url>http://maven.apache.org</url> 11 12 <properties> 13 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 14 15 <repository-url>http://192.168.0.70:8081/content/groups/public/</repository-url> 16 17 <maven-compiler-plugin.version>3.1</maven-compiler-plugin.version> 18 <maven-war-plugin.version>2.4</maven-war-plugin.version> 19 <maven-javadoc-plugin.version>2.9.1</maven-javadoc-plugin.version> 20 <maven-release-plugin.version>2.4.1</maven-release-plugin.version> 21 <maven-deploy-plugin.version>2.7</maven-deploy-plugin.version> 22 23 24 25 <junit.version>4.11</junit.version> 26 <oracle.version>10.2.0.4</oracle.version> 27 <springframework.version>3.2.8.RELEASE</springframework.version> 28 <mybatis.version>3.2.2</mybatis.version> 29 <mybatis-spring.version>1.2.0</mybatis-spring.version> 30 <mysql-driver.version>5.1.25</mysql-driver.version> 31 <aspectjweaver.version>1.7.3</aspectjweaver.version> 32 33 <commons-dbcp.version>1.4</commons-dbcp.version> 34 <commons-pool.version>1.5.5</commons-pool.version> 35 <commons-fileupload.version>1.2.2</commons-fileupload.version> 36 37 <log4j.version>1.2.17</log4j.version> 38 <slf4j-api.version>1.7.5</slf4j-api.version> 39 <slf4j-log4j12.version>1.7.5</slf4j-log4j12.version> 40 41 <freemarker.version>2.3.19</freemarker.version> 42 43 <jackson-core.version>2.5.0</jackson-core.version> 44 <jackson-mapper-asl.version>1.9.7</jackson-mapper-asl.version> 45 46 <javax.servlet-api.version>3.0.1</javax.servlet-api.version> 47 <jsp-api.version>2.2</jsp-api.version> 48 <kryo.version>1.04</kryo.version> 49 <snakeyaml.version>1.8</snakeyaml.version> 50 <jedis.version>2.0.0</jedis.version> 51 <commons-lang.version>2.6</commons-lang.version> 52 53 54 <mockito-core.version>1.8.5</mockito-core.version> 55 <powermock-core.version>1.4.9</powermock-core.version> 56 <powermock-api-mockito.version>1.4.9</powermock-api-mockito.version> 57 <powermock-module-junit4.version>1.4.9</powermock-module-junit4.version> 58 59 60 </properties> 61 62 <dependencyManagement> 63 <dependencies> 64 65 <dependency> 66 <groupId>junit</groupId> 67 <artifactId>junit</artifactId> 68 <version>${junit.version}</version> 69 <scope>test</scope> 70 </dependency> 71 72 <!-- spring jar begin --> 73 <dependency> 74 <groupId>org.springframework</groupId> 75 <artifactId>spring-web</artifactId> 76 <version>${springframework.version}</version> 77 </dependency> 78 79 <dependency> 80 <groupId>org.springframework</groupId> 81 <artifactId>spring-webmvc</artifactId> 82 <version>${springframework.version}</version> 83 </dependency> 84 85 <dependency> 86 <groupId>org.springframework</groupId> 87 <artifactId>spring-beans</artifactId> 88 <version>${springframework.version}</version> 89 </dependency> 90 91 <dependency> 92 <groupId>org.springframework</groupId> 93 <artifactId>spring-context</artifactId> 94 <version>${springframework.version}</version> 95 </dependency> 96 97 <dependency> 98 <groupId>org.springframework</groupId> 99 <artifactId>spring-context-support</artifactId> 100 <version>${springframework.version}</version> 101 </dependency> 102 103 <dependency> 104 <groupId>org.springframework</groupId> 105 <artifactId>spring-core</artifactId> 106 <version>${springframework.version}</version> 107 </dependency> 108 109 <dependency> 110 <groupId>org.springframework</groupId> 111 <artifactId>spring-jdbc</artifactId> 112 <version>${springframework.version}</version> 113 </dependency> 114 115 <dependency> 116 <groupId>org.springframework</groupId> 117 <artifactId>spring-tx</artifactId> 118 <version>${springframework.version}</version> 119 </dependency> 120 121 <dependency> 122 <groupId>org.springframework</groupId> 123 <artifactId>spring-test</artifactId> 124 <version>${springframework.version}</version> 125 </dependency> 126 127 <dependency> 128 <groupId>org.springframework</groupId> 129 <artifactId>spring-expression</artifactId> 130 <version>${springframework.version}</version> 131 </dependency> 132 133 <dependency> 134 <groupId>org.springframework</groupId> 135 <artifactId>spring-aop</artifactId> 136 <version>${springframework.version}</version> 137 </dependency> 138 <!-- spring jar end --> 139 140 <dependency> 141 <groupId>org.mybatis</groupId> 142 <artifactId>mybatis</artifactId> 143 <version>${mybatis.version}</version> 144 </dependency> 145 146 <dependency> 147 <groupId>org.mybatis</groupId> 148 <artifactId>mybatis-spring</artifactId> 149 <version>${mybatis-spring.version}</version> 150 </dependency> 151 152 <dependency> 153 <groupId>mysql</groupId> 154 <artifactId>mysql-connector-java</artifactId> 155 <version>${mysql-driver.version}</version> 156 </dependency> 157 158 <dependency> 159 <groupId>com.oracle</groupId> 160 <artifactId>ojdbc14</artifactId> 161 <version>${oracle.version}</version> 162 </dependency> 163 164 <dependency> 165 <groupId>org.aspectj</groupId> 166 <artifactId>aspectjweaver</artifactId> 167 <version>${aspectjweaver.version}</version> 168 </dependency> 169 170 171 <dependency> 172 <groupId>commons-dbcp</groupId> 173 <artifactId>commons-dbcp</artifactId> 174 <version>${commons-dbcp.version}</version> 175 </dependency> 176 <dependency> 177 <groupId>commons-pool</groupId> 178 <artifactId>commons-pool</artifactId> 179 <version>${commons-pool.version}</version> 180 </dependency> 181 <dependency> 182 <groupId>commons-fileupload</groupId> 183 <artifactId>commons-fileupload</artifactId> 184 <version>${commons-fileupload.version}</version> 185 </dependency> 186 187 188 <!-- log jar --> 189 <dependency> 190 <groupId>log4j</groupId> 191 <artifactId>log4j</artifactId> 192 <version>${log4j.version}</version> 193 </dependency> 194 <dependency> 195 <groupId>org.slf4j</groupId> 196 <artifactId>slf4j-api</artifactId> 197 <version>${slf4j-api.version}</version> 198 </dependency> 199 <dependency> 200 <groupId>org.slf4j</groupId> 201 <artifactId>slf4j-log4j12</artifactId> 202 <version>${slf4j-log4j12.version}</version> 203 </dependency> 204 205 <!-- freemarker --> 206 <dependency> 207 <groupId>org.freemarker</groupId> 208 <artifactId>freemarker</artifactId> 209 <version>${freemarker.version}</version> 210 </dependency> 211 212 213 <!-- jackson --> 214 <dependency> 215 <groupId>com.fasterxml.jackson.core</groupId> 216 <artifactId>jackson-core</artifactId> 217 <version>${jackson-core.version}</version> 218 </dependency> 219 <dependency> 220 <groupId>org.codehaus.jackson</groupId> 221 <artifactId>jackson-mapper-asl</artifactId> 222 <version>${jackson-mapper-asl.version}</version> 223 </dependency> 224 225 <dependency> 226 <groupId>javax.servlet</groupId> 227 <artifactId>javax.servlet-api</artifactId> 228 <version>${javax.servlet-api.version}</version> 229 <scope>provided</scope> 230 </dependency> 231 232 <dependency> 233 <groupId>javax.servlet.jsp</groupId> 234 <artifactId>jsp-api</artifactId> 235 <version>${jsp-api.version}</version> 236 <scope>provided</scope> 237 </dependency> 238 239 <dependency> 240 <groupId>com.googlecode</groupId> 241 <artifactId>kryo</artifactId> 242 <version>${kryo.version}</version> 243 </dependency> 244 245 <dependency> 246 <groupId>org.yaml</groupId> 247 <artifactId>snakeyaml</artifactId> 248 <version>${snakeyaml.version}</version> 249 </dependency> 250 251 <dependency> 252 <groupId>redis.clients</groupId> 253 <artifactId>jedis</artifactId> 254 <version>${jedis.version}</version> 255 </dependency> 256 257 <dependency> 258 <groupId>commons-lang</groupId> 259 <artifactId>commons-lang</artifactId> 260 <version>${commons-lang.version}</version> 261 </dependency> 262 263 264 <dependency> 265 <groupId>org.mockito</groupId> 266 <artifactId>mockito-core</artifactId> 267 <version>${mockito-core.version}</version> 268 <scope>test</scope> 269 </dependency> 270 271 <dependency> 272 <groupId>org.powermock</groupId> 273 <artifactId>powermock-core</artifactId> 274 <version>${powermock-core.version}</version> 275 <scope>test</scope> 276 </dependency> 277 278 <dependency> 279 <groupId>org.powermock</groupId> 280 <artifactId>powermock-api-mockito</artifactId> 281 <version>${powermock-api-mockito.version}</version> 282 <scope>test</scope> 283 </dependency> 284 285 <dependency> 286 <groupId>org.powermock</groupId> 287 <artifactId>powermock-module-junit4</artifactId> 288 <version>${powermock-module-junit4.version}</version> 289 <scope>test</scope> 290 </dependency> 291 292 293 </dependencies> 294 </dependencyManagement> 295 296 <distributionManagement> 297 <repository> 298 <id>releases</id> 299 <name>public</name> 300 <url>http://59.50.95.66:8081/nexus/content/repositories/releases</url> 301 </repository> 302 <snapshotRepository> 303 <id>snapshots</id> 304 <name>Snapshots</name> 305 <url>http://59.50.95.66:8081/nexus/content/repositories/snapshots</url> 306 </snapshotRepository> 307 </distributionManagement> 308 309 310 311 <build> 312 <plugins> 313 314 <plugin> 315 <groupId>org.apache.maven.plugins</groupId> 316 <artifactId>maven-compiler-plugin</artifactId> 317 <version>${maven-compiler-plugin.version}</version> 318 <configuration> 319 <source>1.7</source> <!-- 源代碼使用的開發版本 --> 320 <target>1.7</target> <!-- 需要生成的目標class文件的編譯版本 --> 321 </configuration> 322 </plugin> 323 324 <plugin> 325 <groupId>org.apache.maven.plugins</groupId> 326 <artifactId>maven-javadoc-plugin</artifactId> 327 <version>${maven-javadoc-plugin.version}</version> 328 </plugin> 329 330 331 <plugin> 332 <groupId>org.apache.maven.plugins</groupId> 333 <artifactId>maven-release-plugin</artifactId> 334 <version>${maven-release-plugin.version}</version> 335 </plugin> 336 337 <plugin> 338 <groupId>org.apache.maven.plugins</groupId> 339 <artifactId>maven-deploy-plugin</artifactId> 340 <version>${maven-deploy-plugin.version}</version> 341 <configuration> 342 <updateReleaseInfo>true</updateReleaseInfo> 343 </configuration> 344 </plugin> 345 346 347 348 </plugins> 349 </build> 350 351 352 <pluginRepositories> 353 <pluginRepository> 354 <id>nexus</id> 355 <name>nexus</name> 356 <url>http://192.168.0.70:8081/content/groups/public/</url> 357 <releases> 358 <enabled>true</enabled> 359 </releases> 360 <snapshots> 361 <enabled>true</enabled> 362 </snapshots> 363 </pluginRepository> 364 </pluginRepositories> 365 366 367 </project>
需要注意的是,他的packaging和聚合一樣為pom,作為父模塊的pom,其打包類型必須為pom。父模塊只是為了幫助消除配置的重復,因此他本身不包含除POM的項目文件,也就不需要src/main/java之類的文件夾了。
有了父模塊,就需要讓其他模塊來繼承它,我們來看個實際的例子:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 5 <parent> 6 <groupId>com.uidp</groupId> 7 <artifactId>UidpParent</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 </parent> 10 11 <groupId>com.uidp</groupId> 12 <artifactId>log</artifactId> 13 <packaging>war</packaging> 14 <version>0.0.1-SNAPSHOT</version> 15 <name>log</name> 16 <url>http://maven.apache.org</url> 17 18 <dependencies> 19 20 <dependency> 21 <artifactId>ThirdParty</artifactId> 22 <version>0.0.1-SNAPSHOT</version> 23 <groupId>${project.parent.groupId}</groupId> 24 <type>war</type> 25 </dependency> 26 27 <dependency> 28 <groupId>com.uidp</groupId> 29 <artifactId>WebCommon</artifactId> 30 <version>0.0.1-SNAPSHOT</version> 31 </dependency> 32 33 <dependency> 34 <groupId>junit</groupId> 35 <artifactId>junit</artifactId> 36 <scope>test</scope> 37 </dependency> 38 39 <dependency> 40 <groupId>org.aspectj</groupId> 41 <artifactId>aspectjweaver</artifactId> 42 </dependency> 43 44 <!-- spring jar begin --> 45 <dependency> 46 <groupId>org.springframework</groupId> 47 <artifactId>spring-web</artifactId> 48 </dependency> 49 <dependency> 50 <groupId>org.springframework</groupId> 51 <artifactId>spring-webmvc</artifactId> 52 </dependency> 53 <dependency> 54 <groupId>org.springframework</groupId> 55 <artifactId>spring-beans</artifactId> 56 </dependency> 57 <dependency> 58 <groupId>org.springframework</groupId> 59 <artifactId>spring-context</artifactId> 60 </dependency> 61 <dependency> 62 <groupId>org.springframework</groupId> 63 <artifactId>spring-context-support</artifactId> 64 </dependency> 65 <dependency> 66 <groupId>org.springframework</groupId> 67 <artifactId>spring-core</artifactId> 68 </dependency> 69 <dependency> 70 <groupId>org.springframework</groupId> 71 <artifactId>spring-jdbc</artifactId> 72 </dependency> 73 <dependency> 74 <groupId>org.springframework</groupId> 75 <artifactId>spring-tx</artifactId> 76 </dependency> 77 <dependency> 78 <groupId>org.springframework</groupId> 79 <artifactId>spring-test</artifactId> 80 </dependency> 81 <dependency> 82 <groupId>org.springframework</groupId> 83 <artifactId>spring-expression</artifactId> 84 </dependency> 85 <dependency> 86 <groupId>org.springframework</groupId> 87 <artifactId>spring-aop</artifactId> 88 </dependency> 89 <!-- spring jar end --> 90 91 <!-- Quartz 框架 --> 92 <dependency> 93 <groupId>org.quartz-scheduler</groupId> 94 <artifactId>quartz</artifactId> 95 <version>1.8.6</version> 96 </dependency> 97 98 <!-- mybatis db --> 99 <dependency> 100 <groupId>org.mybatis</groupId> 101 <artifactId>mybatis</artifactId> 102 </dependency> 103 104 <dependency> 105 <groupId>org.mybatis</groupId> 106 <artifactId>mybatis-spring</artifactId> 107 </dependency> 108 109 <!-- mysql驅動依賴包 --> 110 <dependency> 111 <groupId>mysql</groupId> 112 <artifactId>mysql-connector-java</artifactId> 113 </dependency> 114 115 <!-- 數據庫連接池 jar --> 116 <dependency> 117 <groupId>commons-dbcp</groupId> 118 <artifactId>commons-dbcp</artifactId> 119 </dependency> 120 <dependency> 121 <groupId>commons-pool</groupId> 122 <artifactId>commons-pool</artifactId> 123 </dependency> 124 <dependency> 125 <groupId>commons-fileupload</groupId> 126 <artifactId>commons-fileupload</artifactId> 127 </dependency> 128 129 <!-- jackson --> 130 <dependency> 131 <groupId>com.fasterxml.jackson.core</groupId> 132 <artifactId>jackson-core</artifactId> 133 </dependency> 134 <dependency> 135 <groupId>org.codehaus.jackson</groupId> 136 <artifactId>jackson-mapper-asl</artifactId> 137 </dependency> 138 139 <!-- log jar --> 140 <dependency> 141 <groupId>log4j</groupId> 142 <artifactId>log4j</artifactId> 143 </dependency> 144 <dependency> 145 <groupId>org.slf4j</groupId> 146 <artifactId>slf4j-api</artifactId> 147 </dependency> 148 <dependency> 149 <groupId>org.slf4j</groupId> 150 <artifactId>slf4j-log4j12</artifactId> 151 </dependency> 152 153 <!-- freemarker --> 154 <dependency> 155 <groupId>org.freemarker</groupId> 156 <artifactId>freemarker</artifactId> 157 </dependency> 158 159 <dependency> 160 <groupId>com.oracle</groupId> 161 <artifactId>ojdbc14</artifactId> 162 </dependency> 163 164 <!--因為api是打jar包的,所以這裡用compile,如果是正常情況打war包,用private--> 165 <dependency> 166 <groupId>javax.servlet</groupId> 167 <artifactId>javax.servlet-api</artifactId> 168 <scope>compile</scope> 169 </dependency> 170 171 <dependency> 172 <groupId>javax.servlet.jsp</groupId> 173 <artifactId>jsp-api</artifactId> 174 <scope>compile</scope> 175 </dependency> 176 177 <dependency> 178 <groupId>commons-lang</groupId> 179 <artifactId>commons-lang</artifactId> 180 <version>2.6</version> 181 </dependency> 182 183 </dependencies> 184 <build> 185 <plugins> 186 187 <plugin> 188 <groupId>org.mortbay.jetty</groupId> 189 <artifactId>jetty-maven-plugin</artifactId> 190 <version>7.2.2.v20101205</version> 191 <configuration> 192 <stopKey>foo</stopKey> 193 <stopPort>9999</stopPort> 194 <webAppConfig> 195 <contextPath>/</contextPath> 196 </webAppConfig> 197 <!-- 指定額外需要監控變化的文件或文件夾,主要用於熱部署中的識別文件更新 --> 198 <scanTargetPatterns> 199 <scanTargetPattern> 200 <directory>src</directory> 201 <!-- <includes> <include>**/*.java</include> <include>**/*.properties</include> 202 </includes> --> 203 <!-- <excludes> <exclude>**/*.xml</exclude> <exclude>**/myspecial.properties</exclude> 204 </excludes> --> 205 </scanTargetPattern> 206 </scanTargetPatterns> 207 <scanIntervalSeconds>1</scanIntervalSeconds> 208 <webAppSourceDirectory>${basedir}/src/main/webapp</webAppSourceDirectory><!-- 209 指定web頁面的文件夾 --> 210 </configuration> 211 </plugin> 212 213 <plugin> 214 <groupId>org.apache.maven.plugins</groupId> 215 <artifactId>maven-war-plugin</artifactId> 216 <version>${maven-war-plugin.version}</version> 217 <configuration> 218 <failOnMissingWebXml>false</failOnMissingWebXml> 219 <warName>${project.artifactId}</warName> 220 </configuration> 221 </plugin> 222 </plugins> 223 </build> 224 </project>
上述POM中使用parent元素聲明父模塊,parent下的子元素groupId、artifactId和version指定了父模塊的坐標,這三個元素是必須的。relativePath元素是可選的,我這裡用的默認值所以並沒有配置,relativePath元素的默認值為../pom.xml,也就是說,Maven默認父POM在上一層目錄下。當項目構建時,Maven會首先根據relativePath檢查父POM,如果找不到,再從本地倉庫找。所以像我這裡如果本地倉庫中也沒有父POM的話構建就會失敗了,所以最好的做法是配置relativePath為../parent/pom.xml表示父POM的位置與在log/目錄平行的parent/下。這樣就算本地倉庫沒有父POM構建的時候也不會報錯了。
大家有木有發現log的groupId和version元素是與父POM一樣的,所以這裡其實是可以省略不要的,但是如果log有自己專門的groupId和version,那麼就顯示的配置一個就好了。對於artifactId元素來說,子模塊應該顯式的聲明,避免造成坐標沖突和混淆。
對於依賴的繼承,我們可以把依賴放入<dependencyManagement>元素當中,這樣的依賴就成了可選的,我們只要在項目中繼承我們所需的依賴即可,比如:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency>
我們不需要進行版本的聲明,這樣就可以根據自己的需要引入需要的包,而不會繼承全部的包。
對於每個項目都需要繼承的依賴則不放在<dependencyManagement>元素當中,不過個人覺得沒什麼必要,都放在<dependencyManagement>元素當中,需要的時候引入一下就好了。
插件的繼承與依賴的繼承是類似的,這裡不過多的說明,插件也有一個<pluginManagement></pluginManagement>元素,放在下面的插件也表示為可選的。
在使用父POM的時候也要在聚合模塊中加入:
<module>parent</module>
應該是放在最前面,大家可以看看上面的聚合POM。
3.可繼承的POM元素
groupId和version是可以被繼承的,那麼還有哪些POM元素可以被繼承呢?以下是一個完整的列表,並附帶了簡單的說明:
4.聚合與繼承的關系