【问题标题】:How to get the current thymeleaf version from spring boot project?如何从 Spring Boot 项目中获取当前的 thymeleaf 版本?
【发布时间】:2018-11-05 16:38:37
【问题描述】:

我有一个 Spring Boot 2.1.0.RELEASE 项目。如何查看它使用的 thymeleaf 版本?

【问题讨论】:

  • 你在问Spring Boot Thymeleaf Starter 吗?

标签: java spring-boot thymeleaf


【解决方案1】:

如果您使用的是 Maven,则可以使用 list 目标 maven dependency plugin

$ mvn dependency:list -DincludeArtifactIds=thymeleaf

或者使用 Maven Wrapper:

$ ./mvnw dependency:list -DincludeArtifactIds=thymeleaf

maven 的示例输出:

[INFO] --- maven-dependency-plugin:2.8:list (default-cli) @ site ---
[INFO] 
[INFO] The following files have been resolved:
[INFO]    org.thymeleaf:thymeleaf:jar:3.0.9.RELEASE:compile

对于Gradle

$ gradle dependencyInsight --dependency org.thymeleaf:thymeleaf

使用包装器:

$ ./gradlew dependencyInsight --dependency org.thymeleaf:thymeleaf

Gradle 的示例输出:

org.thymeleaf:thymeleaf:2.1.6.RELEASE (selected by rule)
\--- org.thymeleaf:thymeleaf-spring4:2.1.6.RELEASE
     \--- org.springframework.boot:spring-boot-starter-thymeleaf:1.5.9.RELEASE
          \--- compile

org.thymeleaf:thymeleaf:2.1.4.RELEASE -> 2.1.6.RELEASE
\--- nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:1.4.0
     \--- org.springframework.boot:spring-boot-starter-thymeleaf:1.5.9.RELEASE
          \--- compile

org.thymeleaf:thymeleaf-spring4:2.1.6.RELEASE (selected by rule)
\--- org.springframework.boot:spring-boot-starter-thymeleaf:1.5.9.RELEASE
     \--- compile

另见Inspecting dependencies

【讨论】:

  • 哦,谢谢 :D The following files have been resolved: org.thymeleaf:thymeleaf:jar:3.0.11.RELEASE:compile
【解决方案2】:

如果您使用的是 IntelIJ,一个不错且简单的解决方案是单击 Maven 项目(右侧)并展开依赖项。

【讨论】:

    【解决方案3】:

    根据 Spring Docs,您可以使用应用程序属性文件轻松完成此操作。

    您可以使用 Maven 项目自动扩展属性 资源过滤。如果你使用 spring-boot-starter-parent 你可以 然后通过 @..@ 占位符引用您的 Maven “项目属性”

    Maven pom.xml:

    <groupId>com.poo</groupId>
        <artifactId>gar</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
    <name>Poo</name>
    <description>Gar</description>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    

    Spring application.properties:

    poo.app.version=@project.version@
    
    1. 然后可以使用带有@ControllerAdvice 注解的类将版本作为模型属性注入。

       @ControllerAdvice 
       public class ControllerAdvice {
      
      @Value("${poo.app.version}")
      private String applicationVersion;
      
      @ModelAttribute("applicationVersion")
      public String getApplicationVersion() {
          return applicationVersion;
      }}
      
    2. 最后,Thymeleaf 可以像访问其他任何属性一样访问此模型属性。使用 th:text 标签,尝试访问下面的模型属性,以便显示您的应用正在使用的 Thymeleaf 版本:

      ${applicationVersion}

    【讨论】:

    • 谢谢,只看thymeleaf版本,2.x和3.x的sintaxis不同,看是2还是3。pom.xml:&lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;没有版本。跨度>
    猜你喜欢
    • 1970-01-01
    • 2014-06-21
    • 2013-04-22
    • 2017-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    相关资源
    最近更新 更多