maven打包zip包含bin下啟動腳本的完整代碼

maven打包zip包含bin下啟動腳本,這個腳本小編在idea上測試有效:

pom.xml打包

<build>
        <sourceDirectory>src/main/scala</sourceDirectory>
        <testSourceDirectory>src/test/scala</testSourceDirectory>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <!-- 排除外置的配置文件(運行時註釋上,使IDE能讀到配置文件;打包時放開註釋讓配置文件外置,方便修改)可以不配置,maven-jar-plugin下面已配置 -->
                <!--<excludes>
                    <exclude>config.properties</exclude>
                </excludes>-->
            </resource>
            <!-- 配置文件外置的資源(存放到conf目錄,也是classpath路徑,下面會配置)-->
            <!--<resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>config.properties</include>
                </includes>
                <targetPath>${project.build.directory}/conf</targetPath>
            </resource>-->
        </resources>

        <plugins>
            <!--scala編譯打包插件-->
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <id>scala-compile-first</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>add-source</goal>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!--java編譯打包插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!--
                ③打成一個zip包,發佈項目的時候,將zip包copy到服務器上,直接unzip xxx.zip,裡面包含要運行到的jar以及依賴的lib,還有配置的config文件,即可直接啟動服務
            -->

            <!--The configuration of maven-jar-plugin-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <!--The configuration of the plugin-->
                <configuration>
                    <!-- 不打包資源文件(配置文件和依賴包分開) -->
                    <excludes>
                        <exclude>*.properties</exclude>
                        <exclude>*.xml</exclude>
                        <exclude>*.txt</exclude>
                    </excludes>
                    <!--Configuration of the archiver-->
                    <archive>
                        <!--生成的jar中,不要包含pom.xml和pom.properties這兩個文件-->
                        <addMavenDescriptor>false</addMavenDescriptor>
                        <!--Manifest specific configuration-->
                        <manifest>
                            <!--是否把第三方jar放到manifest的classpath中-->
                            <addClasspath>true</addClasspath>
                            <!--生成的manifest中classpath的前綴,因為要把第三方jar放到lib目錄下,所以classpath的前綴是lib/-->
                            <classpathPrefix>lib/</classpathPrefix>
                            <!--應用的main class-->
                            <mainClass>com.swordfall.restserver.base.WebServer</mainClass>
                        </manifest>
                        <!-- 給清單文件添加鍵值對,增加classpath路徑,這裡將conf目錄也設置為classpath路徑 -->
                        <manifestEntries>
                            <Class-Path>conf/</Class-Path>
                        </manifestEntries>
                    </archive>
                    <!--過濾掉不希望包含在jar中的文件-->
                    <!-- <excludes>
                         <exclude>${project.basedir}/xml/*</exclude>
                     </excludes>-->
                </configuration>
            </plugin>

            <!--The configuration of maven-assembly-plugin-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <!--The configuration of the plugin-->
                <configuration>
                    <!--Specifies the configuration file of the assembly plugin-->
                    <descriptors>
                        <descriptor>src/main/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

 assembly.xml打包zip設置

assembly.xml

<assembly>
    <id>bin</id>
    <includeBaseDirectory>false</includeBaseDirectory>
    <!-- 最終打包成一個用於發佈的zip文件 -->
    <formats>
        <format>zip</format>
    </formats>

    <!-- Adds dependencies to zip package under lib directory -->
    <dependencySets>
        <dependencySet>
            <!-- 不使用項目的artifact,第三方jar不要解壓,打包進zip文件的lib目錄 -->
            <useProjectArtifact>false</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <unpack>false</unpack>
        </dependencySet>
    </dependencySets>

    <fileSets>
        <!-- 把項目相關的說明文件,打包進zip文件的根目錄 -->
        <!--<fileSet>
            <directory>${project.basedir}</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>-->

        <!-- 把項目的配置文件,打包進zip文件的config目錄 -->
        <!--<fileSet>-->
        <!--<directory>${project.basedir}/src/main/resources</directory>-->
        <!--<outputDirectory>/conf</outputDirectory>-->
        <!--<includes>-->
        <!--<include>*.xml</include>-->
        <!--<include>*.properties</include>-->
        <!--</includes>-->
        <!--</fileSet>-->

        <!-- 把項目自己編譯出來的jar文件,打包進zip文件的根目錄 -->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>

        <!-- 把項目的腳本文件目錄(src/main/scripts)中的啟動腳本,打包進zip文件的根目錄 -->
        <fileSet>
            <directory>${project.basedir}/src/main/bin</directory>
            <outputDirectory>bin</outputDirectory>
            <includes>
                <include>*.sh</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

ps:下面看下maven 打zip包並包含bin和docs文件夾

maven插件:

<plugin>
 <artifactId>maven-assembly-plugin</artifactId>
 <configuration>
  <appendAssemblyId>false</appendAssemblyId>
  <descriptors>
   <descriptor>src/main/resources/assembly.xml</descriptor>
  </descriptors>
 </configuration>
 <executions>
  <execution>
   <id>make-assembly</id>
   <phase>package</phase>
   <goals>
    <goal>single</goal>
   </goals>
  </execution>
 </executions>
</plugin>

assembly.xml

<assembly>
    <id>package</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.parent.basedir}/bin</directory>
            <outputDirectory>\bin</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.parent.basedir}/db</directory>
            <outputDirectory>\db</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.parent.basedir}/docs</directory>
            <outputDirectory>\docs</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.parent.basedir}</directory>
            <outputDirectory>\</outputDirectory>
            <includes>
                <include>readme.md</include>
                <include>release-notes</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>\</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

到此這篇關於maven打包zip包含bin下啟動腳本的文章就介紹到這瞭,更多相關maven打包zip內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: