【问题标题】:Maven Compile cant find src/main/resources directoryMaven 编译找不到 src/main/resources 目录
【发布时间】:2019-04-05 16:28:11
【问题描述】:

我正在尝试使用 Maven 为我的项目设置集成测试环境,但在运行 Compile 目标时出现以下错误。

[ERROR] 未能执行目标 org.apache.maven.plugins:maven-resources-plugin:3.0.2:resources 项目mavenintegrationtest上的(默认资源):加载错误 属性文件 '/Users/xxx/dev/poc/java/mavenintegrationtest/profiles/dev/config.properties' -> [帮助 1]

错误似乎是在抱怨它无法在该位置找到正确的 config.properties 文件。出于某种原因,它从文件路径中删除了“src/main/resources”位。

所以正确的完整路径是, /Users/xxx/dev/poc/java/mavenintegrationtest/src/main/resources/profiles/dev/config.properties 但由于某种原因,它删除了 src/main/resources ,所以正在查看, /Users/xxx/dev/poc/java/mavenintegrationtest/profiles/dev/config.properties 有谁知道这是什么原因造成的?

我的 POM 如下所示,当我取消注释以下“过滤器”标签时出现此错误,

<filter>${basedir}/profiles/${build.profile.id}/config.properties</filter>

我已尝试删除 ${basedir} 语句,但仍然出现相同的错误。

<?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.stackoverflow</groupId>
  <artifactId>mavenintegrationtest</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>mavenintegrationtest</name>
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <filters>
      <filter>${basedir}/profiles/${build.profile.id}/config.properties</filter>
    </filters>

    <resources>
      <resource>
        <filtering>true</filtering>
        <directory>${basedir}/src/main/resources</directory>
      </resource>
    </resources>

  </build>
  <!-- Profile configuration -->
  <profiles>
    <!-- The configuration of the development profile -->
    <profile>
      <id>dev</id>
      <!-- The development profile is active by default -->
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <build.profile.id>dev</build.profile.id>
      </properties>
    </profile>
    <!-- The configuration of the integration-test profile -->
    <profile>
      <id>integration-test</id>
      <properties>
        <build.profile.id>integration-test</build.profile.id>
      </properties>
    </profile>
  </profiles>

</project>

【问题讨论】:

  • 当你说你删除了 ${basedir} 部分时,我猜你也删除了前导 / 并且只留下了“src/main/resources”?
  • 您没有关于错误消息的更多信息吗?
  • @Sxilderik 是的,这是正确的,我试过只用“src/main/resources”运行
  • @lpinto.eu 我尝试打开调试跟踪,然后得到一个“FileNotFound”异常消息,它很清楚 - 它找不到我的 config.properties 文件,因为它删除了“src/main/路径中的资源”位....我不明白是什么原因造成的。
  • 您的根项目中没有配置文件文件夹。尽管 maven 期待一个,因为您在过滤器部分中指定了它。

标签: java maven-3 maven-plugin maven-surefire-plugin


【解决方案1】:

filter (link) 元素将解析您的文件并对其应用内容过滤器。 profiles (link) 元素可帮助您为构建定义不同的环境。

所有这些都与您的资源有关。这些可以是配置文件或其他类型的文件。如果您过滤,那么您可以使用其他值更改此文件的内容 - 例如pom 属性。使用配置文件时,您可以为每个环境使用不同的替换属性。

您应该将配置文件在树中向上移动以获得默认路径,或者在您的 pom 中为资源位置添加配置。 base dir 是包含你的 pom 的文件夹。您应该在这里有您的个人资料文件夹。

另外,here 是关于配置文件及其配置的一些很好的信息。

【讨论】:

  • @Ipinto.eu 谢谢 - 我会检查这个。您发布的那个链接是我一直关注的那个! :)
  • 我用的一样。只需移动我在项目打印中显示的配置文件文件夹即可。如果它有帮助,请随意投票我的答案或接受它。
  • @Ipinto.eu 谢谢你的工作! :) 有趣的是,我还通过将 src/main/resources 添加到我的过滤器标记中来使我的原始版本工作,如下所示, ${basedir}/src/main/resources/profiles/${build.profile.id}/config .properties 但从你所说的标准方法是在主项目目录中而不是在资源目录中放置“配置文件”目录 - 在这种情况下,我不清楚资源标签有什么影响?我会接受你的答案作为答案。
  • 资源是非源代码文件,您要添加到项目中 - 例如配置、翻译......您将用于“自定义”资源的配置文件,因此,为了区分这一点,您将它们存储在不同的位置。
猜你喜欢
  • 2016-08-22
  • 2013-08-04
  • 1970-01-01
  • 2010-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-13
  • 2013-05-11
相关资源
最近更新 更多