springboot多模块项目(微服务项目)正确打包(jar)方式

大致步骤

  1. 新建一个springboot项目名称为父亲

  2. 添加父快捷方式。新建子模块,子模块同时插入新建springboot的项目,依次创建enty和web模块(关键是并配置好pom文件)

  3. web模块依赖于entiy模块中的实体类,创建测试控制器,先测试项目没问题再开始打包(jar)

  4. 开始打包

  5. 测试jar是否有用

创建项目

注意点 :子模块需要保留xx.iml,xx.mvn文件,父模块保留.idea,.mvn文件 。如果删除了这些可能会报发现主类的错误
要打包项目大致的目录结构如下 :
第一级别:father
第二级别:service、web、entiy
第三级别:eduService
其中web、eduService是web项目可以独立运行,且依赖entiy( 学会了这个,以后所有的多模块项目都能学会打包(jar)
在这里插入图片描述

配置父亲的pom文件

配置父模块注意点一: 修改打包为pom(一般父级的打包方式为pom,所以father、service的打包方式为pom)。

 <packaging>pom</packaging>

配置父模块注意点二: 记得指定该父模块下面有哪些子模块

 <modules>
        <module>entiy</module>
        <module>web</module>
        <module>service</module>
    </modules>

配置父模块注意点三: 记得指定java的版本号

  <properties>
        <java.version>1.8</java.version>
    </properties>     

配置父模块注意点四: 只需在father配置apache的maven打包插件,service的其他父模块不需要配置这个

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <skipTests>true</skipTests>    <!--默认关掉单元测试 -->
                </configuration>
            </plugin>
        </plugins>
    </build>

完整father的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
    </parent>
    <modules>
        <module>entiy</module>
        <module>web</module>
        <module>service</module>
    </modules>
    <groupId>com.zzh</groupId>
    <artifactId>father</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>father</name>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <skipTests>true</skipTests>    <!--默认关掉单元测试 -->
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

配置子模块的pom文件

配置子模块注意点一 : 在parent节点下面加上relativePath,作用是指明依赖哪个父模块pom文件(如果是依赖直接父级就是…/规则和写html那种引用绝对路径一样)。

<relativePath>../pom.xml</relativePath>

配置子模块注意点二 : 修改打包方式为jar

<packaging>jar</packaging>

配置子模块注意点三 : 加上springBoot的maven打包插件,并且指定运行的主入口类(springboot的maven插件,用这个插件打包的Jar包可以直接运行,但是不可依赖!,如果此子模块需要被依赖,那么还需加上这句代码(不加会报找不到类的错误)。entiy模块需要加,其他子模块不要加
感谢大佬的文章

<classifier>exec</classifier>
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                     <classifier>exec</classifier>
                    <!-- 指定该Main Class为全局的唯一入口 -->
                    <mainClass>com.zzh.demo.EntiyApplication</mainClass>
                    <layout>ZIP</layout>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

配置子模块注意点四 : 指定以下bulid的编码规则

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

完整的web模块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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zzh</groupId>
    <artifactId>web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>web</name>
    <packaging>jar</packaging>
    <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.zzh</groupId>
            <artifactId>entiy</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <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>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- 指定该Main Class为全局的唯一入口 -->
                    <mainClass>com.zzh.demo.WebApplication</mainClass>
                    <layout>ZIP</layout>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

完整的eduService模块pom文件(由于service中配置了springboot的打包插件,由于可以依赖传递,这里可以不用配置打包插件)

<?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>service</artifactId>
        <groupId>com.zzh</groupId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>eduService</artifactId>
    <packaging>jar</packaging>
    <name>eduService</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
</project>

编写测试代码

entiy模块
编写entiyTest类,并且自己调用自己的entiyTest

public class entiyTest {
    
    
    public void showEntiyTest() {
    
    
        System.out.println("调用showEntiyTest成功!!");
    }
}
@RestController
@RequestMapping("/entiy")
public class entiyController {
    
    
    @RequestMapping("/test")
    public String testEntiy() {
    
    
        System.out.println("entiySucess");
        return "entiySucess";
    }
}

在这里插入图片描述

web模块(前提:我们引入了entiy模块的)

@RestController
@RequestMapping("/web")
public class controller {
    
    
    @RequestMapping("/test")
    public String testWeb() {
    
    
        entiyTest entiyTest = new entiyTest();
        entiyTest.showEntiyTest();
        return "webSucess";
    }
}

在这里插入图片描述

eduService模块(前提:我们引入了entiy模块的)

@RestController
@RequestMapping("/eduService")
public class controller {
    
    
    @RequestMapping("/test")
    public String testWeb() {
    
    
        entiyTest entiyTest = new entiyTest();
        entiyTest.showEntiyTest();
        return "eduService";
    }
}

在这里插入图片描述

终极打包了

直接点clean接着点package或者install一键打包就ok了?
在这里插入图片描述
然后你会发现你会报这个错哈哈哈哈哈哈
在这里插入图片描述
解决办法,点一下这个在clean、package(maven的编译打包检查:关闭点一下就可以了,忽略检查测试文件
可以参考这个文章
在这里插入图片描述
这下就真的打包成功啦开心吧哈哈哈。输入java -jar 然后按tab键就可以切换jar包名字

在这里插入图片描述
全部启动okk了,注意启动entiy模块的这个jar包,这个才是可执行的jar包另外一个是可依赖的jar包

在需要对外提供依赖的项目的pom里设置(如本项目的xxx-a、xxx-b),这样设置会让项目生成两个jar:一个可执行jar,一个可依赖的jar;

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <!-- 生成可执行的jar的名字:xxx-exec.jar -->
                <!-- 不固定,写成abcd都可以 -->
                <classifier>exec</classifier>
            </configuration>
        </plugin>
    </plugins>
</build>

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
项目源码链接,点个start吧老妹们哈哈哈哈

猜你喜欢

转载自blog.csdn.net/qq_42875345/article/details/110790933