温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

maven如何打包zip包含bin下启动脚本

发布时间:2021-10-31 09:42:58 来源:亿速云 阅读:540 作者:小新 栏目:开发技术

这篇文章主要为大家展示了“maven如何打包zip包含bin下启动脚本”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“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下启动脚本”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI