1.新建Maven Module项目account-web,父项目选中account,生成后,account项目的Pom文件内容为:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.iteye.xujava</groupId> <artifactId>account</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <name>Account</name> <modules> <module>account-email</module> <module>account-persist</module> <module>account-captcha</module> <module>account-service</module> <module>account-web</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <springframework.version>2.5.6</springframework.version> <junit.version>4.9</junit.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>
account-web项目内容的Pom文件内容为:
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.iteye.xujava</groupId> <artifactId>account</artifactId> <version>1.0.0</version> </parent> <artifactId>account-web</artifactId> <name>Account Web</name> <packaging>war</packaging> <dependencies> <dependency> <groupId>${project.groupId}</groupId> <artifactId>account-service</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.4</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> </dependencies> </project>
2.在src/main/java的com.iteye.xujava.account.web的包下新建以下4个文件
package com.iteye.xujava.account.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.iteye.xujava.account.service.AccountService; import com.iteye.xujava.account.service.AccountServiceException; public class ActivateServlet extends HttpServlet { private static final long serialVersionUID = 3668445055149826106L; private ApplicationContext context; @Override public void init() throws ServletException { super.init(); context = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String key = req.getParameter("key"); if (key == null || key.length() == 0) { resp.sendError(400, "No activation key provided."); return; } AccountService service = (AccountService) context.getBean("accountService"); try { service.activate(key); resp.getWriter().write("Account is activated, now you can login."); } catch (AccountServiceException e) { resp.sendError(400, "Unable to activate account"); return; } } }
package com.iteye.xujava.account.web; import java.io.IOException; import java.io.OutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.iteye.xujava.account.service.AccountService; import com.iteye.xujava.account.service.AccountServiceException; public class CaptchaImageServlet extends HttpServlet { private ApplicationContext context; private static final long serialVersionUID = 5274323889605521606L; @Override public void init() throws ServletException { super.init(); context = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String key = request.getParameter("key"); if (key == null || key.length() == 0) { response.sendError(400, "No Captcha Key Found"); } else { AccountService service = (AccountService) context.getBean("accountService"); try { response.setContentType("image/jpeg"); OutputStream out = response.getOutputStream(); out.write(service.generateCaptchaImage(key)); out.close(); } catch (AccountServiceException e) { response.sendError(400, e.getMessage()); } } } }
package com.iteye.xujava.account.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.iteye.xujava.account.service.AccountService; import com.iteye.xujava.account.service.AccountServiceException; public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 929160785365121624L; private ApplicationContext context; @Override public void init() throws ServletException { super.init(); context = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String id = req.getParameter("id"); String password = req.getParameter("password"); if (id == null || id.length() == 0 || password == null || password.length() == 0) { resp.sendError(400, "incomplete parameter"); return; } AccountService service = (AccountService) context.getBean("accountService"); try { service.login(id, password); resp.getWriter().print("Login Successful!"); } catch (AccountServiceException e) { resp.sendError(400, e.getMessage()); } } }
package com.iteye.xujava.account.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.iteye.xujava.account.service.AccountService; import com.iteye.xujava.account.service.AccountServiceException; import com.iteye.xujava.account.service.SignUpRequest; public class SignUpServlet extends HttpServlet { private static final long serialVersionUID = 4784742296013868199L; private ApplicationContext context; @Override public void init() throws ServletException { super.init(); context = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String id = req.getParameter("id"); String email = req.getParameter("email"); String name = req.getParameter("name"); String password = req.getParameter("password"); String confirmPassword = req.getParameter("confirm_password"); String captchaKey = req.getParameter("captcha_key"); String captchaValue = req.getParameter("captcha_value"); if (id == null || id.length() == 0 || email == null || email.length() == 0 || name == null || name.length() == 0 || password == null || password.length() == 0 || confirmPassword == null || confirmPassword.length() == 0 || captchaKey == null || captchaKey.length() == 0 || captchaValue == null || captchaValue.length() == 0) { resp.sendError(400, "Parameter Incomplete."); return; } AccountService service = (AccountService) context.getBean("accountService"); SignUpRequest request = new SignUpRequest(); request.setId(id); request.setEmail(email); request.setName(name); request.setPassword(password); request.setConfirmPassword(confirmPassword); request.setCaptchaKey(captchaKey); request.setCaptchaValue(captchaValue); request.setActivateServiceUrl(getServletContext().getRealPath("/") + "activate"); try { service.signUp(request); resp.getWriter().print("Account is created, please check your mail box for activation link."); } catch (AccountServiceException e) { resp.sendError(400, e.getMessage()); return; } } }
3. 新建src/main/webapp源码目录,在目录下新建login.jsp和signup.jsp,新建WEB-INF,在其下新建web.xml.
xml内容为:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Sample Maven Project: Account Service</display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:/account-persist.xml classpath:/account-captcha.xml classpath:/account-email.xml classpath:/account-service.xml </param-value> </context-param> <servlet> <servlet-name>CaptchaImageServlet</servlet-name> <servlet-class>com.iteye.xujava.account.web.CaptchaImageServlet</servlet-class> </servlet> <servlet> <servlet-name>SignUpServlet</servlet-name> <servlet-class>com.iteye.xujava.account.web.SignUpServlet</servlet-class> </servlet> <servlet> <servlet-name>ActivateServlet</servlet-name> <servlet-class>com.iteye.xujava.account.web.ActivateServlet</servlet-class> </servlet> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.iteye.xujava.account.web.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CaptchaImageServlet</servlet-name> <url-pattern>/captcha_image</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>SignUpServlet</servlet-name> <url-pattern>/signup</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ActivateServlet</servlet-name> <url-pattern>/activate</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>signup.jsp</welcome-file> </welcome-file-list> </web-app>
login.jsp内容为:
<%@ page contentType="text/html; charset=gb2312" language="java" %> <html> <head> <style type="text/css"> .text-field {position: absolute; left: 40%; background-color:rgb(255,230,220);} label {display: inline-table; width: 90px; margin: 0px 0px 10px 20px; } input {display: inline-table; width: 150px; margin: 0px 20px 10px 0px;} h2 {margin: 20px 20px 20px 40px;} button {margin: 20px 20px 10px 110px} </style> </head> <body> <div class="text-field"> <h2>账户登录</h2> <form name="login" action="login" method="post"> <label>账户ID:</label><input type="text" name="id"></input><br/> <label>密码:</label><input type="password" name="password"></input><br/> <button>确认并提交</button> </form> </div> </body> </html>
signup.jsp内容如下:
<%@ page contentType="text/html; charset=gb2312" language="java" %> <%@ page import="com.iteye.xujava.account.service.*, org.springframework.context.ApplicationContext, org.springframework.web.context.support.WebApplicationContextUtils"%> <html> <head> <style type="text/css"> .text-field {position: absolute; left: 40%; background-color:rgb(255,230,220);} label {display: inline-table; width: 90px; margin: 0px 0px 10px 20px; } input {display: inline-table; width: 150px; margin: 0px 20px 10px 0px;} img {width:150px; margin: 0px 20px 10px 110px;} h2 {margin: 20px 20px 20px 40px;} button {margin: 20px 20px 10px 110px} </style> </head> <body> <% ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext( getServletContext() ); AccountService accountervice = (AccountService) context.getBean( "accountService" ); String captchaKey = accountervice.generateCaptchaKey(); %> <div class="text-field"> <h2>注册新账户</h2> <form name="signup" action="signup" method="post"> <label>账户ID:</label><input type="text" name="id"></input><br/> <label>Email:</label><input type="text" name="email"></input><br/> <label>显示名称:</label><input type="text" name="name"></input><br/> <label>密码:</label><input type="password" name="password"></input><br/> <label>确认密码:</label><input type="password" name="confirm_password"></input><br/> <label>验证码:</label><input type="text" name="captcha_value"></input><br/> <input type="hidden" name="captcha_key" value="<%=captchaKey%>"/> <img src="<%=request.getContextPath()%>/captcha_image?key=<%=captchaKey%>"/> </br> <button>确认并提交</button> </form> </div> </body> </html>
4.在src/main/resources目录下新建service.properties文件,内容如下,此处要写正确邮箱,否则测试无法通过:
email.protocol=smtp email.host=smtp.163.com email.port=25 email.username=test@163.com email.password=password email.auth=true email.systemEmail=test@163.com persist.file=E:/mavenspace/account/target/test-classes/persist-data.xml
5.测试:
在account-web项目的POM内容中添加plugin:
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <pluginManagement> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>7.1.0.RC1</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <webAppConfig> <contextPath>/account</contextPath> </webAppConfig> </configuration> </plugin> </plugins> </pluginManagement> </build>
在.m2/中的setting.xml文件中的
<pluginGroups>中增加以下内容:
<pluginGroup>org.mortbay.jetty</pluginGroup>
就可以直接使用mvn jetty:run部署并启动Jetty。
或输入:
mvn jetty:run -Djetty.port=9999改变端口
6.输入:http://localhost:8080/account/signup.jsp进行注册,注册完后会发送邮件到你输入的邮箱,发送到邮箱的链接不正确,正确格式类似于:http://localhost:8080/account/activate?key=hhhjdfbm。
输入链接激活后,就可以输入http://localhost:8080/account/login.jsp登录了。
7.自动部署cargo-maven2-plugin
7.1本地部署
standalone模式:Cargo会从Web容器的安装目录复制一份配置到用户指定的目录,每次重新构建的时候,这个目录会被清空
<plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.0</version> <configuration> <container> <containerId>tomcat6x</containerId> <home>D:\dev\tomcat\6035frame</home> </container> <configuration> <type>standalone</type> <home>${project.build.directory}/tomcat6x</home> </configuration> </configuration> </plugin>
这样生成的访问路径为:http://localhost:8080/account-web-1.0.0/,
要设置finalName,即:
<build> <finalName>account</finalName> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <pluginManagement> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>7.1.0.RC1</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <webAppConfig> <contextPath>/account</contextPath> </webAppConfig> </configuration> </plugin> <plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.0</version> <configuration> <container> <containerId>tomcat6x</containerId> <home>D:\dev\tomcat\6035frame</home> </container> <configuration> <type>standalone</type> <home>${project.build.directory}/tomcat6x</home> </configuration> </configuration> </plugin> </plugins> </pluginManagement> </build>
访问路径为:http://localhost:8080/account/,
配置完成后,运行mvn clean install,再运行mvn cargo:start启动服务器.cargo会把Tomcat中的文件复制到account-web/target/tomcat6x下,再将项目部署到tomcat6x下的webapps中。
运行mvn cargo:stop停止服务器。
eisting模式:用户要指定现有Web容器配置目录,Cargo会直接使用这些配置并将应用部署到其对应的位置
<plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.0</version> <configuration> <container> <containerId>tomcat6x</containerId> <home>D:\dev\tomcat\6035frame</home> </container> <configuration> <type>existing</type> <home>D:\dev\tomcat\6035frame</home> </configuration> </configuration> </plugin>
配置完成后,运行mvn clean install,再运行mvn cargo:start启动服务器.cargo会把项目部署到指定的Tomcat下的webapps中。
运行mvn cargo:stop停止服务器。
7.2远程部署(未测试)
<plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.0</version> <configuration> <container> <containerId>tomcat6x</containerId> <type>remote</type> </container> <configuration> <type>runtime</type> <properties> <cargo.remote.username>username</cargo.remote.username> <cargo.remote.password>password</cargo.remote.password> <cargo.tomcat.manager.url>http://localhost:8080/manager</cargo.tomcat.manager.url> </properties> </configuration> </configuration> </plugin>
运行命令:mvn cargo:redeploy部署
相关推荐
apache-maven-3.6.3-bin。apache-maven-3.6.3-bin。apache-maven-3.6.3-bin。apache-maven-3.6.3-bin。apache-maven-3.6.3-bin。apache-maven-3.6.3-bin。apache-maven-3.6.3-bin。apache-maven-3.6.3-bin。apache-...
exec-maven-plugin是Maven生态系统中的一个插件,它允许用户在Maven构建过程中执行外部命令或脚本。这使得Maven项目可以集成更多的自定义操作,比如运行特定的脚本、调用系统命令等。本文将详细介绍exec-maven-...
apache-maven-3.8.6-bin.tar.tz--test apache-maven-3.8.6-bin.tar.tz--test apache-maven-3.8.6-bin.tar.tz--test apache-maven-3.8.6-bin.tar.tz--test apache-maven-3.8.6-bin.tar.tz--test apache-maven-3.8.6-...
apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-maven-3.8.6-bin.zip apache-...
maven3-plugin-3.0.1-sources.jar
maven-aether-provider-3.2.1-sources.jar maven-antrun-plugin-1.3.jar maven-archiver-2.2.jar maven-artifact-3.2.1-sources.jar maven-assembly-plugin-2.2-beta-5.jar maven-bundle-plugin-1.0.0.jar maven-...
apache-maven-3.0.5 apache-maven-3.1.1 apache-maven-3.2.5 apache-maven-3.3.9 apache-maven-3.5.4 apache-maven-3.6.3 apache-maven-3.8.5 每个版本包含4个文件: apache-maven-3.8.5-bin.tar.gz apache-maven-...
打开并输入:path= D:/Development/eclipse-JavaEE/eclipse/plugins/maven(请参照上面对应你的 maven 插件) 4. 重启 eclipse,OK,完成了,启动后你打开Window ---> Preferences 会发现一个多了一个选项Maven...
`maven-compiler-plugin-3.8.0-source-release` 是 Maven 生态系统中不可或缺的一部分,它提供了可靠的源代码编译功能,使得开发者能够专注于编写代码,而无需关心构建过程的细节。通过理解 Maven 插件的工作原理和...
maven-deploy-plugin-2.8.2.jar
apache-maven-3.0.4-bin.tar.gz apache-maven-3.0.4-bin.zip apache-maven-3.0.5-bin.tar.gz apache-maven-3.0.5-bin.zip apache-maven-3.1.0-bin.tar.gz apache-maven-3.1.0-bin.zip apache-maven-3.1.1-bin.tar.gz...
maven3-plugin-3.0.0-1-sources.jar
maven-jar-plugin-3.1.1.jar
每个阶段都可以通过特定的插件来执行,例如,`maven-compiler-plugin`用于编译源代码,`maven-surefire-plugin`负责运行单元测试。Maven通过使用Project Object Model (POM)文件来描述项目信息,包括依赖、构建配置...
`maven-jetty-plugin`简化了Java Web应用的开发和测试过程,通过Maven的命令行即可轻松启动Jetty服务器。了解并熟练掌握这个插件的使用,对于提升开发效率有着显著的效果。在选择版本时,应根据项目的具体需求和...
maven-antrun-plugin-3.0.0.jar
maven-surefire-plugin-2.22.1.jar
maven-site-plugin-3.3.jar