Handling unmanaged class banners in a library using SBT so that a dependent project can access them

I am writing a library that depends on the code (let it be called foo.jar ), which is only available as a binary jar. As a standard, I put this in the lib/ directory, which SBT will consider as an unmanaged dependency. So far this is wonderful.

However, since this is a library, I would like to publish it so that other projects that depend on it also have access to unmanaged code in foo.jar without having to manually find it. Initially, I thought that I could use a plug-in with a smooth flag, for example SBT Assembly , to create a jar with dependencies, but this does not affect what is actually published using sbt publish-local - it only creates a live jar when you run sbt assembly . Is there a standard easy way to handle this? It seems like a bad idea for every library that uses unmanaged dependencies to break when used by other projects downstream, so I wonder if I am missing something obvious.

+4
source share
2 answers

I do not know if it is good to use sbt-assembly, as other libraries may depend on a different version of foo.jar , etc.

One way around this is to publish foo.jar to the Maven repository yourself. Some people from the Scala community and / or sbt talked about bintray . It is still in beta, but it looks promising if you want to post some jars.

+3
source

You can get the desired result by manipulating mappings in (Compile, packageBin) to include the files you want your packaged jar to have ( publish uses the output from packageBin ). This method will allow you to include absolutely any file that you want in the bank. The sbt whitepaper is here: http://www.scala-sbt.org/0.12.3/docs/Howto/package.html#contents

As an example, consider the general case of including a .properties file in your jar. Suppose you need to include "messages.properties" in the path "com / bigco / messages.properties" in your packaged jar. And let's say that this file is under src / main / scala / ... You can add the following to your build.sbt file:

 mappings in (Compile, packageBin) <+= baseDirectory map { base => (base / "src" / "main" / "scala" / "com" / "bigco" / "messages.properties") -> "com/bigco/messages.properties" } 

To try and answer your original question, you can unzip foo.jar and add each of the class files inside the packed jar according to their correct package paths. So, something similar to

 mappings in (Compile, packageBin) <+= baseDirectory map { base => (base / path / to / unzipped / file.class) -> "path.to.unzipped.file.class" ... } 

Or you can just get away with foo.jar in the root of a packaged jar like this:

 mappings in (Compile, packageBin) <+= baseDirectory map { base => (base / "lib" / "foo.jar") -> "foo.jar" } 
+1
source

Source: https://habr.com/ru/post/1497749/


All Articles