繁体   English   中英

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

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

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

Intellij如何让它正常工作? 我怎么能这样做? 我的项目结构: 在此输入图像描述

它们位于'/ templates'文件夹中

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

在此输入图像描述

正如我所说,没有.html文件,所以Intellij在运行.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构建的普通Spring Boot应用程序将在src/main/resources显示templates文件夹。 这是因为默认情况下,该文件夹的内容可用于类路径并打包在jar文件的根目录下。 我猜你在IntelliJ中修改了项目,也选择了src/main/webapp目录。 不幸的是,在包装罐子时这不起作用。 您需要使用maven资源插件来指定要包含在jar文件中的其他目录。

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

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

通过在运行app后查看其控制台中的第一行,您可以看到IntelliJ正在做什么。 应该有IntelliJ调用的完整命令。

如何构建.jar? Maven的/摇篮? 也许pom / build脚本存在一些问题

它应该是评论,但我的声誉太低了

尝试使用该插件生成可执行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>

构建jar之后(例如mvn clean install),你可以运行jar作为java -jar ./target/project.jar

暂无
暂无

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

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