天天看點

8.Eclipse中建立Maven Web項目



第一步:

建立maven web工程

注意下面一步:

8.Eclipse中建立Maven Web項目

第二步:

繼承parent

修改pom.xml檔案如下

<projectxmlns="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>

 <groupid>cn.toto.maven</groupid>

 <artifactid>web</artifactid>

 <packaging>war</packaging>

 <name>web maven webapp</name>

 <url>http://maven.apache.org</url>

<parent> 

   <groupid>cn.toto.maven</groupid>

<artifactid>parent</artifactid>

  <version>0.0.1-release</version>

   <relativepath>../parent/pom.xml</relativepath> 

 </parent>

 <dependencies>

   <dependency>

     <groupid>junit</groupid>

<artifactid>junit</artifactid>

   </dependency>

     <groupid>cn.toto.maven</groupid>

     <artifactid>makefriends</artifactid>

 </dependencies>

</project>

第三步:

建立測試jsp

<%@ page language="java"contenttype="text/html; charset=utf-8"

pageencoding="utf-8"%>

<%@ pageimport="cn.toto.maven.makefriends.*"%>

<%

makefriendsmakefriends=new makefriends();

   out.println(makefriends.makefriends("wanglipeng"));

%>

第四步:

自動部署到tomcat下面(web項目下的pom.xml中)

<build>

   <finalname>web</finalname>

   <plugins>

         <plugin>

             <groupid>org.codehaus.cargo</groupid>

             <artifactid>cargo-maven2-plugin</artifactid>

           <version>1.2.3</version>

           <configuration>

               <container>

                   <containerid>tomcat7x</containerid>

                     <!—下面是tomcat在電腦上的位置à

                   <home>d:/program files/apachesoftware foundation/tomcat 5.0</home>

               </container>

               <configuration>

                   <type>existing</type>

               </configuration>

           </configuration>

           <executions> 

                 <execution> 

                     <id>cargo-run</id> 

                     <phase>install</phase> 

                     <goals> 

                         <goal>run</goal> 

                     </goals> 

                 </execution> 

             </executions>

         </plugin>

     </plugins>

</build>

第五步:

子產品聚合

修改parent.pom

web子產品中的完整的pox.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>web</artifactid>

<packaging>war</packaging>

<name>web maven webapp</name>

  <groupid>cn.toto.maven</groupid>

    <artifactid>parent</artifactid>

      <version>0.0.1-snapshot</version>

<relativepath>../parent/pom.xml</relativepath> 

</parent>

<dependencies>

<dependency>

<groupid>junit</groupid>

</dependency>

<groupid>cn.toto.maven</groupid>

<artifactid>makefriends</artifactid>

</dependencies>

<finalname>web</finalname>

<plugins>

<plugin>

<groupid>org.codehaus.cargo</groupid>

<artifactid>cargo-maven2-plugin</artifactid>

<!—下面是tomcat在電腦上的位置à

                   <home>d:/program files/apache software foundation/tomcat 5.0</home>

<execution> 

                    <id>cargo-run</id> 

<phase>install</phase> 

<goals> 

<goal>run</goal> 

</goals> 

</execution> 

</executions>

</plugin>

</plugins>

注意:

parent中的pom.xml如下:

xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<version>0.0.1-snapshot</version>

<packaging>pom</packaging>

<name>parent</name>

<url>http://maven.apache.org</url>

<modules>

  <module>../hello</module> 

  <module>../hellofriend</module>   

  <module>../makefriends</module>

  <module>../web</module>

</modules>

<properties>

<project.build.sourceencoding>utf-8</project.build.sourceencoding>

</properties>

<dependencymanagement>

<version>4.9</version>

<scope>test</scope>

    <artifactid>hellofriend</artifactid>

<scope>compile</scope>

<artifactid>hello</artifactid>

</dependencymanagement>

<distributionmanagement>

<repository>

    <id>releases</id>

    <name>internal releases</name>

    <url>http://localhost:8080/nexus-2.1.2/content/repositories/releases/</url>

</repository>

<snapshotrepository>

    <id>snapshots</id>

    <name>internal snapshots</name>

    <url>http://localhost:8080/nexus-2.1.2/content/repositories/snapshots/</url>

</snapshotrepository>

</distributionmanagement>