mvn打jar包的几个插件分析

前言:在部署project项目时,希望项目目录结构是这样的:一个bin目录包含启动脚本、停止脚本、状态检查脚本等(在bin文件夹下 直接 ./项目名 start 就可以后台启动项目;./项目名 status 查看项目运行状态;./项目名 stop 停项目);一个conf目录包含项目配置文件(这里是content.properties文件);一个lib目录包含项目依赖jar包。所有的这些目录都可以由assembly插件完成。DEMO下载地址:https://pan.baidu.com/s/1XOYP5QDsG5iGUUylEOdYFw 提取码:33qt 以下是项目插件分析:

<build>
    <plugins>
        <plugin>
            <!-- 添加maven发布jar文件的插件 -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
        </plugin>

        <plugin>
            <!-- 生成jsw(java service wrapper)后台运行程序的插件 -->
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>appassembler-maven-plugin</artifactId>
            <version>1.10</version>
            <configuration>
                <!-- lib目录中jar的存放规则,默认是${groupId}/${artifactId}的目录格式,flat表示直接把jar放到lib目录,而不把jar包解压 -->
                <repositoryLayout>flat</repositoryLayout>
                <!-- 生成bin文件夹下不同平台可执行文件的配置 -->
                <daemons>
                    <!-- 生成bin,及可执行文件 ./项目名 { console | start | stop | restart | status | dump } -->
                    <daemon>
                        <!-- 应用入口(含main方法)启动类 -->
                        <mainClass>com.springboot.beetlsql.BeetlSQLTest</mainClass>
                        <id>${project.artifactId}</id><!-- 可执行文件的名称 -->
                        <commandLineArguments>
                            <commandLineArgument>start</commandLineArgument>
                        </commandLineArguments>
                        <platforms>
                            <platform>jsw</platform>
                        </platforms>
                        <generatorConfigurations>
                            <generatorConfiguration>
                                <generator>jsw</generator>
                                <includes>
                                    <include>linux-x86-64</include>
                                    <include>windows-x86-64</include>
                                    <include>macosx-universal-64</include>
                                </includes>
                                <configuration>
                                    <property>
                                        <!-- wrapper.conf配置文件存放的路径 -->
                                        <name>configuration.directory.in.classpath.first</name>
                                        <value>etc</value>
                                    </property>
                                    <property>
                                        <name>set.default.REPO_DIR</name>
                                        <value>lib</value>
                                    </property>
                                    <property>
                                        <name>wrapper.logfile.loglevel</name>
                                        <value>NONE</value>
                                    </property>
                                </configuration>
                            </generatorConfiguration>
                        </generatorConfigurations>
                        <jvmSettings>
                            <!-- JVM参数 -->
                            <initialMemorySize>2048M</initialMemorySize>
                            <maxMemorySize>2048M</maxMemorySize>
                            <maxStackSize>128M</maxStackSize>
                            <extraArguments>
                                <extraArgument>-server</extraArgument>
                                <extraArgument>-XX:PermSize=64M</extraArgument>
                                <extraArgument>-XX:MaxPermSize=128m</extraArgument>
                            </extraArguments>
                        </jvmSettings>
                    </daemon>
                </daemons>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>generate-daemons</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.5.5</version>
            <configuration>
                <descriptors>
                    <!-- 文件配置导出描述在jsw.xml中,是project最终目录结构的描述文件 -->
                    <descriptor>src/main/assembly/jsw.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
            <!-- 若要同时打多个包(如windows和linux不同系统的包),可配置多个execution,此处只打tar.gz,因此配置一个 -->
            <!-- 默认生成的一个包 ${project.build.directory}/${project.build.finalName}-bin(这是jsw文件开头的id=bin).tar.gz(这是jsw文件中format=tar.gz)-->
                <execution>
                    <!-- 绑定到package生命周期阶段上,即在mvn package的时候打包 --> 
                    <phase>package</phase>
                    <goals>
                        <!-- 只运行一次 -->
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <!-- 在maven中运行Ant任务,在打包阶段,对文件进行复制 --> 
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-app</id>
                    <phase>package</phase>
                    <configuration>
                        <tasks>
                            <copy todir="../../juwenzhe/springboot"
                                file="${project.build.directory}/${project.build.finalName}-bin.tar.gz" />
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <resources>
        <!-- 需要打包的资源文件 -->
        <resource>
            <directory>src/main/resources/env/${deploy.type}</directory>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
</build>
<!-- 选择不同环境下对应的参数列表 -->
<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <deploy.type>dev</deploy.type>
        </properties>
    </profile>
    <profile>
        <id>sit</id>
        <properties>
            <deploy.type>sit</deploy.type>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <deploy.type>prod</deploy.type>
        </properties>
    </profile>
</profiles>

----------------jsw.xml内容:最终发布包的目录结构描述文件------------------
<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>bin</id><!-- 打成的包附带的名称 如: wx_gws-1.1.5.5-bin.tar.gz-->

    <includeBaseDirectory>false</includeBaseDirectory>

    <formats>
        <format>tar.gz</format>
    </formats>
    <fileSets>
        <fileSet>
            <!-- 需要打包的路径 -->
            <directory>${project.build.directory}/generated-resources/appassembler/jsw/${project.artifactId}</directory>
            <!-- 打包后输出的路径 -->
            <outputDirectory>${project.artifactId}</outputDirectory>
            <!-- 设置文件、文件夹的访问权限755 -->
            <fileMode>0755</fileMode>
            <directoryMode>0755</directoryMode>
        </fileSet>
        <fileSet>
            <!-- 由打包命令mvn package -P sit决定${deploy.type}=sit -->
            <directory>src/main/resources/env/${deploy.type}/conf</directory>
            <outputDirectory>${project.artifactId}/etc/conf</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>${project.artifactId}/logs</outputDirectory>
            <!-- 排除某些文件的样例 -->
            <excludes>
                <exclude>**/*</exclude>
            </excludes>
        </fileSet>
    </fileSets>
    <!-- 打开下面的配置,会生成一个文件夹,存放所有运行时的依赖包到生成项目同级目录的lib文件夹下 -->
    <!-- <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>将scope为runtime的依赖包打包到lib目录下。
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>  -->
</assembly>


可能遇到的问题一:
[root@localhost bin]# ./HelloWorld start
Unable to locate any of the following operational binaries:
  /juwenzhe/HelloWorld/bin/./wrapper-linux-x86-64 (Found but not executable.)
  /juwenzhe/HelloWorld/bin/./wrapper-linux-x86-32
  /juwenzhe/HelloWorld/bin/./wrapper
解决思路与方法:
wrapper-linux-x86-64文件缺少权限
chmod 777 wrapper-linux-x86-64

可能遇到的问题二:
[root@localhost bin]# ./HelloWorld console
Running HelloWorld...
wrapper  | ERROR: Could not write pid file /juwenzhe/HelloWorld/logs/HelloWorld.pid: No such file or directory
解决思路与方法:
手动在解压后的logs添加 HelloWorld.pid 文件 存放项目启动后的进程号


参考文献:
  1.Maven学习总结:几个常用的maven插件 http://shmilyaw-hotmail-com.iteye.com/blog/2170300
  2.maven 安装问题:mvn -v 不是内部命令 https://www.cnblogs.com/starriver/p/7793104.html
  3.Maven命令行使用:mvn clean package(打包) https://www.cnblogs.com/frankyou/p/6062179.html
  4.java学习笔记8 - maven profile实现多环境打包 https://blog.csdn.net/wuxinzaiyu/article/details/8524274
  5.maven集成命令-U -B -P -e https://blog.csdn.net/tlqfreedom/article/details/60955757
  6.Maven Assembly插件介绍 http://blueram.iteye.com/blog/1684070
  7.关于Maven的assembly插件 https://www.douban.com/note/207395473/?type=like
  8.maven打zip包及windows下jsw安装java服务 https://www.2cto.com/kf/201607/522111.html

要使用 Maven 打出 Jar 包,首先需要编写 Maven 的 pom.xml 文件,并在其中配置打包方式和依赖项。下面是一个简单的 pom.xml 文件示例: ``` <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <groupId>com.example</groupId> <artifactId>my-project</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- 添加项目所需的依赖项 --> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <archive> <manifest> <mainClass>com.example.MainClass</mainClass> <!-- 设置启动类 --> </manifest> </archive> </configuration> </plugin> </plugins> </build> </project> ``` 在 pom.xml 文件中,需要注意以下几点: 1. 配置 groupId、artifactId 和 version,分别对应项目的组织名、项目名和版本号。 2. 在 dependencies 标签中添加项目所需的依赖项。 3. 在 build 标签下的 plugins 标签中添加 maven-jar-plugin 插件,并在其中配置启动类(即包含 main 方法的类)。 完成 pom.xml 文件的编写后,就可以使用 Maven 打出 Jar 包了。在项目根目录下执行以下命令即可: ``` mvn package ``` 执行完该命令后,Maven 会在 target 目录下生成一个 Jar 包。如果在 pom.xml 中配置了 mainClass,那么该 Jar 包就可以通过以下命令直接运行: ``` java -jar my-project-1.0-SNAPSHOT.jar ``` 其中,my-project-1.0-SNAPSHOT.jar 对应生成的 Jar 包的文件名。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值