Maven详细配置(完整笔记)

Maven

本质:项目管理工具。可以做到整体编译、测试,快速打包部署。

作用:

  • 项目构建,提供标准的构建方式
  • 依赖管理,避免版本冲突

生命周期:

mvn clean   # 清理编译项目
mvn compile # 编译项目
mvn test    # 测试项目
mvn package # 项目打包
mvn install # 安装到仓库中,会把当前包存放到本地Maven仓库中
mvn clean package -Dmaven.test.skip=true # 跳过测试打包

maven地址:https://maven.apache.org/

下载地址:https://maven.apache.org/download.cgi

仓库地址:https://mvnrepository.com/

创建maven工程

1. 配置环境变量

配置MAVEN_HOME

​ 变量名 - MAVEN_HOME

变量值 - maven的安装目录

(一般xxx_home指的是bin目录上一级,PATH指的是bin目录)

配置PATH

添加%MAVEN_HOME%\bin

配置完,进行验证,打开命令行窗口执行

mvn -v

2. 修改配置文件

maven下载完,配置文件settings.xml

<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 指定包下载的位置
  -->
  <!-- 本地仓库位置 -->
  <localRepository>D:\java\m2</localRepository>
  ...
  <!-- 远程仓库位置 -->
   <mirror>  
      <id>alimaven</id>  
      <mirrorOf>central</mirrorOf>  
      <name>aliyun maven</name>  
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
   </mirror>
   ...
   <!-- 配置基础的JDK版本 -->    
   <profile>
      <id>jdk-1.8</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>

3. 创建项目

可以使用idea开发工具构建,创建maven工程/web工程

image-20220606214712423

创建完,需要配置坐标pom.xml文件

核心概念

解读pop.xml

<!-- groupId 公司或组织id   -->
<groupId>com.alibaba.abc</groupId>
<!-- artifactId 项目名   -->
<artifactId>abc</artifactId>
<!-- version 版本号 例子:SNAPSHOT 快照版本 RELEASE 正式版本   -->
<version>1.1.0</version>

<!-- 打包方式:war(生产web工厂)/jar (Java工厂)/pom (该工程
是用来管理其他工程) -->
<packaging>jar</packaging>

<!-- Maven自定义属性  -->
<properties>
    # 构建源码中读取源码的字符集
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<!-- 配置依赖的具体信息  -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <!-- 依赖范围
              compile:主程序 有效 、测试程序 有效、参与打包
              test:主程序 无效 、测试程序 有效、不参与打包
              provided:主程序 有效 、测试程序 有效、不参与打包
              (比如:servlet-api.jar的包,远程服务tomcat默认是有这些包,本地打包就不再需要,避免冲突)
            -->
        <scope>compile</scope>
        <scope>test</scope>
        <scope>provided</scope>
        <!-- 一般只有<type>pom</type> 才有这个,
			这个只是1个子工程只有1个父工程,如果想引用其他父工程的包,可以使用import导入
 		-->
        <scope>import</scope>
        <!-- 除去一些包,解决包冲突 -->
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<!-- 对构建过程进行自己的定制 -->
<build>
    <!-- 打包后的名字 xxx.jar -->
    <finalName>打包后的名字</finalName>
    <!-- 构建过程所用到的插件 -->
    <plugins>
        <!-- 一般逆向工程,的依赖插件 -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <!-- 如父工程引用过,子工程不需要制定版本号 -->
            <version>1.1.0</version>
            <dependencies>
                <!-- 一般逆向工程,所需要的依赖包 -->
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-xxx</artifactId>
                    <version>1.1.0</version>
                </dependency>
            </dependencies>
            <!-- configuration 配置该插件 -->
            <configuration>
                <excludes>
                    <!-- 打包的时候,减少一些包,(配置处理器)生产环境没必要打包  (去掉自定义配置) -->
                    <exclude>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-configuration-processor</artifactId>
                    </exclude>
                </excludes>
                <!-- 制定JDK版本,也可以在maven的settings.xml配置 -->
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
             </configuration>
        </plugin>
    </plugins>
    
    <resources>
        <!-- 打包过程中,哪些不要 -->
        <resource>
            <!-- 表示当前目录开启的资源 -->
            <directory>src/main/resources</directory>
            <!-- 资源过滤器打开 -->
            <filtering>true</filtering>
            <!-- 全部的以properties为后缀的文件,进行过滤 -->
            <includes>
                <include>*.properties</include>
            </includes>
            <!-- 除掉zdy.properties文件,都进行过滤 -->
            <excludes>
                <exclude>zdy.properties</exclude>
            </excludes>
        </resource>
    </resources>
</build>

基本操作

maven手动配置jar包到本地仓库

mvn install:install-file -Dfile="本地的jar包文件 例 /root/xxx.jar" -DgroupId="一般是groupId的名字 例 com.alibaba.abc" -DartifactId="名字 例 abc" -Dversion=1.1.0 -Dpackaging=jar
# 对应
<dependency>
	<groupId>com.alibaba.abc</groupId>
	<artifactId>abc</artifactId>
	<version>1.1.0</version>
</dependency>    

坐标&pom.xml

常用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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.yan</groupId>
    <artifactId>mjob</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    
    <name>mjob</name>
    <description>聚合服务</description>
     <!-- 打包类型:war/jar/pom(代表当前项目是父工程,做聚合管理)  -->
    <packaging>pom</packaging>

    <!--版本号管理 -->
    <properties>
        <alibaba.version>2.2.6.RELEASE</alibaba.version>
    </properties>

    <!-- 引入子模块 -->
    <modules>
        <module>yan-commo</module>
        <module>yan-coupon</module>
    </modules>

    <!-- 依赖管理 这样子包继承父类,按需下载包,不需要不进行下载,减少包的体积-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${alibaba.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement></project>

子模块配置:

<?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>

    <!-- 通过父工程坐标找到父工程项目  -->
    <parent>
        <artifactId>mjob</artifactId>
        <groupId>cn.yan</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    
    <!-- 如果groupId、version与父工程一样,可以省略 -->
 	<groupId>cn.yan</groupId>
    <version>0.0.1-SNAPSHOT</version>
    
    <artifactId>yan-commo</artifactId>
    <name>yan-commo</name>
    <description>公共组件 - 每个微服务公共依赖</description>
    
    <!-- 打包类型:war/jar/pom(代表当前项目是父工程,做聚合管理)  -->
    <packaging>jar</packaging>
    
    <!-- 依赖包 -->
 	<dependencies>
    	<dependency>
            <!-- 父工程已有该包,子工程可省略version属性 -->
            <groupId>com.yan.bpm</groupId>
            <artifactId>bpm-common</artifactId>
            <version>1.0-SNAPSHOT</version>
            <!-- 依赖范围
              compile:主程序 有效 、测试程序 有效、参与打包
              test:主程序 无效 、测试程序 有效、不参与打包
              provided:主程序 有效 、测试程序 有效、不参与打包
              (比如:servlet-api.jar的包,远程服务tomcat默认是有这些包,本地打包就不再需要,避免冲突)
            -->
            <scope>compile</scope>
            <scope>test</scope>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 除去一些包 -->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
        
    <build>
        <!-- 打包后的名字 xxx.jar -->
        <finalName>打包后的名字</finalName>
    </build>
</project>

导入module

当我们导入进来的项目,idea开发工具没有识别Maven项目,一些代码提示没有,解决:

步骤1:快捷键 Ctrl + Alt + Shift + S  打开module模块
步骤2:点击 + 按钮,找到import module,进行重新导入
步骤3:点击apply,进行重新导入

打包部署

web项目

  1. 项目打包命令

    mvn clean package -Dmaven.test.skip=true # 跳过测试打包
    
  2. 上传包到服务器

  3. 在tomcat服务下启动程序

    /xx/tomcat/bin/startup.sh
    
  4. 然后可以访问地址,进行测试

jar项目

  1. 项目打包命令

  2. 上传包到服务器

  3. 在某个文件下运行jar

    nohup java -jar xxx.jar > xx.log 2>&1 &
    
  4. 然后可以访问地址,进行测试

一些包文件

# 当我们想用@Table(name="表名称")
<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>javax.persistence-api</artifactId>
    <version>2.2</version>
</dependency>
# 热部署
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <version>3.1.3</version>
  	<scope>runtime</scope>
    <optional>true</optional>
</dependency>

解决包冲突

  1. IDEA中的Maven helper插件

    安装完插件,在pom.xml文件中,找到Dependency Analyzer,找到Conflicts,这里显示包冲突;

  2. Maven 的enforcer插件

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>1.4.1</version>
        <executions>
            <execution>
                <id>enforce-dependencies</id>
                <phase>validate</phase>
                <goals>
                    <goal>display-info</goal>
                    <goal>enforce</goal>
                </goals>
            </execution>
        </executions>
        <dependencies>
            <dependency>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>extra-enforcer-rules</artifactId>
                <version>1.0-beta-4</version>
            </dependency>
        </dependencies>
        <configuration>
            <rules>
                <banDuplicateClasses>
                    <findAllDuplicates>true</findAllDuplicates>
                </banDuplicateClasses>
            </rules>
        </configuration>
    </plugin>
    

    配置完,需要执行,然后可以看到包冲突

    mvn clean package enforcer:enforce
    

搭建私服仓库

  1. 下载 nexus

    下载地址:https://help.sonatype.com/repomanager3/product-information/download

  2. 配置环境变量

    NEXUS_HOME配置:指向安装目录的地址

    image-20220626161206114

    然后在Path环境变量下,添加%NEXUS_HOME%\bin

  3. 常用命令

    # 默认端口 8081
    # 在 D:\xxx\nexus-3.20.1-01\etc 目录下 nexus-default.properties文件
    # 修改配置文件 - application-port=8081 更改默认端口
    
    # cmd 命令下进行启动
    (前台运行)启动命令:nexus /run
    (后台运行)安装:nexus.exe /install
    		  卸载:nexus.exe /uninstall
    		  启动:nexus.exe /start
    		  停止:nexus.exe /stop
    		  查看:nexus.exe /status
    
    

    启动成功,访问页面:http://localhost:8081,出现页面代表安装成功

  4. 配置登录密码

    在D:\xxx\sonatype-work\nexus3目录下,存放第一次的默认密码
    记住密码,如果修改密码后,该文件会被删除
    然后,我们在登陆页面,输入用户名(admin)和密码,登录成功会引导创建新的密码
    然后,Configure Anonymous Access 下选择Enable anonymous access (允许匿名下载包)
    
    image-20220626164755807
  5. 配置管理

    小齿轮图标:进行配置,允许匿名授权,如果不允许,需要创建对应的用户账户,分配权限管理。
    image-20220626173850818

  6. nexus与阿里云仓库地址进行配置

    # 我们搭建架构:本地仓库 - 远程私服仓库 - 阿里云远程仓库 - 国外远程仓库
    
    image-20220626174709460

    Create repository : 进行创建自定义的仓库地址

    其中:

    maven-central:中央仓库地址

    maven-public:做聚合的

    maven-releases:是释放版本

    maven-snapshots:是开发版本

    区别:
    如果version是SNAPSHOT,执行maven的deploy,会打包进入到maven-snapshots下
    如果version是RELEASES,执行maven的deploy,会打包进入到maven-releases下
    <!-- 依赖坐标 -->
    <groupId>cn.yan</groupId>
    <artifactId>mjob</artifactId>
    <version>1.0-SNAPSHOT</version>
    <version>1.0-RELEASES</version>
    maven-public:
    	是做聚合的,http://localhost:8081/repository/maven-public,访问这个,就代表会访问maven-central、maven-releases、maven-snapshots地址,不需要再切换其他的地址。
    

    图2. 进行配置地址:

    默认地址: https://repo1.maven.org/maven2

    更改地址: http://maven.aliyun.com/nexus/content/groups/public/

    image-20220626175251475
  7. 本地配置maven

    # 本地的maven的配置xml ,该位置需要配置maven-public,可以在该页面直接点击maven-public的copy得到url
    <mirror>  
         <id>nexus</id>  
         <mirrorOf>*</mirrorOf>  
         <url>http://localhost:8081/repository/maven-public/</url>  
    </mirror>
    
    # 插件仓库配置,修改pom.xml
    <profile>  
        <pluginRepositories>
            <!-- 插件仓库,maven的运行依赖插件,也需要从私服下载插件 -->
            <pluginRepository>
                <!-- 唯一id -->
                <id>nexus</id>           
                <url>http://localhost:8081/repository/maven-public/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
    </profile>  
    
  8. 下载

    # 前提条件 nexus 目录需要有4G以上内存
    # 然后 我们本地引入包
     <dependency>
         <groupId>redis.clients</groupId>
         <artifactId>jedis</artifactId>
         <version>4.2.3</version>
     </dependency>
    # 然后在私有仓库上进行查看,所下载的包
    
    image-20220626195134988
  9. 上传

    # 本地的maven的配置xml ,如果不是匿名访问,也需要配置这个
    <!-- 上传私服 -->
    <servers>
        <server>
            <id>nexus</id> # 与本地私服的名字保持一致
            <privateKey>admin</privateKey> # 用户名
            <passphrase>密码</passphrase>
        </server>
    </servers>
    
    # 传仓库地址,上传2个仓库地址,Releases、Snapshots 修改pom.xml
    <distributionManagement>
        <repository>
            <id>nexus</id>
            <name>Releases</name>
            <url>http://localhost:8081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus</id>
            <name>Snapshots</name>
            <url>http://localhost:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
    
    

配置即可下载又可以上传

nexus nexus http://localhost:8081/repository/maven-snapshots/ true true

然后执行deploy,进行打包,上传。

```shell
命令是:mvn deploy
image-20220626223309991

在私有仓库中可以查看到,自己上传的包,自己定义的版本号。

image-20220626223653405

如果在Linux上安装:

下载xxx.tar.gz安装包,上传到服务器中,进行解压

tar -zxvf xxx.tar.gz

解压完成,进行改目录,到/bin目录下

cd /xx/xxx/bin

进行启动,在bin目录下执行,需要关闭防火墙

# 启动
./nexus start

# 查看状态
./nexus status

# 查看启动端口号,进行页面访问,查看地址,进行访问登录,默认是8081端口
netstat -anp | grep java

进行页面访问,得到登录密码

cd /opt/sonatype-work/nexus3/
# 查看初始密码
cat admin.password
# 得到密码,之后进行页面修改密码
  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值