備註:java
Maven的常見打包方式:jar、war、pommysql
1.pom工程:用在父級工程或聚合工程中。用來作jar包的版本控制。git
2.war工程:將會打包成war,發佈在服務器上的工程。如網站或服務。github
3.jar工程:將會打包成jar用做jar包使用。web
1. 項目結構
xm-parent:父工程,打包方式pom,管理jar包的版本號。redis
| 項目中全部工程都應該繼承父工程。spring
|--xm-shop:服務層工程。聚合工程。Pom工程sql
|--xm-shop-dao:打包方式jarapache
|--xm-shop-model:打包方式jarapi
|--xm-shop-interface:打包方式jar
|--xm-shop-service:打包方式:war
|--xm-shop-web:表現層工程。打包方式war
2. 新建xm-parent
Pom.xml
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 <groupId>com.xm</groupId> 4 <artifactId>xm-parent</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <packaging>pom</packaging> 7 <!-- 集中定義依賴版本號 --> 8 <properties> 9 <spring.version>3.2.4.RELEASE</spring.version> 10 <mybatis.version>3.0.4</mybatis.version> 11 <mybatis.spring.version>1.0.0</mybatis.spring.version> 12 <mysql.version>5.1.32</mysql.version> 13 <druid.version>1.0.9</druid.version> 14 <jstl.version>1.2</jstl.version> 15 <standard.version>1.1.2</standard.version> 16 <servlet-api.version>2.5</servlet-api.version> 17 <jsp-api.version>2.0</jsp-api.version> 18 <commons-lang3.version>3.3.2</commons-lang3.version> 19 <commons-io.version>1.3.2</commons-io.version> 20 <commons-net.version>3.3</commons-net.version> 21 <commons-fileupload.version>1.3.1</commons-fileupload.version> 22 <dubbo.version>2.5.3</dubbo.version> 23 <zookeeper.version>3.4.7</zookeeper.version> 24 <zkclient.version>0.1</zkclient.version> 25 <freemarker.version>2.3.23</freemarker.version> 26 <jackson.version>2.4.2</jackson.version> 27 </properties> 28 <dependencyManagement> 29 <dependencies> 30 <!-- Apache工具組件 --> 31 <dependency> 32 <groupId>org.apache.commons</groupId> 33 <artifactId>commons-lang3</artifactId> 34 <version>${commons-lang3.version}</version> 35 </dependency> 36 <dependency> 37 <groupId>org.apache.commons</groupId> 38 <artifactId>commons-io</artifactId> 39 <version>${commons-io.version}</version> 40 </dependency> 41 <dependency> 42 <groupId>commons-net</groupId> 43 <artifactId>commons-net</artifactId> 44 <version>${commons-net.version}</version> 45 </dependency> 46 <!-- Mybatis --> 47 <dependency> 48 <groupId>org.mybatis</groupId> 49 <artifactId>mybatis</artifactId> 50 <version>${mybatis.version}</version> 51 </dependency> 52 <dependency> 53 <groupId>org.mybatis</groupId> 54 <artifactId>mybatis-spring</artifactId> 55 <version>${mybatis.spring.version}</version> 56 </dependency> 57 <!-- MySql --> 58 <dependency> 59 <groupId>mysql</groupId> 60 <artifactId>mysql-connector-java</artifactId> 61 <version>${mysql.version}</version> 62 </dependency> 63 <!-- 鏈接池 --> 64 <dependency> 65 <groupId>com.alibaba</groupId> 66 <artifactId>druid</artifactId> 67 <version>${druid.version}</version> 68 </dependency> 69 <!-- Spring --> 70 <dependency> 71 <groupId>org.springframework</groupId> 72 <artifactId>spring-context</artifactId> 73 <version>${spring.version}</version> 74 </dependency> 75 <dependency> 76 <groupId>org.springframework</groupId> 77 <artifactId>spring-beans</artifactId> 78 <version>${spring.version}</version> 79 </dependency> 80 <dependency> 81 <groupId>org.springframework</groupId> 82 <artifactId>spring-webmvc</artifactId> 83 <version>${spring.version}</version> 84 </dependency> 85 <dependency> 86 <groupId>org.springframework</groupId> 87 <artifactId>spring-jdbc</artifactId> 88 <version>${spring.version}</version> 89 </dependency> 90 <dependency> 91 <groupId>org.springframework</groupId> 92 <artifactId>spring-aspects</artifactId> 93 <version>${spring.version}</version> 94 </dependency> 95 <dependency> 96 <groupId>org.springframework</groupId> 97 <artifactId>spring-jms</artifactId> 98 <version>${spring.version}</version> 99 </dependency> 100 <dependency> 101 <groupId>org.springframework</groupId> 102 <artifactId>spring-context-support</artifactId> 103 <version>${spring.version}</version> 104 </dependency> 105 <!-- JSP相關 --> 106 <dependency> 107 <groupId>jstl</groupId> 108 <artifactId>jstl</artifactId> 109 <version>${jstl.version}</version> 110 </dependency> 111 <dependency> 112 <groupId>taglibs</groupId> 113 <artifactId>standard</artifactId> 114 <version>${standard.version}</version> 115 </dependency> 116 <dependency> 117 <groupId>javax.servlet</groupId> 118 <artifactId>servlet-api</artifactId> 119 <version>${servlet-api.version}</version> 120 <scope>provided</scope> 121 </dependency> 122 <dependency> 123 <groupId>javax.servlet</groupId> 124 <artifactId>jsp-api</artifactId> 125 <version>${jsp-api.version}</version> 126 <scope>provided</scope> 127 </dependency> 128 <!-- 文件上傳組件 --> 129 <dependency> 130 <groupId>commons-fileupload</groupId> 131 <artifactId>commons-fileupload</artifactId> 132 <version>${commons-fileupload.version}</version> 133 </dependency> 134 <!-- Redis客戶端 --> 135 <dependency> 136 <groupId>redis.clients</groupId> 137 <artifactId>jedis</artifactId> 138 <version>${jedis.version}</version> 139 </dependency> 140 <!-- Jackson Json處理工具包 --> 141 <dependency> 142 <groupId>com.fasterxml.jackson.core</groupId> 143 <artifactId>jackson-databind</artifactId> 144 <version>${jackson.version}</version> 145 </dependency> 146 <!-- dubbo相關 --> 147 <dependency> 148 <groupId>com.alibaba</groupId> 149 <artifactId>dubbo</artifactId> 150 <version>${dubbo.version}</version> 151 </dependency> 152 <dependency> 153 <groupId>org.apache.zookeeper</groupId> 154 <artifactId>zookeeper</artifactId> 155 <version>${zookeeper.version}</version> 156 </dependency> 157 <dependency> 158 <groupId>com.github.sgroschupf</groupId> 159 <artifactId>zkclient</artifactId> 160 <version>${zkclient.version}</version> 161 </dependency> 162 <dependency> 163 <groupId>org.freemarker</groupId> 164 <artifactId>freemarker</artifactId> 165 <version>${freemarker.version}</version> 166 </dependency> 167 168 </dependencies> 169 </dependencyManagement> 170 171 <build> 172 <finalName>${project.artifactId}</finalName> 173 <plugins> 174 <!-- 資源文件拷貝插件 --> 175 <plugin> 176 <groupId>org.apache.maven.plugins</groupId> 177 <artifactId>maven-resources-plugin</artifactId> 178 <version>2.7</version> 179 <configuration> 180 <encoding>UTF-8</encoding> 181 </configuration> 182 </plugin> 183 <!-- java編譯插件 --> 184 <plugin> 185 <groupId>org.apache.maven.plugins</groupId> 186 <artifactId>maven-compiler-plugin</artifactId> 187 <version>3.2</version> 188 <configuration> 189 <source>1.7</source> 190 <target>1.7</target> 191 <encoding>UTF-8</encoding> 192 </configuration> 193 </plugin> 194 </plugins> 195 <pluginManagement> 196 <plugins> 197 <!-- 配置Tomcat插件 --> 198 <plugin> 199 <groupId>org.apache.tomcat.maven</groupId> 200 <artifactId>tomcat7-maven-plugin</artifactId> 201 <version>2.2</version> 202 </plugin> 203 </plugins> 204 </pluginManagement> 205 </build> 206 </project>
3. 新建xm-shop
在xm-shop下新建模塊xm-shop-model
Pom.xml
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 <parent> 4 <groupId>com.xm</groupId> 5 <artifactId>xm-shop</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 </parent> 8 <artifactId>xm-shop-model</artifactId> 9 </project>
在xm-shop下新建模塊xm-shop-dao
Dao須要引來model mybatis mysql druid鏈接池
Pom.xml
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 <parent> 4 <groupId>com.xm</groupId> 5 <artifactId>xm-shop</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 </parent> 8 <artifactId>xm-shop-dao</artifactId> 9 <!-- 依賴的jar包 --> 10 <dependencies> 11 <dependency> 12 <groupId>com.xm</groupId> 13 <artifactId>xm-shop-model</artifactId> 14 <version>0.0.1-SNAPSHOT</version> 15 </dependency> 16 <!-- Mybatis --> 17 <dependency> 18 <groupId>org.mybatis</groupId> 19 <artifactId>mybatis</artifactId> 20 </dependency> 21 <dependency> 22 <groupId>org.mybatis</groupId> 23 <artifactId>mybatis-spring</artifactId> 24 </dependency> 25 <!-- MySql --> 26 <dependency> 27 <groupId>mysql</groupId> 28 <artifactId>mysql-connector-java</artifactId> 29 </dependency> 30 <!-- 鏈接池 --> 31 <dependency> 32 <groupId>com.alibaba</groupId> 33 <artifactId>druid</artifactId> 34 </dependency> 35 </dependencies> 36 </project>
在xm-shop下新建模塊xm-shop-interface
pom.xml
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 <parent> 4 <groupId>com.xm</groupId> 5 <artifactId>xm-shop</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 </parent> 8 <artifactId>xm-shop-interface</artifactId> 9 <!-- 依賴的jar包 --> 10 <dependencies> 11 <dependency> 12 <groupId>com.xm</groupId> 13 <artifactId>xm-shop-model</artifactId> 14 <version>0.0.1-SNAPSHOT</version> 15 </dependency> 16 </dependencies> 17 </project>
在xm-shop下新建模塊xm-shop-service(war)
因爲項目發佈須要war包,所以該模塊打包成war
添加web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>xm-shop-service</display-name> 4 5 </web-app>
Pom.xml
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 <parent> 4 <groupId>com.xm</groupId> 5 <artifactId>xm-shop</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 </parent> 8 <artifactId>xm-shop-service</artifactId> 9 <packaging>war</packaging> 10 <dependencies> 11 <dependency> 12 <groupId>com.xm</groupId> 13 <artifactId>xm-shop-dao</artifactId> 14 <version>0.0.1-SNAPSHOT</version> 15 </dependency> 16 <dependency> 17 <groupId>com.xm</groupId> 18 <artifactId>xm-shop-interface</artifactId> 19 <version>0.0.1-SNAPSHOT</version> 20 </dependency> 21 <dependency> 22 <groupId>com.xm</groupId> 23 <artifactId>xm-shop-model</artifactId> 24 <version>0.0.1-SNAPSHOT</version> 25 </dependency> 26 <!-- spring的依賴 --> 27 <dependency> 28 <groupId>org.springframework</groupId> 29 <artifactId>spring-context</artifactId> 30 </dependency> 31 <dependency> 32 <groupId>org.springframework</groupId> 33 <artifactId>spring-beans</artifactId> 34 </dependency> 35 <dependency> 36 <groupId>org.springframework</groupId> 37 <artifactId>spring-webmvc</artifactId> 38 </dependency> 39 <dependency> 40 <groupId>org.springframework</groupId> 41 <artifactId>spring-jdbc</artifactId> 42 </dependency> 43 <dependency> 44 <groupId>org.springframework</groupId> 45 <artifactId>spring-aspects</artifactId> 46 </dependency> 47 <dependency> 48 <groupId>org.springframework</groupId> 49 <artifactId>spring-jms</artifactId> 50 </dependency> 51 <dependency> 52 <groupId>org.springframework</groupId> 53 <artifactId>spring-context-support</artifactId> 54 </dependency> 55 <!-- dubbo相關 --> 56 <dependency> 57 <groupId>com.alibaba</groupId> 58 <artifactId>dubbo</artifactId> 59 <!-- 排除依賴 --> 60 <exclusions> 61 <exclusion> 62 <artifactId>spring</artifactId> 63 <groupId>org.springframework</groupId> 64 </exclusion> 65 <exclusion> 66 <artifactId>netty</artifactId> 67 <groupId>org.jboss.netty</groupId> 68 </exclusion> 69 </exclusions> 70 </dependency> 71 <dependency> 72 <groupId>org.apache.zookeeper</groupId> 73 <artifactId>zookeeper</artifactId> 74 </dependency> 75 <dependency> 76 <groupId>com.github.sgroschupf</groupId> 77 <artifactId>zkclient</artifactId> 78 <version>0.1</version> 79 </dependency> 80 </dependencies> 81 </project>
4. 新建xm-shop-web
添加web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>xm-shop-service</display-name> 4 5 </web-app>
Pom.xml
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 <parent> 4 <groupId>com.xm</groupId> 5 <artifactId>xm-parent</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 </parent> 8 <groupId>com.xm</groupId> 9 <artifactId>xm-manager-web</artifactId> 10 <version>0.0.1-SNAPSHOT</version> 11 <packaging>war</packaging> 12 <dependencies> 13 <dependency> 14 <groupId>com.xm</groupId> 15 <artifactId>xm-shop-interface</artifactId> 16 <version>0.0.1-SNAPSHOT</version> 17 </dependency> 18 <dependency> 19 <groupId>com.xm</groupId> 20 <artifactId>xm-shop-model</artifactId> 21 <version>0.0.1-SNAPSHOT</version> 22 </dependency> 23 <!-- JSP相關 --> 24 <dependency> 25 <groupId>jstl</groupId> 26 <artifactId>jstl</artifactId> 27 </dependency> 28 <dependency> 29 <groupId>taglibs</groupId> 30 <artifactId>standard</artifactId> 31 </dependency> 32 <dependency> 33 <groupId>javax.servlet</groupId> 34 <artifactId>servlet-api</artifactId> 35 <scope>provided</scope> 36 </dependency> 37 <dependency> 38 <groupId>javax.servlet</groupId> 39 <artifactId>jsp-api</artifactId> 40 <scope>provided</scope> 41 </dependency> 42 <!-- spring的依賴 --> 43 <dependency> 44 <groupId>org.springframework</groupId> 45 <artifactId>spring-context</artifactId> 46 </dependency> 47 <dependency> 48 <groupId>org.springframework</groupId> 49 <artifactId>spring-beans</artifactId> 50 </dependency> 51 <dependency> 52 <groupId>org.springframework</groupId> 53 <artifactId>spring-webmvc</artifactId> 54 </dependency> 55 <dependency> 56 <groupId>org.springframework</groupId> 57 <artifactId>spring-jdbc</artifactId> 58 </dependency> 59 <dependency> 60 <groupId>org.springframework</groupId> 61 <artifactId>spring-aspects</artifactId> 62 </dependency> 63 <dependency> 64 <groupId>org.springframework</groupId> 65 <artifactId>spring-jms</artifactId> 66 </dependency> 67 <dependency> 68 <groupId>org.springframework</groupId> 69 <artifactId>spring-context-support</artifactId> 70 </dependency> 71 <!-- dubbo相關 --> 72 <dependency> 73 <groupId>com.alibaba</groupId> 74 <artifactId>dubbo</artifactId> 75 <!-- 排除依賴 --> 76 <exclusions> 77 <exclusion> 78 <artifactId>spring</artifactId> 79 <groupId>org.springframework</groupId> 80 </exclusion> 81 <exclusion> 82 <artifactId>netty</artifactId> 83 <groupId>org.jboss.netty</groupId> 84 </exclusion> 85 </exclusions> 86 </dependency> 87 <dependency> 88 <groupId>org.apache.zookeeper</groupId> 89 <artifactId>zookeeper</artifactId> 90 </dependency> 91 <dependency> 92 <groupId>com.github.sgroschupf</groupId> 93 <artifactId>zkclient</artifactId> 94 <version>0.1</version> 95 </dependency> 96 </dependencies> 97 </project>
1. 工程啓動
啓動xm-shop工程
須要在xm-shop 的pom工程中,配置tomcat插件。啓動的端口號,和工程名稱。
在xm-shop的pom文件中添加以下配置:
1 <!-- 配置tomcat插件 --> 2 <build> 3 <plugins> 4 <plugin> 5 <groupId>org.apache.tomcat.maven</groupId> 6 <artifactId>tomcat7-maven-plugin</artifactId> 7 <configuration> 8 <port>8080</port> 9 <path>/</path> 10 </configuration> 11 </plugin> 12 </plugins> 13 </build>
啓動tomcat插件:
clean tomcat7:run
先把xm-parent安裝到本地倉庫。而後再啓動。
啓動xm-shop-web工程
須要在xm-shop-web 的pom工程中,配置tomcat插件。啓動的端口號,和工程名稱。
在xm-shop-web的pom文件中添加以下配置:
1 <!-- 配置tomcat插件 --> 2 <build> 3 <plugins> 4 <plugin> 5 <groupId>org.apache.tomcat.maven</groupId> 6 <artifactId>tomcat7-maven-plugin</artifactId> 7 <configuration> 8 <port>8081</port> 9 <path>/</path> 10 </configuration> 11 </plugin> 12 </plugins> 13 </build>
啓動tomcat插件:
clean tomcat7:run
先把xm-shop安裝到本地倉庫。而後再啓動。