intelij idea创建spring boot聚合工程

时间:2018-10-25作者:klpeng分类:IT综合浏览:2645评论:0

1、快速创建spring boot工程(父工程)。

(1.1) File -> New - > Project -> Spring Assistant-> 点击Next 。如图选择default,然后点击next。

intelij idea创建spring boot聚合工程

(1.2)填写项目相关信息,如图:

intelij idea创建spring boot聚合工程

(1.3)下一步,选择core下devtools(热部署),如图:

intelij idea创建spring boot聚合工程

(1.4)选择文件位置:

intelij idea创建spring boot聚合工程

(1.5)创建完之后删除src目录,并修改pom.xml,如下:

<?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">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.tianshuhao</groupId>
   <artifactId>tianshuhao-parent</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>pom</packaging>

   <name>tianshuhao-parent</name>
   <description>tianshuhao-parent project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.6.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
      <fastjson.version>1.2.51</fastjson.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-dependencies</artifactId>
         <version>2.0.6.RELEASE</version>
         <type>pom</type>
         <scope>import</scope>
      </dependency>
      <!--热部署-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-devtools</artifactId>
         <scope>runtime</scope>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
   </dependencies>

   <build>
      <!-- 产生构件的文件名,默认${artifactId}-${version} -->
      <finalName>tianshuhao-parent</finalName>
      <pluginManagement>
         <plugins>
            <plugin>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
         </plugins>
      </pluginManagement>
      <plugins>
         <!-- 跳过测试 -->
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
               <skip>true</skip>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>

2、创建核心模块core,entity,dao,service,web。

(2.1)项目上右键单击new-----Module:

intelij idea创建spring boot聚合工程

(2.2)选择core下的lombok(简化代码,可省略getter/setter/toString等方法),选择路径,如图:

intelij idea创建spring boot聚合工程intelij idea创建spring boot聚合工程

(2.3)模仿core创建其他模块,entity,dao,service和web(其中web打包方式选择war,最后一步项目依赖添加web),如图:

intelij idea创建spring boot聚合工程intelij idea创建spring boot聚合工程

(2.4)创建完后目录结构如图:

intelij idea创建spring boot聚合工程

(2.5)删除各个模块包下的无用类,只保留到包即可(web模块除外)。

3、父工程的pom.xml文件添加<modules>标签,如图:

intelij idea创建spring boot聚合工程

4、子模块添加父工程,entity,dao,service,web模块依赖core模块,dao添加entity模块......最后:

(4.1)core模块的pom.xml:

<?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>
      <groupId>com.tianshuhao</groupId>
      <artifactId>tianshuhao-parent</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <relativePath>../pom.xml</relativePath>
   </parent>
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.tianshuhao</groupId>
   <artifactId>tianshuhao-core</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>tianshuhao-core</name>
   <description>tianshuhao-core project for tianshuhao</description>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter</artifactId>
      </dependency>

      <!-- lombok -->
      <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <optional>true</optional>
      </dependency>
      <!--fastjson-->
      <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>fastjson</artifactId>
         <version>${fastjson.version}</version>
      </dependency>

   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>

(4.2)entity模块的pom.xml:

<?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>
      <groupId>com.tianshuhao</groupId>
      <artifactId>tianshuhao-parent</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <relativePath>../pom.xml</relativePath>
   </parent>
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.tianshuhao</groupId>
   <artifactId>tianshuhao-entity</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>tianshuhao-entity</name>
   <description>tianshuhao-entity project for tianshuhao</description>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>com.tianshuhao</groupId>
         <artifactId>tianshuhao-core</artifactId>
         <version>0.0.1-SNAPSHOT</version>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>

(4.3)dao模块的pom.xml:

<?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>
        <groupId>com.tianshuhao</groupId>
        <artifactId>tianshuhao-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

   <groupId>com.tianshuhao</groupId>
   <artifactId>tianshuhao-dao</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>tianshuhao-dao</name>
   <description>tianshuhao-dao project for tianshuhao</description>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>com.tianshuhao</groupId>
         <artifactId>tianshuhao-core</artifactId>
         <version>0.0.1-SNAPSHOT</version>
      </dependency>
      <dependency>
         <groupId>com.tianshuhao</groupId>
         <artifactId>tianshuhao-entity</artifactId>
         <version>0.0.1-SNAPSHOT</version>
      </dependency>

   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>

(4.4)service模块的pom.xml:

<?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>
      <groupId>com.tianshuhao</groupId>
      <artifactId>tianshuhao-parent</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <relativePath>../pom.xml</relativePath>
   </parent>
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.tianshuhao</groupId>
   <artifactId>tianshuhao-service</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>tianshuhao-service</name>
   <description>tianshuhao-service project for tianshuhao</description>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>com.tianshuhao</groupId>
         <artifactId>tianshuhao-core</artifactId>
         <version>0.0.1-SNAPSHOT</version>
      </dependency>
      <dependency>
         <groupId>com.tianshuhao</groupId>
         <artifactId>tianshuhao-entity</artifactId>
         <version>0.0.1-SNAPSHOT</version>
      </dependency>
      <dependency>
         <groupId>com.tianshuhao</groupId>
         <artifactId>tianshuhao-dao</artifactId>
         <version>0.0.1-SNAPSHOT</version>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-cache</artifactId>
      </dependency>

   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>

(4.5)web模块的pom.xml:

<?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>
      <groupId>com.tianshuhao</groupId>
      <artifactId>tianshuhao-parent</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <relativePath>../pom.xml</relativePath>
   </parent>
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.tianshuhao</groupId>
   <artifactId>tianshuhao-web</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>war</packaging>

   <name>tianshuhao-web</name>
   <description>tianshuhao-web project for tianshuhao</description>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>com.tianshuhao</groupId>
         <artifactId>tianshuhao-core</artifactId>
         <version>0.0.1-SNAPSHOT</version>
      </dependency>
      <dependency>
         <groupId>com.tianshuhao</groupId>
         <artifactId>tianshuhao-entity</artifactId>
         <version>0.0.1-SNAPSHOT</version>
      </dependency>
      <dependency>
         <groupId>com.tianshuhao</groupId>
         <artifactId>tianshuhao-dao</artifactId>
         <version>0.0.1-SNAPSHOT</version>
      </dependency>
      <dependency>
         <groupId>com.tianshuhao</groupId>
         <artifactId>tianshuhao-service</artifactId>
         <version>0.0.1-SNAPSHOT</version>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
         <!--排除内置的tomcat-->
         <!--<exclusions>
            <exclusion>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
         </exclusions>-->
      </dependency>
      <!--<dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-tomcat</artifactId>
         <scope>provided</scope>
      </dependency>-->
   </dependencies>

   <build>
      <!-- 产生构件的文件名,默认${artifactId}-${version} -->
      <finalName>tianshuhao-web</finalName>
   </build>
</project>

5、项目打包,调出maven projects窗口,如图目录运行run maven build:

intelij idea创建spring boot聚合工程

6、控制台显示build success表示打包成功,如图:

intelij idea创建spring boot聚合工程

7、修改web模块的application.proterties为application.yml,并添加一下内容:

intelij idea创建spring boot聚合工程

8、同级目录(resources)下添加文件application-dev.properties,内容为空即可。

9、web子模块的Application启动类:

intelij idea创建spring boot聚合工程

10、运行main方法,访问地址:http://localhost:8080/tianshuhao-web/test

 

打赏
文章版权声明:除非注明,否则均为彭超的博客原创文章,转载或复制请以超链接形式并注明出处。
相关推荐

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

猜你喜欢