springboot+maven+tomcat問題

千狼發表於2017-11-06
版權宣告:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/qq_26654727/article/details/78456345

問題背景

版本介紹

jdk1.7 、springboot:1.3.1.RELEASE、tomcat8 、maven3

解決過程

1. 搭建Springboot+jsp專案。搭建Springboot專案之時,標準配置:
<modelVersion>4.0.0</modelVersion>
<artifactId>manage</artifactId>
<!--<version>1.0-SNAPSHOT</version>-->
<!-- 打war包,不使用springboot內建tomcat-->
<packaging>war</packaging> 

<dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.3.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
</dependencyManagement>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!--移除內建tomcat,打war包-->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
                <!--<exclusion>-->
                    <!--<groupId>ch.qos.logback</groupId>-->
                    <!--<artifactId>logback-classic</artifactId>-->
                <!--</exclusion>-->
            </exclusions>
        </dependency>
        <!--Springboot的監控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- Spring Boot Test 依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
         <!--spring-boot-configuration:spring boot 配置處理器; -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
         <!--關於embed的容器 使用jsp時,必須的內容-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <properties>
        <!-- 這裡指定專案編碼 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.4.0.RELEASE</version>
            </plugin>
            <plugin>
                <!-- maven打包的時候告訴maven不需要web.xml,否剛會報找不到web.xml錯誤 -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                   <!--指定maven的編譯器版本-->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
2. 在maven打包過程中出現了,有關ch.qos.logback版本的問題,而導致tomcat啟動失敗,故在pom檔案中新增限制其版本
<!--logback版本問題導致tomcat啟動失敗-->
        <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.3</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.1.3</version>
            <scope>provided</scope>
        </dependency>
3.本地啟動測試沒問題,放至centos6.5進行測試,手動啟動linux上同版本tomcat,tomcat報錯:

org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.

問題來源:
使用了CKEditor和CKFinder後,在lib裡新增了很多jar包,開啟相應頁面出現以上問題。
org.apache.jasper.compiler.TldLocationsCache tldScanJar
資訊: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
(1)有人說是çé®é¢ï¼å¯æ¯æç页é¢éæ ¹æ¬å°±æ²¡æ
(2)還有說,修改${TOMCAT_HOME}/bin/catalina.sh或${TOMCAT_HOME}/bin/catalina.bat檔案,可是tomcat目錄下沒有這兩個檔案。
(3)還有說,調整${tomcat}/conf/catalina.properties,將提示的jar新增到不掃描清單中。沒有試,而是通過下面的方法解決了。
最終解決:
修改$CATALINA_BASE/conf/catalina.properties檔案,新增org.apache.el.parser.SKIP_IDENTIFIER_CHECK=true選項。

4.上述問題解決之後發現tomcat的catalina.out雖然正常執行,並且未出現任何問題。但任然無法訪問頁面,通過localhost_access_log.txt日誌檔案返回的結果都是404,但tomcat主介面卻可以正常訪問。

問題原因:
linux系統jdk版本與本地版本不一致,本地版本在除錯過程中無意中設定成1.8.並且將maven編譯器jdk編輯版本設定成1.8.導致兩地版本不一致導致。

                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>

5.本除錯過程中最大的問題:

本地啟動正常,linux啟動報錯如下:

06-Nov-2017 10:48:12.336 SEVERE [localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start: 
 org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/nrsmanage]]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:162)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:753)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:729)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:976)
        at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1853)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.slf4j.helpers.NOPLoggerFactory loaded from file:/opt/nrs/apache-tomcat-8.0.47/webapps/nrsmanage/WEB-INF/lib/slf4j-api-1.7.21.jar). If you are using WebLogic you will need to add `org.slf4j` to prefer-application-packages in WEB-INF/weblogic.xml Object of class [org.slf4j.helpers.NOPLoggerFactory] must be an instance of class ch.qos.logback.classic.LoggerContext

考慮到之前有關springboot對於不同版本日誌檔案衝突導致啟動失敗的問題,故一致在本地進行關於日誌工具版本的除錯,都未有結果,後靜下心再考慮該日誌檔案所說內容,隨後把該web專案lib包中有關logback相關的包進行刪除,tomcat正常啟動,並可以正常訪問。
在本地服務中,可以在pom檔案中的相關包設定

<scope>provided</scope>   該標籤表示該包的級別,表示僅在編譯級別支援。打包和執行過程中不含有此包。

總結

1.必須時刻記住版本的重要性,以及對於tomcat原理、maven原理、以及springboot原理的深究。
2.不要盲目的相信各種解決辦法,因為不管是誰的解決方法都是在特定情況下發生的,我可以借鑑他的方法,但必須要考慮清楚本地問題的原因。想清楚解決方案,在進行操作,並注意做好備份,以及對自己各種操作過程要有個記錄,可以做好 Rollback操作。