本文源代码:https://github.com/chensoul/maven-site-github-example/ 。
创建 Java Maven 项目
让我们使用 Maven 创建一个简单的 Java 项目
|
|
这将创建包含 Maven 项目的my-app文件夹。让我们进入该文件夹并确保它编译正常:
|
|
我们还可以生成该项目的站点:
|
|
该站点将在target/site文件夹中生成,可以使用浏览器打开;例如,让我们打开它的 index.html:
|
|
创建 github 项目
在你的 github 上创建一个项目 maven-site-github-example,然后在本地的 maven-site-github-example 目录提交代码:
|
|
GitHub Page 设置
现在我们必须在 Git 存储库上创建 gh-pages分支。
|
|
相应的网站将发布在你的 GitHub 项目的相应 URL 中;访问地址:https://chensoul.github.io/maven-site-github-example:
现在我们可以回到 主分支:
|
|
Maven POM 配置
让我们看看如何配置我们的 POM 以自动将 Maven 站点发布到 GitHub 页面。
在 pluginManagement 部分中添加 maven-scm-publish-plugin:
|
|
maven-scm-publish-plugin 插件需要以下参数,参考 https://maven.apache.org/plugins/maven-scm-publish-plugin/publish-scm-mojo.html:
- tryUpdate: 参数用于指定是否尝试更新源代码管理(SCM)提供商中的项目文件。
当你运行
mvn scm-publish:publish-scm
命令来发布 Maven 项目时,插件会检查 SCM 提供商中的项目文件是否与本地生成的网站内容一致。如果存在差异,插件默认情况下会拒绝发布,并显示错误消息。通过将
tryUpdate
参数设置为true
,你可以告诉插件尝试更新 SCM 提供商中的项目文件,以使其与本地生成的网站内容保持一致。如果更新成功,插件将继续发布网站。否则,插件将显示错误消息。以下是使用
tryUpdate
参数的示例命令:
1
mvn scm-publish:publish-scm -Dscmpublish.tryUpdate=true
请注意,使用
tryUpdate
参数时,插件将直接修改 SCM 提供商中的项目文件。因此,在使用此参数之前,请确保你对 SCM 提供商中的项目文件进行了适当的备份,并且了解潜在的风险。
pubScmUrl:必须,用于指定发布到源代码管理(SCM)提供商的网站的URL。
- 格式:
scm:<scm_provider><delimiter><provider_specific_part>
- 默认值:
${project.distributionManagement.site.url}
,可以通过scmpublish.pubScmUrl
属性设置。
- 格式:
scmBranch:非必须,scm分支用于指定源代码管理(SCM)分支的名称。例如:对应 github,应该是 gh-pages
- 没有默认值,可以通过
scmpublish.scm.branch
设置
- 没有默认值,可以通过
serverId:非必须,是 Maven 中用于标识服务器配置的标识符
- 默认值:
${project.distributionManagement.site.id}
,可以通过scmpublish.serverId
属性设置。
下面是一个示例的服务器配置段落:
1 2 3 4 5 6 7 8 9 10 11
<settings> <servers> <server> <id>my-server</id> <username>my-username</username> <password>my-password</password> </server> <!-- Additional server configurations --> </servers> <!-- Other Maven settings --> </settings>
在上述示例中,
my-server
是serverId
,用于标识服务器配置。你可以根据需要添加多个服务器配置,每个配置都应该有唯一的serverId
。当你在 Maven 项目中引用这些服务器配置时,可以使用相应的
serverId
来指定要应用的服务器配置。例如,当你需要在 Maven 项目中使用某个服务器配置时,可以在项目的
pom.xml
文件中配置如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<project> <!-- Other project configurations --> <build> <plugins> <plugin> <groupId>...</groupId> <artifactId>...</artifactId> <configuration> <serverId>my-server</serverId> <!-- Other plugin configurations --> </configuration> </plugin> </plugins> </build> <!-- Other project configurations --> </project>
在上述示例中,
<serverId>my-server</serverId>
指定了要应用的服务器配置。这样,Maven 在构建过程中将使用相应的服务器配置。请注意,
serverId
的值必须与settings.xml
文件中配置的服务器配置的id
保持一致。- 默认值:
username:非必须,SCM 用户名
- 默认从 serverId 指定的 settings.xml 中的 username 读取,可以通过
username
设置
- 默认从 serverId 指定的 settings.xml 中的 username 读取,可以通过
password:非必须,SCM 密码
- 默认从 serverId 指定的 settings.xml 中的 password 读取,可以通过
password
设置
- 默认从 serverId 指定的 settings.xml 中的 password 读取,可以通过
从上面的参数说明,可以看出:
maven-scm-publish-plugin 插件优先读取
project.distributionManagement.site
节点,所以可以在 pom.xml 中配置一个 site 节点,比如:1 2 3 4 5 6
<distributionManagement> <site> <id>github</id> <url>scm:git:[email protected]:chensoul/maven-site-github-example.git</url> </site> </distributionManagement>
其次,读取
scmpublish.serverId
属性,然后去 settings.xml 中获取 username 和密码,这时候需要配置 settings.xml:1 2 3 4 5 6 7
<servers> <server> <id>github</id> <username>chensoul</username> <password>ghp_XXXXXXXXXXXXXXXXXX</password> </server> </servers>
发布网站
在配置了 project.distributionManagement.site
之后,将 Maven 站点发布到 GitHub 页面只需运行以下命令:
|
|
clean
:删除 target 目录site
:命令用于生成 Maven 项目的网站scm-publish:publish-scm
:用于将 Maven 项目发布到源代码管理(SCM)提供商要使用
mvn scm-publish:publish-scm
命令发布 Maven 项目,请按照以下步骤进行操作:- 确保你的 Maven 项目已经配置了适当的 SCM 插件,以便与你使用的源代码管理系统进行集成。在项目的
pom.xml
文件中,你需要添加 SCM 配置,包括 SCM URL、连接信息和凭据等。例如,对于 Git,你可以添加以下配置:
1 2 3 4 5
<scm> <connection>scm:git:[email protected]:<username>/<repository>.git</connection> <developerConnection>scm:git:[email protected]:<username>/<repository>.git</developerConnection> <url>https://github.com/<username>/<repository></url> </scm>
请将
<username>
替换为你的 GitHub 用户名,<repository>
替换为你的仓库名称。打开终端或命令提示符。
使用
cd
命令导航到 Maven 项目的根目录。运行以下命令:
1
mvn scm-publish:publish-scm
Maven 将执行 SCM Publish 插件,将项目发布到指定的 SCM 提供商。
- 确保你的 Maven 项目已经配置了适当的 SCM 插件,以便与你使用的源代码管理系统进行集成。在项目的
从日志可以看出 git 推送提交的命令:'git' 'push' '[email protected]:chensoul/maven-site-github-example.git' 'refs/heads/gh-pages:refs/heads/gh-pages'
|
|
这里使用的是通过 ssh 协议提交,能够提交成功,是因为我已将本地的 ssh 公钥配置到 github。
现在该网站位于 GitHub Pages 上:
如果不配置 project.distributionManagement.site
,注释 distributionManagement 节点,这需要配置 scmpublish.pubScmUrl,可以在 pom.xml 中设置:
|
|
再次运行:
|
|
美化网站
使用 maven-archetype-site 美化网站:
|
|
执行成功之后,src 目录下新增了一个 site 目录,并且POM中添加了 maven-project-info-reports-plugin 和 i18n 本地化的一些配置。
|
|
此外,最终网站将使用新皮肤 maven-fluido-skin。src/site/site.xml 内容如下:
|
|
让我们再次运行以下命令来发布新网站:
|
|
稍等片刻,等待 github page 部署成功之后,可以看到网站:
可以在 https://github.com/chensoul/maven-site-github-example/actions 查看网站的部署:
现在,可以通过添加/更改/删除目录 src/site 的内容,然后通过 Maven 再次发布该网站。
例如,删除 src/site/apt/index.apt,添加 src/site/apt/index.md.vm ,引入项目中 README.md 内容作为网站首页内容:
|
|
Github Action配置
修改 Aciton 的 Workflow 权限,选中 Read and write permissions
。
创建 .github/workflows 目录,并在该目录创建 site.yml:
|
|
在 github action 中,需要设置 github 的 username 和 password 用于提交代码带 gh-pages 分支,所以需要设置 scmpublish.serverId 并且删除 distributionManagement 下的 site 配置:
|
|
server-id 相关信息可以在 actions/setup-java@v4 中设置,参考 actions/setup-java 的文档:
|
|
- server-username:从环境变量 GITHUB_ACTOR 中读取值,故 在 Publish site to github pages 步骤里面需要设置 env 环境变量:
GITHUB_ACTOR: ${{ secrets.GITHUB_ACTOR }}
- server-password:从环境变量 GITHUB_TOKEN 中读取值,故 在 Publish site to github pages 步骤里面需要设置 env 环境变量:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
另外,git 提交代码时,需要设置用户名和邮箱:
|
|
提交代码之后,Github Action 可以看到一个部署,稍等片刻,部署失败:
|
|
从日志可以看到 maven-clean-plugin 版本是 2.5,maven-site-plugin 版本是 3.3 ,maven-project-info-reports-plugin 版本是 3.6.2
修改 pom.xml ,给 maven-site-plugin 设置版本为 4.0.0-M15:
|
|
再次提交代码,发现 Github action 还是运行失败,异常日志:
|
|
从日志可以看到 git 从 [email protected]:chensoul/maven-site-github-example.git
下载代码,没有权限,原因是没有配置 ssh。故需要修改 scmpublish.pubScmUrl 为 https 协议,将 <scmpublish.pubScmUrl>scm:git:[email protected]:chensoul/maven-site-github-example.git</scmpublish.pubScmUrl>
改为:
|
|
再次提交代码,action 运行成功。
再次打开网站,发现存在 I18N 没有替换成功的问题。
解决办法:
修改 src/site/site.xml 和 src/site/site_fr.xml 文件中 maven-fluido-skin 的版本为当前最新版本:2.0.0-M9
1 2 3 4 5
<skin> <groupId>org.apache.maven.skins</groupId> <artifactId>maven-fluido-skin</artifactId> <version>2.0.0-M9</version> </skin>
修改 src/site/site.xml 和 src/site/site_fr.xml 文件中
${artifactId}
为${this.artifactId}
1 2 3 4 5
<bannerLeft> <name>${this.artifactId}</name> <src>https://maven.apache.org/images/apache-maven-project.png</src> <href>https://www.apache.org/</href> </bannerLeft>
再次发布并打开网站,则正常显示: