gpt4 book ai didi

java - 使用 pax-maven-plugin 构建时将我自己的包源添加到 pax-exam

转载 作者:搜寻专家 更新时间:2023-10-31 19:48:30 25 4
gpt4 key购买 nike

我正在尝试使用 pax-maven-build 构建我的 OSGI 包同时用 pax-exam 测试它.它提供了一些 bundle ,我可以使用以下 pax-exam 测试配置进行测试:

@RunWith(JUnit4TestRunner.class)
@ExamReactorStrategy(AllConfinedStagedReactorFactory.class)
public class OSGILoaderTest {

@Inject
protected BundleContext bundleContext;

@Configuration
public Option[] config() throws MalformedURLException {


String projectRoot = // a path to my project

return options(
junitBundles(),
equinox(),
bundle(projectRoot + "libs/org.eclipse.core.variables_3.2.500.v20110511.jar"),
bundle(projectRoot + "libs/org.eclipse.core.contenttype_3.4.100.v20110423-0524.jar"),
bundle(projectRoot + "libs/org.eclipse.core.expressions_3.4.300.v20110228.jar"),
// etc...
);
}

@Test
public void getBundleContext() throws RodinDBException {
IRodinDB rodinDB = RodinCore.getRodinDB();
assertNotNull(rodinDB);
}
}

在这里,我可以看到我可以从我提供的 jar 访问 IRodinDB 实例。

现在我编写了自己的包,它将使用所有提供的 jar。但我什至无法测试自己的代码,例如:

@Test
public void checkAccessToRodinDbTest() {
VTGService service = null;
assertTrue(true);
}

编译时报错:

[ERROR] Failed to execute goal org.ops4j:maven-pax-plugin:1.5:testCompile (default-testCompile) : Compilation failure

[ERROR] cannot find symbol

[ERROR] symbol : class VTGService

似乎测试编译看不到“src/main/java”,这与 maven-compiler-plugin 的默认行为相反。但就我而言,您可以看到 maven 不使用编译器插件而是使用 maven-pax-plugin。

问题是:我如何使用 pax-exam 测试我自己的包?

更新1

这似乎是最近版本的 maven-pax-plugin 的问题,如 the basic example available in ops4j pax maven plugin (在 在 POM 中使用 Pax 插件部分)似乎遇到了同样的问题。

更新2

根据 Dmytro 的要求,这是我的包的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<parent>
<relativePath>../poms/compiled/</relativePath>
<groupId>fr.xlim.ssd.vtg.build</groupId>
<artifactId>compiled-bundle-settings</artifactId>
<version>0.1-SNAPSHOT</version>
</parent>

<properties>
<bundle.symbolicName>fr.xlim.ssd.vtg.bundle</bundle.symbolicName>
<bundle.namespace>fr.xlim.ssd.vtg.bundle</bundle.namespace>
</properties>

<modelVersion>4.0.0</modelVersion>
<groupId>fr.xlim.ssd.vtg</groupId>
<artifactId>fr.xlim.ssd.vtg.bundle</artifactId>
<version>0.1-SNAPSHOT</version>

<name>${bundle.symbolicName}</name>

<packaging>bundle</packaging>

<dependencies>

<dependency>
<type>pom</type>
<groupId>${project.parent.groupId}</groupId>
<artifactId>provision</artifactId>
<optional>true</optional>
</dependency>

<!-- not needed as equinox bundle are available in provision -->
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi_R4_core</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi_R4_compendium</artifactId>
<optional>true</optional>
</dependency-->

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam</artifactId>
<version>2.3.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-junit4</artifactId>
<version>2.3.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-inject</artifactId>
<version>2.3.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.ops4j.pax.url</groupId>
<artifactId>pax-url-mvn</artifactId>
<version>1.3.5</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-container-native</artifactId>
<version>2.3.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-link-mvn</artifactId>
<version>2.3.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.0</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.0.0</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>

</project>

我不确定这是最优雅的解决方案,但当我可以像问题的源代码中一样导入自己的包时,我创建了一个新的 maven 项目。

有没有一种优雅的方法可以直接将我自己的 java 源代码作为新包添加到同一个 Maven 项目中进行测试?这可能是不可能的(因为捆绑组装操作是在编译和测试之后完成的)...

最佳答案

我使用以下设置来配置被测包。配置测试时,我使用 reference-protocol 提供包(这是 Equinox 和 Felix 的非标准功能,请参阅 here):

@Configuration
public Option[] config() {

return options(
bundle("reference:file:target/classes"),
junitBundles(),
felix()
);
}

当您将 knopplerfish() 指定为环境时,测试用例也会运行。我想那是因为 URL 是由 Pax Exam 解析的,而不是由 OSGi 运行时解析的。我用 maven-bundle-plugin构建我的包。要使其按预期工作,您必须添加以下配置:

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
<executions>
<!-- This execution makes sure that the manifest is available
when the tests are executed -->
<execution>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

否则 list 在测试运行时将不可用,因为默认情况下它是在 package 阶段生成的。

我希望我没有忘记任何事情 - 如果它对您有用,请告诉我!

关于java - 使用 pax-maven-plugin 构建时将我自己的包源添加到 pax-exam,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10120277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com