0
点赞
收藏
分享

微信扫一扫

java基础-day45-Maven

芒果六斤半 2021-09-29 阅读 99

一、引言


1.1 项目管理问题

1.1.1 繁琐
1.1.2 复杂
1.1.3 冗余

1.2 项目管理方案

二、介绍


三、Maven安装


3.1 下载Maven

http://us.mirrors.quenda.co/apache/maven/maven-3/3.5.4/binaries/

3.2 Maven安装

3.2.1 解压
`bin:含有mvn运行的脚本`
`boot:含有plexus-classworlds类加载器框架,Maven 使用该框架加载自己的类库。`
`conf:含有settings.xml配置文件`
`lib:含有Maven运行时所需要的java类库`
3.2.2 环境变量
`MAVEN_HOME = maven的安装目录`
`PATH = maven的安装目录下的bin目录`
3.2.3 测试
mvn -v

四、Maven配置


4.1 本地仓库

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0                http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
  <!-- 选择一个磁盘目录,作为本地仓库 -->
  <localRepository>D:\Program Files\maven\myrepository</localRepository>

4.2 JDK配置

<profiles>
    <!-- 在已有的profiles标签中添加profile标签 -->
    <profile>    
        <id>myjdk</id>    
        <activation>    
            <activeByDefault>true</activeByDefault>    
            <jdk>1.8</jdk>    
        </activation>    
        <properties>    
            <maven.compiler.source>1.8</maven.compiler.source>    
            <maven.compiler.target>1.8</maven.compiler.target>
            <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> 
        </properties>    
    </profile>
</profiles>
<!-- 让增加的 profile生效 -->
<activeProfiles>
    <activeProfile>myjdk</activeProfile>
</activeProfiles>

五、仓库


5.1 概念

5.2 仓库分类

仓库分类

5.3 本地仓库

5.4 远程仓库

5.4.1 中央仓库
5.4.2 公共仓库【重点
<!--setting.xml中添加如下配置-->
<mirrors>
    <mirror>
        <id>aliyun</id>  
        <!-- 中心仓库的 mirror(镜像) -->
        <mirrorOf>central</mirrorOf>    
        <name>Nexus aliyun</name>
        <!-- aliyun仓库地址 以后所有要指向中心仓库的请求,都会指向aliyun仓库-->
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>  
    </mirror>
</mirrors>
5.4.3 私服【了解】

六、Idea-Maven


6.1 在Idea中关联Maven

在全局设置中,关联Maven

6.2 创建Maven项目

6.2.1 新建项目
如下选项

6.2.2 指定项目名
设置项目名

6.2.3 项目位置
项目位置如下

6.2.4 项目结构
项目结构如下:

6.2.5 pom.xml文件
<project > :文件的根节点 .
<modelversion > : pom.xml 使用的对象模型版本
<groupId > :项目名称,一般写项目的域名
<artifactId > :模块名称,子项目名或模块名称
<version > :产品的版本号 .
<packaging > :打包类型,一般有 jar、war、pom 等
<name > :项目的显示名,常用于 Maven 生成的文档。
<description > :项目描述,常用于 Maven 生成的文档
<dependencies> :项目依赖构件配置,配置项目依赖构件的坐标
<build> :项目构建配置,配置编译、运行插件等。
例如:配置tomcat7插件和jdk1.8
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <port>8888</port>
        </configuration>
      </plugin>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
            <target>1.8</target>
            <source>1.8</source>
            <encoding>UTF-8</encoding>
          </configuration>
        </plugin>
    </plugins>
  </build>
在 pom.xml 文件中锁定 jar 包版本配置

<!-- 统一管理jar包版本 -->
<properties>
    <mybatis.version>3.4.5</mybatis.version>
</properties>

<!-- 锁定jar包版本(可省略) -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<!-- 项目依赖jar包 -->
<dependencies>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis.version}</version>
    </dependency>
</dependencies>
6.2.6 项目类型
<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.qf</groupId>
    <artifactId>test01</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- 打包方式,如果是java项目则用 jar,
         如果是web项目则用war -->
    <!--<packaging>war</packaging>-->
    <packaging>jar</packaging>
</project>

6.3 导入依赖jar

6.3.1 查找依赖
查找依赖坐标

6.3.2 导入依赖
在项目的pom.xml文件添加依赖

6.3.3 同步依赖
窗口右下角弹窗,刷新依赖,使新加的配置被maven加载

6.4 创建web项目

6.4.1 打包方式
web项目打包方式为:war

6.4.2 web依赖
<?xml version="1.0" encoding="UTF-8"?>
<project ...>
    ...
    <packaging>war</packaging>

    <!-- 导入JSP 和 Servlet 和 JSTL 依赖 -->
    <dependencies>
        <dependency>
            <!-- jstl 支持 -->
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <!-- servlet编译环境 -->
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <!-- jsp编译环境 -->
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>
6.4.3 webapp目录
新建如下目录和文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!-- 这是一个空白的web.xml文件模板 -->
</web-app>
6.4.4 定义Servlet和Jsp
照常定义即可

6.5 部署web项目

6.5.1 新增Tomcat
新增Tomcat

6.5.2 部署web项目
部署web项目

6.5.3 启动Tomcat
启动Tomcat

6.6 依赖生命周期

6.6.1 概念
6.6.2 使用方式
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
    <!-- 生命周期 -->
    <scope>compile</scope>
</dependency>
<dependency>
    <!-- servlet编译环境 -->
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <!-- 生命周期 -->
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <!-- 生命周期 -->
    <scope>test</scope>
</dependency>
6.6.3 生命周期详解
标识 周期
compile 缺省值,适用于所有阶段(测试运行,编译,运行,打包)
provided 类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet-api.jar;适用于(测试运行,编译)阶段
runtime 只在运行时使用,如 mysql的驱动jar,适用于(运行,测试运行)阶段
test 只在测试时使用,适用于(编译,测试运行)阶段,如 junit.jar
system Maven不会在仓库中查找对应依赖,在本地磁盘目录中查找;适用于(编译,测试运行,运行)阶段

七、Maven指令


7.1 命令行

打开 cmd,并定位到项目目录

执行maven指令

7.2 Maven面板

maven面板

八、私服


8.1 概念

8.2 架构

无私服 有私服

8.3 Nexus安装【了解】

8.3.1 下载
8.3.2 安装
nexus安装目录

8.4 启动【了解】

8.5 Nexus登录【了解】

登录Nexus才可以使用Nexus管理功能

8.6 仓库列表【了解】

仓库类型 描述
group 包含多个仓库,通过group库的地址可以从包含的多个仓库中查找构件
hosted 私服 服务器本地的仓库,其中存储诸多构件
proxy 代理仓库,其会关联一个远程仓库, 比如中央仓库,aliyun仓库,向该仓库查找构件时,如果没有会从其关联的仓库中下载
仓库名 描述
Releases 存放项目的稳定发布版本,一个模块做完后如果需要共享给他人,可以上传到私服的该库
Snapshots 对应不稳定的发布版本
3rd party 存放中央仓库没有的 ,如ojdbc.jar,可以上传到私服的该库中
仓库列表

8.7 Maven配置私服 【重点

8.7.1 仓库组
仓库组 注意:proxy的仓库排序在最后

8.7.2 Maven关联私服
<servers>
    <server> 
        <id>nexus-public</id> <!-- nexus的认证id -->
        <username>admin</username> <!--nexus中的用户名密码-->
        <password>admin123</password> 
    </server>
</servers>
<profiles>
    <profile> 
        <id>nexus</id> 
        <repositories> 
            <repository> 
                <id>nexus-public</id> <!--nexus认证id 【此处的repository的id要和 <server>的id保持一致】-->
                <!--name随便-->
                <name>Nexus Release Snapshot Repository</name> 
                <!--地址是nexus中仓库组对应的地址-->
                <url>http://localhost:8081/nexus/content/groups/public/</url>
                <releases><enabled>true</enabled></releases> 
                <snapshots><enabled>true</enabled></snapshots> 
            </repository>
        </repositories> 
        <pluginRepositories> <!--插件仓库地址,各节点的含义和上面是一样的-->
            <pluginRepository> 
                <id>nexus-public</id> <!--nexus认证id 【此处的repository的id要和 <server>的id保持一致】-->
                <!--地址是nexus中仓库组对应的地址-->
                <url>http://localhost:8081/nexus/content/groups/public/</url>
                <releases><enabled>true</enabled></releases> 
                <snapshots><enabled>true</enabled></snapshots> 
            </pluginRepository> 
        </pluginRepositories> 
    </profile>
</profiles>
<activeProfiles>
    <activeProfile>yourjdk</activeProfile>
    <!-- 使私服配置生效 -->
    <activeProfile>nexus</activeProfile>
</activeProfiles>

8.8 Maven项目部署到私服

    ...
    <dependencies>
        .....
    </dependencies>
    
    <!-- 在项目的pom.xml中 配置私服的仓库地址,可以将项目打jar包部署到私服 -->
    <distributionManagement>
        <repository>
            <id>nexus-public</id> <!-- nexus认证id -->
            <url>http://localhost:8081/nexus/content/repositories/releases</url>
        </repository>
        <snapshotRepository>
            <id>nexus-public</id> <!-- nexus认证id -->
            <url>http://localhost:8081/nexus/content/repositories/snapshots</url>
        </snapshotRepository>
    </distributionManagement>
</project>

九、Maven分模块开发


<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <groupId>org.apache.maven.plugins</groupId>
            <configuration>
                <!--<encoding-->
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>
举报

相关推荐

0 条评论