【问题标题】:FileNotFoundException resource file from external directory maven intellij来自外部目录 maven intellij 的 FileNotFoundException 资源文件
【发布时间】:2019-05-02 01:40:10
【问题描述】:

我在 Intellij 中运行 SpringBoot 应用程序时遇到问题。 @SpringBootApplication 位于子模块中

└── conf
   └── xml
      └──beans.xml
└── source
   └── core
       └── common
        └── MainApplication.java --> @SpringBootApplication
   └── config
   └── pom.xml --> parent

MainApplication.java 有注解 @ImportResource("classpath:/beans.xml")

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.xxx.yyy"})
@EnableCaching
@ImportResource("classpath:/beans.xml")
public class ServiceApplication extends SpringBootServletInitializer implements WebApplicationInitializer // extends SpringBootServletInitializer implements WebApplicationInitializer
{  

“config”模块有 pom.xml,没有 java 文件。它只生成资源

<artifactId>config</artifactId>
    <properties>
        <environment>DEV</environment>
        <country>ID</country>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>
    <build>
        <filters>
            <filter>../../build/server.properties</filter>
        </filters>
        <resources>
            <resource>
                <directory>../../conf</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>**/**/log4j2.xml</exclude>
                </excludes>
                <targetPath>../../../../deploy/conf</targetPath>
            </resource>
...

我尝试在 Intellij 上运行配置,但它为文件 beans.xml 产生错误 FileNotFoundException。此配置已在其他队友的 Eclipse 上运行。我想在 Intellij 上运行它。我已经在项目结构中导入了配置模块,并且 conf 文件夹已经标记为资源根。为什么它仍然不起作用?或者,如果我必须将更改附加到现有的 java 文件或 pom,没关系。只要应用程序可以找到到资源文件夹的方式。我不需要构建到战争中并运行它,但我只想作为 SpringBoot 应用程序运行

谢谢。任何帮助将不胜感激。

【问题讨论】:

    标签: maven spring-boot intellij-idea


    【解决方案1】:

    我会说这不是标准的 java 项目结构......不是好的做法。

    转到文件 -> 项目结构 -> 模块,例如:

    如您所见,您可以编辑源、资源、测试...文件夹的路径或添加更多。

    也许这会有所帮助...

    【讨论】:

      【解决方案2】:

      首先:从@ImportResource("classpath:/beans.xml") 中删除前导/

      这应该是:

      @ImportResource("classpath:beans.xml")
      

      第二:检查你的beans.xml在处理资源时是否被复制到你的项目构建目录中。

      beans.xml 的默认位置是 target/classes/beans.xml

      【讨论】:

        【解决方案3】:

        从@ImportResource("classpath:/beans.xml") 中删除前导/。

        1. 应该是这样的:@ImportResource("classpath:beans.xml")

        2. 查看源文件beans.xml是否已经复制到构建目录:路径:/target/classess/beans.xml

        3. 如果您没有将 beans.xml 放在源文件夹中,则提供实际路径或将其放在资源文件夹中

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-02-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-15
          • 2015-01-10
          • 1970-01-01
          • 2014-12-27
          相关资源
          最近更新 更多