电光石火-穿越时空电光石火-穿越时空


eclipse创建maven项目

1.打开eclipse,文件-新建-其他,选择Maven Project

QQ截图20160728165249.jpg

 

2.第一个打上勾,并输入存储目录

QQ截图20160728165613.jpg

3.下一步,这里的Packing 选择 war的形式

QQ截图20160728170548.jpg

 

4.由于packing是war包,那么下面也就多出了webapp的目录

QQ截图20160728170816.jpg

 

5.由于我们的项目要使用eclipse发布到tomcat下面,这里我们需要先把项目转成dynamic web project
 
在我们的项目上点击右键,选择properties 并找到 Project Facets ,并点击Convert to faceted form…  然后勾选Dynamic Web Module 并点击ok  如下图:(3.0只有tomcat7才支持)

QQ截图20160728171047.jpg

 

6.自己在webapp文件夹下新建两个目录 META-INF ,WEB-INF

QQ截图20160728171509.jpg

7.然后我们要修改发布规则,右键点击项目, 选择 Deployment Assembly

2012010700442041.png

 

应该是这个样子

 

QQ截图20160728172007.jpg

 

8.至此一个基于maven的webapp就建立好了,并可以直接从eclipse中发布到tomcat中

 

补充:我们需要在src/main/webapp/WEB-INF下面创建一个web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>test</display-name>
  	<!-- 加载spring的xml配置文件到 spring的上下文容器中 --> 
	<context-param>
    	<param-name>contextConfigLocation</param-name>
    	<param-value>classpath:config/applicationContext.xml</param-value>
  	</context-param>
  	<!--将session绑定至当前(current)请求的线程上并通过事务管理器自动检测-->
  	<filter>
    	<filter-name>SpringOpenSessionInViewFilter</filter-name>
    	<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
  	</filter>
  	<filter-mapping>
    	<filter-name>SpringOpenSessionInViewFilter</filter-name>
    	<url-pattern>/*</url-pattern>
  	</filter-mapping>
  	<!-- 编码过滤器 -->
  	<filter>
    	<filter-name>characterEncodingFilter</filter-name>
    	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    	<init-param>
      		<param-name>encoding</param-name>
      		<param-value>UTF-8</param-value>
    	</init-param>
    	<init-param>
      		<param-name>forceEncoding</param-name>
      		<param-value>true</param-value>
    	</init-param>
  	</filter>
  	<filter-mapping>
    	<filter-name>characterEncodingFilter</filter-name>
    	<url-pattern>/*</url-pattern>
  	</filter-mapping>
  	<!-- 配置Spring监听 -->  
 	<listener>
    	<listener-class>  
        	org.springframework.web.context.ContextLoaderListener  
    	</listener-class>
  	</listener>
  	<!-- 配置SpringMVC -->
  	<servlet>
    	<servlet-name>SpringMVC</servlet-name>
    	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    	<init-param>
      		<param-name>contextConfigLocation</param-name>
      		<param-value>classpath:config/spring-mvc.xml</param-value>
    	</init-param>
    	<load-on-startup>0</load-on-startup>
  	</servlet>
  	<servlet-mapping>
    	<servlet-name>SpringMVC</servlet-name>
    	<url-pattern>/</url-pattern>
  	</servlet-mapping>
</web-app>


 

src/main/java/下新建config目录

新建applicationContext.xml

<?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:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:jms="http://www.springframework.org/schema/jms"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-jms-4.2.xsd">

	<!-- 当前Spring支持注解 -->
	<context:annotation-config />
	<!-- 设置当前项目的根包 -->
	<context:component-scan base-package="com.nit.test" />
	<!-- spring获取c3p0数据库连接池配置 -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:config/config.properties</value>
		</property>
	</bean>
	<!-- 配置c3p0数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="${jdbc.driverClassName}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="initialPoolSize" value="${connection.initialPoolSize}" />
		<property name="minPoolSize" value="${connection.minPoolSize}" />
		<property name="maxPoolSize" value="${connection.maxPoolSize}" />
		<property name="acquireIncrement" value="${connection.acquireIncrement}" />
		<property name="acquireRetryAttempts" value="${connection.acquireRetryAttempts}" />
		<property name="acquireRetryDelay" value="${connection.acquireRetryDelay}" />
		<property name="maxIdleTime" value="${connection.maxIdleTime}" />
		<property name="idleConnectionTestPeriod" value="${connection.idleConnectionTestPeriod}" />
		<property name="maxStatements" value="${connection.maxStatements}" />
		<property name="maxStatementsPerConnection" value="${connection.maxStatementsPerConnection}" />
		<property name="preferredTestQuery" value="select 1" />
		<property name="breakAfterAcquireFailure" value="true" />
		<property name="testConnectionOnCheckout" value="false" />
	</bean>
	<!-- 配置Hibernate的session工厂 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">false</prop>
				<prop key="hibernate.format_sql">false</prop>
			</props>
		</property>
		<!-- 注解扫描的包 -->
		<property name="packagesToScan">
			<list>
				<value>com.nit.test.model</value>
			</list>
		</property>
	</bean>
	<!-- 配置事务管理 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<!--启动spring事务注解功能 -->
	<tx:annotation-driven transaction-manager="transactionManager" />

</beans>

 

src/main/java/下新建config目录

新建config.properties

###### C3P0 MySQL CONFIG #######  
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=1234

connection.initialPoolSize=1
connection.minPoolSize=1
connection.maxPoolSize=5
connection.acquireIncrement=2
connection.acquireRetryAttempts=10
connection.acquireRetryDelay=1000
connection.maxIdleTime=3600
connection.idleConnectionTestPeriod=1800
connection.maxStatements=0
connection.maxStatementsPerConnection=0

 

src/main/java/下新建config目录

新建spring-mvc.xml

<?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: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-4.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

	<context:component-scan base-package="com.nit.test.controller" />

	<!-- 开启mvc注解 -->
	<mvc:annotation-driven />

	<!-- 允许对静态资源文件的访问 -->
	<mvc:default-servlet-handler />

</beans>

在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>
  <groupId>com.nit</groupId>
  <artifactId>test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<spring.version>4.2.5.RELEASE</spring.version>
		<hibernate.version>5.0.8.Final</hibernate.version>
		<jackson.version>2.7.5</jackson.version>
		<activemq.version>2.7.5</activemq.version>
		<quartz.version>2.2.1</quartz.version>
		<fastjson.version>1.1.41</fastjson.version>
	</properties>
	
	<dependencies>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.38</version>
		</dependency>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>${hibernate.version}</version>
		</dependency>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-c3p0</artifactId>
			<version>${hibernate.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</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-tx</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</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-webmvc-portlet</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
			<version>${jackson.version}</version>
		</dependency>

		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>${jackson.version}</version>
		</dependency>

		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-annotations</artifactId>
			<version>${jackson.version}</version>
		</dependency>

		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<version>2.4</version>
			<classifier>jdk15</classifier>
		</dependency>

	</dependencies>
</project>


至此,项目生成完成

本博客所有文章如无特别注明均为原创。作者:似水的流年
版权所有:《电光石火-穿越时空》 => eclipse创建maven项目
本文地址:http://ilkhome.cn/index.php/archives/94/
欢迎转载!复制或转载请以超链接形式注明,文章为 似水的流年 原创,并注明原文地址 eclipse创建maven项目,谢谢。

评论