前言:在部署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