我在构建的jar中包含了许多静态资源。它是一个jetty服务器,静态资源是一个index.html和其他一些js/css文件。我需要在运行时编辑index.html,我想知道如何在启动服务器的引导文件中,或者在运行启动脚本时使用sed或类似的命令行中进行编辑。
有什么最好的办法来解决这个问题吗?我更喜欢后一种解决方案,这是我可以通过shell命令来完成的。
谢谢!
发布于 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()
像这样..。
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有两个基。
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存在吗?如果是的话,就为它服务。https://stackoverflow.com/questions/50065237
复制相似问题