简体   繁体   English

从IntelliJ运行Jetty不会读取jetty-env.xml

[英]Jetty run from IntelliJ won't read the jetty-env.xml

I'm debugging in IntelliJ and since I can't seem to get JBoss to run (due to context and virtual-host name issues), I'm using Jetty 6.1.26. 我正在IntelliJ中进行调试,由于似乎无法运行JBoss(由于上下文和虚拟主机名问题),因此我正在使用Jetty 6.1.26。 I was able to run and debug while using a DB connection string that did not involve a jndi. 我可以使用不涉及jndi的数据库连接字符串来运行和调试。

Now, I need to use a Jndi and can't seem to get Jetty to find it. 现在,我需要使用Jndi,但似乎无法让Jetty找到它。 I've created a jetty-env.xml file in the WEB-INF directory as such: 我已经在WEB-INF目录中创建了jetty-env.xml文件,如下所示:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">

<Configure id="wac" class="org.mortbay.jetty.webapp.WebAppContext">
    <New id="jdbc.datasource" class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg><Ref id="wac"/></Arg>
        <Arg>jdbc/datasourceArg>
        <Arg>
            <New class="oracle.jdbc.pool.OracleDataSource">
                <Set name="URL">jdbc:oracle:thin:@localhost:1521:xe</Set>
                <Set name="User">user</Set>
                <Set name="Password">password</Set>
            </New>
        </Arg>
    </New>
</Configure>

web.inf has this referenced: web.inf引用了以下内容:

<resource-ref>
    <description>ConnectionPool DataSource Reference</description>
    <res-ref-name>jdbc.datasource</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

as such 因此

and then the application context creates a bean with it: 然后应用程序上下文使用它创建一个bean:

<jee:jndi-lookup id="jndiDataSource" jndi-name="jdbc.datasource"/>

When I run jetty, it tells me that it can't find the data source, so it can't create the bean and the application fails to start. 当我运行jetty时,它告诉我找不到数据源,因此无法创建Bean,并且应用程序无法启动。

I suspect that the problem is that jetty is not actually reading the xml file. 我怀疑问题是码头实际上并未读取xml文件。 I put garbage in there just to see and sure enough there were no errors at all when starting up. 我把垃圾放在那儿只是为了确保启动时根本没有错误。

I added plus to my jetty xml 我在码头XML中添加了加号

<Call name="addLifeCycle">
  <Arg>
    <New class="org.mortbay.jetty.deployer.WebAppDeployer">
      <Set name="contexts"><Ref id="Contexts"/></Set>
      <Set name="webAppDir"><SystemProperty name="jetty.home" default="."/>/webapps</Set>
      <Set name="parentLoaderPriority">false</Set>
      <Set name="extract">true</Set>
      <Set name="allowDuplicates">false</Set>
      <Set name="defaultsDescriptor"><SystemProperty name="jetty.home" default="."/>/etc/webdefault.xml</Set>
      <Set name="configurationClasses"><Ref id="plusConfig"/></Set>
    </New>
</Arg>
</Call>

and I referenced the plus xml from the jetty server setup in IntelliJ. 我从IntelliJ中的码头服务器设置中引用了plus xml。

I am at my wits end. 我没办法。 Everything online tells me that jetty automatically scans that directory for this file but it clearly doesn't seem to happen for me. 在线所有内容都告诉我,jetty会自动在该目录中扫描此文件,但显然对我而言这没有发生。

More than half a decade later, I had the same issue: a jetty run-war run config wasn't picking up the jetty-env.xml I had defined in my build resources. 六个多月后,我遇到了同样的问题: jetty run-war run配置并没有使用我在构建资源中定义的jetty-env.xml。

The fix turned out to be updating the run configuration's command line to include the compile lifecycle stage, in full clean compile jetty:run-war -f pom.xml . 最终,该修复程序正在更新运行配置的命令行,以在完全clean compile jetty:run-war -f pom.xml包含compile生命周期阶段。

Your problem doesn't look like caused by Intellij IDEA. 您的问题似乎不是由Intellij IDEA引起的。 I would suggest you to try to run Jetty with your artifact and startup configuration without IDEA and compare results. 我建议您尝试使用没有IDEA的工件和启动配置来运行Jetty并比较结果。 If there's some difference, please submit a bug report . 如果有差异,请提交错误报告

I'm not familiar with data sources configuration, but able to point to some possible issues with your configuration: 我对数据源配置不熟悉,但是能够指出您的配置可能存在的一些问题:

  1. in the file jetty-env.xml under the xpath /Configure/New/@class should be "org.mortbay.jetty.plus.naming.Resource" instead of "org.eclipse.jetty.plus.jndi.Resource" , since the root package of Jetty 6.1.26 is "org.mortbay.jetty" and it was changed to "org.eclipse.jetty" with a later version of the server. 在xpath /Configure/New/@class下的文件jetty-env.xml ,应该是"org.mortbay.jetty.plus.naming.Resource"而不是"org.eclipse.jetty.plus.jndi.Resource" ,因为Jetty 6.1.26的根包是"org.mortbay.jetty" "org.eclipse.jetty" ,在更高版本的服务器上已更改为"org.eclipse.jetty" Please note, last part of the package also differs ( "naming" , not "jndi" ) 请注意,包的最后一部分也有所不同( "naming" ,不是"jndi"

  2. in the file jetty-env.xml under the xpath /Configure/New/@id should be "datasource" instead of "jdbc.datasource" (id without prefix) xpath /Configure/New/@id下的文件jetty-env.xml的应该是"datasource"而不是"jdbc.datasource" (不带前缀的id)

  3. in the file jetty-env.xml the first <Arg> (containing <Ref id="wac"/> ) looks like not necessary 在文件jetty-env.xml ,第一个<Arg> (包含<Ref id="wac"/> )看起来像没有必要

  4. in the file jetty-env.xml the second <Arg> isn't correctly closed (looks like a typo) 在文件jetty-env.xml ,第二个<Arg>没有正确关闭(看起来像错字)

  5. in the file web.xml (not web.inf ) <res-ref-name> should contain "jdbc/datasource" instead of "jdbc.datasource" , as well as in the <jee:jndi-lookup> the jndi-name attribute should be "jdbc/datasource" instead of "jdbc.datasource" , because "jdbc/" (not "jdbc." ) is the common prefix for data source names in Jetty. 文件web.xml (不是web.inf )中的<res-ref-name>应该包含"jdbc/datasource"而不是"jdbc.datasource" ,以及<jee:jndi-lookup>jndi-name属性应该是"jdbc/datasource"而不是"jdbc.datasource" ,因为"jdbc/" (不是"jdbc." )是Jetty中数据源名称的通用前缀。

  6. I would recommend to change the data source id to somewhat more customized than just "datasource" , say at least to "myDatasource" 我建议将数据源ID更改为比"datasource"更加定制的名称,至少更改为"myDatasource"

Points 2 , 3 and 5 are taken from the following articles: 2,35从以下文章采取:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM