找不到 Maven Webjars

Maven Webjars are not found

我正在尝试根据 documentation

bootstrap 使用 webjars
<dependencies>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.1.0</version>
    </dependency>
</dependencies>

这就是我启动服务器的方式。

public static void main(String[] args) throws Exception {
        final Server server = createServer();
        try {
            server.start();
            server.join();
        } finally {
            server.destroy();
        }
    }
private static Server createServer() throws Exception {
        final int serverPort = getServerPort();

        final Server server = new Server(serverPort);
        final HandlerList  servletContextHandlers = new HandlerList();
        servletContextHandlers.addHandler(buildServletContextHandler());
        server.setHandler(servletContextHandlers);

        return server;
    }

    private static ServletContextHandler buildServletContextHandler() throws ConfigurationException {
        final ResourceConfig resourceConfig = new ResourceConfig();
        resourceConfig.register(new CustomApiBinder());
        resourceConfig.packages("com.foo.api");

        final ServletHolder servletHolder = new ServletHolder(new ServletContainer(resourceConfig));

        final ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
        servletContextHandler.setContextPath("/api");
        servletContextHandler.addServlet(servletHolder, "/*");

        return servletContextHandler;
    }

然后,当我尝试 link bootstrap css sheet 时,我得到一个找不到文件的错误

<link rel='stylesheet' href='webjars/bootstrap/3.1.0/css/bootstrap.min.css'>

我需要专门的处理程序吗?从文档中可以看出,如果您使用 servlet 3,则不需要其他任何东西。

有没有人有 没有 使用 Spring 的例子?

码头 9.4.9.v20180320

球衣 2.26

bootstrap-<ver>.jar 有一个 META-INF/resources/ 子目录。

$ jar -tvf bootstrap-4.0.0.jar | grep META-INF/resources
     0 Thu Jan 18 21:20:32 GMT 2018 META-INF/resources/
     0 Thu Jan 18 21:20:32 GMT 2018 META-INF/resources/webjars/
     0 Thu Jan 18 21:20:32 GMT 2018 META-INF/resources/webjars/bootstrap/
     0 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/
     0 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/
     0 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/
 43852 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-grid-jsf.css
  4076 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-grid-jsf.css.gz
 43852 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-grid.css
  4076 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-grid.css.gz
 95910 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-grid.css.map
 34243 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-grid.min-jsf.css
  3483 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-grid.min-jsf.css.gz
 34243 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-grid.min.css
  3483 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-grid.min.css.gz
 76209 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-grid.min.css.map
178152 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-jsf.css
 22410 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-jsf.css.gz
  4798 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-reboot-jsf.css
  1683 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-reboot-jsf.css.gz
  4798 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-reboot.css
  1683 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-reboot.css.gz
 57721 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-reboot.css.map
  3936 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-reboot.min-jsf.css
  1584 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-reboot.min-jsf.css.gz
  3936 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-reboot.min.css
  1584 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-reboot.min.css.gz
 25881 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-reboot.min.css.map
178152 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap.css
 22410 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap.css.gz
411645 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap.css.map
144877 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap.min-jsf.css
 20563 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap.min-jsf.css.gz
144877 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap.min.css
 20563 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap.min.css.gz
551641 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap.min.css.map
195855 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.bundle.js
 41578 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.bundle.js.gz
326634 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.bundle.js.map
 67742 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.bundle.min.js
 19244 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.bundle.min.js.gz
273872 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.bundle.min.js.map
115048 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.js
 20137 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.js.gz
195373 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.js.map
 48944 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.min.js
 13105 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.min.js.gz
161998 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.min.js.map
   284 Thu Jan 18 21:20:32 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/webjars-requirejs.js
   182 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/webjars-requirejs.js.gz

这种JAR是一个web资源jar,对于Jetty只有在使用完整的时候才可用WebAppContext

Note: When using a Jetty WebAppContext all of the WEB-INF/lib/*.jar!/META-INF/resources/ contents will be unpacked into a temporary directory so that Jetty can serve the contents of those special jar files.

这里有几个选项(从 best/easiest 选择到最复杂的顺序)。

  1. 让 Maven 将您的 META-INF/resources jar 解压到您的资源目录中,然后您通过类路径资源 URL 将其引用为 ServletContextHandler 资源基础位置。
  2. 使用您自己的代码将 META-INF/resources 目录解压缩到一个临时目录中,然后将其用作 ServletContextHandler 资源基目录。
  3. 改用带有 war 文件的完整 WebAppContext 以及启用您要使用的各种功能所需的所有额外配置。

请注意,如果您的最终目标是 to build a jetty uber jar,那么选项 1 将是总体最佳选择。

您所做的选择将取决于您最终希望如何打包项目。

  • 它会是 1 个自执行 jar 吗?然后选项1.
  • 会不会是罐子的集合?那么选项1和2都是不错的选择。
  • 它会是一组 jar 和一个 war 文件吗?那么选项3是一个不错的选择。

基于 META-INF/resources 的内容的最大问题是名称冲突解决。因此,我鼓励您选择选项 1,因为这将解决(在构建时)任何冲突解决问题。

例如:如果您有 2 个 JAR 文件,都带有 META-INF/resources/foo.css 文件(但内容不同)并且请求到达 http://<host>/foo.css,您服务哪一个?

有关选项 1 的示例,请参阅 - https://github.com/jetty-project/embedded-jetty-with-web-resources

一些 embedded-jetty 由 Eclipse Jetty 项目维护的资源:

您可以尝试添加 ResourceHandler,例如:

ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setDirectoriesListed(false);
resourceHandler.setResourceBase(Main.class.getResource("META-INF/resources/webjars").toExternalForm());

ContextHandler webJarContext = new ContextHandler();
webJarContext.setContextPath("/webjars");
webJarContext.setHandler(resourceHandler);

正如 Joakim Erdfelt 所建议的,我使用 maven 将资源复制到目标 类。

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                        <execution>
                        <id>unpack</id>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.webjars</groupId>
                                    <artifactId>swagger-ui</artifactId>
                                    <version>${swagger-ui.version}</version>                                    
                                    <type>jar</type>
                                    <includes>META-INF/resources/webjars/**/*</includes>
                                    <excludes>META-INF/resources/webjars/**/*index.html</excludes>
                                    <outputDirectory>${project.basedir}/target/classes/</outputDirectory>
                                    </artifactItem>
                                </artifactItems>
                            </configuration>
                        </execution>
                </executions>
            </plugin>   
        </plugins>
    </build>