spring boot -- 打包jar,war(一)

1.打成jar

1.在工程目录下使用mvn package 命令

D:\workspace01\FarmerHome>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building FarmerHome 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ FarmerHome ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 7 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ FarmerHome ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ FarmerHome ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 7 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ FarmerHome ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.17:test (default-test) @ FarmerHome ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ FarmerHome ---
[INFO]
[INFO] --- spring-boot-maven-plugin:1.4.3.RELEASE:repackage (default) @ FarmerHome ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.502 s
[INFO] Finished at: 2017-04-25T19:27:16+08:00
[INFO] Final Memory: 24M/220M
[INFO] ------------------------------------------------------------------------
打完jar之后可以在target里面看到jar包

2.运行jar

D:\workspace01\FarmerHome\target>java -jar FarmerHome-0.0.1-SNAPSHOT-20170425.jar
04-25 19:31:54.536 [main] INFO  com.huike.boot.StartApplication - 服务开始启动了
04-25 19:31:55.322 [background-preinit] INFO  org.hibernate.validator.internal.util.Version - HV0000
01: Hibernate Validator 5.2.1.Final

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.3.RELEASE)

04-25 19:31:55.915 [main] INFO  com.huike.boot.StartApplication - No active profile set, falling bac
k to default profiles: default
04-25 19:31:56.711 [main] WARN  org.mybatis.spring.mapper.ClassPathMapperScanner - No MyBatis mapper
 was found in '[com.huike.boot]' package. Please check your configuration.
04-25 19:31:57.446 [main] INFO  org.apache.catalina.core.StandardService - Starting service Tomcat
04-25 19:31:57.449 [main] INFO  org.apache.catalina.core.StandardEngine - Starting Servlet Engine: A
pache Tomcat/8.5.6
04-25 19:31:57.526 [localhost-startStop-1] INFO  org.apache.catalina.core.ContainerBase.[Tomcat].[lo
calhost].[/] - Initializing Spring embedded WebApplicationContext
04-25 19:31:59.753 [main] INFO  org.apache.coyote.http11.Http11NioProtocol - Initializing ProtocolHa
ndler ["http-nio-8080"]
04-25 19:31:59.764 [main] INFO  org.apache.coyote.http11.Http11NioProtocol - Starting ProtocolHandle
r [http-nio-8080]
04-25 19:31:59.772 [main] INFO  org.apache.tomcat.util.net.NioSelectorPool - Using a shared selector
 for servlet write/read
04-25 19:31:59.782 [main] INFO  com.huike.boot.StartApplication - Started StartApplication in 5.128
seconds (JVM running for 5.917)
如上运行成功。

3.如果运行报错找不到依赖包的class,那是因为打jar包默认是不包括依赖的,需要在pom.xml里面配置

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

二.打成war包

1.pom.xml中,

<packaging>jar</packaging>
换成
<packaging>war</packaging>
2.添加
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
3.还需要类

package com.huike.boot;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer{

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application){
		return application.sources(StartApplication.class);
	}
}

或者直接在启动类中修改:

package com.huike.boot;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource("classpath:/spring/applicationContext.xml")
//@Configuration
@EnableAutoConfiguration
@ComponentScan("com.huike.mapper")
// @EnableEurekaServer
public class StartApplication extends SpringBootServletInitializer{

	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		log.info("服务开始启动了");
		SpringApplication app = new SpringApplication(StartApplication.class);
		super.onStartup(servletContext);
		
		
	}

	private static final Logger log = LoggerFactory.getLogger(StartApplication.class);
	
	   @Override
	    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
	        return builder.sources(StartApplication.class);
	    }
	
	public static void main(String[] args) {
		log.info("服务开始启动了");
		SpringApplication app = new SpringApplication(StartApplication.class);
//		app.addListeners(new StartApplicationListener());
		app.run(args);
	}
}


4.使用mvn package打包之后,可以在target下找到



5.把XXXXX.war放到tomcat下运行.



  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zerlinda_Li

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值