尚硅谷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文件中进行配置

原文地址:https://www.cnblogs.com/kebibuluan/p/8982745.html