spring boot 项目 maven 打包问题请教 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
cxsz
0.45D
V2EX    Java

spring boot 项目 maven 打包问题请教

  •  
  •   cxsz 2023 年 10 月 13 日 3408 次点击
    这是一个创建于 924 天前的主题,其中的信息可能已经有所发展或是发生改变。

    分了多个模块,exam-web 里面有 @SpringBootApplication 类作为启动类,exam-web 里面和 exam-file 模块里面都有 Controller

    在 idea 里面运行的时候,可以正常访问 exam-file 模块的 controller 接口,但 mvn install 打包以后,java -jar 启动 exam-web target 下的 jar 包,访问不到 exam-file 模块的接口, 解压 jar 包看了下是没有打包进去,exam-web 的 dependencies 里面已经添加了 exam-file 模块

    想请教下,如何才能全部打包到一个 jar 里面

    项目结构如下

    ├─.idea ├─.mvn │ └─wrapper ├─config ├─exam-common │ ├─src ├─exam-file │ └─src │ └─main │ └─java ├─exam-web │ └─src │ ├─main │ │ ├─java │ │ └─resources │ └─test └─logs ├─debug ├─error └─info 

    各个 pom.xml 文件内容(太长,省略了一些第三方依赖包):

    • 最外层的父级 pom.xml
     <modules> <module>exam-common</module> <module>exam-web</module> <module>exam-file</module> </modules> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.lddq</groupId> <artifactId>exam-web</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.lddq</groupId> <artifactId>exam-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.lddq</groupId> <artifactId>exam-file</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>17</source> <target>17</target> <encoding>UTF-8</encoding> <annotationProcessorPaths> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> </path> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${mapstruct.version}</version> </path> </annotationProcessorPaths> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot.version}</version> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> 
    • exam-web pom.xml
     <dependencies> <dependency> <groupId>com.lddq</groupId> <artifactId>exam-common</artifactId> </dependency> <dependency> <groupId>com.lddq</groupId> <artifactId>exam-file</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>17</source> <target>17</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot.version}</version> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> 
    • exam-file pom.xml
     <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot.version}</version> <configuration> <mainClass>com.lddq.exam.start.ExamApplication</mainClass> </configuration> </plugin> </plugins> </build> 
    • exam-common pom.xml
     <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </build> 
    8 条回复    2023-10-14 21:25:19 +08:00
    zsdroid
        1
    zsdroid  
       2023 年 10 月 13 日   1
    没有扫描到呗。启动类里加上 exam-file 的包名。
    zsdroid
        2
    zsdroid  
       2023 年 10 月 13 日   1
    另外<module>exam-file</module>放在<module>exam-web</module>前面
    retanoj
        3
    retanoj  
       2023 年 10 月 13 日   1
    在 idea 里运行的时候,观察一下运行的 java 命令。你会发现
    java -classpath 里跟了一堆.jar 包
    hai046
        4
    hai046  
       2023 年 10 月 13 日   1
    胖 jar
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.6.0</version>
    <executions>
    <execution>
    <id>copy-dependencies</id>
    <phase>package</phase>
    <goals>
    <goal>copy-dependencies</goal>
    </goals>
    <configuration>
    <outputDirectory>${project.build.directory}/outputs</ouputDirectory>
    </configuration>
    </execution>
    </executions>
    </plugin>
    ikas
        5
    ikas  
       2023 年 10 月 13 日   1
    parent :

    <?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.lddq</groupId>
    <artifactId>exam-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
    <java.version>17</java.version>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring-boot.version>3.1.0</spring-boot.version>
    </properties>

    <modules>
    <module>exam-common</module>
    <module>exam-web</module>
    <module>exam-file</module>
    </modules>

    <dependencyManagement>
    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>${spring-boot.version}</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>
    </dependencies>
    </dependencyManagement>

    <build>
    <pluginManagement>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.3.1</version>
    </plugin>

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.11.0</version>
    <configuration>
    <showWarnings>true</showWarnings>
    <showDeprecation>true</showDeprecation>
    <annotationProcessorPaths>
    <path>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.26</version>
    </path>
    </annotationProcessorPaths>
    </configuration>
    </plugin>

    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${spring-boot.version}</version>
    <executions>
    <execution>
    <id>repackage</id>
    <goals>
    <goal>repackage</goal>
    </goals>
    </execution>
    </executions>
    </plugin>
    </plugins>
    </pluginManagement>

    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    </plugin>

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    </plugin>
    </plugins>
    </build>

    </project>


    common:

    <?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>

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

    <packaging>jar</packaging>
    <artifactId>exam-common</artifactId>

    </project>

    file:

    <?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>

    &t;parent>
    <groupId>com.lddq</groupId>
    <artifactId>exam-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </parent>

    <packaging>jar</packaging>
    <artifactId>exam-file</artifactId>

    <dependencies>
    <dependency>
    <groupId>com.lddq</groupId>
    <artifactId>exam-common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </dependency>
    </dependencies>

    </project>

    web:

    <?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>

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

    <packaging>jar</packaging>
    <artifactId>exam-web</artifactId>

    <dependencies>
    <dependency>
    <groupId>com.lddq</groupId>
    <artifactId>exam-common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </dependency>

    <dependency>
    <groupId>com.lddq</groupId>
    <artifactId>exam-file</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </dependency>

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

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

    </dependencies>

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

    </project>

    ----
    java -jar exam-web-0.0.1-SNAPSHOT.jar
    2023-10-13T21:16:58.496+08:00 INFO 18832 --- [ main] com.lddq.exam.start.ExamApplication : Started ExamApplication in 2.102 seconds (process running for 2.557)
    2023-10-13T21:16:58.500+08:00 INFO 18832 --- [ main] com.lddq.exam.start.ExamApplication : hi file!
    chocotan
        6
    chocotan  
       2023 年 10 月 13 日   1
    spring-boot 的 maven plugin 从 parent 里面删掉
    diagnostics
        7
    diagnostics  
       2023 年 10 月 14 日   1
    spring-boot-maven-plugin 是普通 Spring 应用打包所有依赖到同一个包的插件。
    你在 Root 的 POM 里面定义了 build 里面用了它,意味着所有子模块都会继承这个插件。

    正确做法是:在 Root 里移除 spring-boot-maven-plugin ,放到 Web 项目里面就好,其他的模块你不用来发布出去给其他人用的话,只定义一个 maven-compiler-plugin ,能够编译出来就可以了。。。
    cxsz
        8
    cxsz  
    OP
       2023 年 10 月 14 日
    @chocotan #6
    @diagnostics #7

    感谢,成功搞定
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2948 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 60ms UTC 06:44 PVG 14:44 LAX 23:44 JFK 02:44
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86