1. 程式人生 > >springboot打包jar包找不到jsp檔案以及在linux永久執行java -jar以及springboot的多環境配置

springboot打包jar包找不到jsp檔案以及在linux永久執行java -jar以及springboot的多環境配置

因為springboot打包jar包執行jsp和在linux永久執行java -jar以及springboot的多環境配置 都是springboot的 就放在一起說了  

先說 打包jar 不能找到jsp問題:

打包成jar的時候會找不到jsp檔案  只需要 在pom.xml檔案新增

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<configuration>
					<encoding>UTF-8</encoding>
					<useDefaultDelimiters>true</useDefaultDelimiters>
				</configuration>
			</plugin>
		</plugins>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
			</resource>
			<resource>
				<directory>src/main/webapp</directory>
				 <!--注意此次必須要放在此目錄下才能被訪問到 -->
		         <targetPath>META-INF/resources</targetPath>
		         <includes>
		            <include>**/**</include>
		         </includes>
			</resource>
		</resources>
		<testResources>
			<testResource>
				<directory>src/test/resources</directory>
				<filtering>true</filtering>
			</testResource>
		</testResources>
	</build>


然後打包使用命令  mvn package

然後啟動jar  使用 java -jar app.jar      

在此也說一下spring 的多環境配置:

pom.xml檔案新增:

	<profiles>
		<profile>
			<id>dev</id>
			<properties>
				<profiles.active>dev</profiles.active>
				<maven.test.skip>true</maven.test.skip>
				<scope.jar>compile</scope.jar>
			</properties>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
		<profile>
			<id>test</id>
			<properties>
				<profiles.active>test</profiles.active>
				<maven.test.skip>true</maven.test.skip>
				<scope.jar>provided</scope.jar>
			</properties>
		</profile>
		<profile>
			<id>prod</id>
			<properties>
				<profiles.active>prod</profiles.active>
				<maven.test.skip>true</maven.test.skip>
				<scope.jar>provided</scope.jar>
			</properties>
		</profile>
	</profiles>

然後就可以把springboot的配置檔案分成 開發用 測試用 生產環境用的xml或yml

在打包時候 使用 java -jar app.jar --spring.profiles.active=dev  這樣就能指定開發環境springboot的配置檔案

如果在liunx上啟動jar包 使用傳統的java -jar會導致 操作之後就會關閉這個程式 

所以可以使用:nohup java -jar app.jar --spring.profiles.active=dev & 來啟動這個程式  當然要關閉這個啟動程式 需檢視埠號 

使用:ps aux|grep java 來檢視java執行的埠 最後使用 kill 1020來殺掉這個埠  這裡的 1020 是一個比如的埠號

因為比較急,寫的可能有些粗糙 如果有問題 可以留言,一起討論。