Maven与eclipse整合

jopen 10年前

一. 创建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选项
20140706233059546.png

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

</div> </div>
    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

</div> </div>
    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>  
</div> </div>
第7步:右击pom.xml选择run as 中的命令执行即可

二. 创建Web项目

第1步:创建maven web工程
2.png

第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>  
</div> </div>

第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"));        %>  
</div> </div>

第4步:模块聚合, 修改parent中 pom.xml

</div> </div>
    <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://blog.csdn.net/zdp072/article/details/37355993
</div> </div>