如何正确发布sbt插件到本地文件仓库并实现依赖解析?
Got it, let's walk through how to get your custom local file repository working for both publishing your plugin and resolving it in another project. The core issue here is that your consuming project doesn't know to look in /Users/jast/repo/localtrix/ for the plugin—sbt only checks default repositories like Maven Central and .ivy2/local by default.
Step 1: Ensure Your Plugin is Published in Maven Style
First, let's confirm your plugin project is set up to publish in a Maven-compatible layout (this makes resolution simpler than Ivy-style for custom file repos). In your plugin's build.sbt, add or verify these settings:
// Define your plugin's core coordinates (match these exactly in the consuming project) organization := "org.me" name := "sbt-plugin" version := "1.2.3" // Point to your custom local file repository publishTo := Some(Resolver.file("Local Maven Repository", file("/Users/jast/repo/localtrix/"))) // Force Maven-style publishing (avoids Ivy-specific directory structures that can cause resolution headaches) publishMavenStyle := true
After updating, re-run sbt publish to push the plugin to your repo with the correct layout. You’ll see the familiar Maven-style directory structure: /org/me/sbt-plugin/1.2.3/ containing the JAR, POM, and other necessary files.
Step 2: Configure the Consuming Project to Use Your Local Repository
In the project where you want to use the plugin, you need to tell sbt to check your custom repo when resolving plugins. Edit the project/plugins.sbt file:
Add the custom resolver first (before the plugin dependency—order can matter for resolution):
// Add your local file repository as a resolver for plugin dependencies resolvers += Resolver.file("Local Maven Repository", file("/Users/jast/repo/localtrix/"))Add the plugin dependency (note: you don’t need to include the Scala/sbt version suffix—sbt handles that automatically based on your project’s environment):
addSbtPlugin("org.me" % "sbt-plugin" % "1.2.3")
Key Checks to Avoid Common Pitfalls
- Double-check that the
organization,name, andversionin your plugin'sbuild.sbtmatch exactly what you’re using inaddSbtPlugin. Typos here are the #1 cause of resolution failures. - If you previously published an Ivy-style version of the plugin to your custom repo, delete those old files before re-publishing with Maven style—conflicting layouts can confuse sbt.
- To debug, run
sbt debugin the consuming project and look for lines about repository resolution. This will show you exactly which repos sbt is checking and why it can’t find the plugin.
That's it! Run sbt in your consuming project now, and it should resolve your plugin from the custom local file repository without issues.
内容的提问来源于stack exchange,提问作者Justin Kaeser

