如何使用Maven建立Web項目

  1. 從Maven模板建立web項目,命令以下:
$ mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false -DarchetypeCatalog=local

示例:java

$ mvn archetype:generate -DgroupId=com.citrix -DartifactId=stapler-study -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false -DarchetypeCatalog=local

一個新的web項目stapler-study就建立成功,它的代碼目錄結構也自動建立完成。web

  1. 打開代碼目錄下的/src/main/webapp/WEB-INF/web.xml
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
</web-app>

Servlet 2.3太老了,能夠將其升級爲3.0apache

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="stapler-study" version="3.0">
  <display-name>Stapler Study</display-name>
</web-app>
  1. 在Eclipse IDE中導入項目 方法1 使用maven的eclipse插件生成eclipse的web項目配配置文件
mvn eclipse:eclipse -Dwtpversion=2.0

將項目導入IDE, File->Import...->General->Exist Projects into workspace 添加Maven支持,選中項目右鍵Configure->Convert to maven projectapi

方法2 直接做爲maven項目導入File->Import...->Maven->Exist Maven Projectsapp

  1. 更新POM
<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>
  <groupId>com.citrix</groupId>
  <artifactId>stapler-study</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>stapler-study Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>stapler-study</finalName>
  </build>
</project>

添加依賴包及支持的JAVA版本eclipse

<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>
  <groupId>com.citrix</groupId>
  <artifactId>stapler-study</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>stapler-study Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <properties>
        <jdk.version>1.8</jdk.version>
        <junit.version>4.11</junit.version>
  </properties>
  
  <dependencies>
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
	  <groupId>javax.servlet</groupId>
	  <artifactId>javax.servlet-api</artifactId>
	  <version>3.1.0</version>
	  <scope>provided</scope>
	</dependency>
    
  </dependencies>
  
  <build>
    <finalName>stapler-study</finalName>
    <plugins>
          <!-- Eclipse project -->
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                    <!-- Always download and attach dependencies source code -->
                <downloadSources>true</downloadSources>
                <downloadJavadocs>false</downloadJavadocs>
                <!-- Avoid type mvn eclipse:eclipse -Dwtpversion=2.0 -->
                <wtpversion>2.0</wtpversion>
            </configuration>
          </plugin>
            
          <!-- Set JDK Compiler Level -->
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>
          </plugin>
    </plugins>
  </build>
</project>
相關文章
相關標籤/搜索