Maven中構建項目父子結構

     在實際開發中一個龐大的項目往往是由多個工程組成的,爲了便於管理,Maven提出工程父子結構。

     如圖所示一個大的工程名爲Thesis_parent_LHQ,打開這個文件夾發現是由3個子工程thesis_background_lhq,thesis_book_lhq,thesis_preapply_lhq組成。當從SVN檢出父工程時,三個子工程也會被檢出。

 

 

1、父工程的pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>


	<groupId>thesis</groupId>  <!-- 父工程的組ID-->
	<artifactId>thesis-parent</artifactId> <!-- 父工程的ID-->
	<version>1.1.1</version> <!-- 父工程的版本號,自己定義-->
	<packaging>pom</packaging> <!-- 因爲是引用這種父子結構的所以和以往大大不同,不是用war,也不是用jar而是用pom-->

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!--定義使用UTF-8-->
	</properties>


	<modules><!-- 這個父工程下包含了下面3個子工程 -->
		<module>thesis_background_lhq</module>
		<module>thesis_book_lhq</module>
		<module>thesis_preapply_lhq</module>
	</modules>

<build><!-- 如果沒有這段代碼,程序也能運行,但是發佈到Tomcat服務器上面的時候只有.class文件沒有.java文件 -->
		<finalName>thesis-parent</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.5</source>
					<target>1.5</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<version>2.5</version>
				<configuration>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
		</plugins>
		<resources>
			<resource>
				<directory>src/main/java</directory>
			</resource>
			<resource>
				<directory>src/test/java</directory>
			</resource>
		</resources>
	</build>

   <!-- Junit測試和log4j寫在父工程中,子工程繼承父工程,默認加載這2個jar包 -->
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.10</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
			<scope>compile</scope>
		</dependency>
	</dependencies>
</project>


2、子工程thesis_background_lhq的pom.xml文件

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent><!-- 父工程的組ID 和ID -->
		<groupId>thesis</groupId>
		<artifactId>thesis-parent</artifactId>
		<version>1.1.1</version>
	</parent>
    <!-- 子工程不設置組ID,因爲默認就在父工程的組ID -->
	<artifactId>thesis_background_lhq</artifactId>
	<version>1.1</version>
	<packaging>war</packaging><!-- 子工程是一個可以獨立運行的,格式是war -->
	<name>thesis_background_lhq Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<build>
		<finalName>thesis_background_lhq</finalName><!-- 工程名 -->
	</build>
	<dependencies>
		<!-- struts包 -->
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.3.4</version>
                  </dependency>

      ................

        </dependencies>
</project>

3、thesis_book_lhq,thesis_preapply_lhq的pom.xml配置文件和thesis_background_lhq的pom.xml文件相似,以此類推。
發佈了38 篇原創文章 · 獲贊 119 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章