【问题标题】:Package a spring boot application including JSPs and static resources打包一个包含 JSP 和静态资源的 Spring Boot 应用程序
【发布时间】:2014-04-03 00:49:50
【问题描述】:

我想将 spring-boot 应用程序打包为 jar,我使用 mvn package 进行此操作。

这会生成一个不包含任何/WEB-INF/jsp/src/main/webapp/resources 的jar。

如何确保我的 jar 包含所需的一切?

这里是我当前的pom.xml

<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-samples</artifactId>
    <version>1.0.0.RC3</version>
</parent>

<packaging>jar</packaging>

<properties>
    <main.basedir>${basedir}/../..</main.basedir>
    <m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
</properties>

<dependencies>

    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jdbc</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.3-1101-jdbc41</version>
    </dependency>

</dependencies>

<!-- Package as an executable JAR -->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <useSystemClassLoader>false</useSystemClassLoader>
            </configuration>
        </plugin>
    </plugins>
</build>

<!-- Allow access to Spring milestones and snapshots -->
<!-- (you don't need this if you are using anything after 1.0.0.RELEASE) -->
<repositories>
    <repository>
        <id>spring-snapshots</id>
        <url>http://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <url>http://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <url>http://repo.spring.io/snapshot</url>
    </pluginRepository>
    <pluginRepository>
        <id>spring-milestones</id>
        <url>http://repo.spring.io/milestone</url>
    </pluginRepository>
</pluginRepositories>
<artifactId>com.example.app</artifactId>

【问题讨论】:

    标签: java spring maven spring-mvc spring-boot


    【解决方案1】:

    你有什么理由不能使用战争包装类型吗? https://maven.apache.org/plugins/maven-war-plugin/usage.html 我建议使用 war 打包类型并使用默认的 maven web-application 结构。

    如果你真的想为你的 webapp 使用 jar 插件,你需要为你的项目配置它。由于您的帖子,我不了解您的结构,无法给您举个例子。 jar插件的使用在这里查看:https://maven.apache.org/plugins/maven-war-plugin/usage.html

    【讨论】:

    • 您需要使用战争包装,否则一切看起来都很正常。请参阅 JSP 限制中的 docs
    • 我尝试过使用战争包装,它可以工作。 java -jar myfile.war 看起来有点奇怪,但它确实有效。谢谢!
    【解决方案2】:

    要使用 Spring Boot 创建可运行的 JAR,请在 pom.xml 中使用 spring-boot-maven-plugin

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

    也许你想看看我的示例应用程序http://info.michael-simons.eu/2014/02/20/developing-a-web-application-with-spring-boot-angularjs-and-java-8/(源代码在 GitHub 上,应用程序是实时的并从 JAR 运行)。

    您应该注意的一件事:由于一些嵌入式 Tomcat 问题,来自 JAR 的 JSP 不起作用。我正在使用百里香叶。如果您需要 JSP,请继续使用 WAR 部署。

    【讨论】:

      【解决方案3】:

      以下示例适用于 Spring Boot 1.3.3.RELEASE: https://github.com/ghillert/spring-boot-jsp-demo

      关键是将静态jsp内容放入:

      /src/main/resources/META-INF/resources/WEB-INF/jsp
      

      并确保在 application.properties 中定义视图前缀/后缀:

      spring.mvc.view.prefix=/WEB-INF/jsp/
      spring.mvc.view.suffix=.jsp
      

      【讨论】:

      • 为什么没有教程提到这个?
      【解决方案4】:

      将您的构建标签更改为

       <build>
      <resources>
                  <resource>
                      <directory>${basedir}/src/main/webapp</directory>
                      <includes>
                          <include>**/**</include>
                      </includes>
                  </resource>
                  <resource>
                      <directory>${basedir}/src/main/resources</directory>
                      <includes>
                          <include>**/**</include>
                      </includes>
                  </resource>
              </resources>
              <plugins>
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                  </plugin>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-surefire-plugin</artifactId>
                      <configuration>
                          <useSystemClassLoader>false</useSystemClassLoader>
                      </configuration>
                  </plugin>
              </plugins>
          </build>
      

      【讨论】:

        【解决方案5】:

        我遇到了和你一样的问题。我尝试了上面您标记为正确的答案,但它对我不起作用。

        这有效...将 pom.xml 更改为...

        <packaging>war</packaging>
        

        ...这将导致 maven 创建一个可执行的 WAR 文件,如下所示...

        java -jar yourwarfile.war
        

        我在这里找到了这个类似问题的解决方案...

        WEB-INF not included in WebApp using SpringBoot, Spring-MVC and Maven

        【讨论】:

          猜你喜欢
          • 2018-04-24
          • 2019-07-02
          • 2023-03-31
          • 1970-01-01
          • 2014-09-15
          • 2022-12-04
          • 2016-10-01
          • 2018-07-31
          • 1970-01-01
          相关资源
          最近更新 更多