zoukankan      html  css  js  c++  java
  • 运维_idea下springboot打jar、war包及部署外部tomcat

    IDEA打包

    问题一:命令行执行 java -jar xxx.jar 提示没有主类、主清单。

    问题二:命令行执行 java -jar xxx.jar 提示No auto configuration classes found in META-INF/spring.factories.

    问题三:IDEA的Terminal下执行 mvn clean package,提示 Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing
    in update mode).

    解决方案:改POM文件。

    	<!-- 指定打包方式:jar或war -->
    	<packaging>war</packaging>
    
    	<dependencies>
            <!-- 设置内嵌的tomcat容器只在编译和测试的时候用,以便部署到自己已有的tomcat容器中 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>        
    	</dependencies>
    
    	<build>
            <!-- 指定打包名称 -->
            <finalName>ups</finalName>
            <plugins>
                <!-- 解决:命令行执行 java -jar xxx.jar 提示没有主类、主清单。 -->
                <!-- 解决:命令行执行 java -jar xxx.jar 提示No auto configuration classes found in META-INF/spring.factories. -->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <fork>true</fork>
                        <!-- 启动类全路径 -->
                        <mainClass>com.maiam.ups.Ups8081</mainClass>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <!-- 打war包时需要 -->
                <!-- 解决:IDEA的Terminal下执行 mvn clean package,提示 Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing
    in update mode). -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    

    将 SpringBoot 项目部署外部 Tomcat,除了改POM文件外,启动类也需要稍作调整。

    /**
     * SpringBoot应用部署到自己已有的tomcat容器中,
     * 需要让启动类继承SpringBootServletInitializer,
     * 并重写configure方法。
     */
    @SpringBootApplication(scanBasePackages = {
            "com.maiam.web",
            "com.maiam.ups"
    },exclude = {DataSourceAutoConfiguration.class})
    public class Ups8081 extends SpringBootServletInitializer {
        public static void main(String[] args) {
            SpringApplication.run(Ups8081.class,args);
        }
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
            return builder.sources(Ups8081.class);
        }
    }
    

    重新打包(如果有父子工程,多模块依赖,install时需要先install它们到本地仓库)

    mvn clean package
    

    测试打包是否正常

    java -jar ups.war
    

    浏览器访问...

    将 war包 部署到 tomcat

    1、将 war包 复制到 tomcat 的 webapps 目录下。

    2、修改 conf/server.xml

    <!-- 生产环境,port="8005" shutdown="SHUTDOWN"需要改掉,webapps目录下的文件都删掉 -->
    <Server port="8005" shutdown="SHUTDOWN">
      <Service name="Catalina">  
        <!-- 默认端口号8080 -->  
        <Connector port="8080" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443" /> 
        <Engine name="Catalina" defaultHost="localhost">
          <!-- 如果war包放在webapps下,会自动解压(unpackWARs="true")、自动部署(autoDeploy="true") -->  
          <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
            <!-- 加入以下这行配置 -->  
            <!-- path表示浏览器访问的地址,docBase表示war包的位置 -->
    		<Context path="/" docBase="ups" reloadable="true" debug="0" crossContext="true"/>
          </Host>
        </Engine>
      </Service>
    </Server>
    

    3、修改 conf/logging.properties (可选,解决启动Tomcat时的中文乱码)

    ​ 将以下几行,UTF-8改为GBK

    1catalina.org.apache.juli.AsyncFileHandler.encoding = GBK
    
    2localhost.org.apache.juli.AsyncFileHandler.encoding = GBK
    
    3manager.org.apache.juli.AsyncFileHandler.encoding = GBK
    
    4host-manager.org.apache.juli.AsyncFileHandler.encoding = GBK
    
    java.util.logging.ConsoleHandler.encoding = GBK
    

    4、启动 bin/startup.bat

    5、查看启动日志,浏览器访问...

  • 相关阅读:
    Codeforces 992C(数学)
    Codeforces 990C (思维)
    Codeforces 989C (构造)
    POJ 1511 Invitation Cards(链式前向星,dij,反向建边)
    Codeforces 1335E2 Three Blocks Palindrome (hard version)(暴力)
    POJ 3273 Monthly Expense(二分)
    POJ 2566 Bound Found(尺取前缀和)
    POJ 1321 棋盘问题(dfs)
    HDU 1506 Largest Rectangle in a Histogram(单调栈)
    POJ 2823 Sliding Window(单调队列)
  • 原文地址:https://www.cnblogs.com/maiaimei/p/14219265.html
Copyright © 2011-2022 走看看