1. 程式人生 > 實用技巧 >Eclipse使用Maven建立web專案(轉)

Eclipse使用Maven建立web專案(轉)

1.建立Maven專案:


點選“File”選單,或者通過工具欄的“New”建立Project,如下圖所示:



選擇Maven-》Maven Project,彈出嚮導對話方塊,如下圖所示:



選中Create a simple project……複選框,其它的設定不變,並點選Next,輸入maven專案必須的資訊(groupId,artifactid,version),如下圖所示:



這裡需要選擇Packaging,web專案一般選擇war即可。點選Finish,完成專案建立,下圖為建立的專案結構(專案名稱為:sample-webapp):


2.配置web專案:


右鍵點選sample-webapp專案,依次點選Properties-》Project Facets-》Convert to faceted form… ,如下圖所示:



勾選Dynamic Web Module,並選擇版本(這裡使用2.5,由於3.0版本需要tomcat7),如下圖所示:



點選OK,此時,專案結構樹增加了WebContent資料夾,如下圖所示:



將WebContent下的WEB-INF、META-INF資料夾copy到src/main/webapp目錄下,並且刪除WebContent目錄,完成之後,如下圖所示:



右鍵點選sample-webapp專案,依次點選Properties-》Deployment Assembly,如下圖所示:



此時,通過Remove按鈕刪除/src/test/java、/src/test/resources、/WebContent,並且通過Add新增webapp與maven依賴,如下圖所示:









首先Add-》Folder,選擇webapp目錄並Finish,再選擇Java Build Path Entries-》Maven Dependencies
完成之後,如下圖所示:


3.部署web專案:


新增tomcat server,並部署web專案,這裡不描述,與非maven專案基本相同。





















springmvc+mybatis使用到的一些jar包

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>cn.aslan</groupId>
  <artifactId>0319_webapp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
    <dependencies>  <!-- 遠端依賴庫 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- 1.日誌 -->
        <!--&lt;!&ndash; 實現slf4j介面並整合 &ndash;&gt;-->
        <!--<dependency>-->
        <!--<groupId>ch.qos.logback</groupId>-->
        <!--<artifactId>logback-classic</artifactId>-->
        <!--<version>1.1.1</version>-->
        <!--</dependency>-->
        <!--log4j2支援-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${org.apache.logging.log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${org.apache.logging.log4j.version}</version>
        </dependency>


        <!-- 2.資料庫 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
            <scope>runtime</scope>
        </dependency>
        <!--druid==>阿里巴巴資料庫連線池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${com.alibaba.druid.version}</version>
        </dependency>
        <!--<dependency>-->
        <!--<groupId>c3p0</groupId>-->
        <!--<artifactId>c3p0</artifactId>-->
        <!--<version>0.9.1.2</version>-->
        <!--</dependency>-->

        <!-- DAO: MyBatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${com.mybatis.mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${com.mybatis.mybatis_spring.version}</version>
        </dependency>

        <!-- 3.Servlet web -->
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!--json工具-->
        <!--<dependency>-->
        <!--<groupId>com.fasterxml.jackson.core</groupId>-->
        <!--<artifactId>jackson-databind</artifactId>-->
        <!--<version>2.5.4</version>-->
        <!--</dependency>-->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>${com.google.gson.version}</version>
        </dependency>
        <!--Servlet版本設定-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${javax.servlet.version}</version>
        </dependency>

        <!-- 4.Spring -->
        <!-- 1)Spring核心 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <!-- 2)Spring DAO層 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <!-- 3)Spring web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <!-- 4)Spring test -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <!-- redis客戶端:Jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>${redis.clients.version}</version>
        </dependency>
        <dependency>
            <groupId>com.dyuproject.protostuff</groupId>
            <artifactId>protostuff-core</artifactId>
            <version>${com.dyuproject.protostuff.version}</version>
        </dependency>
        <dependency>
            <groupId>com.dyuproject.protostuff</groupId>
            <artifactId>protostuff-runtime</artifactId>
            <version>${com.dyuproject.protostuff.version}</version>
        </dependency>

        <!-- Map工具類 -->
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.2</version>
        </dependency>

        <!--檔案上傳工具-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>

<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.0.0.GA</version>
</dependency>


<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.2.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-pool/commons-pool -->
<dependency>
    <groupId>commons-pool</groupId>
    <artifactId>commons-pool</artifactId>
    <version>1.3</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>4.3.2.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>4.3.0.Final</version>
</dependency>



    </dependencies>
    
    
    

    <!-- 配置可變版本號,也就是常說的版本管理 (Spring、SpringMvc、Mybatis、Gson、Druid) -->
    <!-- 要針對某個依賴進行升級的時候只需要更改下面對應的版本號 -->
    <!-- 在上面使用版本號的時候需要用固定格式,如: ${包名.version} -->
    <properties>
        <org.apache.logging.log4j.version>2.6.2</org.apache.logging.log4j.version>
        <mysql.version>5.1.37</mysql.version>
        <com.alibaba.druid.version>1.0.25</com.alibaba.druid.version>
        <com.mybatis.mybatis.version>3.4.1</com.mybatis.mybatis.version>
        <com.mybatis.mybatis_spring.version>1.3.0</com.mybatis.mybatis_spring.version>
        <com.google.gson.version>2.7</com.google.gson.version>
        <javax.servlet.version>3.1.0</javax.servlet.version>
        <org.springframework.version>4.3.2.RELEASE</org.springframework.version>
        <redis.clients.version>2.7.3</redis.clients.version>
        <com.dyuproject.protostuff.version>1.0.8</com.dyuproject.protostuff.version>
        <developer.organization><![CDATA[scengine]]></developer.organization>
    </properties>

    <!-- 構建專案的最終名稱 -->
    <build>
        <finalName>SSM_LOG</finalName>
    </build>
</project>

轉載於:https://my.oschina.net/aslanjia/blog/863487