鱼C论坛

 找回密码
 立即注册
查看: 424|回复: 9

[已解决]为什么我创建的这个maven项目不能导入依赖

[复制链接]
发表于 2024-3-28 13:25:52 | 显示全部楼层 |阅读模式
20鱼币
本帖最后由 孤世星辰 于 2024-3-28 13:27 编辑

捕1获.PNG
捕获.PNG
<?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>mybaits_study</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--    导入依赖-->
    <dependencies>
        <!--  mysql驱动      -->
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>

        <!--  mybatis      -->
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>
最佳答案
2024-3-28 13:25:53
修改settings.xml

最佳答案

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-3-28 13:25:53 | 显示全部楼层    本楼为最佳答案   
修改settings.xml

阿里云镜像更新了

阿里云镜像更新了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-3-28 13:26:01 | 显示全部楼层
在maven项目中导入依赖是非常常见的操作,但有时候会遇到导入失败的情况。根据你提供的信息,可能有几个原因导致导入失败。

首先,检查你的pom.xml文件中的依赖声明是否正确。确保groupId、artifactId和version正确匹配。在你的pom.xml文件中,mysql-connector-java的groupId应为mysql而非mysql-connector-java。请根据下面的代码示例进行修改:
<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.46</version>
    </dependency>

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.2</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

然后,尝试清理并重新构建项目。有时候,maven会遇到一些缓存或依赖关系错误的问题。可以尝试执行以下命令来清理本地maven仓库和重新构建项目:
mvn clean
mvn install

最后,检查你的网络连接是否正常。如果你的网络连接不稳定,可能会导致maven无法从远程仓库下载依赖。

希望这些方法可以帮助你解决问题。如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-3-28 15:08:10 | 显示全部楼层
鱼油创建的Maven项目遇到了无法导入依赖的问题,通常这可能是由几个不同的原因引起的:

  • 1. 依赖版本:确认您所指定的依赖在Maven中央仓库中确实存在,且版本号正确无误。
  • 2. Maven配置:确保Maven配置的 settings.xml 文件正确指向了一个有效的Maven仓库,并且该仓库包含您尝试导入的依赖。
  • 3. 网络问题:检查您的开发环境是否能够正常访问Maven中央仓库。如果您在公司或学校网络下,可能需要配置代理。
  • 4. IDEA缓存:有时IDE的缓存可能导致依赖解析问题。您可以尝试使IDE重新索引或刷新项目。
  • 5. Maven版本:在某些情况下,Maven的版本可能影响依赖解析。检查Maven是否为最新版本,或者尝试使用与项目兼容的Maven版本。


根据截图内容,可以尝试以下步骤进行问题排查和解决:

  • - 重新导入项目:在IDEA中重新导入项目,确保所有的Maven设置正确应用。
  • - 清理和构建:运行 mvn clean 和 mvn install 命令看是否能成功构建。
  • - 检查Maven配置:确保 settings.xml 文件中的仓库配置正确无误。
  • - 检查依赖是否存在:在浏览器中访问 https://mvnrepository.com/ ,确认您的依赖确实存在并且版本号无误。
  • - 清除本地仓库:有时本地Maven仓库中可能存在损坏的文件。尝试删除 ~/.m2/repository 下的相关依赖目录,然后重新构建项目。

仍遇到问题,可能需要查看IDEA的错误日志,或者在IDE的终端中运行Maven命令,以获取更详细的错误信息。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-3-28 18:55:08 | 显示全部楼层
本帖最后由 孤世星辰 于 2024-3-28 19:17 编辑
不二如是 发表于 2024-3-28 15:08
鱼油创建的Maven项目遇到了无法导入依赖的问题,通常这可能是由几个不同的原因引起的:


我cmd是可以运行maven命令的,所有的这个依赖也是在那个网站找的但是还是不知道哪里错了
是不是我这个镜像地址有问题,我看着视频敲得,然后我搜索这个地址进不去网站
老师你可以远程操控帮我看看么
<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>nexus-aliyun</id>
      <mirrorOf>central</mirrorOf>
      <name>Nexus-aliyun</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror>
  </mirrors>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-3-29 09:14:49 | 显示全部楼层
本帖最后由 眼前的都是浮云 于 2024-3-29 09:20 编辑

你的maven应该是访问不到国外,可以在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.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>
          -->
                <!--定义本地仓库路径 如果pom.xml文件报错,则切换本地仓库-->
                <localRepository>D:/maven/repository</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>nexus-aliyun</id>
                <mirrorOf>*</mirrorOf>
                <name>Nexus aliyun</name>
                <url>https://maven.aliyun.com/repository/public</url>
                 </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>
            -->
            <!-- 全局jdk配置,settings.xml -->  
            <profile>    
                <id>jdk18</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>

          <!-- activeProfiles
           | List of profiles that are active for all builds.
           |
          <activeProfiles>
            <activeProfile>alwaysActiveProfile</activeProfile>
            <activeProfile>anotherAlwaysActiveProfile</activeProfile>
          </activeProfiles>
          -->
        </settings>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-3-29 09:15:58 | 显示全部楼层
如果还是不行,你就把本地仓库里的文件都删了,重新引一遍依赖
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-3-29 13:56:51 | 显示全部楼层

谢谢大哥,就是地址的问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-3-30 21:47:17 | 显示全部楼层
孤世星辰 发表于 2024-3-29 13:56
谢谢大哥,就是地址的问题

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-3-30 22:01:51 | 显示全部楼层
支持……
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-21 20:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表