天天看點

sts-springboot2.1-maven打包部署到伺服器

好長一段時間沒有寫貼了,如果有人問我這段時間去哪裡了?

咳咳,一言難盡,最近天氣冷了,

我的被子病了,我都在請假照顧他

sts-springboot2.1-maven打包部署到伺服器

我的環境:sts編譯器,springboot2.0架構,使用maven打包部署到伺服器運作;

打包前準備:

1、項目能正常的跑!

2、注釋熱部署,免得無限重新開機

3、洗手(這一步很重要)

4、修改pom.xml 的部分代碼(精華)

——————————————————

這種文章很多,但是我找了7,8個都沒有解決我的問題,于是在群裡求助了一番,

A+大佬貼了一段代碼幫助了我,在此感謝A+大佬仗義相助!

這是我原來的pom.xml代碼,打包最常見的報錯:找不到主類,但是我加了還是不行!

<build>	
        <plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
				<mainClass>com.example.demo.xxxApplication</mainClass>
				<skipTests>true</skipTests>  
				<fork>true</fork>
				</configuration>
				
			</plugin>
		</plugins>
	</build>
           

用大佬的代碼:

先注釋熱部署,然後注釋我原來的配置;設定這一段,

<build>
<!--  	<plugins>
          <!--   maven 打包 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <!--    這個地方應該指定你實際Main方法所在的類 -->
                            <mainClass>com.example.demo.xxxApplication</mainClass>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>./</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>

           <!--    拷貝依賴的jar包到lib目錄 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.directory}/lib
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

          <!--     将資源檔案放到jar外面 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <encoding>UTF-8</encoding>
                        <!--       表示把配置檔案拷到和jar包同一個路徑下 -->
                            <outputDirectory>${project.build.directory}/config</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/resources/</directory>
                                <!--       指定不需要處理的資源 -->
                                    <excludes>
                                        <exclude>*.xml</exclude>
                                        <exclude>static/</exclude>
                                        <exclude>templates/</exclude>
                                        <exclude>rsa/</exclude>
                                    </excludes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
	</plugins>
	</build>
           

如果用mybatis 報缺包的錯誤提示,自己補上去:application.properties

mybatis.mapper-locations=classpath:mapper/*.xml 
           

見證奇迹:

先清理一下緩存,再打包

sts-springboot2.1-maven打包部署到伺服器
sts-springboot2.1-maven打包部署到伺服器

進入相應的目錄 :java -jar xxx.jar 

sts-springboot2.1-maven打包部署到伺服器

(如果還有疑問歡迎加群交流,如果問我為什麼不更新springboot2的文章了,

咳咳,失蹤這段時間換了好多份工作,最近才穩定下來,有空寫部落格;

不過我可是發文說要回複部落格跟貼吧更新的)