tomcat部署的三种方式
前言
年轻人不讲武德,第一次写博客,还请大佬手下留情!! 多多指教 。本人菜鸟一枚第一次研究tomcat源码,简单的看了下源码,发现了tomcat的三种部署方式(实际四种,但本人只描述三种方式):
// Deploy XML descriptors from configBase(文件描述符的方式部署)
方式-1.deployDescriptors(configBase, configBase.list());
// Deploy WARs(war包的方式部署)
方式-2.deployWARs(appBase, filteredAppPaths);
// Deploy expanded folders(文件夹的方式部署)
方式- 3.deployDirectories(appBase, filteredAppPaths);
至于我为啥知道有这三种方式部署,因为看的tomcat的源码(类HostConfig-》method deployApps 哈哈 本人喜欢装逼 !!!!
一、deployDescriptors
翻译:部署XML上下文描述符
/**
* Deploy XML context descriptors.
* @param configBase The config base
* @param files The XML descriptors which should be deployed
*/
protected void deployDescriptors(File configBase, String[] files) {
if (files == null)
return;
ExecutorService es = host.getStartStopExecutor();
List<Future<?>> results = new ArrayList<>();
for (String file : files) {
File contextXml = new File(configBase, file);
if (file.toLowerCase(Locale.ENGLISH).endsWith(".xml")) {
ContextName cn = new ContextName(file, true);
if (isServiced(cn.getName()) || deploymentExists(cn.getName()))
continue;
results.add(
es.submit(new DeployDescriptor(this, cn, contextXml)));
}
}
for (Future<?> result : results) {
try {
result.get();
} catch (Exception e) {
log.error(sm.getString(
"hostConfig.deployDescriptor.threaded.error"), e);
}
}
}
二、WAR包文件部署
/**
* Deploy WAR files.
* @param appBase The base path for applications
* @param files The WARs to deploy
*/
protected void deployWARs(File appBase, String[] files) {
if (files == null)
return;
ExecutorService es = host.getStartStopExecutor();
List<Future<?>> results = new ArrayList<>();
for (String file : files) {
if (file.equalsIgnoreCase("META-INF"))
continue;
if (file.equalsIgnoreCase("WEB-INF"))
continue;
File war = new File(appBase, file);
if (file.toLowerCase(Locale.ENGLISH).endsWith(".war") &&
war.isFile() && !invalidWars.contains(file)) {
ContextName cn = new ContextName(file, true);
if (isServiced(cn.getName())) {
continue;
}
if (deploymentExists(cn.getName())) {
DeployedApplication app = deployed.get(cn.getName());
boolean unpackWAR = unpackWARs;
if (unpackWAR && host.findChild(cn.getName()) instanceof StandardContext) {
unpackWAR = ((StandardContext) host.findChild(cn.getName())).getUnpackWAR();
}
if (!unpackWAR && app != null) {
// Need to check for a directory that should not be
// there
File dir = new File(appBase, cn.getBaseName());
if (dir.exists()) {
if (!app.loggedDirWarning) {
log.warn(sm.getString(
"hostConfig.deployWar.hiddenDir",
dir.getAbsoluteFile(),
war.getAbsoluteFile()));
app.loggedDirWarning = true;
}
} else {
app.loggedDirWarning = false;
}
}
continue;
}
// Check for WARs with /../ /./ or similar sequences in the name
if (!validateContextPath(appBase, cn.getBaseName())) {
log.error(sm.getString(
"hostConfig.illegalWarName", file));
invalidWars.add(file);
continue;
}
results.add(es.submit(new DeployWar(this, cn, war)));
}
}
for (Future<?> result : results) {
try {
result.get();
} catch (Exception e) {
log.error(sm.getString(
"hostConfig.deployWar.threaded.error"), e);
}
}
}
三、文件夹部署
/**
* Deploy exploded webapps.
* @param appBase The base path for applications
* @param files The exploded webapps that should be deployed
*/
protected void deployDirectories(File appBase, String[] files) {
if (files == null)
return;
ExecutorService es = host.getStartStopExecutor();
List<Future<?>> results = new ArrayList<>();
for (String file : files) {
if (file.equalsIgnoreCase("META-INF"))
continue;
if (file.equalsIgnoreCase("WEB-INF"))
continue;
File dir = new File(appBase, file);
if (dir.isDirectory()) {
ContextName cn = new ContextName(file, false);
if (isServiced(cn.getName()) || deploymentExists(cn.getName()))
continue;
results.add(es.submit(new DeployDirectory(this, cn, dir)));
}
}
for (Future<?> result : results) {
try {
result.get();
} catch (Exception e) {
log.error(sm.getString(
"hostConfig.deployDir.threaded.error"), e);
}
}
}
总结
看了上面的代码,相信许多同学知道了为啥部署到tomcat上的项目必须要以war包的形式部署了吧,因为tomcat源码写死了去匹配war包,而不支持jar包的形式部署了吧,其实还有一个原因就是jar包本身可以理解为依赖,tomcat无法识别这个jar是项目的依赖 还是 web应用,所有只识别了war包的部署,而本身war包和jar包没有本质的区别(如有不对的地方,欢迎大佬来怼,奥利给!)