1. 程式人生 > >IDEA springboot 建立多模組專案,打包

IDEA springboot 建立多模組專案,打包

原始碼:https://gitee.com/DencyCheng/springboot-multiModule/tree/dev

1.專案結構

2.專案依賴關係

multi-module :外層專案

model:實體類

persistence:持久化物件

web:控制層物件

web->persistence->model

3.pom檔案

3.1multi-module

<?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.dencycheng</groupId>
	<artifactId>multi-module</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<modules>
		<module>model</module>
		<module>persistence</module>
		<module>web</module>
	</modules>
	<packaging>pom</packaging>

	<name>multi-module</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.0.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>
	</properties>

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

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>

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

</project>

主要注意

	<modules>
		<module>model</module>
		<module>persistence</module>
		<module>web</module>
	</modules>
	<packaging>pom</packaging>

3.1model

<?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>multi-module</artifactId>
        <groupId>com.dencycheng</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>model</artifactId>

</project>

主要注意

    <parent>
        <artifactId>multi-module</artifactId>
        <groupId>com.dencycheng</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

3.2persistence

<?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>multi-module</artifactId>
        <groupId>com.dencycheng</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>persistence</artifactId>

    <dependencies>
        <dependency>
            <artifactId>model</artifactId>
            <groupId>com.dencycheng</groupId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

主要注意

    <dependencies>
        <dependency>
            <artifactId>model</artifactId>
            <groupId>com.dencycheng</groupId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

因為這裡要使用model裡面的物件,所以把它當作依賴引入過來

3.3web

<?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>multi-module</artifactId>
        <groupId>com.dencycheng</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>web</artifactId>

    <dependencies>
        <dependency>
            <artifactId>persistence</artifactId>
            <groupId>com.dencycheng</groupId>
            <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>

主要注意

    <dependencies>
        <dependency>
            <artifactId>persistence</artifactId>
            <groupId>com.dencycheng</groupId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

原理跟persistence類似,這裡就不說明了。還有我們看見了build標籤,這裡先不做解釋,這個是打包用的

4.打jar包

剛才上文提到的

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

為什麼出現在web的pom檔案中,因為啟動類在web裡面,所以把build標籤以及裡面內容放進了web的pom檔案中

下面看一下web的目錄結構

然後就可以打包了。。。

如圖所示

1.開啟idea右側的Maven Projects

2.雙擊

3.執行

java -jar XXXX.jar