2015-01-16 26 views
5

我有一個CXF的JAX-RS應用程序正在建造使用Maven。我正在將它轉換爲Gradle,但使用Ant XJC任務。com.sun.tools.xjc.Plugin:提供<plugin>不是一個亞型

當前版本使用一對夫婦的擴展,其中一個是「元件包裝」插件的副本,而另一種是「JAXB流利-API」。

我試圖把罐子爲這兩個插件到類路徑XJC,但是當我運行XJC任務,我得到如下:

java.util.ServiceConfigurationError: com.sun.tools.xjc.Plugin: Provider dk.conspicio.jaxb.plugins.XmlElementWrapperPlugin not a subtype

的XmlElementWrapperPlugin類擴展「com.sun.tools.xjc 。插入」。

任何想法這裏發生了什麼?

如果它的事項,我對XJC插件Maven配置看起來是這樣的:

<plugin> 
    <groupId>org.apache.cxf</groupId> 
    <artifactId>cxf-xjc-plugin</artifactId> 
    <executions> 
     <execution> 
      <id>generate-sources</id> 
      <phase>generate-sources</phase> 
      <goals> 
       <goal>xsdtojava</goal> 
      </goals> 
      <configuration> 
       <extensions> 
        <extension>JAXBXMLElementWrapperPlugin:JAXBXMLElementWrapperPlugin:1.0.0</extension> 
        <extension>net.java.dev.jaxb2-commons:jaxb-fluent-api:2.1.8</extension> 
       </extensions> 
       <xsdOptions> 
        <xsdOption> 
         <xsd>${basedir}/src/main/resources/schema/serviceCallResults.xsd</xsd> 
         <packagename>com.att.sunlight.service.domain.serviceCallResults</packagename> 
         <extension>true</extension> 
         <extensionArgs> 
          <extensionArg>-Xxew</extensionArg> 
          <extensionArg>-summary ${basedir}/target/xew-summary.txt</extensionArg> 
          <extensionArg>-instantiate lazy</extensionArg> 
          <extensionArg>-Xfluent-api</extensionArg> 
         </extensionArgs> 
        </xsdOption> 
       </xsdOptions> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 

這裏是我的 「的build.gradle」,只有省略掉的倉庫:

apply plugin: 'java' 
apply plugin: 'maven' 
apply plugin: 'war' 

group = 'SunlightDataService' 
version = '1.2.4-SNAPSHOT' 

sourceCompatibility = 1.6 
targetCompatibility = 1.6 

repositories { 
    ... 
} 

configurations { 
    jaxb 
} 

dependencies { 

    jaxb 'com.sun.xml.bind:jaxb-xjc:2.2.7-b41' 
    jaxb 'com.sun.xml.bind:jaxb-impl:2.2.7-b41' 
    jaxb 'javax.xml.bind:jaxb-api:2.2.7' 
    jaxb "JAXBXMLElementWrapperPlugin:JAXBXMLElementWrapperPlugin:1.0.0" 
    jaxb "net.java.dev.jaxb2-commons:jaxb-fluent-api:2.1.8" 

    compile group: 'org.springframework', name: 'spring-beans', version:'3.2.8.RELEASE' 
    compile group: 'org.springframework', name: 'spring-webmvc-portlet', version:'3.2.8.RELEASE' 
    compile group: 'org.apache.cxf', name: 'cxf-rt-transports-http', version:'2.7.7' 
    compile group: 'log4j', name: 'log4j', version:'1.2.16' 
    compile group: 'org.springframework', name: 'spring-jdbc', version:'3.2.8.RELEASE' 
    compile group: 'org.springframework', name: 'spring-context', version:'3.2.8.RELEASE' 
    compile group: 'org.apache.cxf', name: 'cxf-rt-frontend-jaxrs', version:'2.7.7' 
    compile group: 'org.apache.cxf', name: 'cxf-rt-bindings-xml', version:'2.7.7' 
    compile group: 'org.apache.cxf', name: 'cxf-rt-databinding-jaxb', version:'2.7.7' 
    compile group: 'org.apache.cxf', name: 'cxf-rt-core', version:'2.7.7' 
    compile group: 'org.apache.cxf', name: 'cxf-api', version:'2.7.7' 
    compile group: 'org.apache.cxf', name: 'cxf-rt-rs-extension-providers', version:'2.7.7' 
    compile group: 'org.codehaus.jettison', name: 'jettison', version:'1.3.4' 
    compile group: 'org.perf4j', name: 'perf4j', version:'0.9.14' 
    compile group: 'cglib', name: 'cglib', version:'2.2.2' 
    compile group: 'org.aspectj', name: 'aspectjweaver', version:'1.6.12' 
    compile group: 'commons-collections', name: 'commons-collections', version:'3.2.1' 
    compile group: 'esGateKeeper', name: 'GLCookieDecryption', version:'1.0.0' 
    compile group: 'joda-time', name: 'joda-time', version:'2.3' 
    compile group: 'org.apache.jackrabbit', name: 'jackrabbit-core', version:'2.4.0' 
    compile group: 'org.apache.commons', name: 'commons-lang3', version:'3.1' 
    testCompile group: 'org.springframework', name: 'spring-test', version:'3.2.8.RELEASE' 
    testCompile group: 'oracle.jdbc', name: 'oracle.jdbc.OracleDriver', version:'1.0.0' 
    testCompile group: 'com.atomikos', name: 'transactions-jta', version:'3.7.0' 
    testCompile group: 'org.apache.cxf', name: 'cxf-rt-transports-http-jetty', version:'2.7.7' 
    testCompile group: 'com.atomikos', name: 'transactions-jdbc', version:'3.7.0' 
    testCompile group: 'org.mockito', name: 'mockito-all', version:'1.9.5' 
    testCompile group: 'junit', name: 'junit', version:'4.10' 
    testCompile group: 'org.assertj', name: 'assertj-core', version:'1.6.1' 
    providedCompile group: 'javax.transaction', name: 'jta', version:'1.1' 
    providedCompile group: 'javax.servlet.jsp', name: 'jsp-api', version:'2.1' 
    providedCompile group: 'javax.servlet', name: 'servlet-api', version:'2.5' 
} 

task processXSDs() << { 
    ant.taskdef(name: 'xjc', classname: 'com.sun.tools.xjc.XJCTask', 
       classpath: configurations.jaxb.asPath) 

    ant.xjc(destdir: 'tmp', package: "com.att.sunlight.service.domain.serviceCallResults", extension: true) { 
     schema(dir: "src/main/resources/schema", includes: "serviceCallResults.xsd") 
     arg(line: "-Xxew") 
     arg(line: "-summary target/xew-summary.txt") 
     arg(line: "-instantiate lazy") 
     arg(line: "-Xfluent-api") 
    } 
} 

compileJava.dependsOn processXSDs 

更新:

我確定這是不是與「元素包裝」延長的問題。如果我從類路徑中刪除該jar並重建,它會爲「Fluent API」插件報告相同的錯誤。

我也確定這不是一個嚴格的Gradle問題。我用Ant「build.xml」得到相同的症狀,甚至直接調用「XJCFacade」Java類的純shell腳本。事實上,我甚至可以簡化這個腳本,甚至沒有指定任何模式文件,這使得清除在嘗試處理任何模式之前發生這個錯誤。

以下是我當前的腳本:

#! /bin/bash 
java -classpath "lib/commons-beanutils-1.7.0.jar;lib/commons-lang-2.2.jar;lib/commons-logging-1.1.1.jar;lib/istack-commons-runtime-2.16.jar;lib/jaxb2-basics-runtime-0.6.5.jar;lib/jaxb2-basics-tools-0.6.5.jar;lib/jaxb-api-2.2.7.jar;lib/jaxb-core-2.2.7.jar;lib/jaxb-fluent-api-2.1.8.jar;lib/jaxb-xew-plugin-1.4.jar;lib/jaxb-xjc-2.2.7.jar" com.sun.tools.xjc.XJCFacade -extension 

您可以通過下載所有這些文物到您的搖籃或Maven緩存,並將它們複製到一個簡單的文件夾結構構造相同的測試。

另外請注意,我在運行此測試主要是在Windows 7,但我拉上這個項目,並把它搬到我的CentOS的VM,它給我確切的同樣的錯誤。

+0

你可以發佈更多的build.grade的?至少是'dependencies {...}'塊。 –

+0

新版本包含整個腳本,除了存儲庫。 –

+0

嘗試用'arg(value:「...」)替換'arg(line:「...」)''。 –

回答

10

我已經試過

java -cp jaxb-api-2.2.7.jar;jaxb-core-2.2.7.jar;jaxb-xjc-2.2.7.jar;commons-logging-1.1.1.jar;commons-lang-2.2.jar;jaxb2-basics-tools-0.6.5.jar;jaxb-xew-plugin-1.3.jar com.sun.tools.xjc.XJCFacade -verbose -extension -d src xsd

確實是失敗Exception in thread "main" java.util.ServiceConfigurationError: com.sun.tools.xjc.Plugin: Provider com.sun.tools.xjc.addon.xew.XmlElementWrapperPlugin not a subtype,所以這個問題顯然是重複的。我調試了java.util.ServiceLoader,事實證明插件應該作爲參數傳遞給XJC(而不是Java的類路徑)。實際上,所有的Maven插件(如jaxb2-maven-pluginmaven-jaxb2-plugin ...)都知道這個特性,並且正確地形成了XJC的參數。所以,正確的命令行是:

java -cp jaxb-api-2.2.7.jar;jaxb-core-2.2.7.jar;jaxb-xjc-2.2.7.jar;commons-lang-2.2.jar;commons-logging-1.1.1.jar com.sun.tools.xjc.XJCFacade -classpath jaxb-xew-plugin-1.3.jar;jaxb2-basics-tools-0.6.5.jar -verbose -extension -Xxew -d src xsd

注意-classpath是XJC的說法。 commons-xxx庫文件可以轉到系統類路徑,因爲它們導出的包不會被篩選,但jaxb2-basics-tools應該位於XJC類路徑中。如果你有興趣的細節:

這是因爲XJC確實classpath中加載篩選能夠加載更高版本的JRE JAXB API具有較低內置的API的版本。見XJCFacade.java line 69。這意味着,com.sun.tools.xjc.Plugin通過自定義類加載器XJCFacade而同一個類被另一個(實際上,父)類加載器時再Class.forName("com.sun.tools.xjc.addon.xew.XmlElementWrapperPlugin")被稱爲加載加載一次,但現在類是不相等的。

其實你可以解決辦法是這樣的(之後的源代碼檢查發現的解決方案):

java -Dcom.sun.tools.xjc.XJCFacade.nohack=true -cp jaxb-core-2.2.7.jar;jaxb-xjc-2.2.7.jar;commons-lang-2.2.jar;commons-logging-1.1.1.jar;jaxb2-basics-tools-0.6.5.jar;jaxb-xew-plugin-1.3.jar com.sun.tools.xjc.XJCFacade -verbose -extension -Xxew -d src xsd

注意,我已刪除從Java類路徑jaxb-api-2.2.7.jar,但你最好把JAXB API爲lib/endorsed,因爲它可能不適用於不同的Java版本:適用於Java 7,因爲it's JAXB API is close to 2.2.7適用,但可能無法在Java 6 + JAXB API 2.2.11上使用。

+3

非常令人印象深刻。 – lexicore

+0

爲了總結這個解決方案,我試圖描述「引用」XJC而不是「運行」XJC所需的類路徑。將這些條款分開是合理的嗎?例如,主XJC罐子需要「引用」XJC,但擴展​​罐需要在「正在運行」的類路徑中。我認爲把第二部分稱爲「跑步」是有點合理的,但稱另一部分「參考」可能並不完全正確。 –

+0

也許「引用」應該更好地稱爲「初始化」,但從概念上說你是對的:插件應該被提升爲「另一個」類路徑。 –

-1

沒有構建/發佈工程師或JAXB的XJC實施者應強制使用內部專用JAXB-JXC類加載器。最初的設計是如此的JDK 1.x的用戶不會加載JAXB-2.2.0.jar ..since 2010沒有出現過顯著版本更改JAXB和JAXB的XJC開發商停止維持這個插件中的JDK 5,6, 7,8。我在1月31日發送了一封電子郵件給CXF-Metro上的Oracle專家。如果我們沒有聽到他們的回覆,請求Oracle向OpenSource發佈代碼,至少它會被維護。 這個插件的每個實現者都被這個短暫的解決方案絆住了。沒有必要進一步懲罰未經維護的代碼遭受短期攻擊的未來實現者。 另外,-DClassLoaderBuilder.noHack選項完全繞過甲骨文自己的安全Manager..this是禁止的銀行和金融機構...... 我會後回來時,我從CXF地鐵迴音

+0

有沒有新聞? * [15chars] * – Alix

3

榮譽給dma_k,但工作gradle解決方案從未提供。一些實驗後,這是對我工作:

def xjcGeneratedSourcesDir = "$buildDir/generated-sources/xjc" 

configurations { 
    jaxb 
    xjc 
} 

sourceSets { 
    main { 
     java.srcDir xjcGeneratedSourcesDir 
    } 
} 

dependencies { 
    jaxb 'com.sun.xml.bind:jaxb-xjc:2.2.11' 
    jaxb 'com.sun.xml.bind:jaxb-core:2.2.11' 
    jaxb 'com.sun.xml.bind:jaxb-impl:2.2.11' 
    jaxb 'javax.xml.bind:jaxb-api:2.2.11' 

    xjc "com.github.jaxb-xew-plugin:jaxb-xew-plugin:1.4" 
    xjc "net.java.dev.jaxb2-commons:jaxb-fluent-api:2.1.8" 

    // etc. 
} 

task wsdl2java << { 
    file(xjcGeneratedSourcesDir).mkdirs() 

    ant.taskdef(name: 'xjc', classname: 'com.sun.tools.xjc.XJCTask', 
       classpath: configurations.jaxb.asPath) 

    ant.xjc(destdir: xjcGeneratedSourcesDir, 
      package: "com.localhost.jaxb", extension: true) { 
     schema(dir: "src/main/resources/schema", includes: "*.xsd") 
     classpath(path: configurations.xjc.asPath) 
     arg(line: "-Xxew") 
     arg(line: "-Xxew:summary $buildDir/xew-summary.txt") 
     arg(line: "-Xxew:instantiate lazy") 
     arg(line: "-Xfluent-api") 
    } 
} 

compileJava.dependsOn wsdl2java 
+0

對不起,我忘了在此添加更多信息。事實上,我最終也得到了類似的解決方案。我發現我不需要明確指定「jaxb-impl」依賴關係。我在「內線」塊,這是比較搖籃般的定義我的版本「xjcGeneratedSourcesDir」,我也把我的「ant.xjc」呼叫我打電話幾次特定的模式來處理的方法。 –

+0

謝謝。它爲我工作。 – dpg

相關問題