0

我想使用来自 maven 的信息在 .properties 文件中定义属性;例如 app.name=project.build.artifactId。

如果我在构建时调用资源:资源,它会在我的目标文件夹(在 eclipse 中)中创建一个名为 *.properties 的文件,其中包含 app.name 和(希望)我的 artifactId。

但是,当我通过 eclipse 运行 java 应用程序时,它是使用 /target 中的文件,还是使用 /src/main/resources 中的版本(其中包含变量占位符而不是值本身)?

编辑:

这是我希望使用“过滤”值的代码:

// Get project version from maven resource filtering
static {
  Properties prop = new Properties();

  try {
    // load a properties file
    prop.load(new FileInputStream("src/main/resources/project.properties"));

    // get the property value and print it out
    System.out.println(prop.getProperty("app.version"));
  }
  catch (IOException ex) {
    logger.error("Error (1): Failed to get application version from configuration file");
    System.out.println("Unexpected error (1). Aborting...");
  }
}

来自项目的 POM.xml 的相关信息:

<build>
  <!-- Resource filtering -->
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
      <includes>
         <include>**/*.properties</include>
      </includes>
    </resource> 
  </resources>
</build>

project.properties 内容:

app.version=${project.build.version}

当我通过 Eclipse 运行项目(不执行 JAR)时,第一段的输出(在静态块中)按字面意思返回“${project.build.version}”。它实际上并不返回项目标签的值。

我什至尝试过运行 resources:resources 首先查看是否有任何作用,但没有。资源过滤究竟是如何工作的——或者更好的是,你应该什么时候真正使用它?

4

0 回答 0