【问题标题】:Why dependencies are not added to classpath when I run spring boot project in IntellJ IDEA?为什么我在 IntelliJ IDEA 中运行 spring boot 项目时没有将依赖项添加到类路径中?
【发布时间】:2018-10-27 21:10:29
【问题描述】:

我的操作系统是Ubuntu 16.04,IDE是IntellJ IDEA 2018.1.3.

最近我尝试在Maven中运行一个简单的Spring Boot项目,但是当我运行这个应用程序时,我收到一些依赖项不存在的错误消息,如下所示:

我猜依赖没有添加到类路径,但是项目配置和maven依赖是正常的。我尝试在操作系统为Windows 10 的其他计算机上运行相同的项目。效果很好。

  • 马文:

  • 项目结构:

我的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>com.test</groupId>
<artifactId>test1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <junit.version>4.12</junit.version>
    <mysql.version>5.1.43</mysql.version>
</properties>


<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.8.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.1</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
    </dependency>

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.41</version>
    </dependency>
</dependencies>
</project>

【问题讨论】:

  • 您是否使用Spring Initializr 创建了项目?当您创建一个 Maven 项目并添加所有 spring 依赖项时,就会发生这种情况。我也在使用 Ubuntu 16.04
  • @SAM 不,我先创建一个空项目,然后在项目结构中添加新的 maven 模块。
  • 这就是你面临这个问题的原因。但你也可以继续,让我给你发一张屏幕截图。
  • @SAM 我用的是Spring Initializr,但还是报错。
  • 查看我的更新...

标签: java spring maven spring-boot intellij-idea


【解决方案1】:

以下过程用于解决通过Maven Project 创建的项目的library not found 错误。但是如果您使用Spring Initializr 创建一个项目,则不会存在此类问题。现在在Intellij 中打开Project Structure(快捷键Ctrl+Alt+Shift+S)。然后按照这张图片:

点击Create Artifact。然后关注这张图片:

选择所有库,然后右键单击,然后选择Put into /WEB_INF/lib。单击确定然后编译,然后一切都会好起来的。但请记住对每个新添加的库重复该过程。可悲的是,maven project 没有自动化流程,但您可以切换到 Spring Initializr

更新

如果您通过Spring Initializr 创建了项目,那么我怀疑您是否使用了系统上安装的默认Tomcat。在这种情况下,您应该使用运行时由 IDE 自动管理的embedded tomcat。 maven依赖链接如下,去掉之前添加的tomcat配置:

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <version>1.5.13.RELEASE</version>
</dependency>

【讨论】:

  • 如果您发现该解决方案有帮助,则接受该答案作为解决方案。快乐编码!!!
  • 好吧,但是所有的库总是放在/WEB-INF/lib
  • 是的,但不在您的输出文件夹中。您需要按照图像中的描述进行操作,以便库也可以在输出中使用。
  • 好的,我明白你的意思。但是当我创建项目时,我将&lt;packaging&gt;war&lt;/packaging&gt; 添加到我的pom.xml。因此库在war exploded 中的Artifacts 中的/WEB-INF/lib 下是完整的。
  • 你尝试过这个建议吗?
【解决方案2】:

我只是删除了idea/bin/idea.properties文件中idea.system.path的目录。

删除.idea/*.iml文件,然后重新导入项目。

但是我仍然不知道这个问题的原因和解决方法。

【讨论】:

  • @AnandVarkeyPhilips 是的,我已将其设置为答案。
猜你喜欢
  • 2017-01-14
  • 1970-01-01
  • 1970-01-01
  • 2020-07-31
  • 2021-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-19
相关资源
最近更新 更多