在Gradle多项目构建中无法读取另一个项目中的资源文件

时间:2020-04-18 04:49:12

标签: java gradle

在一个名为“笑话”的项目中,我有这个课

public class JokeFactory {

    private static List<String> jokes = null;
    private static Random random = new Random();

    public static FutureTask<String> getJoke() {

        return new FutureTask<>(
                () -> {
                    if (jokes == null) {
                        jokes = readJokesFromTextFile();
                    }
                    int idx = random.nextInt(jokes.size());
                    return jokes.get(idx);
                });
    }

    public static List<String> readJokesFromTextFile() throws URISyntaxException, IOException {
        List<String> jokes =
                Files.readAllLines(Paths.get(JokeFactory.class.getResource("/jokes.txt").toURI()));
        return jokes.stream().filter(j -> !j.isEmpty()).collect(Collectors.toList());
    }
}

并且单元测试通过。因此,我相信它可以读取同一项目中的文件。

在另一个名为“后端”的项目中,它称为JokeFactory.getJoke()

@Api(
    name = "myApi",
    version = "v1",
    namespace =
            @ApiNamespace(
                ownerDomain = "backend.builditbigger.gradle.udacity.com",
                ownerName = "backend.builditbigger.gradle.udacity.com",
                packagePath = ""
            )
)
public class MyEndpoint {

    private static Logger mGlobal = Logger.getGlobal();

    @ApiMethod(name = "jokes", path = "jokes", httpMethod = ApiMethod.HttpMethod.GET)
    public JokeResponse getJoke() {
        try {
            FutureTask<String> joke = JokeFactory.getJoke();
            Executors.newCachedThreadPool().submit(joke);
            return new JokeResponse(joke.get());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
            mGlobal.warning("JokeResponse returns InterruptedException");
            return new JokeResponse("ERROR");
        }
    }
}

然后,我收到FileNotFoundError

java.util.concurrent.ExecutionException: java.nio.file.FileSystemNotFoundException
        at java.util.concurrent.FutureTask.report(FutureTask.java:122)
        at java.util.concurrent.FutureTask.get(FutureTask.java:192)
        at com.udacity.gradle.builditbigger.backend.MyEndpoint.getJoke(MyEndpoint.java:33)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:349)
        at com.google.api.server.spi.handlers.EndpointsMethodHandler$RestHandler.handle(EndpointsMethodHandler.java:127)
        at com.google.api.server.spi.handlers.EndpointsMethodHandler$RestHandler.handle(EndpointsMethodHandler.java:110)
        at com.google.api.server.spi.dispatcher.PathDispatcher.dispatch(PathDispatcher.java:50)
        at com.google.api.server.spi.EndpointsServlet.service(EndpointsServlet.java:80)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
        at org.eclipse.jetty.servlet.ServletHolder$NotAsyncServlet.service(ServletHolder.java:1395)
        at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:755)
        at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1617)
        at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:134)
        at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1604)
        at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
        at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1604)
        at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
        at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1604)
        at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:48)
        at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1604)
        at com.google.appengine.tools.development.jetty9.StaticFileFilter.doFilter(StaticFileFilter.java:123)
        at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1604)
        at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:366)
        at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349)
        at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)
        at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1604)
        at com.google.appengine.tools.development.DevAppServerRequestLogFilter.doFilter(DevAppServerRequestLogFilter.java:28)
        at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1596)
        at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:545)
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
        at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:566)
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)
        at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:235)
        at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1610)
        at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233)
        at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1300)
        at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188)
        at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:485)
        at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1580)
        at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186)
        at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1215)
        at com.google.appengine.tools.development.jetty9.DevAppEngineWebAppContext.doScope(DevAppEngineWebAppContext.java:94)
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)
        at com.google.appengine.tools.development.jetty9.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:610)
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)
        at org.eclipse.jetty.server.Server.handle(Server.java:500)
        at org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:383)
        at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:547)
        at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:375)
        at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:273)
        at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311)
        at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103)
        at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:117)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:806)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:938)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.nio.file.FileSystemNotFoundException
        at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171)
        at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
        at java.nio.file.Paths.get(Paths.java:143)
        at dev.kkweon.joke.JokeFactory.readJokesFromTextFile(JokeFactory.java:31)
        at dev.kkweon.joke.JokeFactory.lambda$getJoke$0(JokeFactory.java:22)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        ... 1 more

这是一个build.gradle的“后端”项目。

dependencies {
    implementation project(path: ':joke')
}

完整的源代码:https://github.com/kkweon/udacity-build-it-bigger

为了复制,

./gradlew appengineRun # start the dev server locally
curl -H 'Content-Type: application/json' -XGET 'http://localhost:8080/_ah/api/myApi/v1/jokes/'

1 个答案:

答案 0 :(得分:0)

我最终添加了额外的资源sourceDirs

import... public class DetailActivity extends AppCompatActivity { TextView detailData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); detailData = findViewById(R.id.tv_nama_usaha); Intent i = getIntent(); String content = i.getStringExtra("nama_usaha"); detailData.setText(content); detailData.setMovementMethod(new ScrollingMovementMethod()); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } } 中,我添加了以下内容。

backend/build.gradle