maven構建項目,分包結構

原文來自:https://blog.csdn.net/lemontreey/article/details/54293755

先說項目使用Maven的好處

1、項目構建。Maven定義了軟件開發的整套流程體系,並進行了封裝,開發人員只需要指定項目的構建流程,無需針對每個流程編寫自己的構建腳本。

2、依賴管理。除了項目構建,Maven最核心的功能是軟件包的依賴管理,能夠自動分析項目所需要的依賴軟件包,併到Maven中心倉庫去下載。

A)管理依賴的jar包

B)管理工程之間的依賴關係。


傳統工程結構


 



Maven管理的工程結構:

不使用maven:工程部署時需要手動複製jar包。完成工程構建。非常繁瑣。

使用maven進行工程構建:

使用maven可以實現一步構建。




1.創建springmvc-parent


1.1創建maven工程




1.1.2修改pom文件


[html] view plain copy
  1. <!-- 集中定義依賴版本號 -->  
  2.     <properties>  
  3.         <junit.version>4.12</junit.version>  
  4.         <spring.version>4.1.3.RELEASE</spring.version>  
  5.         <mybatis.version>3.2.8</mybatis.version>  
  6.         <mybatis.spring.version>1.2.2</mybatis.spring.version>  
  7.         <mybatis.paginator.version>1.2.15</mybatis.paginator.version>  
  8.         <mysql.version>5.1.32</mysql.version>  
  9.         <slf4j.version>1.6.4</slf4j.version>  
  10.         <jackson.version>2.4.2</jackson.version>  
  11.         <druid.version>1.0.9</druid.version>  
  12.         <httpclient.version>4.3.5</httpclient.version>  
  13.         <jstl.version>1.2</jstl.version>  
  14.         <servlet-api.version>2.5</servlet-api.version>  
  15.         <jsp-api.version>2.0</jsp-api.version>  
  16.         <joda-time.version>2.5</joda-time.version>  
  17.         <commons-lang3.version>3.3.2</commons-lang3.version>  
  18.         <commons-io.version>1.3.2</commons-io.version>  
  19.         <commons-net.version>3.3</commons-net.version>  
  20.         <pagehelper.version>3.4.2-fix</pagehelper.version>  
  21.         <jsqlparser.version>0.9.1</jsqlparser.version>  
  22.         <commons-fileupload.version>1.3.1</commons-fileupload.version>  
  23.         <jedis.version>2.7.2</jedis.version>  
  24.         <solrj.version>4.10.3</solrj.version>  
  25.     </properties>  
  26. <!-- 只定義以來的版本,並不實際依賴-->  
  27.   <dependencyManagement>  
  28.         <dependencies>  
  29.             <!-- 時間操作組件 -->  
  30.             <dependency>  
  31.                 <groupId>joda-time</groupId>  
  32.                 <artifactId>joda-time</artifactId>  
  33.                 <version>${joda-time.version}</version>  
  34.             </dependency>  
  35.             <!-- Apache工具組件 -->  
  36.             <dependency>  
  37.                 <groupId>org.apache.commons</groupId>  
  38.                 <artifactId>commons-lang3</artifactId>  
  39.                 <version>${commons-lang3.version}</version>  
  40.             </dependency>  
  41.             <dependency>  
  42.                 <groupId>org.apache.commons</groupId>  
  43.                 <artifactId>commons-io</artifactId>  
  44.                 <version>${commons-io.version}</version>  
  45.             </dependency>  
  46.             <dependency>  
  47.                 <groupId>commons-net</groupId>  
  48.                 <artifactId>commons-net</artifactId>  
  49.                 <version>${commons-net.version}</version>  
  50.             </dependency>  
  51.             <!-- Jackson Json處理工具包 -->  
  52.             <dependency>  
  53.                 <groupId>com.fasterxml.jackson.core</groupId>  
  54.                 <artifactId>jackson-databind</artifactId>  
  55.                 <version>${jackson.version}</version>  
  56.             </dependency>  
  57.             <!-- httpclient -->  
  58.             <dependency>  
  59.                 <groupId>org.apache.httpcomponents</groupId>  
  60.                 <artifactId>httpclient</artifactId>  
  61.                 <version>${httpclient.version}</version>  
  62.             </dependency>  
  63.             <!-- 單元測試 -->  
  64.             <dependency>  
  65.                 <groupId>junit</groupId>  
  66.                 <artifactId>junit</artifactId>  
  67.                 <version>${junit.version}</version>  
  68.                 <scope>test</scope>  
  69.             </dependency>  
  70.             <!-- 日誌處理 -->  
  71.             <dependency>  
  72.                 <groupId>org.slf4j</groupId>  
  73.                 <artifactId>slf4j-log4j12</artifactId>  
  74.                 <version>${slf4j.version}</version>  
  75.             </dependency>  
  76.             <!-- Mybatis -->  
  77.             <dependency>  
  78.                 <groupId>org.mybatis</groupId>  
  79.                 <artifactId>mybatis</artifactId>  
  80.                 <version>${mybatis.version}</version>  
  81.             </dependency>  
  82.             <dependency>  
  83.                 <groupId>org.mybatis</groupId>  
  84.                 <artifactId>mybatis-spring</artifactId>  
  85.                 <version>${mybatis.spring.version}</version>  
  86.             </dependency>  
  87.             <dependency>  
  88.                 <groupId>com.github.miemiedev</groupId>  
  89.                 <artifactId>mybatis-paginator</artifactId>  
  90.                 <version>${mybatis.paginator.version}</version>  
  91.             </dependency>  
  92.             <dependency>  
  93.                 <groupId>com.github.pagehelper</groupId>  
  94.                 <artifactId>pagehelper</artifactId>  
  95.                 <version>${pagehelper.version}</version>  
  96.             </dependency>  
  97.             <!-- MySql -->  
  98.             <dependency>  
  99.                 <groupId>mysql</groupId>  
  100.                 <artifactId>mysql-connector-java</artifactId>  
  101.                 <version>${mysql.version}</version>  
  102.             </dependency>  
  103.             <!-- 連接池 -->  
  104.             <dependency>  
  105.                 <groupId>com.alibaba</groupId>  
  106.                 <artifactId>druid</artifactId>  
  107.                 <version>${druid.version}</version>  
  108.             </dependency>  
  109.             <!-- Spring -->  
  110.             <dependency>  
  111.                 <groupId>org.springframework</groupId>  
  112.                 <artifactId>spring-context</artifactId>  
  113.                 <version>${spring.version}</version>  
  114.             </dependency>  
  115.             <dependency>  
  116.                 <groupId>org.springframework</groupId>  
  117.                 <artifactId>spring-beans</artifactId>  
  118.                 <version>${spring.version}</version>  
  119.             </dependency>  
  120.             <dependency>  
  121.                 <groupId>org.springframework</groupId>  
  122.                 <artifactId>spring-webmvc</artifactId>  
  123.                 <version>${spring.version}</version>  
  124.             </dependency>  
  125.             <dependency>  
  126.                 <groupId>org.springframework</groupId>  
  127.                 <artifactId>spring-jdbc</artifactId>  
  128.                 <version>${spring.version}</version>  
  129.             </dependency>  
  130.             <dependency>  
  131.                 <groupId>org.springframework</groupId>  
  132.                 <artifactId>spring-aspects</artifactId>  
  133.                 <version>${spring.version}</version>  
  134.             </dependency>  
  135.             <!-- JSP相關 -->  
  136.             <dependency>  
  137.                 <groupId>jstl</groupId>  
  138.                 <artifactId>jstl</artifactId>  
  139.                 <version>${jstl.version}</version>  
  140.             </dependency>  
  141.             <dependency>  
  142.                 <groupId>javax.servlet</groupId>  
  143.                 <artifactId>servlet-api</artifactId>  
  144.                 <version>${servlet-api.version}</version>  
  145.                 <scope>provided</scope>  
  146.             </dependency>  
  147.             <dependency>  
  148.                 <groupId>javax.servlet</groupId>  
  149.                 <artifactId>jsp-api</artifactId>  
  150.                 <version>${jsp-api.version}</version>  
  151.                 <scope>provided</scope>  
  152.             </dependency>  
  153.             <!-- 文件上傳組件 -->  
  154.             <dependency>  
  155.                 <groupId>commons-fileupload</groupId>  
  156.                 <artifactId>commons-fileupload</artifactId>  
  157.                 <version>${commons-fileupload.version}</version>  
  158.             </dependency>  
  159.             <!-- Redis客戶端 -->  
  160.             <dependency>  
  161.                 <groupId>redis.clients</groupId>  
  162.                 <artifactId>jedis</artifactId>  
  163.                 <version>${jedis.version}</version>  
  164.             </dependency>  
  165.             <!-- solr客戶端 -->  
  166.             <dependency>  
  167.                 <groupId>org.apache.solr</groupId>  
  168.                 <artifactId>solr-solrj</artifactId>  
  169.                 <version>${solrj.version}</version>  
  170.             </dependency>  
  171.         </dependencies>  
  172.     </dependencyManagement>  
  173.       
  174.     <build>  
  175.         <finalName>${project.artifactId}</finalName>  
  176.         <plugins>  
  177.             <!-- 資源文件拷貝插件,實際依賴 -->  
  178.             <plugin>  
  179.                 <groupId>org.apache.maven.plugins</groupId>  
  180.                 <artifactId>maven-resources-plugin</artifactId>  
  181.                 <version>2.7</version>  
  182.                 <configuration>  
  183.                     <encoding>UTF-8</encoding>  
  184.                 </configuration>  
  185.             </plugin>  
  186.             <!-- java編譯插件 -->  
  187.             <plugin>  
  188.                 <groupId>org.apache.maven.plugins</groupId>  
  189.                 <artifactId>maven-compiler-plugin</artifactId>  
  190.                 <version>3.2</version>  
  191.                 <configuration>  
  192.                     <source>1.7</source>  
  193.                     <target>1.7</target>  
  194.                     <encoding>UTF-8</encoding>  
  195.                 </configuration>  
  196.             </plugin>  
  197.         </plugins>  
  198.         <pluginManagement>  
  199.             <plugins>  
  200.                 <!-- 配置Tomcat插件 -->  
  201.                 <plugin>  
  202.                     <groupId>org.apache.tomcat.maven</groupId>  
  203.                     <artifactId>tomcat7-maven-plugin</artifactId>  
  204.                     <version>2.2</version>  
  205.                 </plugin>  
  206.             </plugins>  
  207.         </pluginManagement>  
  208.     </build>    


1.1.3 將spring-parent安裝到本地倉庫



2.創建spring-common


注意:我們這裏創建common項目的用途是用來放其他工程需要用到的通用組件、工具類、以及單元測試等等,可以讓整個結構體系看起來更加清晰,明確


2.1.1 創建工程(由於我之前建過這個項目所以我這裏只是演示下)





2.1.2修改pom文件


[html] view plain copy
  1. <!-- jar包的依賴 -->  
  2.     <dependencies>  
  3.         <!-- 時間操作組件 -->  
  4.         <dependency>  
  5.             <groupId>joda-time</groupId>  
  6.             <artifactId>joda-time</artifactId>  
  7.         </dependency>  
  8.         <!-- Apache工具組件 -->  
  9.         <dependency>  
  10.             <groupId>org.apache.commons</groupId>  
  11.             <artifactId>commons-lang3</artifactId>  
  12.         </dependency>  
  13.         <dependency>  
  14.             <groupId>org.apache.commons</groupId>  
  15.             <artifactId>commons-io</artifactId>  
  16.         </dependency>  
  17.         <dependency>  
  18.             <groupId>commons-net</groupId>  
  19.             <artifactId>commons-net</artifactId>  
  20.         </dependency>  
  21.         <!-- Jackson Json處理工具包 -->  
  22.         <dependency>  
  23.             <groupId>com.fasterxml.jackson.core</groupId>  
  24.             <artifactId>jackson-databind</artifactId>  
  25.         </dependency>  
  26.         <!-- httpclient -->  
  27.         <dependency>  
  28.             <groupId>org.apache.httpcomponents</groupId>  
  29.             <artifactId>httpclient</artifactId>  
  30.         </dependency>  
  31.         <!-- 單元測試 -->  
  32.         <dependency>  
  33.             <groupId>junit</groupId>  
  34.             <artifactId>junit</artifactId>  
  35.             <scope>test</scope>  
  36.         </dependency>  
  37.         <!-- 日誌處理 -->  
  38.         <dependency>  
  39.             <groupId>org.slf4j</groupId>  
  40.             <artifactId>slf4j-log4j12</artifactId>  
  41.         </dependency>  
  42.     </dependencies>  

2.1.3 更新工程

右鍵項目->Maven->Update Project Configuration

3.創建spring-manager




注意這是個pom工程

5.1修改pom文件

[html] view plain copy
  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/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <parent>  
  5.         <groupId>com.yanl</groupId>  
  6.         <artifactId>springmvc-parent</artifactId>  
  7.         <version>0.0.1-SNAPSHOT</version>  
  8.     </parent>  
  9.     <groupId>com.yanl</groupId>  
  10.     <artifactId>spring-manager</artifactId>  
  11.     <version>0.0.1-SNAPSHOT</version>  
  12.     <packaging>pom</packaging>  
  13.     <description>聚合工程,包含4個模塊三個jar模塊entity,mapper,service以及war模塊controller</description>  
  14.     <!-- 依賴管理 -->  
  15.     <dependencies>  
  16.         <dependency>  
  17.             <groupId>com.yanl</groupId>  
  18.             <artifactId>spring-common</artifactId>  
  19.             <version>0.0.1-SNAPSHOT</version>  
  20.         </dependency>  
  21.     </dependencies>  
  22.     <modules>  
  23.         <module>spring-manager-entity</module>  
  24.         <module>spring-manager-mapper</module>  
  25.         <module>spring-manager-service</module>  
  26.         <module>spring-manager-web</module>  
  27.     </modules>  
  28.     <build>  
  29.         <plugins>  
  30.             <plugin>  
  31.                 <groupId>org.apache.tomcat.maven</groupId>  
  32.                 <artifactId>tomcat7-maven-plugin</artifactId>  
  33.                 <configuration>  
  34.                     <port>8080</port>  
  35.                     <path>/</path>  
  36.                 </configuration>  
  37.             </plugin>  
  38.         </plugins>  
  39.     </build>  
  40. </project>  

4.創建spring-manager-entity


注意:這是一個在manager裏面的模塊所以右鍵spring-manager項目選擇新建項目的Maven Module







5.創建spring-manager-mapper




5.1修改pom文件

[html] view plain copy
  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/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <parent>  
  5.         <groupId>com.yanl</groupId>  
  6.         <artifactId>spring-manager</artifactId>  
  7.         <version>0.0.1-SNAPSHOT</version>  
  8.     </parent>  
  9.     <artifactId>spring-manager-mapper</artifactId>  
  10.     <!-- 依賴管理 -->  
  11.     <dependencies>  
  12.         <dependency>  
  13.             <groupId>com.yanl</groupId>  
  14.             <artifactId>spring-manager-entity</artifactId>  
  15.             <version>0.0.1-SNAPSHOT</version>  
  16.         </dependency>  
  17.         <!-- Mybatis -->  
  18.             <dependency>  
  19.                 <groupId>org.mybatis</groupId>  
  20.                 <artifactId>mybatis</artifactId>  
  21.             </dependency>  
  22.             <dependency>  
  23.                 <groupId>org.mybatis</groupId>  
  24.                 <artifactId>mybatis-spring</artifactId>  
  25.             </dependency>  
  26.             <dependency>  
  27.                 <groupId>com.github.miemiedev</groupId>  
  28.                 <artifactId>mybatis-paginator</artifactId>  
  29.             </dependency>  
  30.             <dependency>  
  31.                 <groupId>com.github.pagehelper</groupId>  
  32.                 <artifactId>pagehelper</artifactId>  
  33.             </dependency>  
  34.             <!-- MySql -->  
  35.             <dependency>  
  36.                 <groupId>mysql</groupId>  
  37.                 <artifactId>mysql-connector-java</artifactId>  
  38.             </dependency>  
  39.             <!-- 連接池 -->  
  40.             <dependency>  
  41.                 <groupId>com.alibaba</groupId>  
  42.                 <artifactId>druid</artifactId>  
  43.             </dependency>  
  44.     </dependencies>  
  45. </project>  


6.創建spring-manager-service




6.1修改pom文件

[html] view plain copy
  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/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <parent>  
  5.         <groupId>com.yanl</groupId>  
  6.         <artifactId>spring-manager</artifactId>  
  7.         <version>0.0.1-SNAPSHOT</version>  
  8.     </parent>  
  9.     <artifactId>spring-manager-service</artifactId>  
  10.     <description>service模塊</description>  
  11.     <!-- 依賴管理 -->  
  12.     <dependencies>  
  13.         <dependency>  
  14.             <groupId>com.yanl</groupId>  
  15.             <artifactId>spring-manager-mapper</artifactId>  
  16.             <version>0.0.1-SNAPSHOT</version>  
  17.         </dependency>  
  18.         <!-- Spring -->  
  19.         <dependency>  
  20.             <groupId>org.springframework</groupId>  
  21.             <artifactId>spring-context</artifactId>  
  22.         </dependency>  
  23.         <dependency>  
  24.             <groupId>org.springframework</groupId>  
  25.             <artifactId>spring-beans</artifactId>  
  26.         </dependency>  
  27.         <dependency>  
  28.             <groupId>org.springframework</groupId>  
  29.             <artifactId>spring-webmvc</artifactId>  
  30.         </dependency>  
  31.         <dependency>  
  32.             <groupId>org.springframework</groupId>  
  33.             <artifactId>spring-jdbc</artifactId>  
  34.         </dependency>  
  35.         <dependency>  
  36.             <groupId>org.springframework</groupId>  
  37.             <artifactId>spring-aspects</artifactId>  
  38.         </dependency>  
  39.     </dependencies>  
  40. </project>  





7.創建spring-manager-web(這個就是我們的前端控制器controller)




7.1修改pom文件

[html] view plain copy
  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/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <parent>  
  5.         <groupId>com.yanl</groupId>  
  6.         <artifactId>spring-manager</artifactId>  
  7.         <version>0.0.1-SNAPSHOT</version>  
  8.     </parent>  
  9.     <artifactId>spring-manager-web</artifactId>  
  10.     <packaging>war</packaging>  
  11.     <!-- 依賴管理 -->  
  12.     <dependencies>  
  13.         <dependency>  
  14.             <groupId>com.yanl</groupId>  
  15.             <artifactId>spring-manager-service</artifactId>  
  16.             <version>0.0.1-SNAPSHOT</version>  
  17.         </dependency>  
  18.         <!-- JSP相關 -->  
  19.         <dependency>  
  20.             <groupId>jstl</groupId>  
  21.             <artifactId>jstl</artifactId>  
  22.             <version>${jstl.version}</version>  
  23.         </dependency>  
  24.         <dependency>  
  25.             <groupId>javax.servlet</groupId>  
  26.             <artifactId>servlet-api</artifactId>  
  27.             <version>${servlet-api.version}</version>  
  28.             <scope>provided</scope>  
  29.         </dependency>  
  30.         <dependency>  
  31.             <groupId>javax.servlet</groupId>  
  32.             <artifactId>jsp-api</artifactId>  
  33.             <version>${jsp-api.version}</version>  
  34.             <scope>provided</scope>  
  35.         </dependency>  
  36.         <!-- 文件上傳組件 -->  
  37.         <dependency>  
  38.             <groupId>commons-fileupload</groupId>  
  39.             <artifactId>commons-fileupload</artifactId>  
  40.             <version>${commons-fileupload.version}</version>  
  41.         </dependency>  
  42.     </dependencies>  
  43. </project>  

至此我們的工程建立完成,我們需要測試這個工程能不能用還需要配置如下




8.配置tomcat插件


運行web工程需要添加一個tomcat插件。插件必須添加到spring-manager工程中。因爲spring-manager是聚合工程。在運行時需要把子工程聚合到一起才能運行。


上面在創建spring-manager時我已經配置了,這裏在配下,提醒。


[html] view plain copy
  1. <build>  
  2.         <plugins>  
  3.             <plugin>  
  4.                 <groupId>org.apache.tomcat.maven</groupId>  
  5.                 <artifactId>tomcat7-maven-plugin</artifactId>  
  6.                 <configuration>  
  7.                     <port>8080</port>  
  8.                     <path>/</path>  
  9.                 </configuration>  
  10.             </plugin>  
  11.         </plugins>  
  12.     </build>  

注意:在運行聚合工程之前需要將parent,common工程安裝至本地倉庫,不然會報找不到依賴的錯誤。
右鍵工程- Run as - mvn install

9.運行聚合工程




看到如下,說明運行成功



如果運行聚合工程報錯invalid LOC header (bad signature),說明jar沒有被maven依賴,有可能是你改了本地maven倉庫地址引起的,需要刪除之前存在的jar文件夾,讓maven重新下載。

使用maven命令運行該工程最好使用 clean tomcat7:run。

然後打開http://localhost:8080/你會看到



大功告成,說明我們的聚合工程沒有問題,這裏有幾點需要解釋下

10.1.關於mybatis分頁插件pagehelper的問題




有關分頁插件請看我之前寫過的一篇pagehelper文章。

10.2  Java編譯插件




10.3  Build時控制檯報錯,一般是提示parent,common沒有安裝,你右鍵這兩個項目安裝下在運行spring-manager就可以了。


10.4 需要用到的倉庫我上傳到我的資源裏面了(由於文件太大,我傳百度網盤了,傳送門 http://pan.baidu.com/s/1cmrIgu),如果maven下載不了直接將這個倉庫覆蓋你自己的本地倉庫,一般本地倉庫在C盤用戶.m2文件夾下,祝你成功。


10.5 工程項目在我資源頁http://download.csdn.net/detail/lemontreey/9738477,我沒有clean所以比較大。

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