0

如果我将应用程序打包为一个战争,我可以使用 web.xml 和 jetty-env.xml 设置 jndi 数据源。但我想将我的应用程序打包为 jar,我的理解是在这种情况下你不能使用 web.xml 和 jetty-env.xml。我已将数据源设置为 jndi 查找。我该如何解决这个问题。有什么特殊的方法可以从我的 maven pom 文件中指向 jetty-env.xml 吗?

以下是我的码头弹簧 xml

<beans>

  <bean id="contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>

  <bean id="triggerServer" class="org.eclipse.jetty.server.Server" init-method="start" destroy-method="stop">

    <property name="threadPool">  
      <bean id="ThreadPool" class="org.eclipse.jetty.util.thread.QueuedThreadPool">
        <property name="minThreads" value="10"/>
        <property name="maxThreads" value="50"/>
      </bean>
    </property>

    <property name="connectors">
      <list>
        <bean id="Connector" class="org.eclipse.jetty.server.nio.SelectChannelConnector">
          <property name="port" value="2080"/>
        </bean>
      </list>
    </property>

    <property name="handler">
      <bean id="handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
        <property name="handlers">
          <list>
            <ref bean="contexts"/>
            <ref bean="testHandler"/>           
          </list>
        </property>
      </bean>
    </property>

    <property name="stopAtShutdown" value="true"/>
    <property name="sendServerVersion" value="true"/>
    <property name="sendDateHeader" value="true"/>
    <property name="gracefulShutdown" value="1000"/>
  </bean>

以下是启动服务器的 java 代码。

public static void main(String args[]) throws Exception {
    try {
        TriggerServer.init();
        Server server = (Server)springContext.getBean("triggerServer"); 
        logInfo(server);

        server.start();             
        server.join();         
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("?", e);
    }
}

以下是我的 jetty-env.xml

<Configure id="thisfile" class="org.eclipse.jetty.webapp.WebAppContext">
  <New class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg></Arg>
    <Arg>jdbc/jndiDB</Arg>
    <Arg>
     <New class="oracle.jdbc.pool.OracleDataSource">
    <Set name="DriverType">thin</Set>
    <Set name="URL">jdbc:oracle:thin:@localhost:1521:orcl</Set>
    <Set name="User">user</Set>
    <Set name="Password">password</Set>
    <Set name="connectionCachingEnabled">true</Set>
    <Set name="connectionCacheProperties">
        <New class="java.util.Properties">
            <Call name="setProperty">
                <Arg>MinLimit</Arg>
                <Arg>5</Arg>
            </Call>
            <!-- put the other properties in here too -->
        </New>
    </Set>

</New>
    </Arg>
</New>
</Configure>

编辑并包括 pom.xml

    <modelVersion>4.0.0</modelVersion>

    <parent>
        <artifactId>project</artifactId>
        <groupId>com.client.project</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.client.project</groupId>
    <artifactId>application</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>application</name>
    <url>http://maven.apache.org</url>

    <properties>
        .....
    </properties>

    <dependencies>
        ......
    </dependencies>

    <build>
        <finalName>application</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/src/main/webapp/WEB-INF/lib</outputDirectory>
                            <includeScope>compile</includeScope>
                            <excludeScope>test</excludeScope>
                            <excludeScope>provided</excludeScope>
                            <excludeArtifactIds>jetty-jsp-2.1,jetty-all-server</excludeArtifactIds>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <outputDirectory>
                        ${project.build.directory}/dist
                    </outputDirectory>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.10</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Specification-Version>3.1</Specification-Version>
                            <Build-Time>${maven.build.timestamp}</Build-Time>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
4

0 回答 0