Java: Spring Boot部署可执行Jar(Maven打包),静态资源无法打包

概述

Spring Boot 在eclipse中使用内嵌服务容器启动,仅需run 启动主函数main来加载配置文件和容器初始化

问题:怎样部署Spring Boot为可执行jar

Spring Boot 使用内嵌容器来提供服务,但是当我使用maven 打可执行jar时,其无法将静态文件(html/jsp)一并打入可执行文件

java标志
image-2731

解决

使用maven resource将静态文件包含在内(注意:targetPath)

<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>
  <groupId>cn.bckf</groupId>
  <artifactId>spring-application-webapp-boot</artifactId>
  <packaging>jar</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>spring-application-webapp-boot Maven Webapp</name>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.2.RELEASE</version>
 	</parent>
	<properties>
			<main.basedir>${basedir}/../..</main.basedir>
			<m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
			 <java.version>1.8</java.version>
	        <tomcat.version>7.0.52</tomcat.version>
	</properties>
	<dependencies>
 		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<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>compile</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-loader</artifactId>
		</dependency>
		<dependency>
			<groupId>org.eclipse.jdt.core.compiler</groupId>
			<artifactId>ecj</artifactId>
			<version>4.5.1</version>
			<scope>runtime</scope>
		</dependency>
  		<dependency>
			<groupId>com.atomikos</groupId>
			<artifactId>transactions-hibernate3</artifactId>
			<version>3.7.0</version>
		</dependency>
  		<dependency>
	  		<groupId>com.atomikos</groupId>
	  		<artifactId>transactions</artifactId>
	  		<version>3.7.0</version>
  		</dependency>
	  	<dependency>
	  		<groupId>com.atomikos</groupId>
	  		<artifactId>transactions-api</artifactId>
	  		<version>3.7.0</version>
	  	</dependency>
	  	<dependency>
	  		<groupId>com.atomikos</groupId>
	  		<artifactId>transactions-jdbc</artifactId>
	  		<version>3.7.0</version>
	  	</dependency>
	  	<dependency>
	  		<groupId>com.atomikos</groupId>
	  		<artifactId>transactions-jta</artifactId>
	  		<version>3.7.0</version>
	  	</dependency>
	    <dependency>  
		    <groupId>org.hibernate</groupId>  
		    <artifactId>hibernate-validator</artifactId>  
		    <version>5.0.2.Final</version>  
		</dependency>  
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
	  		<groupId>taglibs</groupId>
	  		<artifactId>standard</artifactId>
	  		<version>1.1.2</version>
	  	</dependency>
	  	<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.1</version>
		</dependency>
		<dependency>
			<groupId>commons-pool</groupId>
			<artifactId>commons-pool</artifactId>
			<version>1.6</version>
		</dependency>
	  	<dependency>
			<groupId>org.hibernate.javax.persistence</groupId>
			<artifactId>hibernate-jpa-2.1-api</artifactId>
			<version>1.0.0.Final</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-core</artifactId>
			<version>3.2.0.RELEASE</version>
		</dependency> 	
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.7</version>
		</dependency>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
		</dependency>
		 <dependency>
	  		<groupId>jstl</groupId>
	  		<artifactId>jstl</artifactId>
	  		<version>1.1.2</version>
	  	</dependency>
  		
	</dependencies> 
	<build>
	<resources>
	            <resource>
	                <directory>${basedir}/src/main/webapp</directory>
                    <!--注意此次必须要放在此目录下才能被访问到-->
	                <targetPath>META-INF/resources</targetPath> 
	                <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.tomcat.maven</groupId>
			    <artifactId>tomcat7-maven-plugin</artifactId>
			    <version>2.2</version>			    
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<useSystemClassLoader>false</useSystemClassLoader>
				</configuration>
			</plugin>
		</plugins>
    <finalName>spring-application-webapp-boot</finalName>
  </build>
</project>

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

*

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据