简体   繁体   English

从.jar手动启动Spring Boot应用程序无法正常工作

[英]Manually launched Spring Boot application from .jar not working properly

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. 问题是当我从Intellij IDEA运行.jar文件(Spring Boot MVC应用程序)时,应用程序工作正常,但是当我尝试从命令行运行相同的文件时,模板解析器无法找到模板(.html)文件。 And this seems to be reasonable since when I opened .jar with file archiver I did not find any traces of that files. 这似乎是合理的,因为当我用文件存档器打开.jar时,我没有发现任何文件的痕迹。

How Intellij make it work proper? Intellij如何让它正常工作? And how can I do the same? 我怎么能这样做? My project structure: 我的项目结构: 在此输入图像描述

here they are, in '/templates' folder 它们位于'/ templates'文件夹中

.jar file opened in archiver: 在存档器中打开.jar文件:

在此输入图像描述

As I said, there is no .html files, so how Intellij add them when running .jar? 正如我所说,没有.html文件,所以Intellij在运行.jar时如何添加它们?

pom.xml: 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>

A normal Spring Boot application built from http://start.spring.io would show the templates folder within the src/main/resources . http://start.spring.io构建的普通Spring Boot应用程序将在src/main/resources显示templates文件夹。 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. 这是因为默认情况下,该文件夹的内容可用于类路径并打包在jar文件的根目录下。 I am guessing you modified the project in IntelliJ to also pick up the src/main/webapp directory. 我猜你在IntelliJ中修改了项目,也选择了src/main/webapp目录。 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. 您需要使用maven资源插件来指定要包含在jar文件中的其他目录。

My recommendation is to go to start.spring.io. 我的建议是去start.spring.io。 Select Web and Thymeleaf as dependencies and then download the zip file. 选择WebThymeleaf作为依赖项,然后下载zip文件。 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. 在您的应用程序中镜像该结构。

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. 如果您无法更改项目结构,请查看maven-resources-plugin以获取有关如何添加这些目录的详细信息。

You can see what exactly IntelliJ is doing, by looking at first line in its console after running app. 通过在运行app后查看其控制台中的第一行,您可以看到IntelliJ正在做什么。 There should be full command called by IntelliJ. 应该有IntelliJ调用的完整命令。

And how .jar is build? 如何构建.jar? Maven/Gradle? Maven的/摇篮? maybe there's some problem with pom/build script 也许pom / build脚本存在一些问题

It should be comment, but my reputation is too low 它应该是评论,但我的声誉太低了

Try to use the plugin to generate an executable jar: 尝试使用该插件生成可执行jar:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <executable>true</executable>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
 </plugin>

After building the jar (eg mvn clean install), you can run the jar as java -jar ./target/project.jar 构建jar之后(例如mvn clean install),你可以运行jar作为java -jar ./target/project.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM