16.Maven

作者: 星野君 | 来源:发表于2022-04-30 09:29 被阅读0次

    一、下载安装
    Maven – Download Apache Maven

    下载完后添加环境变量


    image.png

    然后再去path下添加


    image.png

    配置完后在cmd命令输入命令查看安装是否成功

    maven -version
    

    修改本地仓库地址:
    config下settings.xml文件中

      <!-- 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>K:\Maven\apache-maven-3.8.5-bin\apache-maven-3.8.5\mvn_resp</localRepository>
    

    换源阿里镜像:

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

    二、常用命令

    1. conpile 编译
    2. clean 清理
    3. test 测试
    4. package 打包
    5. install 安装

    三、Idea配置maven

    1. idea中设置里搜索maven
    2. 设置idea使用本地安装的maven,并修改配置文件路径

    四、配置环境依赖
    坐标:Maven Repository: Search/Browse/Explore (mvnrepository.com)

    在pom.xml里加入

        <dependencies>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.27</version>
            </dependency>
        </dependencies>
    

    五、Maven高级

    1. 分模块开发
    • 创建新模块,在自己的模块里导入新模块的坐标
            <dependency>
                <groupId>com.ylf</groupId>
                <artifactId>maven_03_pojo</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
    
    • 新模块使用install命令创建jar包
    1. . 聚合
    • 创建一个maven模块设置打包类型为pom,设置当前聚合工程包含的子模块名称
    <?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>com.ylf</groupId>
        <artifactId>ssm_maven_parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>pom</packaging>
    
        <modules>
            <module>../maven_02_ssm</module>
            <module>../maven_03_pojo</module>
            <module>../maven_04_dao</module>
        </modules>
    
    </project>
    
    1. 继承
    • 继承可以把公共统一的放在parent模块中,子模块继承
        <parent>
            <groupId>org.example</groupId>
            <artifactId>maven_01_parent</artifactId>
            <version>1.0-SNAPSHOT</version>
            <relativePath>../maven_01_parent/pom.xml</relativePath>
        </parent>
    
    • 父模块使用dependencyManagement可以选的依赖
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>4.13</version>
                    <scope>test</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    

    子模块如果需要就不需要写版本号。。

    1. 属性
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
    
        <properties>
            <spring.version>5.3.18</spring.version>
        </properties>
    

    六、多环境开发

    <profiles>  
        <profile>  
            <!-- 开发 -->  
            <id>dev</id>  
            <activation>  
                <activeByDefault>true</activeByDefault>  
            </activation>  
            <properties>  
                <activatedProperties>dev</activatedProperties>  
            </properties>  
        </profile>  
        <profile>  
            <!-- 测试 -->  
            <id>test</id>  
            <properties>  
                <activatedProperties>test</activatedProperties>  
            </properties>  
        </profile>  
        <profile>  
            <!-- 准生产 -->  
            <id>pre</id>  
            <properties>  
                <activatedProperties>pre</activatedProperties>  
            </properties>  
        </profile>  
        <profile>  
            <!-- 生产 -->  
            <id>prod</id>  
            <properties>  
                <activatedProperties>prod</activatedProperties>  
            </properties>  
        </profile>  
    </profiles> 
    

    指令

    mvn clean package -P prod  
    mvn install -P pre  
    

    七、私服
    Maven私服搭建_maven私服

    相关文章

      网友评论

          本文标题:16.Maven

          本文链接:https://www.haomeiwen.com/subject/bbnisrtx.html