多模块Tycho项目中测试片段依赖编译失败问题求助
多模块Tycho项目测试片段依赖构建问题
项目结构
模块配置如下:
<modules> <module>inez.target</module> <module>inez.parser</module> <module>inez.parser.tests</module> <module>inez.model</module> <module>inez.model.tests</module> </modules>
- 所有模块的
artifactId、Bundle-SymbolicName和Automatic-Module-Name均带有groupId前缀(例如inez.model.tests对应值为io.github.magwas.inez.model.tests) inez.target打包类型为eclipse-target-definition,仅包含目标定义inez.parser及后续模块打包类型为eclipse-plugininez.parser.tests是片段模块,宿主为inez.parserinez.model.tests是片段模块,宿主为inez.model
问题描述
inez.model.tests需要引用inez.parser.tests中的代码,当前在MANIFEST.MF中添加了Require-Bundle: io.github.magwas.inez.parser。
但首次执行mvn install或添加参数-Dtycho.localArtifacts=ignore时编译失败,编译器无法找到inez.parser.tests中的类;多次执行mvn install则能成功构建。
已尝试的无效方案:
- 在
inez.model.tests的build.properties中添加extra.. = platform:/fragment/io.github.magwas.inez.parser - 在
inez.model.tests的pom.xml的target-platform-configuration中添加extraRequirements直接引用测试片段
相关配置文件
inez.model.tests的MANIFEST.MF
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Testing Bundle-SymbolicName: io.github.magwas.inez.model.tests Bundle-Version: 0.4.18 Fragment-Host: io.github.magwas.inez.model;bundle-version="0.0.1" Import-Package: io.github.magwas.inez.parse, io.github.magwas.inez.parser Require-Bundle: org.mockito.mockito-core;bundle-version="5.18.0", wrapped.org.springframework.spring-test;bundle-version="6.2.9", wrapped.io.github.magwas.konveyor-base-testing;bundle-version="0.4.0", io.github.magwas.inez.parser, junit-jupiter-api;bundle-version="5.11.4" Automatic-Module-Name: io.github.magwas.inez.model.tests Bundle-RequiredExecutionEnvironment: JavaSE-21
inez.parser.tests的MANIFEST.MF
Bundle-ManifestVersion: 2 Bundle-Name: Testing Bundle-SymbolicName: io.github.magwas.inez.parser.tests Bundle-Version: 0.4.18 Fragment-Host: io.github.magwas.inez.parser;bundle-version="0.0.1" Require-Bundle: org.mockito.mockito-core;bundle-version="5.18.0", wrapped.io.github.magwas.konveyor-base-testing;bundle-version="0.4.0", junit-jupiter-api;bundle-version="5.11.4" Automatic-Module-Name: io.github.magwas.inez.parser.tests Bundle-RequiredExecutionEnvironment: JavaSE-21
解决方案
1. 修正测试片段的导出与依赖配置
测试片段默认不会被其他模块识别,首先得让inez.parser.tests导出需要被引用的类,同时让inez.model.tests明确依赖这个测试片段:
- 编辑
inez.parser.tests的MANIFEST.MF,添加导出包声明(用x-internal标记仅用于测试场景):
Export-Package: io.github.magwas.inez.parser.tests;x-internal:=true
- 在
inez.model.tests的MANIFEST.MF中,把测试片段加入Require-Bundle:
Require-Bundle: ..., io.github.magwas.inez.parser.tests;bundle-version="0.4.18"
2. 配置Tycho目标平台包含测试片段
Tycho默认不会将测试片段纳入目标平台,需在配置中明确指定:
在inez.model.tests的pom.xml里,修改target-platform-configuration部分:
<plugin> <groupId>org.eclipse.tycho</groupId> <artifactId>target-platform-configuration</artifactId> <version>${tycho.version}</version> <configuration> <dependency-resolution> <extraRequirements> <requirement> <type>eclipse-plugin</type> <id>io.github.magwas.inez.parser.tests</id> <versionRange>[0.4.18,0.4.19)</versionRange> </requirement> </extraRequirements> </dependency-resolution> </configuration> </plugin>
3. 显式指定模块依赖(可选)
虽然Tycho通常会自动按依赖排序,但显式添加依赖能避免首次构建的顺序异常:
在inez.model.tests的pom.xml中添加依赖声明:
<dependencies> <dependency> <groupId>io.github.magwas</groupId> <artifactId>inez.parser.tests</artifactId> <version>0.4.18</version> <type>eclipse-plugin</type> </dependency> </dependencies>
4. 彻底清理后验证
执行以下命令彻底清理本地构建缓存,再重新构建验证问题是否解决:
mvn clean install -Dtycho.localArtifacts=ignore
内容的提问来源于stack exchange,提问作者Árpád Magosányi
相关产品推荐
相关产品推荐

