本人有点傻,研究Maven研究了有一段时间,刚刚有些入门,记录下来方便以后使用

工作环境:jdk7 myeclipse10 maven3.1.1

1 下载maven3.1.1

http://maven.apache.org/download.cgi

2配置环境变量

        

额,别问我为什么叫M2_HOME,事实上我感觉叫MAVEN_HOME更好一点

3 使用myeclipse 新建webproject 并勾选Add Maven support


4 配置maven


点击add 选择maven的文件夹就可以,尽量不要有中文路径

项目建立后 文件夹结构例如以下


MyEclipse不会自己主动建立resources文件夹须要手动建立


以下须要编写 pom.xml

详细须要的东西 能够到 http://mvnrepository.com  查找 


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>sshmaven</groupId>
	<artifactId>sshmaven</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name />
	<description />
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.apache.openejb</groupId>
			<artifactId>javaee-api</artifactId>
			<version>5.0-1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.1</version>
			<scope>provided</scope>
		</dependency>
		<!-- Struts2 -->
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.3.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-spring-plugin</artifactId>
			<version>2.3.1</version>
		</dependency>
		<!-- 加入Hibernate依赖 -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>3.6.5.Final</version>
		</dependency>
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>             <!-- 加入Log4J依赖 -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.6.1</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-nop</artifactId>
			<version>1.6.4</version>
		</dependency>
		<!-- 加入javassist -->
		<dependency>
			<groupId>javassist</groupId>
			<artifactId>javassist</artifactId>
			<version>3.11.0.GA</version>
		</dependency>
		<!-- 加入Spring依赖 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>3.1.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>3.1.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>3.1.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>3.1.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>3.1.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>3.1.1.RELEASE</version>
		</dependency>
	</dependencies>

	<build>
		<directory>target</directory><!-- 这是打包的文件的路径。默认是该工程的根文件夹。 -->
		<finalName>mavenwebdemo</finalName><!--生成的目标文件名称 -->
		<defaultGoal>compile</defaultGoal>
		<plugins>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
			</plugin>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.5</source>
					<target>1.5</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat6-maven-plugin</artifactId>
				<version>2.0-beta-1</version>
				<configuration>
					<url>http://localhost:8080/manager/text</url>
					<server>tomcat7</server>
					<username>admin</username>
					<password>admin</password>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

web.xml的配置

<span style="white-space:pre">	</span><filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/applicationContext.xml</param-value>
	</context-param>
struts.xml配置

<span style="white-space:pre">	</span><package name="myuser" namespace="/" extends="struts-default">
		<action name="userAction" class="com.my.user.action.UserAction">
		<result name="login">login.html</result>
		</action>
	</package>
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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<context:component-scan base-package="com.my"/>
</beans>


UserAction.java

@Controller
public class UserAction extends ActionSupport {
	@Resource
	private UserService userService;

	public void test() {
		System.out.println("hello maven");
		userService.test();
	}

}

UserServices.java
@Service("userService")
public class UserService {

	@Resource
	private UserDao userDao;

	public void test() {
		System.out.println("userservice");
		userDao.test();
	}

UserDao.java

@Repository
public class UserDao {
	
	public void test(){
		System.out.println("userdao");
	}
}

直接部署到tomcat6測试

訪问 http://localhost:8080/sshmaven/userAction!test

输出结果

部署

在pom.xml文件点击右键


刷新项目会在target文件夹出现


拷贝进tomcat就可以使用