我有一些静态资源包含在我构建的jar中。它是一个jetty服务器,静态资源是index.html和其他一些js / css文件。我需要在运行时编辑index.html,我想知道如何在启动服务器的Bootstrap文件中,或者在运行启动脚本时使用sed或类似的命令行
有关最佳方法的任何想法吗?我更喜欢后一种解决方案,我可以通过shell命令做到这一点。
谢谢!
答案 0 :(得分:0)
这会有问题。
JAR本身会改变。
运行时不会看到更改(因为JAR内容在Java中缓存)
需要重新启动二进制文件以获取更改。
如果您的内容可以在运行时更改,请将该内容放在文件系统中的jar之外,或者为静态jar内容提供文件系统覆盖。
如需提供JAR以外的内容,请参阅先前的回答......
Serving static files from alternate path in embedded Jetty
要获得覆盖的替代路径,只需使用org.eclipse.jetty.util.resource.ResourceCollection
作为context.setBaseResource()
喜欢这个......
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
。
$TEMP/resource/<requestedResource>
$JARBASEURI/<requestedResource>
上面的输出告诉你它是如何工作的。
服务器转储包含运行ServletContextHandler
资源库的详细信息。
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
有2个基础。
file:///C:/Users/joakim/AppData/Local/Temp/resource/
jar:file:///C:/code/jetty-examples/target/project.jar!/webroot/
因此,如果请求到达https://myapp.com/js/config.js
,则会发生以下情况......
file:///C:/Users/joakim/AppData/Local/Temp/resource/js/config.js
是否存在?如果是这样,请提供服务。jar:file:///C:/code/jetty-examples/target/project.jar!/webroot/js/config.js
是否存在?如果是这样,请提供服务。