maven多環境配置打包分析

maven+springboot打包

在spring打包時其實是用的maven的環境參數,springboot默認有自己的環境配置,springboot默認找自己路勁下的配置文件
具體看org.springframework.boot.context.config.ConfigFileApplicationListener.Loader#loadForFileExtension類中的加載和實現
maven的相關操作是將文件打包到springboot需要的路徑下,這樣可以在不同的環境下可以使用不同的配置文件。
具體的maven配置如下

<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <spring.atcive>dev</spring.atcive>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>pro</id>
            <properties>
                <spring.atcive>pro</spring.atcive>
            </properties>
        </profile>
    </profiles>
    <build>
        <resources>
            <resource>
               <!--指定資源路徑-->
                <directory>${basedir}/profiles</directory>
                <!--這個聲明是否需要替換-->
                <filtering>true</filtering>
                <includes>
                   <!--指定打包時候要包含的文件,這裏的變量在打包時候會被替換-->
                    <include>application-${spring.atcive}.yml</include>
                </includes>
                <targetPath>${basedir}/target/classes</targetPath>
            </resource>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                   <!--指定打包要排除的文件-->
                    <exclude>application.yml</exclude>
                </excludes>
                <includes>
                	<!--指定打包要包含的文件-->
                    <include>bootstrap.yml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

如果開發工具可以選擇對應的profile項,如果是用命令執行可以參考如下maven命令, -P pro就是告訴maven要使用名字爲pro的profile來打包,即所有的配置文件都使用生產環境(prod是自己定義的,在這裏自定義爲生產環境)。

//指定打包生產環境配置
 mvn clean package -Dmaven.test.skip=true -P prod

maven結合springboot的profile功能

maven支持profile功能,當使用maven profile打包時,可以打包指定目錄和指定文件,且可以修改文件中的變量。spring boot也支持profile功能,只要在application.properties文件中指定spring.profiles.active=xxx 即可,其中xxx是一個變量,當maven打包時,修改這個變量即可。

常見問題

打包後的jar不能執行

配置打包插件

<plugins>
<!-- Spring boot maven 打包插件 -->
	<plugin>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-maven-plugin</artifactId>
	</plugin>
</plugins>

clean package之後
執行啓動java -jar project.jar時候報錯說“沒有主清單屬性”,調整項目的打包

<plugins>
<!-- Spring boot maven 打包插件 -->
	<plugin>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-maven-plugin</artifactId>
		<!--repackage:創建一個自動可執行的jar或war文件。它可以替換常規的artifact,或者用一個單獨的classifier附屬在maven構建的生命週期中。-->
		<executions>
			<execution>
				<goals>
					<goal>repackage</goal>
				</goals>
			</execution>
		</executions>
	</plugin>
</plugins>
執行clean package

打包後的編譯文件不是自己想要的

這個可以調整maven的配置,看看是不是自己沒有完全理解配置文件中的配置的意思。

<build>
        <resources>
            <resource>
               <!--指定資源路徑-->
                <directory>${basedir}/profiles</directory>
                <!--這個聲明是否需要替換-->
                <filtering>true</filtering>
                <includes>
                   <!--指定打包時候要包含的文件,這裏的變量在打包時候會被替換-->
                    <include>application-${spring.atcive}.yml</include>
                </includes>
                <targetPath>${basedir}/target/classes</targetPath>
            </resource>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                   <!--指定打包要排除的文件-->
                    <exclude>application.yml</exclude>
                </excludes>
                <includes>
                	<!--指定打包要包含的文件-->
                    <include>bootstrap.yml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

maven+普通spring項目

打包爲war包,操作和上述的一樣。

<build>
        <finalName>cn-appoint</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!--maven打包的插件 start-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <archiveClasses>true</archiveClasses>
                </configuration>
            </plugin>
            <!--maven打包的插件 end-->
        </plugins>
    </build>
    <profiles>
        <profile>
            <!--開發環境-->
            <id>development</id>
            <build>
                <filters>
                    <filter>profiles/filter-development.properties</filter>
                </filters>
            </build>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!--預發環境-->
            <id>prerelease</id>
            <build>
                <filters>
                    <filter>profiles/filter-prerelease.properties</filter>
                </filters>
            </build>
        </profile>
        <profile>
            <!--生產環境-->
            <id>production</id>
            <build>
                <filters>
                    <filter>profiles/filter-production.properties</filter>
                </filters>
            </build>
        </profile>
    </profiles>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章