【问题标题】:How can I access a gradle.properties file value from a Spring Boot War application?如何从 Spring Boot War 应用程序访问 gradle.properties 文件值?
【发布时间】:2016-11-17 13:59:33
【问题描述】:

我想从我的 Spring Boot 应用程序根目录中的 gradle.properties 文件中访问一个值。 (在首页显示项目的版本。)

我正在构建一场战争,将我的 Spring Boot 应用程序部署到服务器,并使用外部 application.properties 文件 (#9 on this list) 所以this solution 对我不起作用。

我尝试了this solution,但是我将 gradle.properties 文件放入战争中的任何位置(我已经尝试了该目录结构中的所有可能位置),我无法使用this method 访问它。

我该怎么做?

【问题讨论】:

    标签: java gradle spring-boot


    【解决方案1】:

    这就是在上面指定的限制内最终为我工作的东西:

    一句话总结

    为了在我的部署管道(gradle build -> war -> embedded tomcat)和开发(IntelliJ)中工作,我需要一个用于开发的虚拟属性文件,并在 gradle 构建期间复制实际的属性文件.

    代码

    ./gradle.properties

    version=x.x.x
    

    ./src/main/resources/my-project.properties

    version=development
    

    ./build.gradle

    war {
        rootSpec.exclude("**/my-project.properties")
        from('.') {
            include 'gradle.properties'
            into('WEB-INF/classes')
            rename('gradle.properties', 'my-project.properties')
        }   
    }
    

    ./src/main/java/com/company/version/VersionComponent.java

    private String getVersion() {
        String propertyVersion = "";
        Properties properties = new Properties();
        InputStream input = null;
        try {
            input = VersionComponent.class.getClassLoader().getResourceAsStream("my-project.properties");
            properties.load(input);
            propertyVersion = properties.getProperty("version");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return propertyVersion;
    }
    

    【讨论】:

      【解决方案2】:

      我在 grails 中工作,它也是基于 spring boot 构建的。和你一样,我想从我的 gradle 文件中传递属性,但对我来说,它来自 build.gradle 并在 junit 执行期间使用。

      注意:有关加载 application.properties 而不是 gradle.properties 的方法列表,您可以查看here。只是为了确保我们在同一页面上,如果您尝试对正在使用的应用程序进行版本控制(可能将其显示给某些用户),您应该在 application.properties 而不是 gradle.properties 中设置它。如果你想要后者,你need to replace the properties in the file you plan on using with gradle,并更改值,这更复杂。

      无论如何,这是我采用的一种方法,可让我在测试执行期间从 build.gradle 注入属性。

      test {
          def defaultLoginUrl = System.getProperty("testing.defaults.loginUrl")
      
          systemProperty "testing.defaults.loginUrl", defaultLoginUrl?: "https://localhost:8443/"
      }
      

      在我的 Java 代码中:

      public String getLoginUrl() {
          return System.getProperty("testing.defaults.loginUrl")
      }
      

      最后,如果你想access the file as an input stream,你应该把它放在the resources directory, and use the context to fetch it。但是,从阅读您的帖子来看,我认为 application.properties 正是您想要的。这个概念在问题you quoted 中同样得到了解决,因为当您准备部署它时,从构建文件加载属性并不常见。

      【讨论】:

      • 我已经知道如何从 application.properties 加载值,所以这不是我的问题。我也无法更改版本值的放置位置(您将其放置在 application.properties 中的建议是有道理的,但这不是它所在的位置)。您的解决方案似乎是通过 gradle 测试和执行这些测试的好方法,但这也不是我的情况。我们正在使用嵌入式 tomcat 实例将 Spring Boot 应用程序作为战争执行,因此在 gradle 任务中设置一些系统属性然后调用它们对我来说不是解决方案。感谢您的想法!
      • @MattRowe 完成这项工作的唯一方法是如我上一段所述,将文件放在资源目录中并解析它。在 grails 中,需要的路径是 src/main/webapp ,但在 spring 中,它可能是 src/main/resources。然后,当您要加载文件时,可以像这样引用它: /gradle.properties 。您也可以使用以下内容加载它:kamwo.me/java-load-file-from-classpath-in-spring-boot,或者:stackoverflow.com/questions/3294196/…
      猜你喜欢
      • 1970-01-01
      • 2021-03-19
      • 2015-05-27
      • 2018-11-03
      • 1970-01-01
      • 2021-06-22
      • 2020-04-07
      • 1970-01-01
      • 2021-12-28
      相关资源
      最近更新 更多