idea下springboot打包成jar包和war包,并且可以在外部tomcat下运行访问到

 接着上一章走呗:http://www.cnblogs.com/sxdcgaq8080/p/7712874.html

然后声明一点,下面打包的过程中,scope一直都是使用默认的范围

<!--用于编译jsp-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <!--<scope>runtime</scope>-->
        </dependency>

 

把上一章创建好的spring boot项目打包成jar包和war包

【jar和war的区别就是:<packaging>标签中的值更改就行 如下图】

=====================================================================================================================================

好了,这里先打成jar包

 使用maven的插件打包

 

 

 此时打包出来是有默认的名称的jar包。然后在命令行下  运行起这个jar包。当然,jar包名字你需要去上面target路径下去拷贝一下。【是不是很麻烦!!】

 

 然后访问一下jsp页面,因为我方法中加了打印,可以看到进来了

 

 但是页面访问不到:

 

 然后去jar包中查看一下,是因为jar包中没有把webapp下的页面文件等打包进去,当然访问找不到页面了【如果你想jar包也可以打包webapp下的页面的话,可以:http://www.cnblogs.com/sxdcgaq8080/p/7715427.html】

 

 

好吧,jar包既然打包不成功访问,那就打包war包:就把jar改成war就好了

 

【注意:如果嫌弃上面需要去拷贝jar包名字麻烦,想打包出来的名字是自定义的,在build下 规定即可】

在打包成war包

 看看 打包出来的war包名字是不是自定义的

 

 然后再运行war包

 这下是可以访问到的了。

 

 看看war包下,是已经将页面文件打包进去了。

 

=======================================================================================================================================

好了 打包暂时打包成功了,直接拿着上面的war包放到一个外部的tomcat中跑起来试一下:

 

启动一下:

 

 

不成功,按照下面 两步走,解决当前这个在外部tomcat没办法运行起来并访问的问题:

第一步:

   新建一个SpringBootStartApplication 继承自 SpringBootServletInitializer

 作用:因为在外部容器部署的话,就不能依赖于Application的main函数了,而是要以类似于web.xml文件配置的方式来启动Spring应用上下文,

    此时我们需要在启动类中继承SpringBootServletInitializer并实现configure方法,这个类的作用与在web.xml中配置负责初始化Spring应用上下文的监听器作用类似

【注意】:这个新建的类,与springboot的启动类是同级的

package com.sxd;

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

public class SpringBootStartApplication extends SpringBootServletInitializer{

@Override
</span><span style="color: #0000ff">protected</span><span style="color: #000000"> SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    </span><span style="color: #0000ff">return</span> application.sources(FirstdemoApplication.<span style="color: #0000ff">class</span><span style="color: #000000">);
}

}

View Code

 

 

第二步:

   修改pom.xml文件,这里有两种方法,但是最终的目的都是让springboot内嵌的tomcat在运行时不起作用而已。

   两种方法任选一种!!!

   方法1:

      

此时的pom.xml文件内容:   

<?xml version="1.0" encoding="UTF-8"?>
<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>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>com.sxd<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>firstdemo<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>0.0.1-SNAPSHOT<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">packaging</span><span style="color: #0000ff">&gt;</span>war<span style="color: #0000ff">&lt;/</span><span style="color: #800000">packaging</span><span style="color: #0000ff">&gt;</span>

<span style="color: #0000ff">&lt;</span><span style="color: #800000">name</span><span style="color: #0000ff">&gt;</span>firstdemo<span style="color: #0000ff">&lt;/</span><span style="color: #800000">name</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">description</span><span style="color: #0000ff">&gt;</span>Demo project for Spring Boot<span style="color: #0000ff">&lt;/</span><span style="color: #800000">description</span><span style="color: #0000ff">&gt;</span>

<span style="color: #0000ff">&lt;</span><span style="color: #800000">parent</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.springframework.boot<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>spring-boot-starter-parent<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>1.5.8.RELEASE<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">relativePath</span><span style="color: #0000ff">/&gt;</span> <span style="color: #008000">&lt;!--</span><span style="color: #008000"> lookup parent from repository </span><span style="color: #008000">--&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">parent</span><span style="color: #0000ff">&gt;</span>

<span style="color: #0000ff">&lt;</span><span style="color: #800000">properties</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">project.build.sourceEncoding</span><span style="color: #0000ff">&gt;</span>UTF-8<span style="color: #0000ff">&lt;/</span><span style="color: #800000">project.build.sourceEncoding</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">project.reporting.outputEncoding</span><span style="color: #0000ff">&gt;</span>UTF-8<span style="color: #0000ff">&lt;/</span><span style="color: #800000">project.reporting.outputEncoding</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">java.version</span><span style="color: #0000ff">&gt;</span>1.8<span style="color: #0000ff">&lt;/</span><span style="color: #800000">java.version</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">properties</span><span style="color: #0000ff">&gt;</span>

<span style="color: #0000ff">&lt;</span><span style="color: #800000">dependencies</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000">web支持</span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.springframework.boot<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>spring-boot-starter-web<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000">jsp页面使用jstl标签</span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>javax.servlet<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>jstl<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>

    <span style="color: #008000">&lt;!--</span><span style="color: #008000">用于编译jsp</span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.apache.tomcat.embed<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>tomcat-embed-jasper<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #008000">&lt;!--</span><span style="color: #008000">&lt;scope&gt;provided&lt;/scope&gt;</span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000">声明spring boot内嵌tomcat的作用范围  在运行时不起作用</span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.springframework.boot<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>spring-boot-starter-tomcat<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">scope</span><span style="color: #0000ff">&gt;</span>provided<span style="color: #0000ff">&lt;/</span><span style="color: #800000">scope</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>

    <span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.springframework.boot<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>spring-boot-starter-test<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">scope</span><span style="color: #0000ff">&gt;</span>test<span style="color: #0000ff">&lt;/</span><span style="color: #800000">scope</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependencies</span><span style="color: #0000ff">&gt;</span>

<span style="color: #0000ff">&lt;</span><span style="color: #800000">build</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">plugins</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">plugin</span><span style="color: #0000ff">&gt;</span>
            <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.springframework.boot<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
            <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>spring-boot-maven-plugin<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">plugin</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">plugins</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">finalName</span><span style="color: #0000ff">&gt;</span>firstDemo<span style="color: #0000ff">&lt;/</span><span style="color: #800000">finalName</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">build</span><span style="color: #0000ff">&gt;</span>

</project>

View Code

 

好了 这两步完成,就可以按照上面的打包过程,打包为war包,然后放在外部的tomcat下,start.bat就可以访问了。

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

   方法2:

      

此时的pom.xml文件内容:

<?xml version="1.0" encoding="UTF-8"?>
<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>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>com.sxd<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>firstdemo<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>0.0.1-SNAPSHOT<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">packaging</span><span style="color: #0000ff">&gt;</span>war<span style="color: #0000ff">&lt;/</span><span style="color: #800000">packaging</span><span style="color: #0000ff">&gt;</span>

<span style="color: #0000ff">&lt;</span><span style="color: #800000">name</span><span style="color: #0000ff">&gt;</span>firstdemo<span style="color: #0000ff">&lt;/</span><span style="color: #800000">name</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">description</span><span style="color: #0000ff">&gt;</span>Demo project for Spring Boot<span style="color: #0000ff">&lt;/</span><span style="color: #800000">description</span><span style="color: #0000ff">&gt;</span>

<span style="color: #0000ff">&lt;</span><span style="color: #800000">parent</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.springframework.boot<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>spring-boot-starter-parent<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>1.5.8.RELEASE<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">relativePath</span><span style="color: #0000ff">/&gt;</span> <span style="color: #008000">&lt;!--</span><span style="color: #008000"> lookup parent from repository </span><span style="color: #008000">--&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">parent</span><span style="color: #0000ff">&gt;</span>

<span style="color: #0000ff">&lt;</span><span style="color: #800000">properties</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">project.build.sourceEncoding</span><span style="color: #0000ff">&gt;</span>UTF-8<span style="color: #0000ff">&lt;/</span><span style="color: #800000">project.build.sourceEncoding</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">project.reporting.outputEncoding</span><span style="color: #0000ff">&gt;</span>UTF-8<span style="color: #0000ff">&lt;/</span><span style="color: #800000">project.reporting.outputEncoding</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">java.version</span><span style="color: #0000ff">&gt;</span>1.8<span style="color: #0000ff">&lt;/</span><span style="color: #800000">java.version</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">properties</span><span style="color: #0000ff">&gt;</span>

<span style="color: #0000ff">&lt;</span><span style="color: #800000">dependencies</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000">web支持</span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.springframework.boot<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>spring-boot-starter-web<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">exclusions</span><span style="color: #0000ff">&gt;</span>
            <span style="color: #0000ff">&lt;</span><span style="color: #800000">exclusion</span><span style="color: #0000ff">&gt;</span>
                <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.springframework.boot<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
                <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>spring-boot-starter-tomcat<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
            <span style="color: #0000ff">&lt;/</span><span style="color: #800000">exclusion</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">exclusions</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000">jsp页面使用jstl标签</span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>javax.servlet<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>jstl<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>

    <span style="color: #008000">&lt;!--</span><span style="color: #008000">用于编译jsp</span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.apache.tomcat.embed<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>tomcat-embed-jasper<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #008000">&lt;!--</span><span style="color: #008000">&lt;scope&gt;provided&lt;/scope&gt;</span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>


    <span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.springframework.boot<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>spring-boot-starter-test<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">scope</span><span style="color: #0000ff">&gt;</span>test<span style="color: #0000ff">&lt;/</span><span style="color: #800000">scope</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependencies</span><span style="color: #0000ff">&gt;</span>

<span style="color: #0000ff">&lt;</span><span style="color: #800000">build</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">plugins</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">plugin</span><span style="color: #0000ff">&gt;</span>
            <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.springframework.boot<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
            <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>spring-boot-maven-plugin<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">plugin</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">plugins</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">finalName</span><span style="color: #0000ff">&gt;</span>firstDemo<span style="color: #0000ff">&lt;/</span><span style="color: #800000">finalName</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">build</span><span style="color: #0000ff">&gt;</span>

</project>

View Code

好了 这两步完成,就可以按照上面的打包过程,打包为war包,然后放在外部的tomcat下,start.bat就可以访问了。

========================================================================================================================================

最后 启动起来外部的tomcat ,在浏览器中访问路径如下:

需要加上你的项目名字,才能成功访问。也就是这个文件夹的名字。

 

 

好了,到这里为止,终于能将一个springboot项目成功的打包并且放在外部的tomcat下启动并且能够成功访问到页面了。

========================================================================================================================

最后的最后,你是否注意到pom.xml文件中 ,上一个章节到这一章节一直在纠结的一个问题:

 

到现在为止,已经剔除了内置的tomcat的情况下

如果指定scope范围为provided的话

那么  

  1.启动类启动项目的情况下:

    <scope>provided</scope>添加的情况,不能成功启动,项目不能访问

    不添加的情况,使用默认的scope,可以成功启动,项目可以访问

    访问地址:http://localhost:8080/view/hello

  2.在右侧spring-boot:run启动的情况下:

    <scope>provided</scope>添加的情况,可以成功启动,项目可以访问

    不添加的情况,使用默认的scope,可以成功启动,项目可以访问

    访问地址:http://localhost:8080/view/hello

  3.在命令行启动打包的war包情况下:

     <scope>provided</scope>添加的情况,可以成功启动,项目可以访问

    不添加的情况,使用默认的scope,可以成功启动,项目可以访问

    访问地址:http://localhost:8080/view/hello

  4.将war包部署在外部tomcat下运行的情况下:

    <scope>provided</scope>添加的情况,可以成功启动,项目可以访问

    不添加的情况,使用默认的scope,可以成功启动,项目可以访问

    访问地址:http://localhost:8080/firstDemo/view/hello

 

第一种启动方式:

 

 

第二种启动方式:

 

  

第三种启动方式:

 

 

第四种启动方式:

 

 

 

 

 

 

    

 

posted @ 2018-05-15 17:57  星朝  阅读(37030)  评论(3编辑  收藏  举报