springboot打包成jar 运行jsp页面
阅读原文时间:2021年04月20日阅读:1

启动命令 java -Xms1024m -Xmx2G -XX:PermSize=1024m -XX:MaxPermSize=1024m -jar jlifereptileserver.jar

Xms1024m -Xmx2G 中 Xms要小于Xmx,不能相等,如果相等,容易内存溢出

试了很多方案,总结有2种

参考,谢谢:

https://blog.csdn.net/b43505274/article/details/80560049

https://blog.csdn.net/qq_41016552/article/details/90666782

1,spring-boot-maven-plugin 指定版本为1.4.2.RELEASE ,好像只有这个版本支持jsp,高版本不行,

2,打成war包,有点复查

(1)先按照原先插件的方式(任何版本不包括1.4.2版本)打包打成jar包

(2)解压jar包

(3)将 META-INF/resources/WEB-INF 复制到 /WEB-INF目录下(必须在顶层)

(4)重新打成war包(实际是jar包,只是改了个名字)

项目结构

具体操作的maven配置,注意Start-Class为启动类

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.jlife.reptile</groupId>
    <artifactId>jlifereptileserver</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>jlifereptileserver Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
        </dependency>
        <!-- <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> 
            <version>2.1.0</version> </dependency> <dependency> <groupId>de.codecentric</groupId> 
            <artifactId>spring-boot-admin-server-ui</artifactId> <version>2.1.0</version> 
            </dependency> -->

        <!-- <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency> -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> 
            <version>1.3.2</version> </dependency> -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!-- servlet 依赖. -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- jasper -->
        <dependency>
            <groupId>org.springframework.boot</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>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>



        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.44</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>2.1.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <build>
        <finalName>jlifereptileserver</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>

            <resource>
                <directory>src/main/webapp</directory>
                 <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/**</include>
                </includes>
                <filtering>false</filtering>
            </resource>

        </resources>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!--如果要用这个版本,那么就不用解压再打成war了 <version>1.4.2.RELEASE</version> -->
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <compilerArguments>
                        <verbose />
                        <bootclasspath>${env.JAVA_HOME}/jre/lib/rt.jar</bootclasspath>
                        <extdirs>${project.basedir}/src/main/webapp/WEB-INF/lib</extdirs>
                    </compilerArguments>
                </configuration>
            </plugin>


            <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <configuration>
                <tasks>
                    <!--1、将 spring-boot 打包后的 jar 解压-->
                    <unzip
                            src="${project.build.directory}/${project.build.finalName}.${project.packaging}"
                            dest="${project.build.directory}/${project.name}"/>

                    <!-- 2、将 META-INF/resources/WEB-INF 复制到 /WEB-INF-->
                    <copy todir="${project.build.directory}/${project.name}/WEB-INF">
                        <fileset dir="${project.build.directory}/${project.name}/META-INF/resources/WEB-INF/"/>
                    </copy>

                     <!--  3、重新打包为 war -->
                    <jar destfile="${project.build.directory}/${project.name}-${project.version}.war"
                         basedir="${project.build.directory}/${project.name}"
                         compress="false">
                        <manifest>
                            <attribute name="Main-Class" value="org.springframework.boot.loader.JarLauncher"/>
                            <attribute name="Start-Class" value="com.jzlife.syseureka.SysAdminApplication"/>
                            <attribute name="Spring-Boot-Lib" value="WEB-INF/lib/"/>
                        </manifest>
                    </jar>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin> 

        </plugins>
    </build>

</project>


spring:
  application: 
    name: adminserver
  profiles:
    active: dev
  mvc:
    view:
      prefix: /WEB-INF/jsp/
      suffix: .jsp

server:
  port: 8085

如果页面打不开报错500,要加配置

package com.jzlife.servantapplet.config.mvc;

import java.util.List;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfigurerImpl implements WebMvcConfigurer {

    @Bean
    public WebInterceptor webInterceptor() {
        return new WebInterceptor();
    }

//    @Bean
//    public MyArgumentResolver myArgumentResolver() {
//        return new MyArgumentResolver();
//    }

    // 定义sourciId
//    @Override
//    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
//        // TODO Auto-generated method stub
//        resolvers.add(myArgumentResolver());
//    }

    // 拦截器
    public void addInterceptors(InterceptorRegistry registry) {
        // 增加一个拦截器
        registry.addInterceptor(webInterceptor()).addPathPatterns("/**")
        .excludePathPatterns("/swagger-resources/**","/webjars/**", "/v2/**", "/swagger-ui.html/**"
            ,"/index.html","/pagers/**" );
    }

    // 静态资源
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Bean
    public FilterRegistrationBean httpServletRequestReplacedRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new BodyReaderRequestFilter());
        registration.addUrlPatterns("/*");
        registration.addInitParameter("paramName", "paramValue");
        registration.setName("BodyReaderRequestFilter");
        registration.setOrder(1);
        return registration;
    }
}