簡體   English   中英

讀取資源文件時,Teamcity構建失敗,但在本地運行良好

[英]Teamcity build fails when reading resource file but works fine locally

嘗試使用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")

好的,問題出在我設置項目的方式上。

當我將資源文件夾移至/ src / main並使用以下命令設置testResource目錄時,它位於src /下:

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

正在構建中,它運行完美。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM