【问题标题】:Maven dependency randomly not recognizedMaven依赖随机无法识别
【发布时间】:2017-02-22 18:47:26
【问题描述】:

我正在开发一个使用 HTML 和 Thymeleaf 作为前端的 Java Spring Boot Web 应用程序。我遇到的问题是,当我尝试运行我的项目时,我的 ThymeleafConfig 类会随机出现错误。我将在下面更详细地解释,但首先是我的代码。

Pom.xml 依赖:

<dependency>
     <groupId>org.thymeleaf.extras</groupId>
     <artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>

ThymeleafConfig 类:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect;

@Configuration
public class ThymeleafConfig {

    @Bean
    public SpringSecurityDialect springSecurityDialect() {
        return new SpringSecurityDialect();
    }
}

所以为了进一步解释,我的代码会很好(根据我的 IDE),我的任何行都没有错误,可以运行。我将运行我的应用程序类来运行我的项目,我会得到编译错误。

IntelliJ 会自动打开包含错误的文件,即我的ThymeleafConfig 类。出现错误时,导入语句import org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect; 行将显示为灰色且无法识别。导致类中的错误。

要修复它,我右键单击我的 pom.xml 文件 -> Maven -> 重新导入。这将重新导入我的所有依赖项,一切都会恢复正常。

我还想强调,并非每次运行应用程序时都会发生这种情况。有时它会连续发生 3 次,有时我会运行它 5 次以上,然后才会再次出现错误。

我尝试了mvn clean,但没有解决问题。我还将依赖项移动到 pom.xml 文件中的不同行。

编辑: 这是我的 Application.java 类

@SpringBootApplication
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

要运行应用程序,我右键单击此文件并单击运行“应用程序”按钮。

【问题讨论】:

  • 能否请您告诉我们您是如何构建/运行您的应用程序的?在我看来,您以某种方式删除了库/破坏了类路径。
  • 当然 - 见编辑
  • 我的 pom.xml 中有 15 个以上的其他依赖项 - 都按应有的方式运行。只是这个 Thymeleaf-security-4 依赖有问题

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


【解决方案1】:

Spring boot 已经包含 thymeleaf(不同版本),并且您可能在类路径上有多个版本。 Spring Boot 在其文档中对 thymeleaf 非常清楚:

Spring boot 1.4.2's documentation 使用 thymeleaf 3.0.0

默认情况下,spring-boot-starter-thymeleaf 使用 Thymeleaf 2.1。如果你 正在使用 spring-boot-starter-parent,您可以通过以下方式使用 Thymeleaf 3 覆盖 thymeleaf.version 和 thymeleaf-layout-dialect.version 属性,例如:

<properties>
    <thymeleaf.version>3.0.0.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.0.3</thymeleaf-layout-dialect.version>
</properties>

...

如果您使用任何其他自动配置的 Thymeleaf Extras (Spring Security、Data Attribute 或 Java 8 Time)您还应该 将它们的每个版本覆盖为兼容的版本 百里香叶 3.0。

如果你查看spring-boot-dependency's pom on github,thymeleaf 属性是:

<thymeleaf.version>2.1.5.RELEASE</thymeleaf.version>          
<thymeleaf-extras-springsecurity4.version>2.1.2.RELEASE</thymeleaf-extras-springsecurity4.version>

<thymeleaf-extras-conditionalcomments.version>2.1.2.RELEASE</thymeleaf-extras-conditionalcomments.version>

<thymeleaf-layout-dialect.version>1.4.0</thymeleaf-layout-dialect.version>

<thymeleaf-extras-data-attribute.version>1.3</thymeleaf-extras-data-attribute.version>

<thymeleaf-extras-java8time.version>2.1.0.RELEASE</thymeleaf-extras-java8time.version>

回顾

我相信您至少需要定义以下属性(可能还要定义一些属性以避免版本不匹配?):

  • thymeleaf.version
  • thymeleaf-extras-springsecurity4.version

【讨论】:

  • 您提供的&lt;properties&gt;sn-p 究竟应该在哪里定义?在 pom 本身还是在单独的配置文件中?
  • 在你的 pom 中,就像你通常定义属性一样。
猜你喜欢
  • 2021-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-27
  • 2023-01-18
  • 2013-02-09
相关资源
最近更新 更多