java - Spring 与 Maven : Maven does not find my resources

标签 java spring maven

我是新来的,我不想询问,但似乎我别无选择。

我正在编写自动化测试,不久前我发现了 Spring 和依赖注入(inject)及其所有优点。然后我在测试中使用它作为我的主要数据提供者(尽管我的一位同事不同意这种用法,我不明白为什么),如下所示:

/src/test/java/automation/data list :

@ContextConfiguration("classpath:/spring/surveyFlows.xml")
public class SurveyDataProvider
{

    @Autowired
    private List<Survey> allSurveys;

    public List<Survey> getAllSurveys()
    {
        return allSurveys;
    }
    public SurveyDataProvider()
    {
        TestContextManager testContextManager = new TestContextManager(getClass());
        try
        {
            testContextManager.prepareTestInstance(this);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

当然,我的 XML 已经在里面了 /src/test/resources/spring/

有趣的是,如果我用 IntelliJ 运行它,它就可以正常工作,但是 maven 拒绝运行,给我那个该死的错误:

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring/surveyFlows.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring/surveyFlows.xml] cannot be opened because it does not exist

我在互联网上浏览了几个小时,尝试了各种各样的事情:

  • 将资源移动到/src/main
  • 将资源目录放入pom.xml

...没有骰子。

所以我有点被困在这里,我正在认真考虑使用硬编码参数恢复到旧的 Java 风格。

如果有人有线索......

最佳答案

这对我们有用。如果您找到替代方法,请告诉我。

<build>
    <!--Your other configuration -->

    <testResources>
        <!-- Not sure if including resource from main is needed -->
        <testResource>
            <directory>${project.basedir}/src/main/resources</directory>
        </testResource>

        <testResource>
            <directory>${project.basedir}/src/test/resources</directory>
        </testResource>
    </testResources>
</build>

关于java - Spring 与 Maven : Maven does not find my resources,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15188734/

相关文章:

java - Spring Roo 没有已知的附加组件

java - Spring - 如何为不同的消费者定制bean值

java - 在 eclipse maven 中链接源文件夹时出现错误

java - maven使用spring bootparent创建可执行jar包

java - 使用 s :action in JSP 时如何修复 java.lang.StackOverflowError

java - 使用 pom.xml 属性填充环境属性文件

java - 64 位 JVM 中的 int 数据类型。 "inefficient"比 long 多吗?

java - 使用 Rest CXF 管理异常

maven-2 - 5 分钟内 Maven 无法工作

java - 将函数名称传递给函数