1. 程式人生 > >【譯】用maven使java web應用執行在內嵌的Jetty或Tomcat容器中

【譯】用maven使java web應用執行在內嵌的Jetty或Tomcat容器中

更多0

開發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>
  • 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伺服器及其帶來的所有成本。

參考