Maven使用总结


本文记录一下日常使用maven的一些用法和一些问题解决方案,方便查找

本地仓库配置

在文件Maven安装路径\conf\settings.xml中配置标签,如下图所示
<

镜像源配置

默认仓库地址https://repo .maven.apache.org/maven2下载比较慢

全局配置

在文件Maven安装路径\conf\settings.xml配置使用的仓库地址,找到mirrors标签

  <mirrors>
    <!-- 阿里云中央仓库 -->
    <mirror>
      <id>nexus-aliyun</id>
      <mirrorOf>central</mirrorOf>
      <name>Nexus aliyun</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror>
  </mirrors>

如果想在特定的Maven项目中使用国内镜像源,可以在项目的pom.xml文件中进行设置

<repositories>
     <repository>
         <id>central</id>
         <name>Central Repository</name>
         <url>https://repo .maven.apache.org/maven2</url>
         <layout>default</layout>
         <snapshots>
         	<enabled>false</enabled>
         </snapshots>
     </repository>
 </repositories>

单个项目配置

在项目的根目录下的pom.xml中配置仓库

<repositories>
  <repository>
    <id>my-repo</id> <!-- 仓库的唯一标识符 -->
    <url>https://myrepository.example.com/maven2</url>
    <releases>
      <enabled>true</enabled> <!-- 是否启用发布版本 -->
    </releases>
    <snapshots>
      <enabled>true</enabled> <!-- 是否启用快照版本 -->
    </snapshots>
  </repository>
  <!-- 另一个仓库地址 -->
   <repository>
       <id>another-repo</id>
       <url>https://anotherrepository.example.com/maven2</url>
       <releases>
           <enabled>true</enabled>
       </releases>
       <snapshots>
           <enabled>false</enabled>
       </snapshots>
   </repository>
</repositories>

插件仓库配置

资源打包配置

构建Maven项目的时候,如果没有进行特殊的配置,Maven会按照标准的目录结构查找和处理各种类型文件。

<build>
 <resources>
  <resource>
      <!-- 设定主资源目录  -->
      <directory>src/main/java</directory>

      <!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,只处理如下配置中包含的资源类型 -->
      <includes>
          <include>**/*.xml</include>
      </includes>

      <!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,不处理如下配置中包含的资源类型(剔除下如下配置中包含的资源类型)-->
      <excludes>
          <exclude>**/*.yaml</exclude>
      </excludes>
      <!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,指定处理后的资源文件输出目录,默认是${build.outputDirectory}指定的目录-->
      <!--<targetPath>${build.outputDirectory}</targetPath> -->

      <!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,是否对主资源目录开启资源过滤 -->
      <filtering>true</filtering>
  </resource>
<!-- 资源目录  -->
  <resource>
      <directory>src/main/resources</directory>
      <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
          <include>**/*.html</include>
          <include>**/*.css</include>
      </includes>
  </resource>
 </resources>
 <testResources>
  <!--和resources一样-->
 </testResources>
</build>

插件使用

编译插件 maven-compiler-plugin

常见配置

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version> <!-- 指定插件版本 -->
            <configuration>
                <source>11</source> <!-- 指定源代码的 Java 版本 -->
                <target>11</target> <!-- 指定生成的字节码的 Java 版本 -->
                <compilerArgs>
                    <arg>-Xlint:deprecation</arg> <!-- 开启关于已弃用的 API 的警告 -->
                    <arg>-Xlint:unchecked</arg> <!-- 开启关于未检查操作的警告 -->
                </compilerArgs>
                <fork>true</fork> <!-- 在单独的进程中运行编译器 -->
                <compilerVersion>11</compilerVersion> <!-- 指定使用的编译器版本 -->
            </configuration>
        </plugin>
    </plugins>
</build>

maven-enforcer-plugin

https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-enforcer-plugin
用于 Maven 构建的工具,强制执行项目依赖和配置的某些规则和约束。其主要目标是确保项目遵循定义的标准和最佳实践,从而提高构建的可靠性和一致性。

主要功能

  1. 依赖管理:防止使用可能导致冲突或问题的特定依赖或版本。确保在构建中包含或排除特定依赖。
  2. 项目结构强制:检查项目结构和命名约定的一致性。验证项目是否遵循最佳实践。
  3. 版本控制:确保使用指定的插件或依赖版本,防止由于版本变化而导致的意外行为。
  4. 环境检查:验证构建环境是否符合特定标准,例如 Java 版本或 Maven 版本。
  5. 自定义规则:允许用户定义适合其项目特定需求的自定义规则。

常见配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <id>enforce-dependencies</id>
                    <goals>
                        <goal>enforce</goal>
                    </goals>
                    <configuration>
                        <rules>
                        <!-- 确保使用依赖的最高版本。如果项目中有多个版本的同一依赖,只会使用最高版本,避免版本冲突。 -->
                            <requireUpperBoundDeps />
                            <requireJavaVersion>
                                <version>[1.8,2.0)</version>
                            </requireJavaVersion>
                        </rules>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

同步修改多模块的版本号

使用场景:maven 多模块项目,更新parent版本号,子模块版本号没有更新,需要一个个的手动去更新,麻烦且容易出错

1.使用versions-maven-plugin插件
官网:https://www.mojohaus.org/versions/versions-maven-plugin/plugin-info.html

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>versions-maven-plugin</artifactId>
            <version>2.7</version>
            <configuration>
                <generateBackupPoms>false</generateBackupPoms>
            </configuration>
        </plugin>
    </plugins>
</build>
 

执行命令:mvn -N versions:update-child-modules

  1. 在父Maven项目testproject时,运行如下命令将更新全部项目的版本号,包括子项目之间的依赖也都同步更新:
    mvn versions:set -DnewVersion=2.0-SNAPSHOT
  2. 在子Maven项目时,运行如下命令将更新全部项目对该子项目引用的版本号:
    mvn versions:set -DnewVersion=2.1-SNAPSHOT
  3. 当更改版本号时有问题,可以通过以下命令进行版本号回滚:
    mvn versions:revert
  4. 如果一切都没有问题,那就直接提交版本号:
    mvn versions:commit

通过这个插件就可以对特定项目更新版本号时,其它与它有关联的项目都可以一并更新。

2.在父Maven项目定义一个属性叫做,然后每个项目的节点都引用这个变量${projectVersion},包括依赖上的都使用这个。那么当更新版本时手动修改一处地方即可。虽然解决了大量工作,但是小量的修改还是可以接受的。

3.使用gradle

impsort-maven-plugin 排序源代码中的import语句

项目地址:https://github.com/revelc/impsort-maven-plugin

参考文档:https://code.revelc.net/impsort-maven-plugin/

问题解决方案

1.cannot download sources

使用Idea点开源码时
在这里插入图片描述
点击Download Sources后,弹出警告框
在这里插入图片描述

执行命令:mvn dependency:resolve -Dclassifier=sources
此命令会下载项目所有的依赖的源码

2.Idea中Maven插件无法下载

在标签里配置的插件无法下载,其他依赖可以正常下载

解决办法:将插件的坐标作为依赖放到项目依赖配置里,即<dependencies>里,然后刷新项目,重新加载maven依赖,这时就可以发现下载下来了,然后再改回来

3. 执行命令控制台乱码

Maven Runner添加VM Option:-Dfile.encoding=GBK,具体编码视操作系统编码而定
在这里插入图片描述

  1. Maven执行报错:No plugin found for prefix ‘xxx’
    [ERROR] No plugin found for prefix ‘xxx’ in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/path/to/localRepository), nexus-aliyun (http://maven.aliyun.com/nexus/content/groups/public)] -> [Help 1] [ERROR]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值