You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven打包缺失同Pom树自定义构件MyElectricity-aWattar求助

Maven多模块打包问题:可执行Jar缺失自身代码

我搭建了一个Maven多模块项目,结构如下:

  • 父POM项目:MyElectricity(packaging为pom)
  • 子Jar项目1:MyElectricity-Common,存放公共类和JAXB/Jakarta绑定逻辑
  • 子Jar项目2:MyElectricity-aWattar,存放核心业务逻辑,需要打包成可独立运行的Jar

执行MyElectricity-aWattar的install命令后,生成的可执行Jar里包含了所有依赖(包括自定义的MyElectricity-Common、SpringBoot等公共依赖),但唯独缺失MyElectricity-aWattar自身的代码,求排查思路。


父项目MyElectricity的POM配置

<groupId>de.gombers.electricity</groupId>
<artifactId>MyElectricity</artifactId>
<packaging>pom</packaging>
<properties>
  <revision>1.0.0</revision>
</properties>

<parent>
  <groupId>de.gombers.myhome</groupId>
  <artifactId>MyHome</artifactId>
  <version>1.0.0</version>
</parent>

<modules>
  <module>MyElectricity-aWattar</module>
  <module>MyElectricity-Common</module>
</modules>

MyElectricity-Common的POM配置

<artifactId>MyElectricity-Common</artifactId>

<parent>
    <groupId>de.gombers.electricity</groupId>
    <artifactId>MyElectricity</artifactId>
    <version>1.0.0</version>
</parent>
<build>
    <finalName>Common</finalName>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>xjc</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <clearOutputDir>true</clearOutputDir>
                <extension>true</extension>
                <sources>
                    <!-- only xsd files under here -->
                    <source>src/main/xsd</source>
                </sources>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.glassfish.jaxb</groupId>
                    <artifactId>jaxb-xjc</artifactId>
                    <version>${version.maven.org.glassfish.jaxb}</version>
                </dependency>
                <dependency>
                    <groupId>jakarta.activation</groupId>
                    <artifactId>jakarta.activation-api</artifactId>
                    <version>${version.maven.jakarta.activation.api}</version>
                </dependency>
                <dependency>
                    <groupId>jakarta.xml.bind</groupId>
                    <artifactId>jakarta.xml.bind-api</artifactId>
                    <version>${version.maven.jakarta.xml.bind-api}</version>
                </dependency>
                <dependency>
                    <groupId>com.sun.xml.bind</groupId>
                    <artifactId>jaxb-impl</artifactId>
                    <version>${version.maven.jaxb-impl}</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

MyElectricity-aWattar的POM配置

<artifactId>MyElectricity-aWattar</artifactId>
    
<parent>
    <groupId>de.gombers.electricity</groupId>
    <artifactId>MyElectricity</artifactId>
     <version>1.0.0</version>
</parent>

<build>
    <finalName>aWattar</finalName>
    
    <plugins>
        <plugin>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <encoding>UTF-8</encoding>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>
            </configuration>
        </plugin>
        
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>de.gombers.electricity.awattar.MainDataGatherer</mainClass>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        
    </plugins>
</build>
    

<dependencies>
    <dependency>
        <groupId>de.gombers.electricity</groupId>
        <artifactId>MyElectricity-Common</artifactId>
        <version>${revision}</version>
    </dependency>
</dependencies>

环境信息

  • Java版本:17
  • IDE:Eclipse 2024-09
  • Maven版本:Apache Maven 3.9.9

排查&解决思路

  1. 检查模块编译产物
    查看MyElectricity-aWattar的target/classes目录,确认是否存在编译后的class文件:

    • 若不存在,检查源码路径是否符合Maven规范(src/main/java下是否有业务代码)
    • 执行mvn clean compile,查看编译日志是否有报错信息
  2. 调整Spring Boot Maven插件配置
    当前插件仅配置repackage目标,可能导致原Jar的自身代码未被正确合并。添加jar目标,确保先打包出包含自身代码的Jar,再进行可执行Jar的重打包:

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <mainClass>de.gombers.electricity.awattar.MainDataGatherer</mainClass>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>jar</goal>
                    <goal>repackage</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    
  3. 确认模块构建顺序
    父项目modules顺序为aWattar在前、Common在后,但aWattar依赖Common。建议先构建Common模块:

    • 在父项目执行mvn clean install -pl MyElectricity-Common -am,再单独构建aWattar模块
  4. 验证可执行Jar结构
    用解压工具打开生成的aWattar.jar,查看BOOT-INF/classes目录下是否存在自身业务类。若不存在,删除target目录后重新执行mvn clean install

内容的提问来源于stack exchange,提问作者Ulrich

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 11:37:31