springboot之assembly的文件配置


 

  一、在使用springboot框架的時候,存在一個問題。就是我們配置yaml文件,需要單獨提出來做參數修改。當然這個是可以通過spring.profiles.active的方式來配置dev,prod等環境的激活。但是我們如果存在環境不確定,或者需要啟動腳本,啟動項目的時候,這樣通過jar的方式后續會處理很多工作。所以前期的集成工作還是很有必要的。

  二、這里有一個簡單的例子,用於參數配置方式

  1)目錄結構

  

  2)需要的依賴包(pom.xml)

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

  3)maven的構建過程

   <build>
        <finalName>assembly</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
            <plugin>
                <!--主要使用的是maven提供的assembly插件完成-->
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <configuration>
                            <appendAssemblyId>false</appendAssemblyId>
                            <!--具體的配置文件-->
                            <descriptors>${project.basedir}/src/main/resources/assembly/package.xml</descriptors>
                        </configuration>
                        <id>make-assembly</id>
                        <!--綁定到maven操作類型上-->
                        <phase>package</phase>
                        <!--運行一次-->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

  4)集成過程(package.xml)

<?xml version='1.0' encoding='UTF-8'?>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <!--打包名稱,唯一標識-->
    <id>${project.build.finalName}</id>
    <!--打包格式,可以手動修改-->
    <formats>
        <format>tar.gz</format>
    </formats>
    <!--文件設置-->
    <fileSets>
        <fileSet>
            <!--目標目錄,會處理目錄里面的所有文件-->
            <directory>${project.basedir}/src/main/resources/config</directory>
            <!--相對於打包后的目錄-->
            <outputDirectory>config</outputDirectory>
            <!--文件過濾-->
            <includes>
                <include>*.*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.basedir}/src/main/resources/script</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.*</include>
            </includes>
            <!--文件權限-->
            <fileMode>0755</fileMode>
            <!--如果是腳本,一定要改為unix.如果是在windows上面編碼,會出現dos編寫問題-->
            <lineEnding>unix</lineEnding>
        </fileSet>
    </fileSets>
    <files>
        <!--包含打包后的jar文件,可以不加入<outputDirectory/>,默認打包的目錄-->
        <file>
            <source>${project.build.directory}/${project.build.finalName}.jar</source>
        </file>
        <!--這種方式也可以進行文件處理,但是針對單文件-->
       <!-- <file>
            <source>${project.basedir}/src/main/resources/script/start.sh</source>
            <fileMode>0755</fileMode>
            <lineEnding>unix</lineEnding>
        </file>-->
    </files>
</assembly>

  備注:具體的參數的意義可以參考官網:http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html

  5)通過maven的package打包

  

  

  

 

  三、源碼:https://github.com/lilin409546297/springboot-assembly

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM