欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot項(xiàng)目父子多模塊打包方式

 更新時(shí)間:2024年01月02日 09:02:30   作者:洋哥登陸  
這篇文章主要介紹了springboot項(xiàng)目父子多模塊打包方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

springboot項(xiàng)目父子多模塊打jar包

1.項(xiàng)目工程架構(gòu)

  • Student:父工程Maven
  • main:子模塊,也是啟動(dòng)類MainApplication所在的模塊,main依賴其他模塊
  • commoncore、student等:子模塊。除main模塊之外,依賴部分或不依賴的其他模塊

注意事項(xiàng):父工程創(chuàng)建為Maven,啟動(dòng)類所在模塊創(chuàng)建時(shí)一定要用SpringBoot項(xiàng)目,其他子模塊創(chuàng)建時(shí)Maven、SpringBoot都可以。

2.各個(gè)模塊pom文件配置

父工程Student

<?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.example</groupId>
    <artifactId>Student</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Student</name>
 
    <modules>
        <module>main</module>
        <module>student</module>
        <module>struct</module>
        <module>core</module>
        <module>common</module>
    </modules>
 
</project>

注:在父工程的pom文件中一定不能有spring-boot-maven-plugin插件,否者這個(gè)插件就將傳遞到每一個(gè)子模塊中,打包會(huì)出錯(cuò);

main啟動(dòng)類模塊

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!-- parent修改為父工程信息 -->
    <parent>
        <groupId>com.example</groupId>
        <artifactId>Student</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>main</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>main</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
 
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
 
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </dependency>
 
<!-- ************************************************************************ -->
        <!-- 子模塊依賴 -->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>core</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
 
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
 
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>student</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
 
    </dependencies>
 
    <build>
        <finalName>main</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
 
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- 指定該Main Class為全局的唯一入口 -->
                    <mainClass>com.example.main.MainApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal><!--可以把依賴的包都打包到生成的Jar包中-->
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

注:在該子模塊的pom文件中必須引入spring-boot-maven-plugin打包插件,并且要配置將所依賴的jar包全都打到這一包中,如果在其他模塊中引入了spring-boot-maven-plugin將會(huì)每個(gè)模塊都單獨(dú)打成一個(gè)包,無(wú)法拉取到啟動(dòng)類所在包中;

core子模塊pom文件,其他子模塊類似

<?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為父工程信息 -->
    <parent>
        <artifactId>Student</artifactId>
        <groupId>com.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
 
    <artifactId>core</artifactId>
 
    <dependencies>
            <!-- dependency依賴區(qū) -->
    </dependencies>

</project>

3.執(zhí)行生成jar包和部署過(guò)程

通過(guò)maven菜單,在父工程下package(或mvn package命令)

這里需要注意的是雖然我們是將所有的jar都搭到main這個(gè)jar中,但是我們執(zhí)行clean和package卻是要對(duì)父工程進(jìn)行;

部署:將打好的jar包上傳到服務(wù)器的任意路徑上,執(zhí)行 java -jar main.jar(前提需要安裝jdk并配置環(huán)境變量,并且版本和項(xiàng)目構(gòu)建的版本一致)

注意:在linux環(huán)境下(通過(guò)xshell連接)運(yùn)行"java -jar xxx.jar",項(xiàng)目成功運(yùn)行后關(guān)閉窗口或者ctrl+z,會(huì)直接關(guān)閉掉項(xiàng)目,所以需要通過(guò)nohup和&命令配合:“nohup java -jar xxx.jar &”,在目錄下會(huì)生成nohup.out日志文件查看:"tail -f nohup.out"就可以查看項(xiàng)目日志了。

springboot項(xiàng)目父子多模塊打war包

修改main啟動(dòng)類模塊pom

<!-- 打包方式為war包 -->
<packaging>war</packaging>
 
<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>         
 
 <!-- war包插件 -->
 <plugin>
     <artifactId>maven-war-plugin</artifactId>
     <version>3.2.3</version>
     <configuration>
           <!-- 把class打包jar作為附件 -->
           <attachClasses>true</attachClasses>
     </configuration>
 </plugin>

修改springboot啟動(dòng)類,啟動(dòng)類繼承SpringBootServletInitializer,重寫configure方法,讓Servlet容器啟動(dòng)時(shí)啟動(dòng)Spring Boot

@SpringBootApplication
@ComponentScan("com.example.**")
@MapperScan("com.example.**.mapper")
public class MainApplication extends SpringBootServletInitializer {
 
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
 
    /**
     *  繼承SpringBootServletInitializer ,覆蓋configure(),把啟動(dòng)類Application注冊(cè)進(jìn)去。
     *  外部web應(yīng)用服務(wù)器構(gòu)建Web Application Context的時(shí)候,會(huì)把啟動(dòng)類添加進(jìn)去
     * @param builder
     * @return
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(MainApplication.class);
    }
}

其他說(shuō)明,當(dāng)前環(huán)境及工具版本: 

springboot版本2.4.5 

jdk版本1.8.0 

tomcat版本apache-tomcat-8.5.45

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論