Java Web開發入門 - 第4章 Maven實戰

#Maven Maven官網
Maven下載 Maven安裝配置 #Maven安裝 ##Windows安裝 ###配置Javahtml

  • 下載JRE或者JDK
  • 配置JAVA_HOME環境變量到JRE或者JDK的根目錄 ###下載並解壓Maven
  • Maven官網下載壓縮包,解壓縮
  • 配置M2_HOME環境變量 ##Ubuntu安裝
sudo apt-get install maven

##CentOS安裝java

yum install maven

##Mac OS X安裝web

brew insall maven3

##二進制安裝 #Maven實戰 ##Maven核心概念apache

##人肉式api

  • 手動編譯
  • 手動打war包
  • 手動複製war包運行

##IDE ###eclipsetomcat

  • 下載依賴包:去哪裏下載
  • 編寫源代碼、配置文件 目錄結構怎麼定
  • 編譯、測試、打包、發行 構建流程不統一
  • 項目代碼共享以及依賴
  • 各類依賴的版本管理
  • 遠程部署問題 ##Maven是什麼
  • Apache基金會出品
  • 開源
  • Java編寫 一般把Maven理解成項目構建和依賴管理工具

##爲何是Maven架構

  • 約定(慣例)優先原則
  • 三方依賴管理 - 提供遠程倉庫,解決了依賴維護
  • 提供一致的項目構建管理方式 - 減小構建工做量
  • 插件式架構,大量的可重用插件
  • 方便集成IDE
  • 開源項目使用Maven

##Maven安裝app

##pom.xmleclipse

  • groupId 組織
  • artifactId 項目
  • version 版本

上面三個稱爲Maven項目的惟一標示curl

  • packaging 打包類型
  • dependencies 依賴項目
<?xml version="1.0" encoding="UTF-8"?>
<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 組織,使用java包式描述 -->
    <groupId>groupId</groupId>
<!-- artifactId 項目標識符(不要使用標點) -->
    <artifactId>TomcatStudy</artifactId>
<!-- 版本號SNAPSHOT會替換時間戳快照,發佈 -->
    <version>1.0-SNAPSHOT</version>
<!-- 打包類型war或者jar -->
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
<!-- 存在編譯須要而發佈不須要的jar包,能夠用scope標籤,值設爲provided -->
<scope>provided</scope>
        </dependency>
    </dependencies>
</project>

##Maven基本命令

  • mvn archetype:generate - 使用模板生成項目
  • mvn compile 編譯源代碼
  • mvn test 跑單元測試
  • mvn package 打包應用
  • mvn deploy 部署
  • mvn site生成項目相關的文檔站點
  • mvn clean 清理
  • mvn install 把包安裝到本地倉庫

##Maven Tomcat插件

  • Tomcat官方提供的一個Maven插件,用於方便應用和tomcat的開發運行調試
  • 詳細幫助
    • mvn help:describe -Dplugin=tomcat7
  • 常見命令
    • mvn tomcat7:run 啓動一個嵌入式的tomcat實例(在maven中)
    • mvn tomcat:deploy
  • *mvn tomcat:undeploy

#Maven Tomcat實踐演示 肯定maven工做正常

mvn

##Maven生成webapp項目 經過模板建立應用。在肯定的項目目錄內執行一下命令,建立規範的項目目錄樹:
命令以下:

mvn archetype:generate -DgroupId=com.netease.restaurant -DartifactId=Restaurant -Dpackage=com.netease -Dversion=1.0.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-webapp

打印以下,程序須要向咱們進行確認:

zhanpeng@GE70:~/Workspace/maven/restaurant$ mvn archetype:generate -DgroupId=com.netease.restaurant -DartifactId=Restaurant -Dpackage=com.netease -Dversion=1.0.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-webapp
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> maven-archetype-plugin:2.4:generate (default-cli) @ standalone-pom >>>
[INFO] 
[INFO] <<< maven-archetype-plugin:2.4:generate (default-cli) @ standalone-pom <<<
[INFO] 
[INFO] --- maven-archetype-plugin:2.4:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] Using property: groupId = com.netease.restaurant
[INFO] Using property: artifactId = Restaurant
[INFO] Using property: version = 1.0.0-SNAPSHOT
[INFO] Using property: package = com.netease
Confirm properties configuration:
groupId: com.netease.restaurant
artifactId: Restaurant
version: 1.0.0-SNAPSHOT
package: com.netease
 Y: :

輸入y進行確認,確認以後咱們查看目錄結構。
咱們能夠看到以下目錄樹:

zhanpeng@GE70:~/Workspace/maven/restaurant$ tree
.
└── Restaurant
    ├── pom.xml
    └── src
        └── main
            ├── resources
            └── webapp
                ├── index.jsp
                └── WEB-INF
                    └── web.xml

6 directories, 3 files

補充創建目錄並把以前項目的文件放置到正確位置:

zhanpeng@GE70:~/Workspace/maven/restaurant$ tree
.
└── Restaurant
    ├── pom.xml
    └── src
        ├── main
        │   ├── java
        │   │   └── com
        │   │       └── netease
        │   │           ├── HelloServlet.java
        │   │           └── NoodlesServlet.java
        │   ├── resources
        │   └── webapp
        │       ├── index.jsp
        │       ├── SoybeanMilk.html
        │       └── WEB-INF
        │           ├── web.xml
        └── test
            └── java

11 directories, 6 files

源代碼參考以前代碼 ##Maven添加依賴 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>
  <groupId>com.netease.restaurant</groupId>
  <artifactId>Restaurant</artifactId>
  <packaging>war</packaging>
  <version>1.0.0-SNAPSHOT</version>
  <name>Restaurant 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>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>Restaurant</finalName>
  </build>
</project>

##Maven添加插件

<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">
  ...
  <dependencies>
  ...
  </dependencies>
  <build>
    <finalName>Restaurant</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <uriEncoding>UTF-8</uriEncoding>
          <finalName>Restaurant</finalName>
          <server>tomcat</server>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

##maven-tomcat-plugin運行

mvn tomcat7:run

**注意:**課程給的例子是發生錯誤,因爲serlvet包並無添加<scope>provided</scope>,注意以下內容的修改:

<dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>

運行測試

zhanpeng@GE70:~$ curl http://localhost:8080/Restaurant/noodles
<html><body>
<h1> Noodles with Tomato</h1>
</html></body>

#Maven管理多模塊Web項目實戰 以前的配置是單個項目,可是在後面的項目內容,更多的是分模塊進行項目內容開發。這就涉及到多模塊管理與依賴關係描述。多個項目相互依賴很是複雜,maven幫助咱們管理多模塊內容。 ##建立子模塊

mvn archetype:generate -DgroupId=com.netease.restaurant -DartifactId=Kitchen -Dpackage=com.netease -Dversion=1.0.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-quickstart

咱們能夠看到目錄樹變動爲:

zhanpeng@GE70:~/Workspace/maven/restaurant$ tree
.
├── Kitchen
│   ├── pom.xml
│   └── src
│       ├── main
│       │   └── java
│       │       └── com
│       │           └── netease
│       │               └── App.java
│       └── test
│           └── java
│               └── com
│                   └── netease
│                       └── AppTest.java
└── Restaurant
    ├── pom.xml
    ├── Restaurant.iml
    ├── src
    │   ├── main
    │   │   ├── java
    │   │   │   └── com
    │   │   │       └── netease
    │   │   │           ├── HelloServlet.java
    │   │   │           └── NoodlesServlet.java
    │   │   ├── resources
    │   │   └── webapp
    │   │       ├── index.jsp
    │   │       ├── SoybeanMilk.html
    │   │       └── WEB-INF
    │   │           ├── web.xml
    │   │           └── web.xml~
    │   └── test
    │       └── java

修改子模塊代碼

##管理兩個子項目的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>
  <groupId>com.netease.restaurant</groupId>
  <artifactId>restarant-parent</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>pom</packaging> <!-- 管理子項目 -->
  <name>Multi modules demo</name>
  
  <modules>
    <module>Restaurant</module>
    <module>Kitchen</module>
  </modules>
</project>

備註:正確添加pom.xml文件以後,就可使用idea打開多模塊項目 ##修改子模塊的pom.xml配置 Kitchen項目的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>
  <groupId>com.netease.restaurant</groupId>
  <artifactId>Kitchen</artifactId>
  <packaging>jar</packaging>
  <version>1.0.0-SNAPSHOT</version>
  <name>Kitchen</name>
  <url>http://maven.apache.org</url>

  <!-- 添加總管理依賴 -->
  <parent>
    <groupId>com.netease.restaurant</groupId>
    <artifactId>restarant-parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Restaurant項目的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>
  <groupId>com.netease.restaurant</groupId>
  <artifactId>Restaurant</artifactId>
  <packaging>war</packaging>
  <version>1.0.0-SNAPSHOT</version>
  <name>Restaurant Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <!-- 添加總管理依賴 -->
  <parent>
    <groupId>com.netease.restaurant</groupId>
    <artifactId>restarant-parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>

  <dependencies>
    <!-- 依賴Kitchen -->
    <dependency>
      <groupId>com.netease.restaurant</groupId>
      <artifactId>Kitchen</artifactId>
      <version>1.0.0-SNAPSHOT</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</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>Restaurant</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <uriEncoding>UTF-8</uriEncoding>
          <finalName>Restaurant</finalName>
          <server>tomcat</server>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

注意:idea開啓自動導入包,否則子項目包沒法載入
##把包安裝到本地倉庫

mvn install

到web子項目啓動運行:

mvn tomcat7:run

訪問測試:

zhanpeng@GE70:~$ curl http://localhost:8080/Restaurant/hello
hello world
Hello Kitchen

肯定子項目被加載進來 #eclipse方式使用maven ##eclipse安裝maven插件 工具欄:help->Install New Software
eclipse neon沒法安裝插件 ##eclipse導入項目 選擇項目
輸入圖片說明
確認導入項目信息
輸入圖片說明
項目導入成功
輸入圖片說明
##eclipse使用maven進行運行 須要執行的Web項目上右鍵->Run As->Run Configurations
雙擊Maven Build建立運行內容
輸入圖片說明
注意:Base Directory填寫${workspace_loc:/Restaurant},Goals填寫tomcat7:run
輸入圖片說明
點擊運行,並確認運行結果

zhanpeng@GE70:~$ curl http://localhost:8080/Restaurant/hello
hello world
Hello Kitchen

#Maven FAQ Q: 我執行tomcat7插件相關的操做時,怎麼一直報錯,提示找不到tomcat7插件?
A: 這個是由於tomcat7插件的groupId不在maven默認的pluginGroups中,解決方法有一下幾種:

  1. 在一個配置了tomcat7插件的項目目錄下執行,由於配置了tomcat7插件,全部maven就知道了其groupId,因此就可以找到了
  2. 使用帶groupId的全限定名,例子以下: mvn help:describe -Dplugin=org.apache.tomcat.maven:tomcat7-maven-plugin:2.2 因爲在指定plugin的時候,寫了完整的groupId,因此這樣也是能夠的
  3. 修改一下mvn的配置文件settings.xml, 在<pluginGroups> 這個配置項下面,增長一個<pluginGroup>配置便可 <pluginGroups> <pluginGroup>org.apache.tomcat.maven</pluginGroup> </pluginGroups>

想了解更多爲何會這樣,請參考 http://maven.apache.org/guides/introduction/introduction-to-plugin-prefix-mapping.html

Q: 我在實戰maven管理多項目的時候,執行mvn tomcat7:run 報了錯誤,錯誤信息是「The POM for com.netease.restaurant:Kitchen:jar:1.0.0-SNAPSHOT is missing, no dependency information available」,怎麼辦?

A: 在多項目管理實戰中的這個例子中,Restaurant對Kitchen有依賴,若是你直接在Restaurant這個項目中執行 mvn tomcat7:run的話,因爲此時Kitchen尚未放到本地倉庫中,所以Restaurant找不到這個Kitchen的依賴,因此報錯了。解決方案以下: 1.能夠在Kitchen這個項目中執行 mvn install,此時會把這個Kitchen項目安裝maven的本地倉庫,而後再在Restaurant項目中引用,就能找到了 2.直接在頂層的 restaurant-parent 這個項目下先執行 mvn install,這樣的話,頂層的這個父項目會把全部的子項目都安裝到本地倉庫,並維護好依賴關係

相關文章
相關標籤/搜索