Release idea with jar and war package

Posted by daiwa on Thu, 17 Feb 2022 08:21:29 +0100

Then declare that in the following packaging process, the scope always uses the default scope

<!-- Used to compile JSP -- >
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <!--<scope>runtime</scope>-->
        </dependency>

 

Package the spring boot project created in the previous chapter into jar package and war package

[the difference between jar and war is that the value in the < packaging > tag can be changed, as shown in the figure below]

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

OK, let's make it into a jar package first

Packaged using maven's plug-in

 

 

At this time, the jar package with the default name is packaged. Then run the jar package from the command line. Of course, you need to copy the jar package name from the target path above. [isn't it troublesome!!]

 

Then visit the jsp page, because I added printing in my method, you can see that it comes in

 

But the page cannot be accessed:

 

Then go to the jar package and check it. The reason is that the page files under webapp are not packaged in the jar package. Of course, you can't find the page by visiting [if you want to package the pages under webapp with jar package, you can: http://www.cnblogs.com/sxdcgaq8080/p/7715427.html ]

 

 

Well, since the jar package cannot be accessed successfully, package the war package: just change the jar to war

 

[Note: if you don't think it's troublesome to copy the jar package name above, the name you want to package is user-defined, which can be specified under build]

Packing into a war package

See if the name of the packaged war package is customized

 

Then run the war package

This is accessible.

 

Look under the war package. The page file has been packaged.

 

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

OK, the package is successful temporarily. Take the war package directly and put it into an external tomcat to try:

 

Start:

 

 

If not, follow the following two steps to solve the current problem that external tomcat cannot run and access:

Step 1:

Create a new SpringBootStartApplication that inherits from {SpringBootServletInitializer

Function: if deployed in an external container, you can't rely on the main function of the Application, but in a way similar to web XML file configuration to start the Spring Application context,

At this time, we need to inherit SpringBootServletInitializer in the startup class and implement the configure method. The function of this class is the same as that in web The listener responsible for initializing the Spring application context is configured in XML, which has a similar function

[note]: this new class is the same level as the startup class of springboot

 View Code

 

 

Step 2:

Modify POM XML file, there are two methods, but the ultimate goal is to make the tomcat embedded in springboot ineffective at runtime.

Choose one of the two methods!!!

Method 1:

      

POM XML file content:

<?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 support</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 Page usage jstl label</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">For compilation 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">statement spring boot Embedded tomcat The scope of does not work at runtime</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>

 

 

Well, after these two steps are completed, you can package it into a war package according to the above packaging process, and then put it under the external tomcat and start Bat can be accessed.

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

Method 2:

      

POM XML file content:

 View Code

Well, after these two steps are completed, you can package it into a war package according to the above packaging process, and then put it under the external tomcat and start Bat can be accessed.

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

Finally, start the external tomcat, and the access path in the browser is as follows:

You need to add your project name to successfully access. That's the name of the folder.

 

 

Well, so far, a springboot project can be successfully packaged and launched under the external tomcat, and the page can be accessed successfully.

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

Finally, did you notice POM In the XML file, a problem that has been struggling from the previous chapter to this chapter:

 

So far, the built-in tomcat has been eliminated

If the specified scope is provided

So

  1. In the case of start-up projects:

< scope > provided < / scope > the added items cannot be started successfully and the project cannot be accessed

If it is not added, the default scope can be used to start successfully and the project can be accessed

Access address: http://localhost:8080/view/hello

  2. When spring boot: run starts on the right:

< scope > provided < / scope > the added condition can be successfully started and the project can be accessed

If it is not added, the default scope can be used to start successfully and the project can be accessed

Access address: http://localhost:8080/view/hello

  3. When the packaged war package is started from the command line:

< scope > provided < / scope > the added condition can be successfully started and the project can be accessed

If it is not added, the default scope can be used to start successfully and the project can be accessed

Access address: http://localhost:8080/view/hello

  4. Deploy the war package to run under external tomcat:

< scope > provided < / scope > the added condition can be successfully started and the project can be accessed

If it is not added, the default scope can be used to start successfully and the project can be accessed

Access address: http://localhost:8080/firstDemo/view/hello

 

First startup mode:

 

 

The second startup mode:

 

  

The third startup mode:

 

 

The fourth startup mode:

 

 

 

Topics: Java Tomcat