鱼C论坛

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

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

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

捕1获.PNG
捕获.PNG
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <modelVersion>4.0.0</modelVersion>

  6.     <!--    父工程-->
  7.     <groupId>org.example</groupId>
  8.     <artifactId>mybaits_study</artifactId>
  9.     <version>1.0-SNAPSHOT</version>

  10.     <!--    导入依赖-->
  11.     <dependencies>
  12.         <!--  mysql驱动      -->
  13.         <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  14.         <dependency>
  15.             <groupId>mysql</groupId>
  16.             <artifactId>mysql-connector-java</artifactId>
  17.             <version>5.1.46</version>
  18.         </dependency>

  19.         <!--  mybatis      -->
  20.         <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
  21.         <dependency>
  22.             <groupId>org.mybatis</groupId>
  23.             <artifactId>mybatis</artifactId>
  24.             <version>3.5.2</version>
  25.         </dependency>
  26.         <!-- https://mvnrepository.com/artifact/junit/junit -->
  27.         <dependency>
  28.             <groupId>junit</groupId>
  29.             <artifactId>junit</artifactId>
  30.             <version>4.12</version>
  31.             <scope>test</scope>
  32.         </dependency>
  33.     </dependencies>

  34.     <properties>
  35.         <maven.compiler.source>8</maven.compiler.source>
  36.         <maven.compiler.target>8</maven.compiler.target>
  37.     </properties>

  38. </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。请根据下面的代码示例进行修改:

  1. <dependencies>
  2.     <dependency>
  3.         <groupId>mysql</groupId>
  4.         <artifactId>mysql-connector-java</artifactId>
  5.         <version>5.1.46</version>
  6.     </dependency>

  7.     <dependency>
  8.         <groupId>org.mybatis</groupId>
  9.         <artifactId>mybatis</artifactId>
  10.         <version>3.5.2</version>
  11.     </dependency>

  12.     <dependency>
  13.         <groupId>junit</groupId>
  14.         <artifactId>junit</artifactId>
  15.         <version>4.12</version>
  16.         <scope>test</scope>
  17.     </dependency>
  18. </dependencies>
复制代码


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

  1. mvn clean
  2. 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命令的,所有的这个依赖也是在那个网站找的但是还是不知道哪里错了
是不是我这个镜像地址有问题,我看着视频敲得,然后我搜索这个地址进不去网站
老师你可以远程操控帮我看看么
  1. <mirrors>
  2.     <!-- mirror
  3.      | Specifies a repository mirror site to use instead of a given repository. The repository that
  4.      | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
  5.      | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
  6.      |
  7.     <mirror>
  8.       <id>mirrorId</id>
  9.       <mirrorOf>repositoryId</mirrorOf>
  10.       <name>Human Readable Name for this Mirror.</name>
  11.       <url>http://my.repository.com/repo/path</url>
  12.     </mirror>
  13.      -->
  14.   <mirror>
  15.       <id>nexus-aliyun</id>
  16.       <mirrorOf>central</mirrorOf>
  17.       <name>Nexus-aliyun</name>
  18.       <url>http://maven.aliyun.com/nexus/content/groups/public</url>
  19.     </mirror>
  20.   </mirrors>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

你的maven应该是访问不到国外,可以在settings.xml配置阿里云镜像
  1.         <?xml version="1.0" encoding="UTF-8"?>

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

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

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

  18.         <!--
  19.          | This is the configuration file for Maven. It can be specified at two levels:
  20.          |
  21.          |  1. User Level. This settings.xml file provides configuration for a single user,
  22.          |                 and is normally provided in ${user.home}/.m2/settings.xml.
  23.          |
  24.          |                 NOTE: This location can be overridden with the CLI option:
  25.          |
  26.          |                 -s /path/to/user/settings.xml
  27.          |
  28.          |  2. Global Level. This settings.xml file provides configuration for all Maven
  29.          |                 users on a machine (assuming they're all using the same Maven
  30.          |                 installation). It's normally provided in
  31.          |                 ${maven.conf}/settings.xml.
  32.          |
  33.          |                 NOTE: This location can be overridden with the CLI option:
  34.          |
  35.          |                 -gs /path/to/global/settings.xml
  36.          |
  37.          | The sections in this sample file are intended to give you a running start at
  38.          | getting the most out of your Maven installation. Where appropriate, the default
  39.          | values (values used when the setting is not specified) are provided.
  40.          |
  41.          |-->
  42.         <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  43.                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.                   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  45.           <!-- localRepository
  46.            | The path to the local repository maven will use to store artifacts.
  47.            |
  48.            | Default: ${user.home}/.m2/repository
  49.           <localRepository>/path/to/local/repo</localRepository>
  50.           -->
  51.                 <!--定义本地仓库路径 如果pom.xml文件报错,则切换本地仓库-->
  52.                 <localRepository>D:/maven/repository</localRepository>
  53.           <!-- interactiveMode
  54.            | This will determine whether maven prompts you when it needs input. If set to false,
  55.            | maven will use a sensible default value, perhaps based on some other setting, for
  56.            | the parameter in question.
  57.            |
  58.            | Default: true
  59.           <interactiveMode>true</interactiveMode>
  60.           -->

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

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

  79.           <!-- proxies
  80.            | This is a list of proxies which can be used on this machine to connect to the network.
  81.            | Unless otherwise specified (by system property or command-line switch), the first proxy
  82.            | specification in this list marked as active will be used.
  83.            |-->
  84.           <proxies>
  85.             <!-- proxy
  86.              | Specification for one proxy, to be used in connecting to the network.
  87.              |
  88.             <proxy>
  89.               <id>optional</id>
  90.               <active>true</active>
  91.               <protocol>http</protocol>
  92.               <username>proxyuser</username>
  93.               <password>proxypass</password>
  94.               <host>proxy.host.net</host>
  95.               <port>80</port>
  96.               <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
  97.             </proxy>
  98.             -->
  99.           </proxies>

  100.           <!-- servers
  101.            | This is a list of authentication profiles, keyed by the server-id used within the system.
  102.            | Authentication profiles can be used whenever maven must make a connection to a remote server.
  103.            |-->
  104.           <servers>
  105.             <!-- server
  106.              | Specifies the authentication information to use when connecting to a particular server, identified by
  107.              | a unique name within the system (referred to by the 'id' attribute below).
  108.              |
  109.              | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
  110.              |       used together.
  111.              |
  112.             <server>
  113.               <id>deploymentRepo</id>
  114.               <username>repouser</username>
  115.               <password>repopwd</password>
  116.             </server>
  117.             -->

  118.             <!-- Another sample, using keys to authenticate.
  119.             <server>
  120.               <id>siteServer</id>
  121.               <privateKey>/path/to/private/key</privateKey>
  122.               <passphrase>optional; leave empty if not used.</passphrase>
  123.             </server>
  124.             -->
  125.           </servers>

  126.           <!-- mirrors
  127.            | This is a list of mirrors to be used in downloading artifacts from remote repositories.
  128.            |
  129.            | It works like this: a POM may declare a repository to use in resolving certain artifacts.
  130.            | However, this repository may have problems with heavy traffic at times, so people have mirrored
  131.            | it to several places.
  132.            |
  133.            | That repository definition will have a unique id, so we can create a mirror reference for that
  134.            | repository, to be used as an alternate download site. The mirror site will be the preferred
  135.            | server for that repository.
  136.            |-->
  137.           <mirrors>
  138.             <!-- mirror
  139.              | Specifies a repository mirror site to use instead of a given repository. The repository that
  140.              | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
  141.              | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
  142.              |
  143.             <mirror>
  144.               <id>mirrorId</id>
  145.               <mirrorOf>repositoryId</mirrorOf>
  146.               <name>Human Readable Name for this Mirror.</name>
  147.               <url>http://my.repository.com/repo/path</url>
  148.             </mirror>
  149.              -->
  150.                 <!--阿里云镜像-->
  151.                 <mirror>
  152.                 <id>nexus-aliyun</id>
  153.                 <mirrorOf>*</mirrorOf>
  154.                 <name>Nexus aliyun</name>
  155.                 <url>https://maven.aliyun.com/repository/public</url>
  156.                  </mirror>
  157.           </mirrors>

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

  193.               <activation>
  194.                 <jdk>1.4</jdk>
  195.               </activation>

  196.               <repositories>
  197.                 <repository>
  198.                   <id>jdk14</id>
  199.                   <name>Repository for JDK 1.4 builds</name>
  200.                   <url>http://www.myhost.com/maven/jdk14</url>
  201.                   <layout>default</layout>
  202.                   <snapshotPolicy>always</snapshotPolicy>
  203.                 </repository>
  204.               </repositories>
  205.             </profile>
  206.             -->

  207.             <!--
  208.              | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
  209.              | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
  210.              | might hypothetically look like:
  211.              |
  212.              | ...
  213.              | <plugin>
  214.              |   <groupId>org.myco.myplugins</groupId>
  215.              |   <artifactId>myplugin</artifactId>
  216.              |
  217.              |   <configuration>
  218.              |     <tomcatLocation>${tomcatPath}</tomcatLocation>
  219.              |   </configuration>
  220.              | </plugin>
  221.              | ...
  222.              |
  223.              | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
  224.              |       anything, you could just leave off the <value/> inside the activation-property.
  225.              |
  226.             <profile>
  227.               <id>env-dev</id>

  228.               <activation>
  229.                 <property>
  230.                   <name>target-env</name>
  231.                   <value>dev</value>
  232.                 </property>
  233.               </activation>

  234.               <properties>
  235.                 <tomcatPath>/path/to/tomcat/instance</tomcatPath>
  236.               </properties>
  237.             </profile>
  238.             -->
  239.             <!-- 全局jdk配置,settings.xml -->  
  240.             <profile>   
  241.                 <id>jdk18</id>   
  242.                 <activation>   
  243.                     <activeByDefault>true</activeByDefault>   
  244.                     <jdk>1.8</jdk>   
  245.                 </activation>   
  246.                 <properties>   
  247.                     <maven.compiler.source>1.8</maven.compiler.source>   
  248.                     <maven.compiler.target>1.8</maven.compiler.target>   
  249.                     <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>   
  250.                 </properties>     
  251.             </profile>
  252.           </profiles>

  253.           <!-- activeProfiles
  254.            | List of profiles that are active for all builds.
  255.            |
  256.           <activeProfiles>
  257.             <activeProfile>alwaysActiveProfile</activeProfile>
  258.             <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  259.           </activeProfiles>
  260.           -->
  261.         </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-4-27 15:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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