文章首发于公众号【大数据学徒】,感兴趣请搜索 dashujuxuetu 或者文末扫码关注。
Java 项目在开发和正式 release 之后的运行方式是不同的,开发时,只需要在 IDEA 中运行主函数即可,但发行后,需要处理 JAR 包依赖、CLASSPATH、配置文件、启动脚本、环境变量等各种问题,这也是很多项目都会提供 source
和 binary
两种下载包的原因,source
包用来开发,binary
直接用来运行,本文介绍如何通过 Maven 来构建二进制包并通过 Github 分发。
内容提要:
- 环境说明
- 预期效果
- 配置流程
- 通过 Github 分发
代码已上传至 Github:https://github.com/iamabug/sunny
1. 环境说明
JDK版本:1.8
IDE:IntelliJ IDEA + Maven 插件
2. 预期效果
以我的一个项目为例,项目名为 sunny
,初始的目录结构如下:
sunny $ tree -L 1
.
├── bin
├── conf
├── pom.xml
├── sunny-server
└── sunny-web
其中 sunny-server
和 sunny-web
是两个子模块,pom.xml
不必多说,bin
和 conf
目录分别存放启动脚本和配置文件,二进制包构建完毕的效果是有一个压缩包,名为 sunny-dist-0.0.1-bin.tar.gz
,解压之后目录结构如下:
sunny-dist-0.0.1-bin $ tree -L 1
.
├── bin
├── conf
└── lib
其中 bin
目录和 conf
目录存放的内容与打包之前相同,lib 中存放着 sunny-server
模块的 jar
包和 sunny-web
模块的 war
包,拿到这个压缩包的人可以直接通过 bin
目录下的脚本启动项目。
下面介绍如何实现。
3. 配置流程
首先创建 sunny-dist
子模块,它的 pom.xml
内容如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>sunny</artifactId>
<groupId>iamabug</groupId>
<version>0.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sunny-dist</artifactId>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>sunny-server</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>sunny-web</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>build package distribution</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/dist.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
注意这里有两点比较重要:
- 将
sunny-server
和sunny-web
两个模块指定为依赖,并指明sunny-web
模块的打包方式为war
; - 使用
maven-assembly-plugin
插件,在package
阶段执行二进制包的构建,并指明使根据src/main/dist.xml
来进行构建。
然后在 sunny-dist
模块下新建 src/main.dist.xml
文件,文件内容为:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>bin</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<baseDirectory>sunny-dist-${project.version}-bin</baseDirectory>
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<useTransitiveDependencies>true</useTransitiveDependencies>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
<excludes>
<exclude>${project.groupId}:*:*</exclude>
</excludes>
</dependencySet>
</dependencySets>
<moduleSets>
<moduleSet>
<useAllReactorProjects>true</useAllReactorProjects>
<binaries>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
</binaries>
</moduleSet>
</moduleSets>
<fileSets>
<fileSet>
<directory>../bin</directory>
<directoryMode>755</directoryMode>
<fileMode>755</fileMode>
</fileSet>
<fileSet>
<directory>../conf</directory>
</fileSet>
</fileSets>
</assembly>
其中值得注意的点有:
- 通过
format
标签指定打包类型,除了tar.gz
还可以打成zip
格式,也可以同时指定两种; -
includeBaseDirectory
为true
表示要为打包的内容指定一个基础目录,目录名称通过baseDirectory
标签指定; -
dependencySets
标签表明需要把这个模块的所有依赖全部拷贝到lib
目录,但是不包括本模块依赖的两个模块; -
moduleSets
标签表明需要把本项目的两个模块也拷贝到最终的lib
目录,但是这里可以选择其他目录; -
fileSets
标签用来将bin
目录和conf
目录拷贝基础目录下,并且可以指定这些目录和文件的权限掩码。
最后,在项目根目录下执行 mvn clean package
,执行完毕后就会在 sunny-dist/target
目录下生成 sunny-dist-0.0.1-bin.tar.gz
,截图如下:
4. 通过 Github 分发
打开项目主页,看到 releases
数量为 0:
点击 releases
:
在下图中各个位置填写必要的信息,然后上传上一步生成的压缩包,点击 Publish release
:
然后应该会看到如下页面:
image看起来还挺像回事的哈,打完收工。
欢迎交流讨论,吐槽建议,分享收藏。
勤学似春起之苗,不见其增,日有所长
辍学如磨刀之石,不见其损,日有所亏
关注【大数据学徒】,用技术干货助你日有所长
网友评论