简体   繁体   English

为什么Eclipse JUnit4 Runner没有选择META-INF / services文件

[英]Why does the Eclipse JUnit4 Runner not pick up META-INF/services files

I am using Maven with Eclipse (using M2E) to build a project that relies on java.util.ServiceLoader to dynamically load some factory classes. 我正在使用Maven和Eclipse(使用M2E)构建一个依赖java.util.ServiceLoader来动态加载一些工厂类的项目。 It works fine when I run it in Maven, but when I run a test using the inbuilt Eclipse JUnit4 Runner, it fails to pick up the services and the tests do not succeed. 当我在Maven中运行它时它工作正常,但是当我使用内置的Eclipse JUnit4 Runner运行测试时,它无法获取服务并且测试不成功。

Is there something I need to do to manually add the META-INF/services to the JUnit build path? 我需要做些什么来手动将META-INF /服务添加到JUnit构建路径吗? I couldn't make it work in either src/main/resources/META-INF/services or src/main/java/META-INF/services. 我无法在src / main / resources / META-INF / services或src / main / java / META-INF / services中使用它。 Is it related to the way M2E sets up the build path? 它与M2E建立构建路径的方式有关吗? I made up the test in a completely new project and it still failed. 我在一个全新的项目中完成了测试,但仍然失败了。

The basic code that is failing is: 失败的基本代码是:

public class TestServiceLoader<S>
{
   public TestServiceLoader(final Class<S> serviceClass)
   {

       ServiceLoader<S> serviceLoader =
            java.util.ServiceLoader.load(serviceClass, serviceClass.getClassLoader());

       Iterator<S> services = serviceLoader.iterator();

       if(!services.hasNext())
       {
           throw new RuntimeException("Failed to get any services for this class");
       }
   }
}

The test looks like: 测试看起来像:

@Test
public final void testTestServiceLoader()
{
    TestServiceLoader<TestFactory> serviceLoader = new TestServiceLoader<TestFactory>(TestFactory.class);
}

TestFactory is defined as: TestFactory定义为:

public interface TestFactory
{

}

TestFactoryImpl is defined as: TestFactoryImpl定义为:

public class TestFactoryImpl implements TestFactory
{

}

I originally was using the MetaInfServices generator from http://weblogs.java.net/blog/kohsuke/archive/2009/03/my_project_of_t.html but when I removed that and manually created the files by hand, it was still failing in the same way for Eclipse while succeeding when run using the Maven Surefire plugin. 我最初使用的是来自http://weblogs.java.net/blog/kohsuke/archive/2009/03/my_project_of_t.html的MetaInfServices生成器,但当我删除它并手动手动创建文件时,它仍然在在使用Maven Surefire插件运行时成功使用Eclipse的方式相同。

The M2E developers appear to believe that any resources will affect a maven build, even in the case where META-INF/services/ is a functional part, albeit a resource: M2E开发人员似乎相信任何资源都会影响maven构建,即使在META-INF / services /是功能部分的情况下,尽管是资源:

"Actually project resource folder doesn't really need to be added to the buildpath (Maven Builder is going to work without it), but it been considered convenient and look better in the Package Explorer and other Eclipse views." “实际上,项目资源文件夹并不需要添加到构建路径中(Maven Builder在没有它的情况下可以正常工作),但在Package Explorer和其他Eclipse视图中它被认为是方便且看起来更好。” M2E FAQ M2E常见问题

If you want to hack around it, you can apparently encode a special M2E profile into your pom.xml files to make the M2E incremental build filter and copy resources Sonatype M2Eclipse Wiki 如果你想破解它,你可以显然将一个特殊的M2E配置文件编码到你的pom.xml文件中,以制作M2E增量构建过滤器和复制资源Sonatype M2Eclipse Wiki

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

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