【Dubbo】依赖dubbo源码打包项目,报dubbo.xsd找不到

问题描述

项目依赖源码打包的时候,会报dubbo.xsd找不到的问题。http://code.alibabatech.com/schema/dubbo/dubbo.xsd读取不到

目录结构

【Dubbo】依赖dubbo源码打包项目,报dubbo.xsd找不到_第1张图片
项目目录结构
  • dubbo-demo-provider.xml



    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

重现

  • 使用如下pom.xml打包

        mydubbo-server
        
            
                ${project.build.directory}/classes
                src/main/resources
                true
                
                    **/*.xml
                    **/*.properties
                
            
            
            
                ${project.build.directory}/classes/META-INF/spring
                src/main/resources/spring
                true
                
                    spring-context.xml
                
            
        
        
            
            
                org.apache.maven.plugins
                maven-jar-plugin
                
                    target/classes/
                    
                        
                            com.alibaba.dubbo.container.Main
                            
                            false
                            true
                            lib/
                        
                        
                            .
                        
                    
                
            
            
                org.apache.maven.plugins
                maven-dependency-plugin
                
                    
                        copy-dependencies
                        package
                        
                            copy-dependencies
                        
                        
                            jar
                            jar
                            false
                            
                                ${project.build.directory}/lib
                            
                        
                    
                
            
        
    
  • 执行构建
    mvn clean install
  • 构建结果:将mydubbo-server.jar和lib目录拷贝到指定的目录下,命令行运行java -jar mydubbo-server.jar就可以运行dubbo服务了。(注:lib目录下的所有jar包是mydubbo-server.jar的依赖jar)
【Dubbo】依赖dubbo源码打包项目,报dubbo.xsd找不到_第2张图片
构建结果
【Dubbo】依赖dubbo源码打包项目,报dubbo.xsd找不到_第3张图片
lib
  • 执行jar文件
    E:\dubbo\incubator-dubbo\dubbo-demo\dubbo-demo-provider\target> java -jar .\mydubbo-server.jar

  • 出现异常

[07/06/18 11:25:35:035 CST] main  WARN xml.XmlBeanDefinitionReader: Ignored XML validation warning
org.xml.sax.SAXParseException; lineNumber: 24; columnNumber: 46; schema_reference.4: 无法读取方案文档 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd', 原因为 1) 无法找到文档; 2) 无法读取文档; 3) 文档的 根元素不是 。
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.warning(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaErr(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaWarning(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getSchemaDocument1(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getSchemaDocument(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.findSchemaGrammar(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.emptyElement(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
        at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:76)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadDocument(XmlBeanDefinitionReader.java:429)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:391)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252)
        at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
        at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
        at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
        at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:614)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:515)
        at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
        at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:93)
        at com.alibaba.dubbo.container.spring.SpringContainer.start(SpringContainer.java:45)
        at com.alibaba.dubbo.container.Main.main(Main.java:85)

原因分析

dubbo的标签都是通过spring.handler文件中配置的对应的namespaceHandler解析,解析时还会去加载http\://code.alibabatech.com/schema/dubbo/dubbo.xsd指向的xsd文件,这个映射是在dubbo的spring.schemas中配置的。由于我们使用的是源码构建的,dubbo的spring.schemas文件是在dubbo-config-spring-2.6.1.jar里(路径dubbo-config-spring-2.6.1.jar\META-INF\spring.schemas,内容是http\://code.alibabatech.com/schema/dubbo/dubbo.xsd=META-INF/dubbo.xsd),
但是spring并不知道这个路径下还有spring.schemas,也就无法加载http\://code.alibabatech.com/schema/dubbo/dubbo.xsd指向的xsd文件,所以spring会报加载不到*.xsd文件的异常。

解决方案

现在的解决方案是将所有的spring.schemas合并在一起,而不用去加载单个jar里面的映射文件。所以我们使用maven的shade插件将所有jar里的spring.schemas文件进行合并,在最终生成的单一jar包里,spring.schemas包含了所有出现过的映射

  • pom.xml中新增shade插件
      
                org.apache.maven.plugins
                maven-shade-plugin
                
                    
                        package
                        
                            shade
                        
                        
                            
                                
                                    META-INF/spring.handlers
                                
                                
                                
                                    META-INF/spring.schemas
                                
                                
                                
                            
                        
                    
                
            
  • 打包结果


    【Dubbo】依赖dubbo源码打包项目,报dubbo.xsd找不到_第4张图片
    mydubbo-server.jar\META-INF
  • 查看合并后的文件mydubbo-server.jar\META-INF\spring.schemas

...
http\://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd
http\://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config/spring-aop-2.5.xsd
http\://www.springframework.org/schema/aop/spring-aop-3.0.xsd=org/springframework/aop/config/spring-aop-3.0.xsd
http\://www.springframework.org/schema/aop/spring-aop-3.1.xsd=org/springframework/aop/config/spring-aop-3.1.xsd
http\://www.springframework.org/schema/aop/spring-aop-3.2.xsd=org/springframework/aop/config/spring-aop-3.2.xsd
http\://www.springframework.org/schema/aop/spring-aop-4.0.xsd=org/springframework/aop/config/spring-aop-4.0.xsd
http\://www.springframework.org/schema/aop/spring-aop-4.1.xsd=org/springframework/aop/config/spring-aop-4.1.xsd
http\://www.springframework.org/schema/aop/spring-aop-4.2.xsd=org/springframework/aop/config/spring-aop-4.2.xsd
http\://www.springframework.org/schema/aop/spring-aop-4.3.xsd=org/springframework/aop/config/spring-aop-4.3.xsd
http\://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-4.3.xsd

http\://code.alibabatech.com/schema/dubbo/dubbo.xsd=META-INF/dubbo.xsd
...
  • 运行成功 java -jar mydubbo-server.jar
[07/06/18 01:28:29:029 CST] main DEBUG env.PropertySourcesPropertyResolver: Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
[07/06/18 01:28:29:029 CST] main DEBUG support.DefaultListableBeanFactory: Returning cached instance of singleton bean 'lifecycleProcessor'
[07/06/18 01:28:29:029 CST] main  INFO container.Main:  [DUBBO] Dubbo SpringContainer started!, dubbo version: 2.6.1, current host: 192.168.0.1
[2018-06-07 13:28:29] Dubbo service server started!

你可能感兴趣的:(【Dubbo】依赖dubbo源码打包项目,报dubbo.xsd找不到)