Maven JavaFX子模块无法解析父模块依赖求助
Maven多模块项目依赖错误解决
问题描述
我维护一个包含JavaFX子模块的Maven多模块项目,创建Git仓库后出现依赖解析错误,子模块无其他功能性改动,曾怀疑<build>配置问题但尝试调整无效。执行mvn clean javafx:run运行User模块时,抛出以下错误:
[ERROR] Failed to execute goal on project User: Could not resolve dependencies for project org.example:User:pom:1.0.1: Failure to find org.example:ChoreSystem:jar:1.0.1 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced
父模块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"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>ChoreSystem</artifactId> <packaging>pom</packaging> <version>1.0.1</version> <modules> <module>ChoreServer</module> <module>User</module> </modules> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <dependencies> ... </dependencies> </project>
子模块User的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>ChoreSystem</artifactId> <groupId>org.example</groupId> <version>1.0.1</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>User</artifactId> <packaging>pom</packaging> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> <version>17-ea+11</version> </dependency> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-fxml</artifactId> <version>17-ea+11</version> </dependency> <dependency> <groupId>org.example</groupId> <artifactId>ChoreSystem</artifactId> <version>1.0.1</version> <scope>compile</scope> </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> </configuration> </plugin> <plugin> <groupId>org.openjfx</groupId> <artifactId>javafx-maven-plugin</artifactId> <version>0.0.6</version> <executions> <execution> <!-- Default configuration for running with: mvn clean javafx:run --> <id>default-cli</id> <configuration> <mainClass>AppLauncher</mainClass> </configuration> </execution> </executions> </plugin> </plugins> </build>
错误原因
- 父模块
ChoreSystem的打包方式为pom,仅作为多模块聚合与继承的父项目,不会生成jar包。 - 子模块User中错误地添加了对父模块的
jar类型依赖,Maven在本地仓库和中央仓库找不到对应的jar文件,导致依赖解析失败。 - 子模块User的打包方式设置为
pom,不符合JavaFX应用模块的打包要求,无法生成可执行的应用包。
解决方案
删除子模块中对父模块的无效依赖
子模块通过<parent>标签已经继承了父模块的配置,无需再添加显式依赖,删除以下代码块:<dependency> <groupId>org.example</groupId> <artifactId>ChoreSystem</artifactId> <version>1.0.1</version> <scope>compile</scope> </dependency>修正子模块的打包方式
将User模块的<packaging>从pom改为jar,确保Maven生成正确的应用包:<packaging>jar</packaging>按顺序执行Maven命令
- 先在项目根目录执行
mvn clean install,完成所有模块的构建并安装到本地仓库。 - 进入User模块目录,执行
mvn clean javafx:run启动JavaFX应用。
- 先在项目根目录执行
内容的提问来源于stack exchange,提问作者Capnsockless
相关产品推荐
相关产品推荐

