【问题标题】:Jetty resource base different in Maven Nested Multi-module project of IntelliJ IDEA vs EclipseIntelliJ IDEA vs Eclipse的Maven嵌套多模块项目中的Jetty资源库不同
【发布时间】:2016-07-13 19:52:37
【问题描述】:

我们有一个嵌套的多模块项目。我们的开发人员是 IntelliJ IDEA 和 Eclipse 用户的混合体。

在内部模块中运行码头服务器时,似乎我们需要根据我们使用的 IDE 将资源库设置为不同的值。

对于 IntelliJ:

root.setResourceBase("myModule/src/main/webapp");

对于 Eclipse:

root.setResourceBase("src/main/webapp");

我们不希望通过调整 IDE 来使其正常工作,例如我不想更改 IntelliJ 中的某些设置以使其与 Eclipse 版本的代码一起使用。

有什么想法吗?

【问题讨论】:

    标签: java eclipse maven intellij-idea jetty


    【解决方案1】:

    简短的回答

    Eclipse 与 Intellij 之间的执行差异可以通过不同的 PWD、${user.dir} 或工作目录设置来解释。

    更好的答案

    那么不要使用文件系统路径。

    通过Classloader.getResource() 在该位置查找已知资源,然后将父目录传递到root.setResourceBase()

    例子:

        Server server = new Server(8080);
    
        // Figure out what path to serve content from
        ClassLoader cl = WebAppContextFromClasspath.class.getClassLoader();
        // We look for a file, as ClassLoader.getResource() is not
        // designed to look for directories (we resolve the directory later)
        URL f = cl.getResource("hello.html");
        if (f == null)
        {
            throw new RuntimeException("Unable to find resource directory");
        }
    
        // Resolve file to directory
        URI webRootUri = f.toURI().resolve("./").normalize();
        System.err.println("WebRoot is " + webRootUri);
    
        WebAppContext webapp = new WebAppContext();
        webapp.setContextPath("/");
        webapp.setWar(webRootUri.toASCIIString());
        webapp.setParentLoaderPriority(true);
    
        server.setHandler(webapp);
    
        server.start();
        server.join();
    

    您可以在embedded-jetty-cookbook 示例中看到这一点:

    另一个更好的答案

    另一种方法是找到 src/main/webapp 的几种不同方式,具体取决于它的运行方式

    embedded-jetty-live-war 示例中查看ServerMain.java 中的操作模式。

    【讨论】:

      猜你喜欢
      • 2015-03-28
      • 1970-01-01
      • 1970-01-01
      • 2013-07-24
      • 2011-07-08
      • 2011-11-24
      • 2020-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多