简体   繁体   English

OSGi / BND:如何从捆绑包生成中排除类?

[英]OSGi/BND: How to exclude classes from bundle generation?

I have a bundle project (Eclipse) that has the following structure: 我有一个具有以下结构的捆绑项目(Eclipse):

 src/main/java Bundle source files here src/test/java Bundle internal test cases 

When I try to make the bundle I get an error that there are some "Unsolved references". 当我尝试制作捆绑包时,出现一个错误,提示有一些“未解决的引用”。 The error is causes by the internal test classes. 错误是由内部测试类引起的。 How do I configure BND to ignore those classes? 如何配置BND忽略这些类?

Splitting the test cases into a separate project is not an option as the test cases are of a much finer granularity as the API provided by the bundle. 不能将测试用例拆分到一个单独的项目中,因为测试用例的粒度比捆绑软件提供的API的粒度要好得多。

The problem is that Eclipse is compiling both src/main/java and src/test/java into the same output directory, probably "bin", and Bnd works by scanning the compiled .class files in the bin folder. 问题在于Eclipse会将src/main/javasrc/test/java都编译到同一个输出目录(可能是“ bin”)中,并且Bnd通过扫描bin文件夹中的已编译.class文件来工作。

You can fix this by following these steps: 您可以按照以下步骤解决此问题:

  1. Right click on src/test/java and select Build Path > Configure Output Folder... 右键单击src/test/java然后选择“构建路径”>“配置输出文件夹...”。

  2. Click "specific output folder" and enter a directory name such as bin_tests . 单击“特定输出文件夹”,然后输入目录名称,例如bin_tests

Now Eclipse will compile the test classes into a separate folder, and Bnd will only see the real classes. 现在,Eclipse将把测试类编译到一个单独的文件夹中,而Bnd将仅看到真实的类。

The good approach for unit tests in OSGi is to use fragments. 在OSGi中进行单元测试的好方法是使用片段。 Therefore you could put your tests in a fragment bundle, and you wouldn't have this issue anymore. 因此,您可以将测试放在一个片段包中,并且不再有此问题。 And moreover, the tests will have access to all the classes and not only to APIs as it would be the case if you put them in a simple bundle 而且,测试将可以访问所有类,而不仅是对API的访问,如果将它们放在一个简单的包中会是这种情况。

I assume that your test classes are included in the jar bundle file (what should not be the case). 我假设您的测试类包含在jar捆绑文件中(事实并非如此)。 Check the content of your jar file and modify the package process to not include the test classes (a maven build does not include src/test/java ). 检查jar文件的内容并修改打包过程以不包含测试类(maven构建不包含src/test/java )。

I sometimes noticed this behaviour when using m2eclipse to package my project whereas running maven from the command line worked well. 当使用m2eclipse打包我的项目时,有时我会注意到这种现象,而从命令行运行maven效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM