Maven与eclipse整合

时间:2019-04-15
本文章向大家介绍Maven与eclipse整合,主要包括Maven与eclipse整合使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
版权声明: https://blog.csdn.net/zdp072/article/details/37355993

一. 创建Java项目

第1步:首先导入前面命令行建立的两个maven项目Hello和HelloFriend。
              方法:选择file-->import-->Existing MAVEN PROJECTS选项选择相应项目路径导入就可以

第2步:按顺序先后运行Hello和HelloFriend项目的的构建
              方法:右击各自项目pom.xml文件,选择run as中的maven install命令将构件安装至仓库中

第3步:通过eclipse新建第三个maven项目。选择file-->new-->other-->MAVEN PROJECT选项


第4步:在src/main/java中新建文件com.zdp.maven.MakeFriends.java

public class MakeFriends {
	
	public String makeFriends(String name){
		HelloFriend friend = new HelloFriend();
		friend.sayHelloToFriend("litingwei");	
		String str = "Hey,"+friend.getMyName()+" make a friend please.";
		System.out.println(str);
		return str;
	}
}

第5步:在src/test/java中新建文件com.zdp.maven.MakeFriendsTest.java

public class MakeFriendsTest {
	
	@Test
	public void testMakeFriends(){		
		MakeFriends makeFriend = new MakeFriends();
		String str = makeFriend.makeFriends("litingwei");
		assertEquals("Hey,John make a friend please.",str);		
	}
}

第6步:点击根文件夹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.zdp.maven</groupId>
	<artifactId>MakeFriends</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>MakeFriends</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.9</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>com.zdp.maven</groupId>
			<artifactId>HelloFriend</artifactId>
			<version>0.0.1-SNAPSHOT</version>
			<scope>compile</scope>
		</dependency>
	</dependencies>
</project>

第7步:右击pom.xml选择run as 中的命令运行就可以


二. 创建Web项目

第1步:创建maven web工程


第2步: 继承parent, 改动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>
	<artifactId>webs</artifactId>
	<packaging>war</packaging>
	<name>webs Maven Webapp</name>

	<parent>
		<groupId>com.zdp.maven</groupId>
		<artifactId>Parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../Parent/pom.xml</relativePath>
	</parent>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
		</dependency>
		<dependency>
			<groupId>com.zdp.maven</groupId>
			<artifactId>MakeFriends</artifactId>
		</dependency>
	</dependencies>
</project>

第3步:建立測试jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="com.zdp.maven.*"%>
<%
    MakeFriends makeFriends=new MakeFriends();
    out.println(makeFriends.makeFriends("wanglipeng"));
%>


第4步:模块聚合, 改动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>
	<groupId>com.zdp.maven</groupId>
	<artifactId>Parent</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	<name>Parent</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	
	<modules>
		<module>../Hello</module>
		<module>../HelloFriend</module>
		<module>../MakeFriends</module>
		<module>../webs</module>
	</modules>
	
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>junit</groupId>
				<artifactId>junit</artifactId>
				<version>4.9</version>
				<scope>test</scope>
			</dependency>
			<dependency>
				<groupId>com.zdp.maven</groupId>
				<artifactId>Hello</artifactId>
				<version>0.0.1-SNAPSHOT</version>
				<scope>compile</scope>
			</dependency>
			<dependency>
				<groupId>com.zdp.maven</groupId>
				<artifactId>HelloFriend</artifactId>
				<version>0.0.1-SNAPSHOT</version>
				<scope>compile</scope>
			</dependency>
			<dependency>
				<groupId>com.zdp.maven</groupId>
				<artifactId>MakeFriends</artifactId>
				<version>0.0.1-SNAPSHOT</version>
				<scope>compile</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
</project>


第5步:改动web项目, 设置自己主动部署到tomcat以下

        <build>
		<finalName>web</finalName>
		<plugins>
			<plugin>
				<groupId>org.codehaus.cargo</groupId>
				<artifactId>cargo-maven2-plugin</artifactId>
				<version>1.2.3</version>
				<configuration>
					<container>
						<containerId>tomcat5x</containerId>
						<home>D:/tomcats/apache-tomcat-6.0.32_Demo</home>
					</container>
					<configuration>
						<type>existing</type>
						<home>D:/tomcats/apache-tomcat-6.0.32_Demo</home>
					</configuration>
				</configuration>
				<executions>
					<execution>
						<id>cargo-run</id>
						<phase>install</phase>
						<goals>
							<goal>run</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>



上一页 下一页

原文地址:http://www.manongjc.com/article/103412.html