您的位置:首页 > 编程语言 > Java开发

Maven Jetty 插件及数据源配置

2010-09-06 15:57 423 查看
一直以来做JSP开发使用的都是Eclipse+Tomcat,虽然IDE工具带来了巨大的便利性,但是基于JAVA的IDE最大的一个特点就是占用资源。

maven 是优秀的项目管理工具,最近的项目逐渐转向了使用maven管理,命令行式的操作,让我怀念起了linux的那些岁月。

大凡web项目都会用到数据库,本文就maven下,jetty服务器中配置数据源简单说明,希望对大家有帮助:)

 

1)使用maven创建项目web项目

 

mvn archetype:create -DgroupId=sample -DartifactId=sample -DarchetypeArtifactId=maven-archetype-webapp
 

 

2)JNDI配置: 在项目的src/main/resources/目录下创建jetty-env.xml

 

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
<!-- MsSqlServer DataSource -->
<New id="sample" class="org.mortbay.jetty.plus.naming.Resource">
<Arg>jdbc/sample</Arg>
<Arg>
<New class="org.apache.commons.dbcp.BasicDataSource">
<Set name="driverClassName">com.microsoft.sqlserver.jdbc.SQLServerDriver</Set>
<Set name="url">jdbc:sqlserver://127.0.0.1:1433;databaseName=sample;</Set>
<Set name="username">sa</Set>
<Set name="password">sa</Set>
</New>
</Arg>
</New>

</Configure>


 

3) 在web.xml中加入对

<resource-ref>
<description>Microsoft SQL Server 2000 Datasource</description>
<res-ref-name>jdbc/sample</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
 

3) jetty-maven-plugin:

在项目的pom.xml中加入jetty-maven-plugin

<build>
…
<plugins>
…
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.25</version>
<configuration>
<jettyEnvXml>src/main/resources/jetty-env.xml</jettyEnvXml>
<scanIntervalSeconds>10</scanIntervalSeconds>
</configuration>
<dependencies>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>microsoft</groupId>
<artifactId>sqljdbc</artifactId>
<version>2.0.1803</version>
<type>jar</type>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>


 

4)访问数据源示例:

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup(“java:comp/env”);
DataSource ds = (DataSource)
envCtx.lookup(“jdbc/sample”);
 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息