[英]How can I create executable jars with embedded tomcat 9?
Has anyone tried the plugin to build an executable war/jar using Tomcat 9?有没有人尝试过使用 Tomcat 9 构建一个可执行的 war/jar 插件?
I attempted to do so however ran into:我试图这样做但是遇到了:
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.catalina.startup.Catalina.setConfig(Ljava/lang/String;)V at org.apache.tomcat.maven.runner.Tomcat7Runner.run(Tomcat7Runner.java:240) at org.apache.tomcat.maven.runner.Tomcat7RunnerCli.main(Tomcat7RunnerCli.java:204)
I looked at the source and changed Catalina.setConfig() to Catalina.setConfigFile() based on docs here .我查看了源代码并根据此处的文档将 Catalina.setConfig() 更改为 Catalina.setConfigFile() 。 After doing so the .extract dir is just empty:这样做之后 .extract 目录是空的:
use extractDirectory:.extract populateWebAppWarPerContext warValue:ROOT.war|ROOT populateWebAppWarPerContext contextValue/warFileName:ROOT/ROOT.war webappWarPerContext entry key/value: ROOT/ROOT.war expand to file:.extract/webapps/ROOT.war Exception in thread "main" java.lang.Exception: FATAL: impossible to create directories:.extract/webapps at org.apache.tomcat.maven.runner.Tomcat7Runner.extract(Tomcat7Runner.java:586) at org.apache.tomcat.maven.runner.Tomcat7Runner.run(Tomcat7Runner.java:204) at org.apache.tomcat.maven.runner.Tomcat7RunnerCli.main(Tomcat7RunnerCli.java:204)使用extractDirectory:.extract populateWebAppWarPerContext warValue:ROOT.war|ROOT populateWebAppWarPerContext contextValue/warFileName:ROOT/ROOT.war webappWarPerContext 条目键/值:ROOT/ROOT.war 扩展到文件:.extract/webapps/ROOT.war 线程中的异常“ main" java.lang.Exception: 致命:无法在 org.apache.tomcat.maven.runner.Tomcat7Runner.extract(Tomcat7Runner.java:586) 上创建目录:.extract/webapps 在 org.apache.tomcat.maven.runner .Tomcat7Runner.run(Tomcat7Runner.java:204) 在 org.apache.tomcat.maven.runner.Tomcat7RunnerCli.main(Tomcat7RunnerCli.java:204)
.... although there is a ROOT.war, server.xml, web.xml in the *-exec-war.jar. ....虽然在*-exec-war.jar中有ROOT.war、server.xml、web.xml。
Is there a better way to be creating exec-jars with embedded tomcat 9?有没有更好的方法来创建带有嵌入式 tomcat 9 的 exec-jars?
For those looking for a solution it was fairly straight forward to checkout the code for the plugin and make a few changes to get this to work.对于那些正在寻找解决方案的人来说,检查插件的代码并进行一些更改以使其正常工作是相当直接的。 Namely: Update POM to change the depends to Tomcat 9 Fix compile errors which generally stem from deprecated methods.即:更新 POM 以将依赖项更改为 Tomcat 9 修复通常源于已弃用方法的编译错误。 The lookup on these methods can be found here .可以在此处找到对这些方法的查找。 For example:例如:
- container.setConfig( serverXml.getAbsolutePath() );
+ container.setConfigFile( serverXml.getAbsolutePath() );
... and ... ... 和 ...
- staticContext.addServletMapping( "/", "staticContent" );
+ staticContext.addServletMappingDecoded( "/", "staticContent" );
There are a few others but generally not difficult to resolve.还有一些其他的,但通常不难解决。 After doing so I updated my app's pom to use the modified version and was able to generate a Tomcat 9 exec jar.这样做之后,我更新了我的应用程序的 pom 以使用修改后的版本,并且能够生成一个 Tomcat 9 exec jar。
I would love to hear what others are doing here.我很想听听其他人在这里做什么。 I know some are programmatically initializing Tomcat via a new Tomcat()
instance however curious what other solutions exist ready made.我知道有些人正在通过一个new Tomcat()
实例以编程方式初始化 Tomcat,但很好奇还有哪些现成的解决方案。 Thanks谢谢
For future searchs, one solution is to use the DirResourceSet or JarResourceSet.对于将来的搜索,一种解决方案是使用 DirResourceSet 或 JarResourceSet。
String webAppMount = "/WEB-INF/classes";
WebResourceSet webResourceSet;
if (!isJar()) {
webResourceSet = new DirResourceSet(webResourceRoot, webAppMount, getResourceFromFs(), "/");
} else {
webResourceSet = new JarResourceSet(webResourceRoot, webAppMount, getResourceFromJarFile(), "/");
}
webResourceRoot.addJarResources(webResourceSet);
context.setResources(webResourceRoot);
public static boolean isJar() {
URL resource = Main.class.getResource("/");
return resource == null;
}
public static String getResourceFromJarFile() {
File jarFile = new File(System.getProperty("java.class.path"));
return jarFile.getAbsolutePath();
}
public static String getResourceFromFs() {
URL resource = Main.class.getResource("/");
return resource.getFile();
}
When add the webapp, use root path "/" for docBase:添加 webapp 时,为 docBase 使用根路径“/”:
tomcat.addWebapp("", "/")
Credits for: https://nkonev.name/post/101学分: https : //nkonev.name/post/101
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.