整體架構圖
1.新建父工程
新建maven父項目(用來管理jar包版本),使子系統使用同一個版本的jar包。
File-》New-》Other-》Maven Project,打包方式選pom
<!-- 集中定義依賴版本號 --> <properties> <activemq.version>5.11.2</activemq.version> <freemarker.version>2.3.23</freemarker.version> <quartz.version>2.2.2</quartz.version> </properties> <!-- dependencyManagement管理包的版本,並沒有添加依賴 --> <dependencyManagement> <dependencies> <!-- 時間操作組件 --> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>${joda-time.version}</version> </dependency> </dependencies> </dependencyManagement> <build> <finalName>${project.artifactId}</finalName> <plugins> <!-- 資源文件拷貝插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- java編譯插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> <!-- 插件管理,此處並沒有使用,子項目中build --> <pluginManagement> <plugins> <!-- 配置Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> </plugins> </pluginManagement> </build>
2.新建子工具工程
創建common通用工具類項目,父類為剛才創建的項目,打包方式為jar
<parent> <groupId>cn.e3mall</groupId> <artifactId>e3-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>cn.e3mall</groupId> <artifactId>e3-common</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 依賴的jar包 --> <dependencies> <!-- Jackson Json處理工具包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> <!-- httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </dependency> <!-- 單元測試 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <!-- 日志處理 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </dependency> </dependencies>
3.新建服務工程(POM)
創建聚合項目,聚合pojo、dao、interface、service等子項目。打包方式為pom,父項目為剛才創建的jar包版本管理項目。
<parent> <groupId>cn.e3mall</groupId> <artifactId>e3-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>cn.e3mall</groupId> <artifactId>e3-manager</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <dependencies> <!--依賴e3-common通用工具類工程 --> <dependency> <groupId>cn.e3mall</groupId> <artifactId>e3-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <!-- e3-manager是聚合工程,聚合pojo、dao、service等子項目 --> <modules> <module>e3-manager-pojo</module> <module>e3-manager-dao</module> <module>e3-manager-interface</module> <module>e3-manager-service</module> <module>e3-manager-web</module> </modules> <!-- 配置tomcat插件 --> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/</path> <!-- tomcat項目路徑 --> <port>8080</port> <!-- tomcat端口 --> </configuration> </plugin> </plugins> </build>
4.創建服務層的子項目
右擊步驟3創建的聚合項目,選擇Maven Module,輸入Module項目創建子項目,next選擇jar打包方式,用相同的方法創建pojo、dao、interface、service、web(這個打包方式選擇war)子項目。
5.子項目依賴關系
dao和interface依賴pojo,service依賴dao和interface,web依賴service。
其中dao的pom文件為
<dependencies> <dependency> <groupId>cn.e3mall</groupId> <artifactId>e3-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> <!-- 如果不添加此節點mybatis的mapper.xml以及db.properties文件都會被漏掉。 --> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
6.右擊manager聚合項目Run as-》Maven build-》clean tomcat7:run
7.如果發現執行失敗,且maven倉庫中找不到父項目和common項目,此時需要右擊這兩個項目Run as-》Maven Install即可加載到本地倉庫中。
8.執行成功,瀏覽器輸入地址即可查看