尚硅谷maven視頻教程筆記


07.尚硅谷_Maven_部署Maven核心程序.avi

第一步先安裝jdk

第二步下載maven

特別需要注意的是maven不能存儲在有中文和空格的目錄下面

3.調試是否安裝成功,在cmd中輸入 mvn -version

08.尚硅谷_Maven_約定的目錄結構說明.avi

 

 

3.第一個Maven工程
①目錄結構
Hello
|---src
|---|---main
|---|---|---java
|---|---|---resources
|---|---test
|---|---|---java
|---|---|---resources
|---pom.xml

②POM文件內容
<?xml version="1.0" ?>
<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.atguigu.maven</groupId>
<artifactId>Hello</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>Hello</name>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
③編寫主程序代碼
在src/main/java/com/atguigu/maven目錄下新建文件Hello.java,內容如下
package com.atguigu.maven;
public class Hello {
public String sayHello(String name){
return "Hello "+name+"!";
}
}
④編寫測試代碼
在/src/test/java/com/atguigu/maven目錄下新建測試文件HelloTest.java
package com.atguigu.maven;
import org.junit.Test;
import static junit.framework.Assert.*;
public class HelloTest {
@Test
public void testHello(){
Hello hello = new Hello();
String results = hello.sayHello("litingwei");
assertEquals("Hello litingwei!",results);
}
}
⑤運行幾個基本的Maven命令
mvn compile 編譯
mvn clean 清理
mvn test 測試
mvn package 打包
※注意:運行Maven命令時一定要進入pom.xml文件所在的目錄!

目錄如下

 

我們進入到pom.xml所在的目錄執行上面的命令

 

 當前例如是adminstrator用戶

maven默認的本地倉庫的位置是:

在d盤的D:\RepMaven文件夾中我們下載好了maven所需的jar包,我們可以修改maven本地倉庫的地址執行該目錄

我們找到maven解壓目錄下的conf文件夾下面的setting.xml修改如下

 

 

 

 

Maven是當前流行的項目管理工具,但官方的庫在國外經常連不上,連上也下載速度很慢。國內oschina的maven服務器很早之前就關了。今天發現阿里雲的一個中央倉庫,親測可用。

修改settings.xml文件,在<mirrors>標簽下加入上述內容即可

<mirror>
     <id>alimaven</id>
     <mirrorOf>central</mirrorOf>
     <name>aliyun maven</name>
     <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
 </mirror>

 

 

12.尚硅谷_Maven_POM.avi

接下來我們來學習maven的依賴,我們創建一個maven工程,依賴我們的上面第一個Hello這個工程,要依賴Hello工程,我們要使用maven install命令,將Hello這個工程添加到本次倉庫中

4.第二個Maven工程
①工程名:HelloFriend
②目錄結構與第一個Maven工程相同
③POM文件

<?xml version="1.0" ?>
<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.atguigu.maven</groupId>
<artifactId>HelloFriend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>HelloFriend</name>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>    

<dependency>
<groupId>com.atguigu.maven</groupId>
<artifactId>Hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>

</dependencies>
</project>

 


④主程序:在src/main/java/com/atguigu/maven目錄下新建文件HelloFriend.java
package com.atguigu.maven;
import com.atguigu.maven.Hello;
public class HelloFriend {
public String sayHelloToFriend(String name){
Hello hello = new Hello();
String str = hello.sayHello(name)+" I am "+this.getMyName();
System.out.println(str);
return str;
}
public String getMyName(){
return "John";
}
}
⑤測試程序:在/src/test/java/com/atguigu/maven目錄下新建測試文件HelloFriendTest.java
package com.atguigu.maven;
import static junit.framework.Assert.assertEquals;
import org.junit.Test;
import com.atguigu.maven.Hello;

public class HelloFriendTest {
@Test
public void testHelloFriend(){
HelloFriend helloFriend = new HelloFriend();
String results = helloFriend.sayHelloToFriend("litingwei");
assertEquals("Hello litingwei! I am John",results);
}
}

在上面的HelloFriend中

<scope>test</scope> 依賴的junit對應的范圍是測試程序才有效果,對主程序沒有效果,test表示測試范圍內的依賴

<scope>compile</scope>依賴的Hello jar包在主程序中可以使用,在測試test模塊中也可以使用,都可以直接new Hello對象,complie表示測試范圍和主程序都要有效

 

 

例如如果使用compile依賴spring的jar,該spring的jar就會被打包在應用中,並且因為tomcat容器沒有提供該spring的jar包,就會被部署到容器中,compile指定的jar包在主程序和測試程序都有效,會隨項目發布

例如如果使用test依賴junit的jar,compile指定的jar包只能在測試程序中有效,不會部署不會打包在應用中,只在測試時使用,用於編譯和運行測試代碼。不會隨項目發布。 

對應privodie修飾的jar包,在主程序和測試程序都有效,privide默認tomcat容器會提供該jar包,所以不會被打包也不會被部署到應用服務器中,類似compile,期望JDK、容器或使用者會提供這個依賴。如servlet.jar。 

 

接下來,我們講解如何在myeclipse上面新建使用maven建立web工程

第一步

將第一個選項enable Maven4MyEclipse 這個選擇上

第二步新建立一個web工程

這里把add maven sporrt勾選上。如果沒有第一步的操作,這里就沒有add maven support這個選項

接下來填寫你的maven坐標。勾選stander這個選項

這里上默認不勾選

我們來看看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.weiyuan.maven</groupId>
  <artifactId>Hello</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>MavenWeb</name>
  <description/>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <version>3.0</version>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

 

在上面創建的文件夾中index.jsp有紅叉,是因為缺少sevlet這個依賴

我們在pom.xml中添加servlet的依賴

<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.weiyuan.maven</groupId>
  <artifactId>Hello</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>MavenWeb</name>
  <description/>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <version>3.0</version>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
 <dependencies>
   <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
 </dependencies>
</project>
這樣index.jsp就不會出現錯誤了,千萬要注意scope是provide,如果寫成compile ,該jar就會部署到tomcat中,tomcat容易中也存在該servlet jar,就會存在jar的沖突
接下來還有兩個地方需要配置的

 

 

 上面install要選擇我們解壓在本地的maven按照目錄

user setting選擇我們本地maven解壓下的conf.xml

上面這兩個配置在myeclipse工作區間發生變化的時候都需要重新配置

接下面我們講解如何導入maven工程

 

選擇maven工程所在的目錄,導入就可以了

 接下來我們講解下maven的依賴性

我們在上面創建了兩個maven項目。hello 和hellofreind

hellofriend依賴hello,hello已經使用mvn install 安裝到了本地的倉庫中

下面在我們在hello中添加了一個spring的依賴

在hellofreiend中會自動加載hello中引入的依賴,這就是依賴的傳遞性,hello直接依賴spring ,hellofriend間接依賴spring

 

上面的hello就是所謂的最下面,但是主要非compile范圍的不能被傳遞

 

接下面我們講解下依賴的排除

上面hello中存在log的jar包,會自動的傳遞到HelloFriend中,但是如果helloFreien中不想需要這個log依賴如何處理了,在hellofriend的pom.xml中

接下來我們分析下maven依賴的原則

makefreind依賴hellofriend ,hellofrien依賴hello

hello中使用了log4j 1.2.17版本,hellofrien中使用了1.2.104版本,按照上面依賴的傳遞性,makefriend就會存在1.2.17和1.2.14版本,這里時候使用哪個版本了,安裝就近原則,makefriend依賴hellofreind比較近,相當於hellofriend是makefreind的爸爸,hello是她的也爺爺

隔爸爸比較近,就使用爸爸中的

接下面我們看第二個場景

makefreind有兩個爸爸

這個時候如何選擇了,這個時候就看那個先使用depency聲明就使用那個,在makefrien的pom,xml中那個先聲明就使用那個

 

 接下來我們講解proprert標簽在maven中的使用

 

我們在pom.xml中依賴了很多jar包,都是4.0.0版本的,我們需要在下次運行的時候統一升級成4.1.0版本,就可以使用propetiies

接下面我們講解下maven的繼承,要實現junit因為junt是test范圍的,每個項目使用都要自己配置,下面可以使用父工程來解決這個問題

 第一步先創建一個父親工程

這里是最關鍵的對於父親工程,對於的打包方式一定要選擇為pom方式,如果是java工程選擇jar,web工程選擇war

 

 這就是父親工程的目錄結構

接下來,在子工程中聲明對父親工程的引用

我們在hello子工程的pom.xml中引用付工程

<?xml version="1.0" ?>
<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>

    <artifactId>Hello</artifactId>

    <!-- 子工程中聲明父工程 -->
    <parent>

<groupId>com.weiyuan.test</groupId>
<artifactId>Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>


        
        <!-- 以當前文件為基准的父工程pom.xml文件的相對路徑 -->
        <relativePath>../Parent/pom.xml</relativePath>
    </parent>

    <properties>
        <atguigu.spring.version>4.1.1.RELEASE</atguigu.spring.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <name>Hello</name>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${atguigu.spring.version}</version>
            <scope>compile</scope>
        </dependency>
        <!-- <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> 
            <version>1.2.17</version> </dependency> -->

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${atguigu.spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${atguigu.spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${atguigu.spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${atguigu.spring.version}</version>
        </dependency>

    </dependencies>
</project>

 

 上面引用的是父工程的坐標,其中 <relativePath>../Parent/pom.xml</relativePath>表示一當前hello的pom.xml為坐標尋找父親工程的pom.xml對應的位置,首先后退到上一節目錄就是Hello目錄,parent目錄和Hello在同一級目錄

接下來在父工程中配置需要被管理的對象,例如junt

<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.weiyuan.test</groupId>
  <artifactId>Parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  
  <!-- 配置版本統一管理 -->
  <dependencyManagement>
   <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
   </dependencies>
  </dependencyManagement>
  <build/>
</project>
接下來在子工程中對應的junit配置刪除就可以了。

 

 

 接下來我們講講聚合的概念

上面有了繼承之后。我們使用make install 安裝的時候,都是先安裝父工程,然后再安裝子工程,能不能一鍵安裝所以的工程了,這就是聚合的概念,我們可以在父工程的pom.xml文件中進行配置

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM