来自外部目录Maven Intellij的FileNotFoundException资源文件

时间:2019-05-02 01:40:11

标签: maven spring-boot intellij-idea

我在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上运行配置,但是它为文件bean.xml生成错误FileNotFoundException。此配置已在其他队友的Eclipse上运行过。我想在Intellij上运行它。我已经在项目结构中导入了配置模块,并且conf文件夹已经被标记为Resource Root。为什么它仍然不起作用?或者,如果我必须将更改附加到现有的Java文件或pom,则可以。只要应用程序可以找到通往资源文件夹的路径。我不需要构建战争并运行它,但是我只想作为SpringBoot应用程序运行

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

3 个答案:

答案 0 :(得分:0)

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

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

enter image description here

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

也许这会有所帮助...

答案 1 :(得分:0)

首先:从/中删除开头的@ImportResource("classpath:/beans.xml")

这应该是:

@ImportResource("classpath:beans.xml")

第二:处理资源后,检查beans.xml是否已复制到项目构建目录。

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

答案 2 :(得分:0)

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

  1. 应为:@ImportResource("classpath:beans.xml")

  2. 检查源文件beans.xml是否已复制到构建目录:Path:/target/classess/beans.xml

  3. 如果尚未将beans.xml放置在源文件夹中,请提供实际路径或将其放置在资源文件夹中

相关问题