国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > Java > 正文

基于maven使用IDEA創(chuàng)建多模塊項(xiàng)目

2024-07-14 08:40:24
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

鑒于最近學(xué)習(xí)一個(gè)分布式項(xiàng)目的開發(fā),講一下關(guān)于使用IntelliJ IDEA基于Maven創(chuàng)建多模塊項(xiàng)目的實(shí)際開發(fā),可能有不合適的地方,但是項(xiàng)目是可以跑通的,也請(qǐng)有不足之處,都提出來(lái),一起討論下。

一. 項(xiàng)目工程目錄

首先展示一下,最終整個(gè)項(xiàng)目的工程目錄:

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

簡(jiǎn)單介紹一下目錄結(jié)構(gòu):
common-parent為所有項(xiàng)目的父項(xiàng)目,主要用來(lái)管理所有項(xiàng)目使用的jar包及其版本。
common-utils為公共的工具類項(xiàng)目,繼承父項(xiàng)目,它會(huì)被打成jar包供其它項(xiàng)目使用。
taotao-manager為我們自己的項(xiàng)目,繼承與我們的父項(xiàng)目。
taotao-manager-pojo為我們自己項(xiàng)目的一個(gè)子模塊,依賴與taotao-manager,打成jar包
taotao-manager-mapper為我們自己項(xiàng)目的一個(gè)子模塊,依賴與taotao-manager-pojo,打成jar包
taotao-manager-service為我們自己項(xiàng)目的一個(gè)子模塊,依賴與taotao-manager-mapper,打成jar包
taotao-manager-web為我們自己項(xiàng)目的一個(gè)子模塊,依賴與taotao-manager-service,打成war包

二. 創(chuàng)建父工程

下面為具體操作:

1. 首先打開IntelliJ IDEA,按下面步驟來(lái):

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

或者第一次打開IDEA,

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

2. 來(lái)到創(chuàng)建目錄,選擇Empty project,next

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

3. 來(lái)到New Project頁(yè)面,填寫project name,選擇Project location,點(diǎn)Finish

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

等待IDEA加載完成,進(jìn)行下一步

4. 選擇File–>New–>Module,進(jìn)入New Module頁(yè)面:

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

5. 在New Module頁(yè)面,按如下步驟操作:

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

6. 填寫組織名稱和項(xiàng)目名稱,以及版本號(hào)

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

7. 配置本地maven目錄及maven倉(cāng)庫(kù)配置文件

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

8. 創(chuàng)建父工程最后一步,給Module取名,及選擇工作目錄,選擇完成

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

等待生成pom文件,注意修改打包方式為pom,修改pom文件如下:(由于pom文件較長(zhǎng),只給出部分,獲取所有,請(qǐng)關(guān)注github)

<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>  <groupId>cn.william</groupId>  <artifactId>common-parent</artifactId>  <version>1.0-SNAPSHOT</version>  <packaging>pom</packaging><!--修改打包方式-->  <name>common-parent</name>  <url>http://maven.apache.org</url>  <!--集中定義依賴版本號(hào)-->  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    <junit.version>4.12</junit.version>    ...(省略)  </properties>  <dependencyManagement>    <dependencies>      <!-- 時(shí)間操作組件 -->      <dependency>        <groupId>joda-time</groupId>        <artifactId>joda-time</artifactId>        <version>${joda-time.version}</version>      </dependency>      ...(省略)  </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.5.1</version>        <configuration>          <source>1.8</source>          <target>1.8</target>          <encoding>UTF-8</encoding>        </configuration>      </plugin>    </plugins>  </build></project>

三. 創(chuàng)建公共工具類

首先,公共工具類是繼承自父項(xiàng)目common-parent的。

需要注意的一點(diǎn)是一點(diǎn),為了項(xiàng)目看起來(lái)為分模塊的形式(類似在eclipse中開發(fā)),在創(chuàng)建common-utils中有一步需要注意:

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

下一步特別注意,這一步會(huì)影響目錄結(jié)構(gòu),

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

同樣注意項(xiàng)目的打包方式為jar,和父項(xiàng)目有所不同。

<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>common-parent</artifactId>    <groupId>cn.william</groupId>    <version>1.0-SNAPSHOT</version>    <relativePath>../common-parent/pom.xml</relativePath>  </parent>  <modelVersion>4.0.0</modelVersion>  <artifactId>common-utils</artifactId>  <packaging>jar</packaging><!-- 打成jar包 -->  <name>common-utils</name>  <url>http://maven.apache.org</url>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>  <!-- jar包的依賴 -->  <dependencies>    <!-- 時(shí)間操作組件 -->    <dependency>      <groupId>joda-time</groupId>      <artifactId>joda-time</artifactId>    </dependency>    ...(省略)  </dependencies></project>

三. 創(chuàng)建開發(fā)項(xiàng)目

創(chuàng)建開發(fā)項(xiàng)目taotao-manager,其創(chuàng)建步驟同上,同樣是繼承common-parent,同樣修改其pom文件,詳細(xì)如下:

<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>common-parent</artifactId>    <groupId>cn.william</groupId>    <version>1.0-SNAPSHOT</version>    <relativePath>../common-parent/pom.xml</relativePath>  </parent>  <modelVersion>4.0.0</modelVersion>  <artifactId>taotao-manager</artifactId>  <packaging>pom</packaging>  <name>taotao-manager</name>  <url>http://maven.apache.org</url>  <modules>    <module>taotao-manager-pojo</module>    <module>taotao-manager-mapper</module>    <module>taotao-manager-service</module>    <module>taotao-manager-web</module>  </modules>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>  <!-- 依賴管理 -->  <dependencies>    <dependency>      <groupId>cn.william</groupId>      <artifactId>common-utils</artifactId>      <version>1.0-SNAPSHOT</version>    </dependency>  </dependencies>  <build>    <!-- 配置插件 -->    <plugins>      <plugin>        <groupId>org.apache.tomcat.maven</groupId>        <artifactId>tomcat7-maven-plugin</artifactId>        <configuration>          <port>8080</port>          <path>/</path>        </configuration>      </plugin>    </plugins>  </build></project>

四. 創(chuàng)建開發(fā)項(xiàng)目子模塊

創(chuàng)建taotao-manager-pojo,mapper,service,web等子模塊,詳細(xì)如下:

右鍵點(diǎn)擊taotao-manager,選擇New,Module,

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

這一步注意,與創(chuàng)建common-utils時(shí)不同,是將其子模塊添加到taotao-manager目錄下的:

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

maven的相關(guān)配置,保持默認(rèn):

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

注意子模塊所在目錄就好:

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

創(chuàng)建taotao-manager下其它子模塊

上面說(shuō)的時(shí)創(chuàng)建taotao-manager-pojo子模塊,另外三個(gè)子模塊的創(chuàng)建方式與其類似,有一點(diǎn)不同時(shí),在創(chuàng)建taotao-manager-web子模塊時(shí),maven項(xiàng)目的類型選擇如下:

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

修改子模塊的pom文件

子模塊pojo,mapper,service的打包方式全為jar,只有web子模塊打包為war包。

pojo子模塊,pom.xml詳細(xì)如下:

<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>cn.william</groupId>    <version>1.0-SNAPSHOT</version>  </parent>  <modelVersion>4.0.0</modelVersion>  <artifactId>taotao-manager-pojo</artifactId>  <packaging>jar</packaging>  <name>taotao-manager-pojo</name>  <url>http://maven.apache.org</url>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties></project>

mapper子模塊依賴與pojo子模塊,pom.xml詳細(xì)如下:

<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>cn.william</groupId>    <version>1.0-SNAPSHOT</version>  </parent>  <modelVersion>4.0.0</modelVersion>  <artifactId>taotao-manager-mapper</artifactId>  <packaging>jar</packaging>  <name>taotao-manager-mapper</name>  <url>http://maven.apache.org</url>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>  <dependencies>    <!-- 依賴與pojo子模塊 -->    <dependency>      <groupId>cn.william</groupId>      <artifactId>taotao-manager-pojo</artifactId>      <version>1.0-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>

service子模塊依賴與mapper子模塊,pom.xml詳細(xì)如下:

<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>cn.william</groupId>    <version>1.0-SNAPSHOT</version>  </parent>  <modelVersion>4.0.0</modelVersion>  <artifactId>taotao-manager-service</artifactId>  <packaging>jar</packaging>  <name>taotao-manager-service</name>  <url>http://maven.apache.org</url>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>  <dependencies>    <!-- 依賴與mapper子模塊 -->    <dependency>      <groupId>cn.william</groupId>      <artifactId>taotao-manager-mapper</artifactId>      <version>1.0-SNAPSHOT</version>    </dependency>    <!-- Spring -->    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-context</artifactId>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-beans</artifactId>    </dependency>    ...(省略)  </dependencies></project>

web子模塊依賴與service子模塊,pom.xml詳細(xì)如下:

<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">  <parent>    <artifactId>taotao-manager</artifactId>    <groupId>cn.william</groupId>    <version>1.0-SNAPSHOT</version>  </parent>  <modelVersion>4.0.0</modelVersion>  <artifactId>taotao-manager-web</artifactId>  <packaging>war</packaging><!--打成war包-->  <name>taotao-manager-web Maven Webapp</name>  <url>http://maven.apache.org</url>  <dependencies>    <!-- 依賴于service -->    <dependency>      <groupId>cn.william</groupId>      <artifactId>taotao-manager-service</artifactId>      <version>1.0-SNAPSHOT</version>    </dependency>    ...(省略)  </dependencies>  <build>    <finalName>taotao-manager-web</finalName>  </build></project>

五. 運(yùn)行項(xiàng)目

創(chuàng)建index.jsp

至此,項(xiàng)目創(chuàng)建已經(jīng)完成,我們可以在taotao-manager-web的webapp目錄下創(chuàng)建index.jsp,詳情如下:

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

使用tomcat插件

我們使用maven的tomcat插件來(lái)運(yùn)行項(xiàng)目,在此之前,確保在taotao-manager的pom文件中配置了tomcat的插件:

...(省略)<build>  <!-- 配置插件 -->  <plugins>    <plugin>      <groupId>org.apache.tomcat.maven</groupId>      <artifactId>tomcat7-maven-plugin</artifactId>      <configuration>        <port>8080</port>        <path>/</path>      </configuration>    </plugin>  </plugins></build>...(省略)

配置Maven Tomcat Plugin運(yùn)行web項(xiàng)目

(1) 添加新配置

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

(2) 點(diǎn)擊運(yùn)行,或者點(diǎn)擊旁邊的debug

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

(3) 出項(xiàng)錯(cuò)誤

出現(xiàn)錯(cuò)誤,可能是common-parent和common-utils沒(méi)有安裝到本地倉(cāng)庫(kù),出現(xiàn)的錯(cuò)誤是找不到common-utils的jar包,那就需要我們安裝一下。

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

等待將jar包安裝到本地倉(cāng)庫(kù),稍微有點(diǎn)慢可能,耐心等。。。

安裝完成之后,再次運(yùn)行,出現(xiàn)下面的信息,證明運(yùn)行成功:

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

通過(guò)瀏覽器訪問(wèn):

maven,多模塊項(xiàng)目,maven創(chuàng)建多模塊,maven創(chuàng)建模塊項(xiàng)目

至此,我們的項(xiàng)目環(huán)境搭建成功,接下來(lái)就是實(shí)際的項(xiàng)目開發(fā)了。

聲明:

① 關(guān)于上面代碼中出現(xiàn)的省略的部分,是由于內(nèi)容偏長(zhǎng),所以省略,想要查看完成項(xiàng)目,請(qǐng)查看本人的github,地址:
https://github.com/williamHappy/tao-shopping-mall

② 關(guān)于上面有任何不足之處,敬請(qǐng)各位諒解,畢竟本人技術(shù)有限,也在學(xué)習(xí)階段,也可以給出合理意見,共同改進(jìn)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 巫溪县| 柘荣县| 宜宾市| 叶城县| 洛宁县| 元朗区| 六枝特区| 浙江省| 永新县| 繁峙县| 伊通| 前郭尔| 上思县| 沈阳市| 富阳市| 元江| 文水县| 加查县| 武威市| 陇川县| 汉川市| 青阳县| 阳城县| 革吉县| 文安县| 千阳县| 邢台县| 高密市| 三原县| 丰原市| 石嘴山市| 祁阳县| 富民县| 东平县| 乐业县| 和林格尔县| 封丘县| 庄浪县| 策勒县| 彰化市| 双江|