1. 程式人生 > >maven建立多模組web專案

maven建立多模組web專案

實現步驟


建立專案目錄
進入“工作空間”目錄,建立名為tradework 的資料夾,切換至控制檯,進入該資料夾。

 
配置模組


生成各個模組
該步驟會依次生成專案的各個模組,但是生成的模組並沒有 建立依賴,只是最簡單的並且符合maven 要求的專案結構的模組,關於什麼是 maven 的標準專案結構,可以參考 maven 官方文件,或者《 maven 權威指南》。

 
#core 模組建立
mvn archetype:create -DgroupId=uppower.trade -DartifactId=trade-core
#client 模組建立
mvn archetype:create -DgroupId=uppower.trade -DartifactId=trade-client

#server 模組建立
mvn archetype:create -DgroupId=uppower.trade -DartifactId=trade-server -DpackageName=uppower.trade -DarchetypeArtifactId=maven-archetype-webapp


配置專案模組
在tradework 根目錄下新建一個 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>
  <parent>
  <groupId>uppower</groupId>
  <artifactId>parent</artifactId>
  <version>1.0.1</version>
 </parent>
  <groupId>uppower.trade</groupId>
  <artifactId>trade-parent</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>trade-parent</name>
  <url>http://maven.apache.org</url>
   <modules>
  <module>trade-client</module>
  <module>trade-core</module>
  <module>trade-server</module>
  </modules>
</project>

 
這裡需要注意的是modules 節點,配置了上一步生成的四個模組。這樣就組織起了其依賴關係。
生成好各個模組之後在tradework 根目錄下會生成三個目錄,每個目錄下都會有一個 pom.xml 的配置檔案,將這些配置檔案開啟。新增 parent 節點,client模組內容如下:

<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>
  <groupId>uppower.trade</groupId>
  <artifactId>trade-parent</artifactId>
  <version>1.0-SNAPSHOT</version>
 </parent>
  <groupId>uppower.trade</groupId>
  <artifactId>trade-client</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>trade-client</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>
  </dependencies>
</project> 

 
同理,針對core模組:

<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>
  <groupId>uppower.trade</groupId>
  <artifactId>trade-parent</artifactId>
  <version>1.0-SNAPSHOT</version>
 </parent>
  <groupId>uppower.trade</groupId>
  <artifactId>trade-core</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>trade-core</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>
    <dependency>
     <groupId>uppower.trade</groupId>
    <artifactId>trade-client</artifactId>
     <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project> 

 
同理,針對server模組,其中值得注意的是,server模組是打包為war包:

<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>
  <groupId>uppower.trade</groupId>
  <artifactId>trade-parent</artifactId>
  <version>1.0-SNAPSHOT</version>
 </parent>
  <groupId>uppower.trade</groupId>
  <artifactId>trade-server</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>trade-server Maven Webapp</name>
  <url>http://maven.apache.org</url>
<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
</dependency>
</dependencies>
 </project>

 
配置依賴
進入tradework 根目錄,在 pom.xml 檔案中加入 dependencies 節點,加入如下內容: 

<dependencyManagement>
   <dependencies>
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.7</version>
       <scope>test</scope>
     </dependency>
     <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring</artifactId>
   <version>2.0.7</version>
  </dependency>
  <dependency>
   <groupId>org.apache.ibatis</groupId>
   <artifactId>ibatis-sqlmap</artifactId>
   <version>2.3.0</version>
  </dependency>
  <dependency>
   <groupId>com.ibatis</groupId>
   <artifactId>ibatis-dao</artifactId>
   <version>2.1.0.565</version>
  </dependency>
  <dependency>
   <groupId>commons-dbcp</groupId>
   <artifactId>commons-dbcp</artifactId>
   <version>1.2.2</version>
  </dependency>
  
  <dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.12</version>
</dependency>
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>2.5</version>
</dependency>
<dependency>
  <groupId>apache-log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.15</version>
</dependency>
   </dependencies>
  </dependencyManagement>

  
進入tradework 根目錄下,trade-server目錄,在 pom.xml 檔案中加入 dependencies 節點,內容如下:

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>
    <dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring</artifactId>
		 
		</dependency>
		<dependency>
			<groupId>org.apache.ibatis</groupId>
			<artifactId>ibatis-sqlmap</artifactId>
		 
		</dependency>
		<dependency>
			<groupId>com.ibatis</groupId>
			<artifactId>ibatis-dao</artifactId>
		 
		</dependency>
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
	 		</dependency>
	 		
	 <dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
  <groupId>apache-log4j</groupId>
  <artifactId>log4j</artifactId>
</dependency>
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
 
</dependency>
    <dependency>
    	<groupId>uppower.trade</groupId>
  		<artifactId>trade-client</artifactId>
    	<version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
    	<groupId>uppower.trade</groupId>
  		<artifactId>trade-core</artifactId>
    	<version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>trade-server</finalName>
  </build>

修改配置完成之後,在tradework目錄下,執行:
mvn eclipse:eclipse
生成 Eclipse 專案檔案及包引用定義,注意,需確保定義Classpath Variables: M2_REPO,指向本地maven類庫目錄。

開啟Eclipse ,匯入工程即可。

                                                                                                                          2010-05-04