在Spring Boot应用程序中找不到ruby gem

时间:2018-01-05 06:18:30

标签: java spring-boot jruby

我创建了一个jar文件,其中包含一个由Spring服务包装的Ruby gem(activemerchant),它使用JRuby ScriptContainer调用一个利用ruby gem的小Ruby脚本。对于这个jar的单元测试都可以正常使用Ruby gem - 宝石被拾取并按预期运行。

jar包含以下文件夹:

com
    ... <not relevant> ...
gateways
    ... <not relevant> ...
gems
    activemerchant-1.75.0
    activesupport-5.2.0.beta2
    builder-3.2.3
    concurrent-ruby-1.0.5-java
    i18n-0.9.1
    minitest-5.11.1
    nokogiri-1.8.1-java
    thread_safe-0.3.6-java
    tzinfo-1.2.4
META-INF
    MANIFEST.MF
scripts
    main.rb
specifications
    activemerchant-1.75.0.gemspec
    activesupport-5.2.0.beta2.gemspec
    builder-3.2.3.gemspec
    concurrent-ruby-1.0.5-java.gemspec
    i18n-0.9.1.gemspec
    minitest-5.11.1.gemspec
    nokogiri-1.8.1-java.gemspec
    thread_safe-0.3.6-java.gemspec
    tzinfo-1.2.4.gemspec

jar嵌入到使用嵌入式Tomcat容器的Spring Boot应用程序中。在启动Spring Boot应用程序时,我收到错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'activeMerchantGatewayProvider': Invocation of init method failed; nested exception is org.jruby.embed.EvalFailedException: (LoadError) no such file to load -- activemerchant
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:220)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:334)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1088)
    at com.mydomain.financial.gateway.GatewayService.lambda$init$0(GatewayService.java:39)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
    at com.mydomain.financial.gateway.GatewayService.init(GatewayService.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:365)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:310)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133)
    ... 81 common frames omitted
Caused by: org.jruby.embed.EvalFailedException: (LoadError) no such file to load -- activemerchant
    at org.jruby.embed.internal.EmbedEvalUnitImpl.run(EmbedEvalUnitImpl.java:131)
    at org.jruby.embed.ScriptingContainer.runUnit(ScriptingContainer.java:1307)
    at org.jruby.embed.ScriptingContainer.runScriptlet(ScriptingContainer.java:1352)
    at com.mydomain.financial.gateway.activemerchant.ActiveMerchantGatewayProvider.init(ActiveMerchantGatewayProvider.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:365)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:310)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133)
    ... 108 common frames omitted
Caused by: org.jruby.exceptions.RaiseException: (LoadError) no such file to load -- activemerchant
    at org.jruby.RubyKernel.require(org/jruby/RubyKernel.java:955)
    at uri_3a_classloader_3a_.META_minus_INF.jruby_dot_home.lib.ruby.stdlib.rubygems.core_ext.kernel_require.require(uri:classloader:/META-INF/jruby.home/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb:55)
    at RUBY.<main>(classpath:/scripts/main.rb:2)

如何让Spring Boot嵌入式Tomcat / JRuby识别嵌入式jar中的宝石?

1 个答案:

答案 0 :(得分:0)

什么是PITA。我找到了一个工作,但需要花费一些力气。

失败的原因是JRuby无法扫描内部jar文件中的宝石。而这似乎是Java的问题,而不是JRuby的问题。我也无法找到一种方法来扫描jar中jar的目录,尽管我可以从jar中的jar中获取文件。我的解决方案是在Spring jar应用程序启动期间在内部jar中创建一个gems列表并检索该列表文件。

具体做法是:

在内部jar中,我更改了ScriptContainer逻辑,如下所示(GEMS_PATH = / gems):

@PostConstruct
private void init() {
    ScriptingContainer scriptingContainer = new ScriptingContainer(LocalContextScope.CONCURRENT);

    // Locate load paths and add them to the container. This doesn't work in JRuby as expected
    // once this jar is embedded into a Spring Boot uber project, so I need to do it explicitly
    // here.
    List<String> loadPaths = scriptingContainer.getLoadPaths();
    URL resource = getClass().getResource(GEMS_PATH + "/gems.list");
    if (resource == null) {
        throw new RuntimeException("Unable to find " + GEMS_PATH + "/gems.list");
    }

    try {
        log.debug("ActiveMerchant gems root: {}", resource);
        String content = IOUtils.toString(resource, "UTF-8");
        if (StringUtils.isEmpty(content)) {
            throw new RuntimeException(GEMS_PATH + "/gems.list is empty");
        }

        Stream.of(content.split(";")).forEach(gem -> {
            String gemDir = "uri:classloader:" + GEMS_PATH + "/" + gem + "/lib";
            if (!loadPaths.contains(gemDir)) {
                loadPaths.add(gemDir);
                log.debug("ActiveMerchant added gem entry to load paths: {}", gemDir);
            }
        });
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

正如您所看到的,这是内部jar中Spring管理组件的一部分。

在它的pom.xml中,我添加了这个插件:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <phase>prepare-package</phase>
            <configuration>
                <target>
                    <dirset id="gems-dir" dir="${project.build.outputDirectory}/gems" includes="*"/>
                    <property name="gem-dirs" refid="gems-dir"/>
                    <echo file="${project.build.outputDirectory}/gems/gems.list">${gem-dirs}</echo>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

这将在内部jar中创建文件/gems/gems.list,其中包含打包到jar中的所有gem。当Spring Boot初始化组件时,它会找到gems.list并使用它来构建加载路径列表。