开发java web应用时,能在“真实”的环境中有快速的反馈是非常实用的。本文将探寻如何使用maven使java web应用运行在内嵌的Jetty或Tomcat容器中。我将展示如何配置他们来开发Podcastpedia.org网站的工程podcastpedia。
环境准备
你需要有Maven,至少安装了Java 7。正常情况你应该可以自行部署和启动podcastpedia工程并看到效果
Jetty Maven Plugin
插件配置
<!-- http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html --><plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
<jettyConfig>${project.basedir}/src/main/resources/config/jetty9.xml</jettyConfig>
<stopKey>STOP</stopKey>
<stopPort>9999</stopPort>
<scanIntervalSeconds>5</scanIntervalSeconds>
<scanTargets>
<scanTarget>${project.basedir}/src/main</scanTarget>
<scanTarget>${project.basedir}/src/test</scanTarget>
</scanTargets>
<contextXml>${project.basedir}/src/test/resources/jetty-context.xml</contextXml>
<webAppConfig>
<contextPath>/</contextPath>
</webAppConfig>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.java.version}</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>${java.mail.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>${tomcat.jdbc.version}</version>
</dependency>
</dependencies></plugin>注意:
jettyConfig 指定Jetty的配置文件,下一部分将有该配置文件的具体内容
scanTargets 指定了Jetty监控文件变化的文件夹
指定连接数据库和发邮件的 依赖包
Jetty.xml配置文件
<?xml version="1.0" encoding="UTF-8"?><Configure class="org.eclipse.jetty.webapp.WebAppContext"> <New id="pcmdbDS" class="org.eclipse.jetty.plus.jndi.Resource"> <Arg>jdbc/pcmDB</Arg> <Arg> <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource"> <Set name="Url">jdbc:mysql://localhost:3307/pcmDB?allowMultiQueries=true </Set> <Set name="User">pcm</Set> <Set name="Password">pcm_pw</Set> </New> </Arg> </New> <New id="mailSessionId" class="org.eclipse.jetty.plus.jndi.Resource"> <Arg>mail/Session</Arg> <Arg> <New class="org.eclipse.jetty.jndi.factories.MailSessionReference"> <Set name="user">[email protected]</Set> <Set name="password">test-dev</Set> <Set name="properties"> <New class="java.util.Properties"> <Put name="mail.host">mail.podcastpedia.org</Put> <Put name="mail.debug">true</Put> <Put name="mail.transport.protocol">smtp</Put> <Put name="mail.smtp.port">25</Put> <Put name="mail.smtp.auth">true</Put> </New> </Set> </New> </Arg> </New></Configure>
在 Jetty配置文件 中,你需要配置一下内容:
Server类(或者子类)以及全局的可选项
一个线程池(最小、最大线程数)
Connectors连接器(端口号、超时时间、缓冲区大小、协议)
处理器结构(handler structure)(默认的处理器或者一个contextHandlerCollections)
扫描部署的webapps和容器上下文的部署管理器
提供认证的登录服务
请求日志
Apache Tomcat Maven插件
apache tomcat maven插件的配置
<!-- https://tomcat.apache.org/maven-plugin-trunk/index.html --><plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- http port -->
<port>8080</port>
<!-- application path always starts with /-->
<path>/</path>
<!-- optional path to a context file -->
<contextFile>context.xml</contextFile>
<!-- optional system propoerties you want to add -->
<systemProperties>
<appserver.base>${project.build.directory}/appserver-base</appserver.base>
<appserver.home>${project.build.directory}/appserver-home</appserver.home>
<derby.system.home>${project.build.directory}/appserver-base/logs</derby.system.home>
<java.io.tmpdir>${project.build.directory}</java.io.tmpdir>
</systemProperties>
<!-- if you want to use test dependencies rather than only runtime -->
<useTestClasspath>false</useTestClasspath>
<!-- optional if you want to add some extra directories into the classloader -->
<additionalClasspathDirs>
<additionalClasspathDir></additionalClasspathDir>
</additionalClasspathDirs>
</configuration>
<!-- For any extra dependencies needed when running embedded Tomcat (not WAR dependencies) add them below -->
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.java.version}</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>${java.mail.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>${tomcat.jdbc.version}</version>
</dependency>
</dependencies></plugin>注意:
指定tomcat的 端口号
指定 contextFile ,用来告诉tomcat配置文件在哪里
指定连接数据库和发邮件的 依赖包
context.xml
<Context> <Resource name="jdbc/pcmDB" auth="Container" type="javax.sql.DataSource" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" initialSize="5" maxActive="55" maxIdle="21" minIdle="13" timeBetweenEvictionRunsMillis="34000" minEvictableIdleTimeMillis="55000" validationQuery="SELECT 1" validationInterval="34" testOnBorrow="true" removeAbandoned="true" removeAbandonedTimeout="233" username="pcm" password="pcm_pw" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3307/pcmDB?allowMultiQueries=true" /> <Resource name="mail/Session" auth="Container" type="javax.mail.Session" username="[email protected]" password="test-dev" mail.smtp.host="mail.podcastpedia.org" mail.smtp.port="25" mail.smtp.user="[email protected]" mail.transport.protocol="smtp" mail.smtp.auth="true" /> </Context>
在context.xml中,定义了数据库和邮件资源
就这样,使用了spring框架的Java web应用可以运行轻量级的servlet容器,显然这种方式可以代替JavaEE服务器及其带来的所有成本。
参考
翻译:http://www.javacodegeeks.com/2015/06/run-java-web-apps-in-embedded-containers-with-maven-jetty-and-tomcat.html
原创文章: 【译】用maven使java web应用运行在内嵌的Jetty或Tomcat容器中 ,转载请注明:转载自 戎码一生
Post Footer automatically generated by wp-posturl plugin for wordpress.
共同學習,寫下你的評論
評論加載中...
作者其他優質文章