如何在 OSGi 中 运行 时将 groovy-all 从 2.4 升级到 2.5?

How to upgrade groovy-all from 2.4 to 2.5 when running in OSGi?

我正在维护一个部分基于 Groovy 的开源库,名为 Rest Assured. In the next version I'd like to upgrade the Groovy dependency from 2.4.x to 2.5.x. However when doing this I run into issues when running the OSGi tests. The tests are using Pax Exam they typically look something like this:

@RunWith(PaxExam.class)
public class XmlPathOSGiITest {

    @Configuration
    public static Option[] configure() {
        return new Option[]
                {
                        mavenBundle("org.apache.servicemix.bundles", "org.apache.servicemix.bundles.hamcrest", "1.3_1"),
                        junitBundles(),
                        systemProperty("pax.exam.osgi.unresolved.fail").value("true"),
                        systemProperty("org.ops4j.pax.logging.DefaultServiceLog.level").value("INFO"),

                        /* Transitive dependencies needed in the Pax Exam container.
                        Some of these need to be wrapped because they are not available as OSGi bundles */
                        mavenBundle("org.apache.commons", "commons-lang3").versionAsInProject(),
                        wrappedBundle(mavenBundle().groupId("org.ccil.cowan.tagsoup").artifactId("tagsoup").versionAsInProject()),
                        wrappedBundle(mavenBundle("javax.xml.bind", "jaxb-api").versionAsInProject()),
                        wrappedBundle(mavenBundle("javax.activation", "activation").version("1.1.1")),
                        wrappedBundle(mavenBundle().groupId("org.codehaus.groovy").artifactId("groovy-all").version("2.5.6")),
                        wrappedBundle(mavenBundle("org.apache.httpcomponents", "httpclient").versionAsInProject()),
                        wrappedBundle(mavenBundle("org.apache.httpcomponents", "httpmime").versionAsInProject()),
                        wrappedBundle(mavenBundle("org.apache.httpcomponents", "httpcore").versionAsInProject()),

                        /* Rest Assured dependencies needed in the Pax Exam container to be able to execute the tests below */
                        mavenBundle("io.rest-assured", "json-path").versionAsInProject(),
                        mavenBundle("io.rest-assured", "xml-path").versionAsInProject(),
                        mavenBundle("io.rest-assured", "rest-assured").versionAsInProject(),
                        mavenBundle("io.rest-assured", "rest-assured-common").versionAsInProject()
                };
    }

    @Test
    public void getUUIDParsesAStringResultToUUID() {
        final String UUID_XML = "<some>\n" +
                "  <thing id=\"1\">db24eeeb-7fe5-41d3-8f06-986b793ecc91</thing>\n" +
                "  <thing id=\"2\">d69ded28-d75c-460f-9cbe-1412c60ed4cc</thing>\n" +
                "</some>";

        final UUID uuid = from(UUID_XML).getUUID("some.thing[0]");

        assertThat(uuid, Matchers.equalTo(UUID.fromString("db24eeeb-7fe5-41d3-8f06-986b793ecc91")));
    }
}

运行这个测试会报错:

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.86 sec <<< FAILURE! - in io.restassured.test.osgi.XmlPathOSGiITest
getUUIDParsesAStringResultToUUID(io.restassured.test.osgi.XmlPathOSGiITest)  Time elapsed: 1.85 sec  <<< ERROR!
java.io.IOException: Error resolving artifact org.codehaus.groovy:groovy-all:jar:2.5.6: Could not find artifact org.codehaus.groovy:groovy-all:jar:2.5.6 in central (http://repo1.maven.org/maven2/)
        at org.ops4j.pax.url.mvn.internal.AetherBasedResolver.resolve(AetherBasedResolver.java:626)

兴趣点大概是这一行:

wrappedBundle(mavenBundle().groupId("org.codehaus.groovy").artifactId("groovy-all").version("2.5.6")),

当 Groovy 被指定为使用 version 2.4.15 时一切正常。所以我的问题是:

过去依赖于 2.4 的 groovy-all jar 时,如何在 OSGi 上下文中将 Groovy 从 2.4 升级到 2.5?我如何在测试中反映这一点?

从中央检索 Groovy 2.5.6 似乎有问题。我在尝试直接使用 Maven 时遇到同样的错误:mvn org.apache.maven.plugins:maven-dependency-plugin:3.1.1:get org.codehaus.groovy:groovy-all:jar:2.5.6

查看 server 您可以看到只有文档和源可用,但没有 JAR 文件。 Groovy-all.

的所有 2.5 版本都是这种情况

我认为这是因为所有 2.5 版本都只是某种 "meta artifact" 没有自己的代码(因此没有可用的 JAR)。实际实现分为以下工件 groovy-全部取决于:

org.codehaus.groovy:groovy
org.codehaus.groovy:groovy-ant
org.codehaus.groovy:groovy-cli-commons
org.codehaus.groovy:groovy-cli-picocli
org.codehaus.groovy:groovy-console
org.codehaus.groovy:groovy-datetime
org.codehaus.groovy:groovy-docgenerator
org.codehaus.groovy:groovy-groovydoc
org.codehaus.groovy:groovy-groovysh
org.codehaus.groovy:groovy-jmx
org.codehaus.groovy:groovy-json
org.codehaus.groovy:groovy-jsr223
org.codehaus.groovy:groovy-macro
org.codehaus.groovy:groovy-nio
org.codehaus.groovy:groovy-servlet
org.codehaus.groovy:groovy-sql
org.codehaus.groovy:groovy-swing
org.codehaus.groovy:groovy-templates
org.codehaus.groovy:groovy-test
org.codehaus.groovy:groovy-test-junit5
org.codehaus.groovy:groovy-testng
org.codehaus.groovy:groovy-xml

因此,为了修改您的代码,您必须确定您真正需要的 groovy 个工件,并将它们一一添加到您的代码中。

我觉得从2.5版本开始就没有单独的jar了https://issues.apache.org/jira/browse/GROOVY-8751

事实证明,在选择了正确的工件后,在本例中为 groovy 和 groovy-json,您还需要 Apache Aries SPI Fy

mavenBundle().groupId("org.apache.aries.spifly").artifactId("org.apache.aries.spifly.dynamic.bundle").version("1.2.1")

如果您不添加该捆绑包,groovy-json OSGi 片段捆绑包不会得到解决,日志记录中没有任何关于原因的提示。