如何将jar发布到本地存储库?

发布于 2024-12-10 03:28:25 字数 210 浏览 1 评论 0原文

我有一个编译为 jar 的库(不是 sbt 项目,没有源代码,只有 jar 文件),该库在存储库中不可用。

有没有办法在本地发布 jar,以便我可以使用 libraryDependency += "org.xxx" % "xxx" % "1.0" 表示法添加依赖项? (我已经知道如何通过将文件复制到 lib 文件夹来将其添加到项目中。)

I have a library compiled to a jar (not an sbt project, no source code, just the jar file) that's not available on a repository.

Is there a way to publish the jar locally so I can add the dependency using the libraryDependencies += "org.xxx" % "xxx" % "1.0" notation? (I already know how to add the file to a project by copying it to the lib folder.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

世界和平 2024-12-17 03:28:25

publishLocal 操作用于将项目发布到本地 Ivy 存储库。默认情况下,此本地存储库位于 ${user.home}/.ivy2/local 中。然后,您可以在同一台计算机上的其他项目中使用该项目

编辑: 抱歉我看错你的问题了。这是一个将 jar 或源发布到本地 ivy 存储库的示例

The publishLocal action is used to publish your project to a local Ivy repository. By default, this local repository is in ${user.home}/.ivy2/local. You can then use this project from other projects on the same machine source

EDIT: Sorry I misread your question. Here is an example to publish a jar or sources to your local ivy repo.

ぇ气 2024-12-17 03:28:25

tl;dr 我认为这是一个技巧,而不是 sbt 的功能。你已被警告过。

假设您有要发布的 file.jar。与任何构建工具(包括 sbt)一样,它执行最终从项目文件中创建工件(大多数情况下是 jar 文件)的任务。

该项目设置工件的坐标

诀窍是利用 sbt 所需的内容来设置要发布的 jar 的环境(= 坐标)(否则您必须在命令行上指定它们)这可能对用户很友好,也可能不太友好)。

使用必要的设置创建一个 build.sbt - organizationnameversion 以及可能的 scalaVersion< /code> - 并将其保存在 jar 文件所在的位置。

organization := "org.abc"

name := "my-own-publish-jar"

version := "1.0.0"

scalaVersion := "2.11.3"

packageBin in Compile := file(s"${name.value}_${scalaBinaryVersion.value}.jar")

您可能已经注意到,构建将 compile:package 任务更改为指向 jar 文件。

就是这样。

执行sbtpublishLocal,jar文件应该在ivy2本地存储库中,即~/.ivy2/local/org.abc/my-own-publish-jar_2.11/1.0.0 /jars/my-own-publish-jar_2.11.jar

protip 编写一个插件来使用命令行上指定的坐标来完成此操作现在应该非常容易。

tl;dr I'd call it a trick not a feature of sbt. You've been warned.

Let's say you've got file.jar to publish. As is for any build tool, sbt including, it's to execute tasks that eventually create an artifact - a jar file in most cases - out of the files in a project.

The project sets the coordinates for the artifact.

The trick is to leverage what sbt requires to set up the environment (= the coordinates) for the jar to be published (otherwise you'd have to specify them on command line that may or may not be very user friendly).

Create a build.sbt with the necessary settings - organization, name, version and possibly scalaVersion - and save it where the jar file is.

organization := "org.abc"

name := "my-own-publish-jar"

version := "1.0.0"

scalaVersion := "2.11.3"

packageBin in Compile := file(s"${name.value}_${scalaBinaryVersion.value}.jar")

You may've noticed, the build changes compile:package task to point at the jar file.

That's it.

Execute sbt publishLocal and the jar file should be in the Ivy2 local repository, i.e. ~/.ivy2/local/org.abc/my-own-publish-jar_2.11/1.0.0/jars/my-own-publish-jar_2.11.jar.

protip Writing a plugin to do it with the coordinates specified on command line should be quite easy now.

手心的温暖 2024-12-17 03:28:25

假设您有 wetElephant.jarwetElephant-javadoc.jar 文件,其中包含一些第 3 方库和相应的 javadoc,您希望将其发布到本地存储库并从另一个项目引用它使用libraryDependency sbttaskKey

这就是你需要做的。

  1. 将您的库(wetElephant.jarwetElephant-javadoc.jar)放入 modules\wetElephant
  2. 在构建中定义项目.sbt 文件(或 Build.scala 文件)

    lazy val stokenLib = 项目
      .in(文件(“模块/wetElephant”))
      。设置(
        组织 := "com.stolenLibs",
        名称 := "湿大象",
        版本 := "0.1-IDonKnow",
        crossPaths := false, //不要将 scala 版本添加到存储库中的此工件中
        发布MavenStyle := true,
        autoScalaLibrary := false, //不附加 scala 库作为依赖项
        description := "将依赖发布到maven repo的项目,使用'sbtpublishLocal'来安装它",
        Compile 中的 packageBin := baseDirectory.value / s"${name.value}.jar",
        编译中的 packageDoc := baseDirectory.value / s"${name.value}-javadoc.jar"
      )
    
  3. 调用 publishLocal 任务来自 sbt/activator (我是从 activator 做的,并用项目名称作为前缀):

    ./activator wetElephant/publishLocal                   
    

...并读取输出以查看发布的内容和位置:

    /cygdrive/d/devstation-workspace/projects/m4l-patches 1
      [info] Loading project definition from D:\devstation-workspace\projects\m4l-patches\project
      [info] Set current project to m4l-patches (in build file:/D:/devstation-workspace/projects/m4l-patches/)
      [info] Updating {file:/D:/devstation-workspace/projects/m4l-patches/}wetElephant...
    [info] Packaging D:\devstation-workspace\projects\m4l-patches\modules\wetElephant\target\wetelephant-0.1-IDonKnow-sources.jar ...
    [info] Done packaging.
    [info] Wrote D:\devstation-workspace\projects\m4l-patches\modules\wetElephant\target\wetelephant-0.1-IDonKnow.pom
      [info] Resolving org.fusesource.jansi#jansi;1.4 ...4 ....
    [info] Done updating.
    [info] :: delivering :: com.stolenLibs#wetelephant;0.1-IDonKnow :: 0.1-IDonKnow :: release :: Sun Dec 20 20:09:24 CET 2015
      [info]  delivering ivy file to D:\devstation-workspace\projects\m4l-patches\modules\wetElephant\target\ivy-0.1-IDonKnow.xml
      [info]  published wetelephant to C:\Users\pawell\.ivy2\local\com.stolenLibs\wetelephant\0.1-IDonKnow\poms\wetelephant.pom
      [info]  published wetelephant to C:\Users\pawell\.ivy2\local\com.stolenLibs\wetelephant\0.1-IDonKnow\jars\wetelephant.jar
      [info]  published wetelephant to C:\Users\pawell\.ivy2\local\com.stolenLibs\wetelephant\0.1-IDonKnow\srcs\wetelephant-sources.jar
      [info]  published wetelephant to C:\Users\pawell\.ivy2\local\com.stolenLibs\wetelephant\0.1-IDonKnow\docs\wetelephant-javadoc.jar
      [info]  published ivy to C:\Users\pawell\.ivy2\local\com.stolenLibs\wetelephant\0.1-IDonKnow\ivys\ivy.xml
      [success] Total time: 1 s, completed 2015-12-20 20:09:24
  1. 可以选择在另一个项目中使用这些库

    libraryDependency += "com.stolenLibs" % "wetElephant" % "0.1-IDontKnow"
    

免责声明:我不知道如何不发布源代码...

Let's say you have wetElephant.jar and wetElephant-javadoc.jar files some 3rd party library and corresponding javadocs which you want to publish to your local repo and referrence it from another project using libraryDependencies sbt taskKey.

Here's what you need to do.

  1. Put your libraries (wetElephant.jar and wetElephant-javadoc.jar) into modules\wetElephant
  2. Define project in your build.sbt file (or Build.scala file)

    lazy val stolenLib = project
      .in(file("modules/wetElephant"))
      .settings(
        organization              := "com.stolenLibs",
        name                      := "wetElephant",
        version                   := "0.1-IDonKnow",
        crossPaths                := false,  //don't add scala version to this artifacts in repo
        publishMavenStyle         := true,
        autoScalaLibrary          := false,  //don't attach scala libs as dependencies
        description               := "project for publishing dependency to maven repo, use 'sbt publishLocal' to install it",
        packageBin in Compile     := baseDirectory.value / s"${name.value}.jar",
        packageDoc in Compile     := baseDirectory.value / s"${name.value}-javadoc.jar"
      )
    
  3. Call publishLocal task from sbt/activator (I did it from activator and prefixed it with proejct name):

    ./activator wetElephant/publishLocal                   
    

... and read the output to see what and where was published:

    /cygdrive/d/devstation-workspace/projects/m4l-patches 1
      [info] Loading project definition from D:\devstation-workspace\projects\m4l-patches\project
      [info] Set current project to m4l-patches (in build file:/D:/devstation-workspace/projects/m4l-patches/)
      [info] Updating {file:/D:/devstation-workspace/projects/m4l-patches/}wetElephant...
    [info] Packaging D:\devstation-workspace\projects\m4l-patches\modules\wetElephant\target\wetelephant-0.1-IDonKnow-sources.jar ...
    [info] Done packaging.
    [info] Wrote D:\devstation-workspace\projects\m4l-patches\modules\wetElephant\target\wetelephant-0.1-IDonKnow.pom
      [info] Resolving org.fusesource.jansi#jansi;1.4 ...4 ....
    [info] Done updating.
    [info] :: delivering :: com.stolenLibs#wetelephant;0.1-IDonKnow :: 0.1-IDonKnow :: release :: Sun Dec 20 20:09:24 CET 2015
      [info]  delivering ivy file to D:\devstation-workspace\projects\m4l-patches\modules\wetElephant\target\ivy-0.1-IDonKnow.xml
      [info]  published wetelephant to C:\Users\pawell\.ivy2\local\com.stolenLibs\wetelephant\0.1-IDonKnow\poms\wetelephant.pom
      [info]  published wetelephant to C:\Users\pawell\.ivy2\local\com.stolenLibs\wetelephant\0.1-IDonKnow\jars\wetelephant.jar
      [info]  published wetelephant to C:\Users\pawell\.ivy2\local\com.stolenLibs\wetelephant\0.1-IDonKnow\srcs\wetelephant-sources.jar
      [info]  published wetelephant to C:\Users\pawell\.ivy2\local\com.stolenLibs\wetelephant\0.1-IDonKnow\docs\wetelephant-javadoc.jar
      [info]  published ivy to C:\Users\pawell\.ivy2\local\com.stolenLibs\wetelephant\0.1-IDonKnow\ivys\ivy.xml
      [success] Total time: 1 s, completed 2015-12-20 20:09:24
  1. Optionally use these libraries in another project

    libraryDependencies += "com.stolenLibs" % "wetElephant" % "0.1-IDontKnow"
    

Disclaimer: I don't know how not to publish sources...

陪你搞怪i 2024-12-17 03:28:25

这是几个月前我关注的一篇博客文章,将 sbt 工件推送到 Maven 存储库(本地和远程)。

http://brizzled.clapper.org/id/100/

Here is a blog post I followed to push sbt artifact to a maven repository (local and remote) a few months ago.

http://brizzled.clapper.org/id/100/

八巷 2024-12-17 03:28:25

我创建了一个示例 Play Framework/sbt 项目,该项目在此处创建本地存储库(不仅仅是 publish-local):https://github.com/muymoo/local-ivy-repo-sbt
具体看Build.scala


makeLocalRepoSettings(已发布项目):_*

<代码>
localRepoArtifacts +=“org.apache.ws.security”%“wss4j”%“1.6.9”

这些 localRepoArtifacts 在我的本地 ivy 存储库中找到,但我认为您也可以编辑它以使用普通的旧 jar 文件。

运行: play local-repository-created

它是 https 的更简单版本://github.com/sbt/sbt-remote-control 在其 Build.scala 中做了更多的事情。

I created a sample Play Framework/sbt project that creates a local repository (not just publish-local) here: https://github.com/muymoo/local-ivy-repo-sbt
Specifically look at Build.scala


makeLocalRepoSettings(publishedProjects):_*

and


localRepoArtifacts += "org.apache.ws.security" % "wss4j" % "1.6.9"

These localRepoArtifacts are found in my local ivy repo, but I think you could edit this to work with plain old jar files as well.

To run: play local-repository-created

It is a simpler version of https://github.com/sbt/sbt-remote-control which does a whole lot more in their Build.scala.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文