将maven管理的web模块发布到jetty服务器中
student-parent模块的pom.xml中声明插件
<!-- jetty插件 --> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.10</version> <configuration> <scanIntervalSeconds>1000</scanIntervalSeconds> </configuration> </plugin> </plugins>
student-web模块的pom.xml中引入插件
<plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> </plugin>
开启服务:
pom.xml右键---build---goals:jetty:run
停止服务:
直接在eclipse的控制台点击停止按钮即可!
将maven管理的web模块发布到tomcat服务器中
第一种:作为一个独立的项目进行发布,不与其它项目一起部署,tomcat启动后只能访问该项目
student-web模块的pom.xml
<!-- cargo插件 配置tomcat--> <!-- 独立发布 --> <plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <configuration> <container> <containerId>tomcat6x</containerId> <home>E:\technology-hqh\soft\server\apache-tomcat-6.0.29</home> </container> <configuration> <type>standalone</type> <home>${project.build.directory}/tomcat6-deploy</home> <properties> <cargo.servlet.port>9999</cargo.servlet.port> </properties> </configuration> </configuration> </plugin>
第二种:war包被放到tomcat的webapps目录下,tomcat启动后,所有项目都能被访问到
student-web模块的pom.xml
<!-- 将war包加入到tomcat的webapps下,与其它项目一起运行 --> <plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <configuration> <!-- Container configuration --> <container> <containerId>tomcat6x</containerId> <home>E:\technology-hqh\soft\server\apache-tomcat-6.0.29</home> </container> <!-- Configuration to use with the container (which will also configure the deployer) --> <configuration> <type>existing</type> <home>E:\technology-hqh\soft\server\apache-tomcat-6.0.29</home> </configuration> </configuration> </plugin>
启动服务
pom.xml右键---build---goals:celan package cargo:start
pom.xml右键---build---goals:cargo:run
停止服务
pom.xml右键---build---goals:cargo:stop
注意:使用eclipse控制的停止按钮不会停止tomcat服务,端口8080仍然被监听着,必须使用cargo:stop命令才能在eclipse中停止掉tomcat服务!
---------------------------------------------------------------------------------------------------------------
项目相关的配置文件
parent模块的pom.xml
<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> <!-- 聚合:聚合其它模块 --> <modules> <module>../student-core</module> <module>../student-persistent</module> <module>../student-service</module> </modules> <!-- 继承:超级pom的坐标,该pom中的配置用于其它模块继承所用 --> <groupId>com.hqh.student</groupId> <artifactId>student-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <!-- 配置项目发布到私服的哪个仓库中 --> <distributionManagement> <!-- 配置SNAPSHOT版本发布的目标仓库 --> <snapshotRepository> <id>student-SNAPSHOT</id> <name>student project snapshot</name> <!-- 使用nexus默认提供的snapshot仓库,没有按项目为单位创建仓库 --> <url>http://localhost:8081/nexus/content/repositories/snapshots/</url> </snapshotRepository> <!-- 配置release版本发布的目标仓库 --> <repository> <id>student-release</id> <name>student project release</name> <!-- 使用nexus默认提供的release仓库,没有按项目为单位创建仓库 --> <url>http://localhost:8081/nexus/content/repositories/releases/</url> </repository> </distributionManagement> <name>student-parent</name> <url>http://maven.apache.org</url> <!-- 单点维护属性,其它地方通过${name}来引用 --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <junit.version>4.10</junit.version> <spring.version>3.0.5.RELEASE</spring.version> </properties> <!-- 下面的配置需要子类显示声明才能被继承 --> <dependencyManagement> <dependencies> <!-- 模块 --> <dependency> <groupId>${project.groupId}</groupId> <artifactId>student-core</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>${project.groupId}</groupId> <artifactId>student-persistent</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>${project.groupId}</groupId> <artifactId>student-service</artifactId> <version>${project.version}</version> </dependency> <!-- junit测试单元 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- 日志 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version> </dependency> <!-- SPRING --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.11</version> </dependency> <!-- ORM --> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.1.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.6.10.Final</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.4</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.12.1.GA</version> </dependency> <!-- database --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.19</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!-- j2se --> <dependency> <groupId>com.kenai.nbpwr</groupId> <artifactId>javax-inject</artifactId> <version>1.0-201002241208</version> </dependency> <!-- commons --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.2</version> </dependency> <!-- servlet+jstl+jsp --> <dependency> <groupId>servletapi</groupId> <artifactId>servletapi</artifactId> <version>2.4</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>jsptags</groupId> <artifactId>pager-taglib</artifactId> <version>2.0</version> </dependency> <!-- json --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.6</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.6</version> </dependency> </dependencies> </dependencyManagement> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <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> <!-- jetty插件 --> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.10</version> <configuration> <scanIntervalSeconds>1000</scanIntervalSeconds> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>
student-web模块的web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:beans.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>srpingMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>srpingMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
springMVC需要的servlet配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven/> <!-- 配置需要被扫描的包 --> <context:component-scan base-package="com.hqh.student"/> <!-- 配置对静态资源文件的访问不被过滤 --> <mvc:resources location="/resources/" mapping="/resources/**"/> <!-- 配置返回的数据如何呈现:前缀+逻辑视图+后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <!-- <property name="defaultErrorView"> <value>failure</value> </property> --> <property name="exceptionMappings"> <props> <prop key="com.hqh.student.exception.StudentException">error</prop> </props> </property> </bean> </beans>
student-web模块的pom.xml
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- 继承 --> <parent> <groupId>com.hqh.student</groupId> <artifactId>student-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../student-parent/pom.xml</relativePath> </parent> <artifactId>student-web</artifactId> <packaging>war</packaging> <name>student-web Maven Webapp</name> <dependencies> <dependency> <groupId>${project.groupId}</groupId> <artifactId>student-service</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> </dependencies> <build> <finalName>student-web</finalName> <plugins> <!-- jetty插件 --> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> </plugin> <!-- cargo插件 配置tomcat--> <!-- 第一种方式:独立发布 <plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <configuration> <container> <containerId>tomcat6x</containerId> <home>E:\technology-hqh\soft\server\apache-tomcat-6.0.29</home> </container> <configuration> <type>standalone</type> <home>${project.build.directory}/tomcat6-deploy</home> <properties> <cargo.servlet.port>9999</cargo.servlet.port> </properties> </configuration> </configuration> </plugin> --> <!-- 第二种方式:与其它项目一起运行(将war包加入到tomcat的webapps下) --> <plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <configuration> <!-- Container configuration --> <container> <containerId>tomcat6x</containerId> <home>E:\technology-hqh\soft\server\apache-tomcat-6.0.29</home> </container> <!-- Configuration to use with the container (which will also configure the deployer) --> <configuration> <type>existing</type> <home>E:\technology-hqh\soft\server\apache-tomcat-6.0.29</home> </configuration> </configuration> </plugin> </plugins> </build> </project>