Spring boot WAR 从 maven 运行,而不是从命令行运行



我检查了这个问题,但答案(虽然有趣(基本上是"不要使用 WAR 打包",这不是这里的选项。

我有一个使用嵌入式Tomcat的Spring Boot重新打包的WAR(Spring Boot 1.5.3.RELEASE,嵌入式Tomcat 8.5.14(。 重新打包的应用程序使用 mvn spring-boot:run 命令运行良好,但是当我尝试使用"java -jar target\mywar.war"运行它时,我得到这个:

java.io.FileNotFoundException: file:C:workspring-boottargetspring-boot-mywar.war!WEB-INFclasses!mywar.war (The filename, directory name, or volume label syntax is incorrect)

这是当 Spring 引导尝试加载"包装"战争的上下文时导致的:

Context context = tomcat.addWebapp("/mywar", Thread.currentThread().getContextClassLoader().getResource("mywar.war").getPath());

实际错误发生在嵌入式 Tomcat 类中:

private URL getWebappConfigFileFromJar(File docBase, String contextName) {
    URL result = null;
    try (JarFile jar = new JarFile(docBase)) {
        JarEntry entry = jar.getJarEntry(Constants.ApplicationContextXml);
        if (entry != null) {
            result = UriUtil.buildJarUrl(docBase, Constants.ApplicationContextXml);
        }
    } catch (IOException e) {
        Logger.getLogger(getLoggerName(getHost(), contextName)).log(Level.WARNING,
                "Unable to determine web application context.xml " + docBase, e);
    }
    return result;
}

该操作new JarFile(docBase)抛出FileNotFoundException

当使用 Maven spring-boot:run 目标时,这工作正常,所以我觉得基本结构是合理的 - 我认为存在一些类路径问题或阻止它工作的东西。

有没有人建议在使用 WAR 打包时在命令行上复制spring-boot:run的环境?

对于 Spring 启动应用程序,您可以使用 mvn spring-boot:run 运行,使用 java -jar thePackage.war 或将 war 包放在 tomcat web 应用程序中。每种方式都应该运行你的应用。

所以我认为你的项目存在一些问题。确保 pom 文件中有spring-boot-maven-plugin。然后当你使用mvn package时,你应该看到一些关于重新打包的日志。如果你解压缩战争文件,你应该会看到一些目录,比如:"META-INFO"和"BOOT-INF"。

在探索插件的实际 spring-boot:run 目标做什么后,事实证明它重建了项目并从 target/classes 文件夹中的编译类启动应用程序。 它似乎根本不使用 WAR 文件:

[INFO] --- spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) @ spring-boot-app ---
11:58:55.526 [main] INFO app.MyApplication - Beginning run() of MyApplication
2017-05-22 11:58:55.791 DEBUG 4140 --- [           main] .b.l.ClasspathLoggingApplicationListener : Application started with classpath: [file:/C:/work/myapp/target/classes/....[SNIP]
WAR

无法运行的原因是内部包含的应用程序 WAR 文件未被提取并写入嵌入式 Tomcat 上下文。 手动提取"内部 WAR"并将其写入上下文位置解决了问题:

/**
 * Utility method which exports and unpacks the WAR file into the Tomcat context directory
 * @param warName
 * @param contextPath
 * @return
 * @throws IOException
 * @throws URISyntaxException
 */
private static String exportWar(String warName, String contextPath) throws IOException, URISyntaxException {
    log.info("Beginning export WAR");
    try {
        UnzipUtility unzipUtility = new UnzipUtility();
        unzipUtility.unzip(warName, contextPath);
    } catch (IOException ex) {
        throw ex;
    }
    return contextPath + warName;
}

下面是基于 codejava.net 示例的 UnzipUtility:

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
 * This utility extracts files and directories of a standard zip file to
 * a destination directory.
 * @author www.codejava.net
 *
 */
public class UnzipUtility {
    /**
     * Size of the buffer to read/write data
     */
    private static final int BUFFER_SIZE = 4096;
    public void unzip(String zipFileName, String destDirectory) throws IOException {
        File destDir = new File(destDirectory);
        if (!destDir.exists()) {
            destDir.mkdir();
        }
        ZipInputStream zipIn = new ZipInputStream(Thread.currentThread().getContextClassLoader().getResourceAsStream(zipFileName));
        ZipEntry entry = zipIn.getNextEntry();
        // iterates over entries in the zip file
        while (entry != null) {
            String filePath = destDirectory + File.separator + entry.getName();
            if (!entry.isDirectory()) {
                // if the entry is a file, extracts it
                ensureParentExists(filePath);
                extractFile(zipIn, filePath);
            } else {
                // if the entry is a directory, make the directory
                File dir = new File(filePath);
                dir.mkdir();
            }
            zipIn.closeEntry();
            entry = zipIn.getNextEntry();
        }
        zipIn.close();
    }
    /**
     * Extracts a zip entry (file entry)
     * @param zipIn
     * @param filePath
     * @throws IOException
     */
    private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] bytesIn = new byte[BUFFER_SIZE];
        int read = 0;
        while ((read = zipIn.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
        }
        bos.close();
    }
    private void ensureParentExists(String filePath) {
        File parent = new File(filePath).getParentFile();
        if ( parent != null && !parent.exists()) {
            // parent of parent - recursive
            ensureParentExists(parent.getPath());
            // make this dir
            parent.mkdir();
        }
    }
}

最新更新