欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

maven打包zip包含bin下启动脚本的完整代码

程序员文章站 2022-03-16 20:10:41
maven打包zip包含bin下启动脚本,这个脚本小编在idea上测试有效:pom.xml打包 src/main/...

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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!