首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >修改Jar中的静态资源

修改Jar中的静态资源
EN

Stack Overflow用户
提问于 2018-04-27 15:03:44
回答 1查看 1.9K关注 0票数 0

我在构建的jar中包含了许多静态资源。它是一个jetty服务器,静态资源是一个index.html和其他一些js/css文件。我需要在运行时编辑index.html,我想知道如何在启动服务器的引导文件中,或者在运行启动脚本时使用sed或类似的命令行中进行编辑。

有什么最好的办法来解决这个问题吗?我更喜欢后一种解决方案,这是我可以通过shell命令来完成的。

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2018-04-27 15:35:26

这是有问题的。

罐子本身就会变。

运行时不会看到更改(因为JAR内容在Java中缓存)

需要重新启动二进制文件才能获取更改。

如果您有可以在运行时更改的内容,可以将该内容放在jar之外的文件系统中,或者为静态jar内容提供文件系统覆盖。

关于在罐子外提供内容,请参阅先前的回答.

Serving static files from alternate path in embedded Jetty

若要使用替代路径进行重写,只需为您的org.eclipse.jetty.util.resource.ResourceCollection使用context.setBaseResource()

像这样..。

代码语言:javascript
复制
package jetty;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.resource.PathResource;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceCollection;

public class ResourceCollectionDefaultServlet
{
    public static void main(String[] args) throws Exception
    {
        Server server = new Server();
        ServerConnector connector = new ServerConnector(server);
        connector.setPort(8080);
        server.addConnector(connector);

        // Jar Resource
        String resourceExample = "/webroot/index.html";
        URL jarUrl = server.getClass().getResource(resourceExample);
        if(jarUrl == null)
        {
            throw new FileNotFoundException("Unable to find JAR Resource: " + resourceExample);
        }
        URI jarUriBase = jarUrl.toURI().resolve("./");
        Resource jarResource = Resource.newResource(jarUriBase);

        // FileSystem Resource
        // Example here uses Path: $TEMP/resource/
        // You can pick a location on disk of your own choice (use a System property if you want)
        Path fsPath = new File(System.getProperty("java.io.tmpdir")).toPath().resolve("resource");
        if(!Files.exists(fsPath))
            Files.createDirectory(fsPath);
        Resource fsResource = new PathResource(fsPath);

        // Resource Collection
        ResourceCollection resourceCollection = new ResourceCollection(
                fsResource, // check FileSystem first
                jarResource // fall back to jar for all content not on filesystem
        );

        System.out.println("Resource Collection: " + resourceCollection);

        ServletContextHandler context = new ServletContextHandler();
        context.setContextPath("/");
        context.setBaseResource(resourceCollection);
        server.setHandler(context);
        context.setWelcomeFiles(new String[] { "index.html" });

        // TODO: Your servlets and filters here

        // Lastly, the default servlet for root content (always needed, to satisfy servlet spec)
        // It is important that this is last.
        ServletHolder holderPwd = new ServletHolder("default", DefaultServlet.class);
        holderPwd.setInitParameter("dirAllowed","true");
        context.addServlet(holderPwd,"/");

        server.setDumpAfterStart(true);
        server.start();
        server.join(); // wait on server to stop
    }

}

这将设置一个包含2个条目的ResourceCollection

  1. 检查FileSystem first $TEMP/resource/<requestedResource>
  2. 检查JAR文件$JARBASEURI/<requestedResource>

上面的输出告诉您它是如何工作的。

服务器转储包含运行ServletContextHandler资源库的详细信息。

代码语言:javascript
复制
2018-04-27 10:25:02.314:INFO::main: Logging initialized @338ms to org.eclipse.jetty.util.log.StdErrLog
Resource Collection: [file:///C:/Users/joakim/AppData/Local/Temp/resource/, jar:file:///C:/code/jetty-examples/target/project.jar!/webroot/]
2018-04-27 10:25:02.422:INFO:oejs.Server:main: jetty-9.4.9.v20180320; built: 2018-03-20T07:21:10-05:00; git: 1f8159b1e4a42d3f79997021ea1609f2fbac6de5; jvm 9.0.4+11
2018-04-27 10:25:02.477:INFO:oejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@13c10b87{/,[file:///C:/Users/joakim/AppData/Local/Temp/resource/, jar:file:///C:/code/jetty-examples/target/project.jar!/webroot/],AVAILABLE}
2018-04-27 10:25:02.609:INFO:oejs.AbstractConnector:main: Started ServerConnector@c267ef4{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
org.eclipse.jetty.server.Server@473b46c3[9.4.9.v20180320] - STARTING
...(snip)...
 += o.e.j.s.ServletContextHandler@13c10b87{/,[file:///C:/Users/joakim/AppData/Local/Temp/resource/, jar:file:///C:/code/jetty-examples/target/project.jar!/webroot/],AVAILABLE} - STARTED
 |   += org.eclipse.jetty.servlet.ServletHandler@1aa7ecca - STARTED
 |   |   += default@5c13d641==org.eclipse.jetty.servlet.DefaultServlet,jsp=null,order=-1,inst=false - STARTED
 |   |   |   +- dirAllowed=true
 |   |   +- [/]=>default
 |   +> No ClassLoader
 |   +> Handler attributes o.e.j.s.ServletContextHandler@13c10b87{/,[file:///C:/Users/joakim/AppData/Local/Temp/resource/, jar:file:///C:/code/jetty-examples/target/project.jar!/webroot/],AVAILABLE}
 |   |   +- org.eclipse.jetty.server.Executor=QueuedThreadPool[qtp1018298342]@3cb1ffe6{STARTED,8<=8<=200,i=3,q=0}
 |   +> Context attributes o.e.j.s.ServletContextHandler@13c10b87{/,[file:///C:/Users/joakim/AppData/Local/Temp/resource/, jar:file:///C:/code/jetty-examples/target/project.jar!/webroot/],AVAILABLE}
 |   |   +- org.eclipse.jetty.util.DecoratedObjectFactory=org.eclipse.jetty.util.DecoratedObjectFactory[decorators=1]
 |   +> Initparams o.e.j.s.ServletContextHandler@13c10b87{/,[file:///C:/Users/joakim/AppData/Local/Temp/resource/, jar:file:///C:/code/jetty-examples/target/project.jar!/webroot/],AVAILABLE}
2018-04-27 10:25:02.651:INFO:oejs.Server:main: Started @682ms

在这个执行过程中,我可以看到ResourceCollection有两个基。

  • FileSystem基地:file:///C:/Users/joakim/AppData/Local/Temp/resource/
  • Jar基础:jar:file:///C:/code/jetty-examples/target/project.jar!/webroot/

因此,如果请求(例如https://myapp.com/js/config.js )到达,则会发生以下情况.

  1. file:///C:/Users/joakim/AppData/Local/Temp/resource/js/config.js存在吗?如果是的话,就为它服务。
  2. jar:file:///C:/code/jetty-examples/target/project.jar!/webroot/js/config.js存在吗?如果是的话,就为它服务。
  3. 报告404未找到
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50065237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档