从.jar手动启动Spring Boot应用程序无法正常工作 [英] Manually launched Spring Boot application from .jar not working properly

查看:214
本文介绍了从.jar手动启动Spring Boot应用程序无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是当我从Intellij IDEA运行.jar文件(Spring Boot MVC应用程序)时,应用程序工作正常,但是当我尝试从命令行运行同一文件时,模板解析器无法找到模板(.html)文件。这似乎是合理的,因为当我用文件存档器打开.jar时,我没有发现任何文件的痕迹。

The problem is when I run .jar file(Spring Boot MVC app) from Intellij IDEA, application works fine, but when I try to run same file from the command line, template resolver cannot find template(.html) files. And this seems to be reasonable since when I opened .jar with file archiver I did not find any traces of that files.

Intellij如何让它正常工作?我怎么能这样做?
我的项目结构:

How Intellij make it work proper? And how can I do the same? My project structure:

这里是'/ templates'文件夹

here they are, in '/templates' folder

.jar文件在存档器中打开:

.jar file opened in archiver:

正如我所说,没有.html文件,所以Intellij在运行.jar时如何添加它们?

As I said, there is no .html files, so how Intellij add them when running .jar?

pom .xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.vlad.pet.contactlist</groupId>
  <artifactId>web-app</artifactId>
  <version>1.0-ALPHA</version>
  <packaging>jar</packaging>

  <name>web-app</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>


  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.6.RELEASE</version>
  </parent>

  <dependencies>
    <dependency>
      <groupId>com.vlad.pet.contactlist</groupId>
      <artifactId>model</artifactId>
      <version>1.1-inmemory</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
      <groupId>org.thymeleaf.extras</groupId>
      <artifactId>thymeleaf-extras-springsecurity4</artifactId>
      <version>2.1.2.RELEASE</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>


推荐答案

http://start.spring.io 将显示 src / main / resources中的templates文件夹。这是因为默认情况下,该文件夹的内容可用于类路径并打包在jar文件的根目录下。我猜你在IntelliJ中修改了项目,也拿起 src / main / webapp 目录。不幸的是,在包装罐子时这不起作用。您需要使用maven资源插件来指定要包含在jar文件中的附加目录。

A normal Spring Boot application built from http://start.spring.io would show the templates folder within the src/main/resources. This is because by default the contents of that folder are made available to the classpath and packaged at the root of the jar file. I am guessing you modified the project in IntelliJ to also pick up the src/main/webapp directory. Unfortunately that doesn't work when it comes to packaging the jar. You would need to work with the maven resources plugin to specify the additional directory to be included in the jar file.

我的建议是转到start.spring.io。选择 Web Thymeleaf 作为依赖项,然后下载zip文件。当您查看该文件时,您将看到默认结构以及任何插件的设置以使其工作。在您的应用程序中镜像该结构。

My recommendation is to go to start.spring.io. Select Web and Thymeleaf as dependencies and then download the zip file. When you look in that file you will see the default structure along with settings for any plugins to make it work. Mirror that structure within you application.

如果您无法更改项目结构,请查看 maven-resources-plugin ,了解如何添加这些目录。

If you don't have the ability to change the project structure then check out the maven-resources-plugin for specifics on how to add those directories.

这篇关于从.jar手动启动Spring Boot应用程序无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆