如何让 maven 项目从第三方 jar 中识别 xml?

Posted

技术标签:

【中文标题】如何让 maven 项目从第三方 jar 中识别 xml?【英文标题】:How to make maven project recognize xml from third party jar? 【发布时间】:2014-02-01 17:43:27 【问题描述】:

在我的项目中,我使用了第三方 jar。这个jar依赖于jndi.xml和jndi.properties文件。

独立第三方jar的结构:

    /
    |- TP.jar
    |- jndi.xml
    |- jndi.properties

在这个 jar 的 applicationContext 内部 - 有一个对 jndi.xml 中创建的 bean 的引用 这个 jar 是使用 ant 构建的,构建工作正常。

我将这个 jar 作为依赖项包含在我的 maven 项目中。在我的 maven 项目 dispatcher-servlet 中,导入了 TP.jar 的应用上下文以访问 TP.jar 中的 bean。

我的 maven 项目部署失败并出现以下错误:

javax.naming.NameNotFoundException: Name [JNDIDatasource] is not bound in this Context. Unable to find [JNDIDatasource].

jndi.properties:

java.naming.factory.initial=org.apache.xbean.spring.jndi.SpringInitialContextFactory

jndi.xml:

<beans>
<bean class="org.apache.xbean.spring.jndi.DefaultContext" id="jndi">
    <property name="entries">
        <map>
             <entry key="JNDIDatasource">
                <bean class="org.apache.tomcat.dbcp.dbcp.BasicDataSource" destroy-method="close" singleton="false">
                    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
                    <property name="url" value="jdbc:sqlserver://<host>:1433;DatabaseName=test"></property>
                    -----
                </bean>
            </entry>
              -----------
 </bean>
 --------------
</beans>

TP.jar applicationContext.xml:

<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="JNDIDatasource" />
</bean>

当在我的 maven 项目中导入上面的 applicationContext 时,tomcat 不会启动。 我对如何解决这个问题非常困惑,我们将不胜感激。

我尝试将 jndi.xml 和 jndi.properties 添加到 src/main/resources 下的我的 maven 项目中,希望它们将包含在我的 maven 项目的路径中,并且 bean 可能会在那里被检测到。但这并没有帮助。

【问题讨论】:

有人有什么建议吗? 【参考方案1】:

我在我的应用程序中这样做了。

应用程序上下文

     <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"   
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
            http://www.springframework.org/schema/jee
            http://www.springframework.org/schema/jee/spring-jee-3.1.xsd        
            http://www.springframework.org/schema/tx 
            ..
            http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd
            ">

<jee:jndi-lookup id="cdrDataSource" jndi-name="jdbc/cdr_source" resource-ref="true" />

</beans>

jndi.properties

 java.naming.factory.initial=org.apache.xbean.spring.jndi.SpringInitialContextFactory
java.naming.provider.url = classpath:/jndi.xml

jndi.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:jee="http://www.springframework.org/schema/jee"   
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd      ">


    <bean id="jndi" class="org.apache.xbean.spring.jndi.DefaultContext">
        <property name="entries">
            <map>   
                <entry key="jdbc/cdr">
                    <bean class="org.apache.commons.dbcp.BasicDataSource">
                        <property name="driverClassName">
                            <value>net.sourceforge.jtds.jdbc.Driver</value>
                        </property>
                        <property name="url">
                            <value>jdbc:jtds:sybase://server:7306/cdr</value>
                        </property>     
....

客户来电

@Repository
public class CDRDataRetriever 

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource(name = "cdrDataSource")
    private DataSource cdrDataSource;

【讨论】:

以上是关于如何让 maven 项目从第三方 jar 中识别 xml?的主要内容,如果未能解决你的问题,请参考以下文章

maven 手动复制repository 里的jar包

请问,maven项目发布为jar到私服,其他项目如何引用内部依赖?

如何在插件开发中正确使用第三方jar包

如何从 Eclipse 中的 Maven 项目中删除未使用的依赖项? [复制]

Maven使用详解

如何在 pom.xml 中添加\安装到新创建\第三方 jar 的 maven 依赖项