Maven 3.8.6 快速入门使用

Maven 3.8.6

maven是什么

image-20220927160435281

maven下载

image-20220927160608016

maven解压

apache-maven-3.8.6-bin.zip 解压到 d:\maven目录

image-20220927161022383

maven conf/settings.xml 配置文件

原配置文件

<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user,
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in
 |                 ${maven.conf}/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.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>
  -->

  <!-- interactiveMode
   | This will determine whether maven prompts you when it needs input. If set to false,
   | maven will use a sensible default value, perhaps based on some other setting, for
   | the parameter in question.
   |
   | Default: true
  <interactiveMode>true</interactiveMode>
  -->

  <!-- offline
   | Determines whether maven should attempt to connect to the network when executing a build.
   | This will have an effect on artifact downloads, artifact deployment, and others.
   |
   | Default: false
  <offline>false</offline>
  -->

  <!-- pluginGroups
   | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
   | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
   | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
   |-->
  <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>

  <!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->
  <proxies>
    <!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    -->
  </proxies>

  <!-- servers
   | This is a list of authentication profiles, keyed by the server-id used within the system.
   | Authentication profiles can be used whenever maven must make a connection to a remote server.
   |-->
  <servers>
    <!-- server
     | Specifies the authentication information to use when connecting to a particular server, identified by
     | a unique name within the system (referred to by the 'id' attribute below).
     |
     | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
     |       used together.
     |
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    -->

    <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
  </servers>

  <!-- mirrors
   | This is a list of mirrors to be used in downloading artifacts from remote repositories.
   |
   | It works like this: a POM may declare a repository to use in resolving certain artifacts.
   | However, this repository may have problems with heavy traffic at times, so people have mirrored
   | it to several places.
   |
   | That repository definition will have a unique id, so we can create a mirror reference for that
   | repository, to be used as an alternate download site. The mirror site will be the preferred
   | server for that repository.
   |-->
  <mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
    <mirror>
      <id>maven-default-http-blocker</id>
      <mirrorOf>external:http:*</mirrorOf>
      <name>Pseudo repository to mirror external repositories initially using HTTP.</name>
      <url>http://0.0.0.0/</url>
      <blocked>true</blocked>
    </mirror>
  </mirrors>

  <!-- profiles
   | This is a list of profiles which can be activated in a variety of ways, and which can modify
   | the build process. Profiles provided in the settings.xml are intended to provide local machine-
   | specific paths and repository locations which allow the build to work in the local environment.
   |
   | For example, if you have an integration testing plugin - like cactus - that needs to know where
   | your Tomcat instance is installed, you can provide a variable here such that the variable is
   | dereferenced during the build process to configure the cactus plugin.
   |
   | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
   | section of this document (settings.xml) - will be discussed later. Another way essentially
   | relies on the detection of a system property, either matching a particular value for the property,
   | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
   | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
   | Finally, the list of active profiles can be specified directly from the command line.
   |
   | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
   |       repositories, plugin repositories, and free-form properties to be used as configuration
   |       variables for plugins in the POM.
   |
   |-->
  <profiles>
    <!-- profile
     | Specifies a set of introductions to the build process, to be activated using one or more of the
     | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
     | or the command line, profiles have to have an ID that is unique.
     |
     | An encouraged best practice for profile identification is to use a consistent naming convention
     | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
     | This will make it more intuitive to understand what the set of introduced profiles is attempting
     | to accomplish, particularly when you only have a list of profile id's for debug.
     |
     | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
    <profile>
      <id>jdk-1.4</id>

      <activation>
        <jdk>1.4</jdk>
      </activation>

      <repositories>
        <repository>
          <id>jdk14</id>
          <name>Repository for JDK 1.4 builds</name>
          <url>http://www.myhost.com/maven/jdk14</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>
    -->

    <!--
     | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
     | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
     | might hypothetically look like:
     |
     | ...
     | <plugin>
     |   <groupId>org.myco.myplugins</groupId>
     |   <artifactId>myplugin</artifactId>
     |
     |   <configuration>
     |     <tomcatLocation>${tomcatPath}</tomcatLocation>
     |   </configuration>
     | </plugin>
     | ...
     |
     | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
     |       anything, you could just leave off the <value/> inside the activation-property.
     |
    <profile>
      <id>env-dev</id>

      <activation>
        <property>
          <name>target-env</name>
          <value>dev</value>
        </property>
      </activation>

      <properties>
        <tomcatPath>/path/to/tomcat/instance</tomcatPath>
      </properties>
    </profile>
    -->
  </profiles>

  <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->
</settings>

修改后的配置文件settings.xml

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
    <!-- 配置本地库目录,会自动建立目录  -->
    <localRepository>d:/maven/repo</localRepository>

    <!-- 配置国内仓库镜像 -->
    <mirrors>
        <!-- 阿里镜像仓库 -->
        <mirror>
            <id>aliyunmaven</id>
            <mirrorOf>*</mirrorOf>
            <name>阿里云公共仓库</name>
            <url>https://maven.aliyun.com/repository/public</url>
        </mirror>
    </mirrors>

    <!-- 配置项目开发编译的jdk版本 -->
    <profiles>
        <profile>
            <id>jdk-17</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>17</jdk>
            </activation>
            <properties>
                <maven.compiler.source>17</maven.compiler.source>
                <maven.compiler.target>17</maven.compiler.target>
                <maven.compiler.compilerVersion>17</maven.compiler.compilerVersion>
            </properties>
        </profile>
    </profiles>
</settings>

配置环境变量

image-20220927161951872

检查环境变量

image-20220927161641866

idea开发工具配置maven

image-20220927162103297

image-20220927162154801

idea建立maven项目

xml文件的特点

  • 严格区分大小
  • 标签必须有结束
  • 一个xml文件只能有一个根元素
  • xml文件一般有一个规范约束文件,用于约束标签的位置的

1. 建立项目

image-20220927162314165

2. 打开https://mvnrepository.com/

image-20220927162533091

3. 搜索项目用的依赖包

image-20220927162611247

4. 打开项目的pom.xml文件

<!-- maven项目的根目录有一个pom.xml文件 -->

<!-- 此处是添加项目依赖包集合 -->
<dependencies>
    <!-- jdbc mysql 依赖包 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.30</version>
    </dependency>
    ...
</dependencies>

image-20220927162840688

5. 在项目结构中查看

image-20220927162921573

maven配置jdk

全局配置

conf/settings.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
  <!-- 配置本地库目录,会自动建立目录  -->
  <localRepository>d:/maven/repo</localRepository>
   
  <!-- 配置国内仓库镜像 -->
  <mirrors>
  	<!-- 阿里镜像仓库 -->
    <mirror>
      <id>aliyunmaven</id>
      <mirrorOf>*</mirrorOf>
      <name>阿里云公共仓库</name>
      <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
  </mirrors>

  <!-- 配置项目开发编译的jdk版本 -->
  <profiles>
	    <profile>
		<id>jdk-17</id>
		<activation>
		    <activeByDefault>true</activeByDefault>
		    <jdk>17</jdk>
		</activation>
		<properties>
		    <maven.compiler.source>17</maven.compiler.source>
		    <maven.compiler.target>17</maven.compiler.target>
		    <maven.compiler.compilerVersion>17</maven.compiler.compilerVersion>
		</properties>
	    </profile>
	</profiles>
</settings>

项目局部配置

pom.xml文件

<?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>org.example</groupId>
    <artifactId>untitled1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>8</java.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <finalName>${project.artifactId}</finalName>
        <testSourceDirectory>src/test/java</testSourceDirectory>
        <sourceDirectory>src/main/java</sourceDirectory>
        <!-- 处理无法加载资源配置文件 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.html</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.html</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>

        <plugins>
            <!-- maven compiler plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>

            <!-- maven war plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
            </plugin>
        </plugins>
    </build>
</project>

pom.xml简化配置,添加

<build>
    <plugins>
        <!-- maven compiler plugin 配置jdk 为1.8 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.10.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>utf-8</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>
deepseek从入门到精通[WARNING] The artifact mysql:mysql-connector-java:jar:8.0.33 has been relocated to com.mysql:mysql-connector-j:jar:8.0.33: MySQL Connector/J artifacts moved to reverse-DNS compliant Maven 2+ coordinates. [INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary for ruoyi 3.8.6: [INFO] [INFO] ruoyi .............................................. SUCCESS [ 0.066 s] [INFO] ruoyi-common ....................................... SUCCESS [ 4.218 s] [INFO] ruoyi-system ....................................... SUCCESS [ 3.606 s] [INFO] ruoyi-framework .................................... SUCCESS [ 1.699 s] [INFO] ruoyi-quartz ....................................... SUCCESS [ 1.151 s] [INFO] ruoyi-generator .................................... SUCCESS [ 1.239 s] [INFO] ruoyi-admin ........................................ FAILURE [ 0.115 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 12.243 s [INFO] Finished at: 2025-04-01T23:26:33+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal on project ruoyi-admin: Could not resolve dependencies for project com.ruoyi:ruoyi-admin:jar:3.8.6: Could not find artifact org.apache.commons.codec:commons-codec:jar:1.9 at specified path D:\2025\2025_毕业设计\软件\软件4\zhitan-pv-master\ruoyi-admin\src\main\resources\lib\commons-codec-1.9.jar -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException [ERROR] [ERROR] After correcting the problems, you can resume the build with the command [ERROR] mvn <args> -rf :ruoyi-admin
04-02
<think>好的,用户遇到了一个Maven构建失败的问题,需要我来帮忙分析解决。让我仔细看看错误信息。 首先,错误信息显示在构建ruoyi-admin模块时失败,原因是无法解析依赖项org.apache.commons.codec:commons-codec:jar:1.9。Maven提示在指定路径下找不到这个jar包,路径是D盘下的一个本地lib目录。这说明项目可能直接引用了本地的jar文件,而不是通过Maven仓库获取。 接下来,我需要考虑可能的原因。首先,检查pom.xml中是否有对这个依赖项的本地路径引用。如果有,这样的配置会导致Maven在构建时去指定路径查找,而如果路径不正确或者文件缺失,就会失败。另一个可能是该依赖项在Maven中央仓库中是否存在,但commons-codec 1.9其实是存在的,所以更可能问题出在本地引用上。 然后,解决方案的第一步应该是检查ruoyi-admin模块的pom.xml,查找是否有类似`<dependency>`里使用`<systemPath>`指向本地lib目录的情况。如果存在,应该将其替换为标准的Maven依赖坐标,让Maven从仓库自动下载。正确的依赖应该是: ```xml <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> ``` 同时,需要删除或注释掉原有的本地路径依赖部分。这样Maven就会从中央仓库获取该jar包,避免路径问题。 另外,用户提到的MySQL连接器版本被重定位的警告信息,虽然不影响构建失败,但也建议更新依赖为新的groupId:com.mysql,替换原来的mysql-connector-java为mysql-connector-j,版本保持8.0.33。这可能需要检查其他模块的pom文件是否有相关旧依赖,进行统一调整。 处理完这些后,执行mvn clean install重新构建项目,应该可以解决依赖问题。如果用户本地仓库没有该版本的commons-codec,Maven会自动下载。另外,确保网络连接正常,或者检查是否有公司内部仓库配置,可能需要调整settings.xml文件。 如果用户按照步骤操作后仍然有问题,可能需要进一步检查Maven的配置文件,或者确认本地lib目录下的jar是否存在,是否有权限问题。但最根本的解决方式还是改为使用中央仓库的依赖,而不是本地路径引用,这样更符合Maven的最佳实践,也便于项目的可移植性。</think>以下是对该Maven构建问题的逐步解析和解决方案: --- ### **问题分析** 从报错信息可以看出`ruoyi-admin`模块构建失败,主要原因是: ```log Could not find artifact org.apache.commons.codec:commons-codec:jar:1.9 at specified path D:\2025\...\lib\commons-codec-1.9.jar ``` 关键问题在于: 1. 项目尝试从本地路径直接加载依赖(`D:\2025\...\lib\commons-codec-1.9.jar`) 2. 该路径下未找到对应的JAR文件 3. 依赖声明方式不符合Maven规范(应通过中央仓库/私有仓库获取依赖) --- ### **解决方案** #### **步骤1:检查依赖声明** 1. 打开`ruoyi-admin`模块的`pom.xml` 2. 查找类似以下配置的依赖项: ```xml <dependency> <groupId>org.apache.commons.codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> <scope>system</scope> <systemPath>D:\2025\...\lib\commons-codec-1.9.jar</systemPath> </dependency> ``` #### **步骤2:修复依赖配置** 删除上述`<scope>system</scope>`和`<systemPath>`配置,改用标准Maven依赖: ```xml <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> ``` #### **步骤3:处理MySQL依赖警告(可选)** 修改所有`mysql-connector-java`依赖的`groupId`: ```xml <!-- 原始配置(已过时) --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> <!-- 修改后 --> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>8.0.33</version> </dependency> ``` #### **步骤4:清理并重新构建** ```bash # 清理旧构建 mvn clean # 重新构建项目 mvn install -DskipTests ``` --- ### **补充说明** 1. **依赖管理原则** Maven应通过`<dependencies>`自动从仓库下载依赖,**避免硬编码本地路径**。标准流程: $$ \text{Maven客户端} \rightarrow \text{本地仓库} \rightarrow \text{远程仓库} $$ 2. **依赖验证方法** 使用以下命令检查依赖树: ```bash mvn dependency:tree -Dincludes=commons-codec ``` 3. **常见问题排查** - 检查本地仓库路径(默认:`~/.m2/repository`) - 确认网络可访问Maven中央仓库(或企业私有仓库) - 检查`settings.xml`中镜像配置 --- ### **扩展建议** 1. **统一依赖版本管理** 在父POM中使用`<dependencyManagement>`统一管理版本号: ```xml <dependencyManagement> <dependencies> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.15</version> <!-- 建议升级到新版本 --> </dependency> </dependencies> </dependencyManagement> ``` 2. **使用阿里云镜像加速** 在`settings.xml`中配置镜像: ```xml <mirror> <id>aliyunmaven</id> <mirrorOf>*</mirrorOf> <name>阿里云公共仓库</name> <url>https://maven.aliyun.com/repository/public</url> </mirror> ``` --- 通过以上步骤,可修复依赖解析问题并优化构建流程。如果仍有问题,建议检查完整的依赖树或提供完整的`pom.xml`片段进一步分析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值