使用Maven做爲後臺管理工程的構建工具,主要用到了如下功能java
依賴管理:包括jar的依賴,工程之間的依賴mysql
項目構建:實現項目的一步構建git
工程聚合、繼承、依賴github
Maven工程類型:war、jar、pomweb
父工程應該是一個pom工程。redis
在父工程中定義依賴的jar的版本信息、Maven插件的版本spring
完整的pom文件以下sql
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.taotao</groupId> 8 <artifactId>taotao-parent</artifactId> 9 <version>0.0.1-SNAPSHOT</version> 10 <packaging>pom</packaging> 11 12 <!-- 集中定義依賴版本號 --> 13 <properties> 14 <junit.version>4.12</junit.version> 15 <spring.version>4.1.3.RELEASE</spring.version> 16 <mybatis.version>3.3.0</mybatis.version> 17 <mybatis.spring.version>1.2.3</mybatis.spring.version> 18 <mybatis.paginator.version>1.2.15</mybatis.paginator.version> 19 <mysql.version>5.1.32</mysql.version> 20 <slf4j.version>1.6.4</slf4j.version> 21 <jackson.version>2.4.2</jackson.version> 22 <druid.version>1.0.9</druid.version> 23 <httpclient.version>4.3.5</httpclient.version> 24 <jstl.version>1.2</jstl.version> 25 <servlet-api.version>2.5</servlet-api.version> 26 <jsp-api.version>2.0</jsp-api.version> 27 <joda-time.version>2.5</joda-time.version> 28 <commons-lang3.version>3.3.2</commons-lang3.version> 29 <commons-io.version>1.3.2</commons-io.version> 30 <commons-net.version>3.3</commons-net.version> 31 <pagehelper.version>3.4.2-fix</pagehelper.version> 32 <jsqlparser.version>0.9.1</jsqlparser.version> 33 <commons-fileupload.version>1.3.1</commons-fileupload.version> 34 <jedis.version>2.7.2</jedis.version> 35 <solrj.version>4.10.3</solrj.version> 36 </properties> 37 <!-- 只定義依賴的版本,並不實際依賴 --> 38 <dependencyManagement> 39 <dependencies> 40 <!-- 時間操做組件 --> 41 <dependency> 42 <groupId>joda-time</groupId> 43 <artifactId>joda-time</artifactId> 44 <version>${joda-time.version}</version> 45 </dependency> 46 <!-- Apache工具組件 --> 47 <dependency> 48 <groupId>org.apache.commons</groupId> 49 <artifactId>commons-lang3</artifactId> 50 <version>${commons-lang3.version}</version> 51 </dependency> 52 <dependency> 53 <groupId>org.apache.commons</groupId> 54 <artifactId>commons-io</artifactId> 55 <version>${commons-io.version}</version> 56 </dependency> 57 <!--通訊用的 ftp等--> 58 <dependency> 59 <groupId>commons-net</groupId> 60 <artifactId>commons-net</artifactId> 61 <version>${commons-net.version}</version> 62 </dependency> 63 <!-- Jackson Json處理工具包 --> 64 <dependency> 65 <groupId>com.fasterxml.jackson.core</groupId> 66 <artifactId>jackson-databind</artifactId> 67 <version>${jackson.version}</version> 68 </dependency> 69 <!-- httpclient --> 70 <dependency> 71 <groupId>org.apache.httpcomponents</groupId> 72 <artifactId>httpclient</artifactId> 73 <version>${httpclient.version}</version> 74 </dependency> 75 <!-- 單元測試 --> 76 <dependency> 77 <groupId>junit</groupId> 78 <artifactId>junit</artifactId> 79 <version>${junit.version}</version> 80 <scope>test</scope> 81 </dependency> 82 <!-- 日誌處理 --> 83 <dependency> 84 <groupId>org.slf4j</groupId> 85 <artifactId>slf4j-log4j12</artifactId> 86 <version>${slf4j.version}</version> 87 </dependency> 88 <!-- Mybatis --> 89 <dependency> 90 <groupId>org.mybatis</groupId> 91 <artifactId>mybatis</artifactId> 92 <version>${mybatis.version}</version> 93 </dependency> 94 <dependency> 95 <groupId>org.mybatis</groupId> 96 <artifactId>mybatis-spring</artifactId> 97 <version>${mybatis.spring.version}</version> 98 </dependency> 99 <dependency> 100 <groupId>com.github.miemiedev</groupId> 101 <artifactId>mybatis-paginator</artifactId> 102 <version>${mybatis.paginator.version}</version> 103 </dependency> 104 <dependency> 105 <groupId>com.github.pagehelper</groupId> 106 <artifactId>pagehelper</artifactId> 107 <version>${pagehelper.version}</version> 108 </dependency> 109 <!-- MySql --> 110 <dependency> 111 <groupId>mysql</groupId> 112 <artifactId>mysql-connector-java</artifactId> 113 <version>${mysql.version}</version> 114 </dependency> 115 <!-- 鏈接池 --> 116 <dependency> 117 <groupId>com.alibaba</groupId> 118 <artifactId>druid</artifactId> 119 <version>${druid.version}</version> 120 </dependency> 121 <!-- Spring --> 122 <dependency> 123 <groupId>org.springframework</groupId> 124 <artifactId>spring-context</artifactId> 125 <version>${spring.version}</version> 126 </dependency> 127 <dependency> 128 <groupId>org.springframework</groupId> 129 <artifactId>spring-beans</artifactId> 130 <version>${spring.version}</version> 131 </dependency> 132 <dependency> 133 <groupId>org.springframework</groupId> 134 <artifactId>spring-webmvc</artifactId> 135 <version>${spring.version}</version> 136 </dependency> 137 <dependency> 138 <groupId>org.springframework</groupId> 139 <artifactId>spring-jdbc</artifactId> 140 <version>${spring.version}</version> 141 </dependency> 142 <dependency> 143 <groupId>org.springframework</groupId> 144 <artifactId>spring-aspects</artifactId> 145 <version>${spring.version}</version> 146 </dependency> 147 <!-- JSP相關 --> 148 <dependency> 149 <groupId>jstl</groupId> 150 <artifactId>jstl</artifactId> 151 <version>${jstl.version}</version> 152 </dependency> 153 <dependency> 154 <groupId>javax.servlet</groupId> 155 <artifactId>servlet-api</artifactId> 156 <version>${servlet-api.version}</version> 157 <scope>provided</scope> 158 </dependency> 159 <dependency> 160 <groupId>javax.servlet</groupId> 161 <artifactId>jsp-api</artifactId> 162 <version>${jsp-api.version}</version> 163 <scope>provided</scope> 164 </dependency> 165 <!-- 文件上傳組件 --> 166 <dependency> 167 <groupId>commons-fileupload</groupId> 168 <artifactId>commons-fileupload</artifactId> 169 <version>${commons-fileupload.version}</version> 170 </dependency> 171 <!-- Redis客戶端 --> 172 <dependency> 173 <groupId>redis.clients</groupId> 174 <artifactId>jedis</artifactId> 175 <version>${jedis.version}</version> 176 </dependency> 177 <!-- solr客戶端 --> 178 <dependency> 179 <groupId>org.apache.solr</groupId> 180 <artifactId>solr-solrj</artifactId> 181 <version>${solrj.version}</version> 182 </dependency> 183 </dependencies> 184 </dependencyManagement> 185 <build> 186 <finalName>${project.artifactId}</finalName> 187 <plugins> 188 <!-- 資源文件拷貝插件 --> 189 <plugin> 190 <groupId>org.apache.maven.plugins</groupId> 191 <artifactId>maven-resources-plugin</artifactId> 192 <version>2.7</version> 193 <configuration> 194 <encoding>UTF-8</encoding> 195 </configuration> 196 </plugin> 197 <!-- java編譯插件 --> 198 <plugin> 199 <groupId>org.apache.maven.plugins</groupId> 200 <artifactId>maven-compiler-plugin</artifactId> 201 <version>3.2</version> 202 <configuration> 203 <source>1.7</source> 204 <target>1.7</target> 205 <encoding>UTF-8</encoding> 206 </configuration> 207 </plugin> 208 </plugins> 209 <pluginManagement> 210 <plugins> 211 <!-- 配置Tomcat插件 --> 212 <plugin> 213 <groupId>org.apache.tomcat.maven</groupId> 214 <artifactId>tomcat7-maven-plugin</artifactId> 215 <version>2.2</version> 216 </plugin> 217 </plugins> 218 </pluginManagement> 219 </build> 220 221 222 </project>
把各個模塊都會用到的一些通用功能抽取成一個個的工具類,組合成一個common工程apache
這個common工程最後打成一個jar包,其餘項目只須要依賴這個jar,就可以使用common提供的各類功能api
common工程繼承taotao-parent工程
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>taotao-parent</artifactId> 7 <groupId>com.taotao</groupId> 8 <version>0.0.1-SNAPSHOT</version> 9 <relativePath>../taotao-parent/pom.xml</relativePath> 10 </parent> 11 <modelVersion>4.0.0</modelVersion> 12 13 <groupId>com.taotao</groupId> 14 <artifactId>taotao-common</artifactId> 15 16 <!-- jar包的依賴 --> 17 <dependencies> 18 <!-- 時間操做組件 --> 19 <dependency> 20 <groupId>joda-time</groupId> 21 <artifactId>joda-time</artifactId> 22 </dependency> 23 <!-- Apache工具組件 --> 24 <dependency> 25 <groupId>org.apache.commons</groupId> 26 <artifactId>commons-lang3</artifactId> 27 </dependency> 28 <dependency> 29 <groupId>org.apache.commons</groupId> 30 <artifactId>commons-io</artifactId> 31 </dependency> 32 <dependency> 33 <groupId>commons-net</groupId> 34 <artifactId>commons-net</artifactId> 35 </dependency> 36 <!-- Jackson Json處理工具包 --> 37 <dependency> 38 <groupId>com.fasterxml.jackson.core</groupId> 39 <artifactId>jackson-databind</artifactId> 40 </dependency> 41 <!-- httpclient --> 42 <dependency> 43 <groupId>org.apache.httpcomponents</groupId> 44 <artifactId>httpclient</artifactId> 45 </dependency> 46 <!-- 單元測試 --> 47 <dependency> 48 <groupId>junit</groupId> 49 <artifactId>junit</artifactId> 50 <scope>test</scope> 51 </dependency> 52 <!-- 日誌處理 --> 53 <dependency> 54 <groupId>org.slf4j</groupId> 55 <artifactId>slf4j-log4j12</artifactId> 56 </dependency> 57 </dependencies> 58 59 60 </project>
後臺工程是一個web項目,能夠將Controller、Service、Mapper、Pojo各層分離,每一層都是一個jar,而後使用聚合工程將他們組合起來。結構以下
後臺管理系統工程結構:
taotao-parent -- 管理依賴jar包的版本,全局,公司級別
|--taotao-common --- 通用組件、工具類
|--taotao-manage -- 後臺系統
|--com.taotao.manage.web
|--com.taotao.manage.service
|--com.taotao.manage.mapper
|--com.taotao.manage.pojo
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>taotao-parent</artifactId> 7 <groupId>com.taotao</groupId> 8 <version>0.0.1-SNAPSHOT</version> 9 <relativePath>../taotao-parent/pom.xml</relativePath> 10 </parent> 11 <modelVersion>4.0.0</modelVersion> 12 13 <groupId>com.taotao</groupId> 14 <artifactId>taotao-manager</artifactId> 15 <packaging>pom</packaging> 16 <!-- 依賴管理 --> 17 <dependencies> 18 <dependency> 19 <groupId>com.taotao</groupId> 20 <artifactId>taotao-common</artifactId> 21 <version>0.0.1-SNAPSHOT</version> 22 </dependency> 23 </dependencies> 24 25 26 </project>
<?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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>taotao-manager</artifactId>
<groupId>com.taotao</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.taotao</groupId>
<artifactId>taotao-manager-pojo</artifactId>
<packaging>jar</packaging>
</project>
<?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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>taotao-manager</artifactId>
<groupId>com.taotao</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.taotao</groupId>
<artifactId>taotao-manager-mapper</artifactId>
<!-- 依賴管理 -->
<dependencies>
<dependency>
<groupId>com.taotao</groupId>
<artifactId>taotao-manager-pojo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<dependency>
<groupId>com.github.miemiedev</groupId>
<artifactId>mybatis-paginator</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 鏈接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
</dependencies>
</project>
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.taotao</groupId>
<artifactId>taotao-manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>taotao-manager-service</artifactId>
<!-- 依賴管理 -->
<dependencies>
<dependency>
<groupId>com.taotao</groupId>
<artifactId>taotao-manager-mapper</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
</dependencies>
</project>
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.taotao</groupId>
<artifactId>taotao-manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>taotao-manager-web</artifactId>
<packaging>war</packaging>
<!-- 依賴管理 -->
<dependencies>
<dependency>
<groupId>com.taotao</groupId>
<artifactId>taotao-manager-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- JSP相關 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- 文件上傳組件 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
</dependencies>
</project>
注意:每一個工程建立完後都安裝到maven,以便被其餘項目依賴,包括pom工程,也要安裝測試:運行taotao-manager聚合工程的tomcat7:run,起來後在瀏覽器輸入指定地址,看看主頁是否顯示到目前爲止,代碼在 https://git.oschina.net/sherryBy/taotao.git 的 提交id爲 8189d151ca10e11888952face0529a0ce7cfa7d9