sbt-sonatype 项目教程
1. 项目的目录结构及介绍
sbt-sonatype 是一个用于将 Scala/Java 项目发布到 Maven Central 的 sbt 插件。以下是其基本的目录结构:
sbt-sonatype/
├── build.sbt
├── project/
│ ├── build.properties
│ ├── plugins.sbt
│ └── src/
├── src/
│ ├── main/
│ │ ├── resources/
│ │ └── scala/
│ └── test/
│ ├── resources/
│ └── scala/
├── README.md
└── LICENSE
build.sbt
: 项目的主要构建文件。project/
: 包含 sbt 的配置文件和插件。build.properties
: 指定 sbt 版本。plugins.sbt
: 引入 sbt-sonatype 和其他插件。
src/
: 源代码目录。main/
: 主代码目录。resources/
: 资源文件。scala/
: Scala 源代码。
test/
: 测试代码目录。resources/
: 测试资源文件。scala/
: 测试 Scala 代码。
README.md
: 项目说明文档。LICENSE
: 项目许可证。
2. 项目的启动文件介绍
sbt-sonatype 项目的启动文件主要是 build.sbt
和 project/plugins.sbt
。
build.sbt
build.sbt
是 sbt 项目的主要构建文件,包含项目的依赖、插件配置等信息。以下是一个示例:
name := "sbt-sonatype"
version := "3.9.12"
organization := "org.xerial.sbt"
scalaVersion := "2.12.13"
libraryDependencies ++= Seq(
"com.github.sbt" % "sbt-pgp" % "2.1.2"
)
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.12")
project/plugins.sbt
project/plugins.sbt
用于引入 sbt 插件。以下是一个示例:
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.12")
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.1.2")
3. 项目的配置文件介绍
sbt-sonatype 的配置主要在 build.sbt
中完成。以下是一些关键配置项:
引入插件
在 project/plugins.sbt
中引入 sbt-sonatype 和 sbt-pgp 插件:
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.12")
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.1.2")
配置发布
在 build.sbt
中配置发布信息:
publishTo := sonatypePublishToBundle.value
credentials += Credentials(
"Sonatype Nexus Repository Manager",
"oss.sonatype.org",
"your-sonatype-username",
"your-sonatype-password"
)
sonatypeProfileName := "org.xerial"
publishMavenStyle := true
GPG 签名
使用 sbt-pgp 插件进行 GPG 签名:
useGpg := true
pgpPassphrase := Some(sys.env.getOrElse("PGP_PASSPHRASE", "").toCharArray)
通过以上配置,你可以使用 sbt publishSigned
和 sbt sonatypeBundleRelease
命令将项目发布到 Maven Central。