springboot父子項目


springboot父子項目

父子項目即在父級項目中包含多個子級項目, 子級項目可以看模塊划分, 例如web層, 服務層等

注意點:

  1. 子項目可以繼承父項目的依賴, 前提是需要使用parent標簽指定父項目
  2. 子項目可以單獨引入依賴, 依賴只對當前子項目有效
  3. 子項目中沒有頁面打包方式可以為jar, 有頁面打包使用war

搭建父子項目案例

項目結構如下:

使用idea搭建父子項目

1. 創建父級項目jt

使用idea創建一個springboot項目, 打包方式為pom方式

<packing>pom</packing>

切記把父項目中的build標簽刪除, 因為父級項目不需要打包

父級項目導入的maven依賴可以被子類繼承

2. 創建子maven項目jt-common

在父項目jt上右鍵 new model, 選擇maven項目即可

common一般可以被公共模塊調用, 即通用模塊, 打包方式為jar包

<packing>jar</packing>

公共API項目也不需要build標簽

但是, 如果子項目要繼承父項目的jar包, 需要使用parent標簽, 指定父級項目

<parent>
    <groupId>com.jt</groupId>
    <artifactId>jt</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

創建完畢有查看父級項目中有沒有自動生成modules標簽

3. 創建子模塊jt-manager

在父項目jt上右鍵 new model, 選擇maven項目即可

也是創建maven項目, 打包方式為war

<packing>war</packing>

此項目需要使用build標簽, 否則項目可能無法運行

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

此項目也需要繼承父級項目的jar包, 需要使用parent標簽

<parent>
    <groupId>com.jt</groupId>
    <artifactId>jt</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

導入公共API, (jt-common)

<dependencies>
    <dependency>
        <groupId>com.jt</groupId>
        <artifactId>jt-common</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <!-- 等其他依賴 -->
</dependencies>

然后創建springboot的啟動類即可

創建完畢有查看父級項目中有沒有自動生成modules標簽

4. 創建web模塊jt-web等...

在父項目jt上右鍵 new model, 選擇maven項目即可

此項目依然為maven項目, 創建方式可參照第三步

如歸父級項目中有數據源的依賴, 而此模塊不需要直接訪問數據庫, 即不需要數據源的配置, 可以在主啟動類@SpringBootApplication注解中加入排除配置類的屬性, 例如

@SpringBootApplication(exclude=DataSourceAutoConfiguration.class)

可以創建多個子模塊的項目... 參照第三步即可

5. 檢查父級項目

父級項目中如果生成如下內容表示成功, 沒有則手動敲上去

<modules>
    <module>jt-common</module>
    <module>jt-manager</module>
    <module>jt-web</module>
</modules>

完整文件內容如下

jt/pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.jt</groupId>
	<artifactId>jt</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>

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

	<properties>
		<java.version>8</java.version>
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
		<skipTests>true</skipTests>
	</properties>

	<dependencies>
		<!-- springboot中導入的maven依賴, 此處略 -->
	</dependencies>
	
	<!-- 切記不要加build標簽 -->

	<modules>
		<module>jt-common</module>
		<module>jt-manager</module>
        <module>jt-web</module>
    </modules>
</project>

jt-common/pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.jt</groupId>
    <artifactId>jt</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>jt-common</artifactId>
</project>

jt-manager/pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<artifactId>jt-manager</artifactId>
	<packaging>war</packaging>

	<parent>
		<groupId>com.jt</groupId>
		<artifactId>jt</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<dependencies>
        <!-- 導入公共API- jt-common -->
        <dependency>
            <groupId>com.jt</groupId>
            <artifactId>jt-common</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>

jt-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>
        <artifactId>jt</artifactId>
        <groupId>com.jt</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>jt-web</artifactId>
    <!--指定打包類型-->
    <packaging>war</packaging>

    <!--添加依賴信息-->
    <dependencies>
         <!-- 導入公共API- jt-common -->
        <dependency>
            <groupId>com.jt</groupId>
            <artifactId>jt-common</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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM