读取资源文件时Teamcity构建失败但在本地工作正常

时间:2015-07-17 15:07:17

标签: java maven resources teamcity

尝试使用TeamCity构建项目时,我遇到了一个问题。

在我的项目中,我有一个文件夹src / resource /,其中包含我所有查询的子文件夹。我有一个帮助函数,使用IOUtils读取文件作为资源:

    public static String loadResourceToString(final String path) {
    final InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
    try {
        return IOUtils.toString(stream);
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    } finally {
        IOUtils.closeQuietly(stream);
    }
}

这段代码在我的本地机器上完美运行,当我使用mvn包构建jar时。但是,当我尝试通过teamcity构建相同的代码时,它会失败,抛出nullpointerexception:

java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1049)
at org.apache.commons.io.IOUtils.toString(IOUtils.java:359)
at com.company.util.AppUtil.loadResourceToString(AppUtil.java:255)
at com.company.data.UserData.registerStatements(UserData.java:27)
at com.company.web.data.Data.<init>(Data.java:39)
at com.company.data.UserData.<init>(UserData.java:22)
at com.company.data.UserTest.getAllDivisions(UserTest.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)

我称之为这门课的方式是:

AppUtil.loadResourceToString("Queries/getlist_by_id.sql")

1 个答案:

答案 0 :(得分:0)

好的问题在于我设置项目的方式。

我在src /下有资源文件夹,当我将它移动到/ src / main然后使用以下命令设置testResource目录:

<testResources>
        <testResource>
            <directory>src/main/Resource</directory>
        </testResource>
    </testResources>

在构建下它完美运作。