首页 > 编程知识 正文

maven怎么运行java

时间:2023-05-04 01:26:00 阅读:203755 作者:4723

在开发Java Web应用程序时,从“真实”环境中获得快速反馈非常实用。 在本文中,我将探讨如何在嵌入式容器Jetty或Tomcat中运行带有Maven的Java Web应用程序。 在Podcastpedia.org网站的支持下,我将展示如何配置它们以开发podcastpedia项目。

先决条件

您应该已经安装了Maven并至少安装了Java 7 。 理想情况下,您可以自己设置podcastpedia项目,以进行实际操作。

Jetty Maven插件

插件配置

<!-- 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每5秒查找一次更改( scanInterval ) 定义的外部依赖关系以连接到数据库并发送电子邮件 Jetty.xml配置文件

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">test-dev@podcastpedia.org</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配置文件(jetty.xml)中 ,进行了以下配置:

Server类(或子类,如果已扩展)和全局选项。 ThreadPool(最小和最大线程)。 连接器(端口,超时,缓冲区大小,协议)。 处理程序结构(默认处理程序和/或contextHandlerCollections)。 扫描并部署Web应用程序和上下文的Deployment Manager。 提供身份验证检查的登录服务。 请求日志。 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运行的端口 在Tomcat查找配置时指定contextFile 定义的外部依赖关系以连接到数据库并发送电子邮件 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="test-dev@podcastpedia.org" password="test-dev" mail.smtp.host="mail.podcastpedia.org" mail.smtp.port="25" mail.smtp.user="test-dev@podcastpedia.org" mail.transport.protocol="smtp" mail.smtp.auth="true"/> </Context>

在context.xml中,定义了数据库和电子邮件资源。

随您去吧…由Spring Framework支持的Java Web应用程序运行轻型servlet容器,可以真正替代JAVA EE服务器及其所有成本。

注意:
这些是简单的配置,但足以满足当前的开发需求。 我的建议是阅读相应的文档以获取更多高级选项和功能。

资源资源 Jetty Maven插件 Apache Tomcat Maven插件

翻译自: https://www.javacodegeeks.com/2015/06/run-java-web-apps-in-embedded-containers-with-maven-jetty-and-tomcat.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。