You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a scala application and I am packaging it using sbt-one-jar. I am trying to use the ClassUtil to get some annotation details from my jar file. However, I am not able to get the details from one-jar. It is working fine if I run directly in the project. But once the jar is created, it is not getting the classes. I know that the problem is with the one-jar as it packages the jar files in different folder. Is there any way to make the ClassUtil work with one-jar. My sample code with ClassUtil is given below.
def getAnnotations ={
val finder = ClassFinder()
println("full list "+finder.getClasses().size)
finder.getClasses().toList.map(x=> println(" -- "+x))
val classes = finder.getClasses().filter(c => c.implements("work.com.pack.FunctionMetadata"))
println("classes size = "+classes.size)
val allClasses = classes.map { clazz => Class.forName(clazz.name) }
println("size ="+allClasses.size)
val fmAnnotations = allClasses.map { clazz =>
val fields = clazz.getDeclaredFields.filter(field => field.getDeclaredAnnotations.nonEmpty)
val result = fields.toList.map { field =>
val result = field.getDeclaredAnnotations.find(a => a.annotationType().getName == classOf[MyAnnotation].getName)
if (result.isDefined) Some(result.get.asInstanceOf[MyAnnotation]) else None
}
result.filter(_.isDefined).map(_.get)
}.toList.flatten
println("Annotations = "+fmAnnotations)
fmAnnotations
}
The output I am getting is from OneJar classes. Output from running the above code is
This is problematic. One-Jar creates, in effect, a jar of jars, with custom One-Jar-provided classes and a classloader that knows how to dig your real classes out of the One-Jar consolidation.
There's certainly a "way" (several ways, in fact) to write code to make ClassUtil work with this kind of packaging mechanism, but I'm not convinced it's worth the effort. There are other "fat jar" mechanisms out there that don't use this kind of odd approach.
For instance, sbt-assembly also generates fat jars that can be run with java -jar thejar.jar, but they're flattened jars that contain all your dependencies—regular jars, in other words, that contain all the classes in all your dependencies. An sbt-assembly-created jar doesn't add special classes or use a special class loader; it's just a regular jar. There's a Maven Assembly Plugin that does the same thing with a Maven build.
I really don't like the One-Jar approach. Maven Assembly and sbt-assembly are much simpler (with far fewer moving parts in the final packaged jar).
At the moment, I'm disinclined to spend the time supporting One-Jar. If there's enough demand, I may reconsider.
I have a scala application and I am packaging it using sbt-one-jar. I am trying to use the ClassUtil to get some annotation details from my jar file. However, I am not able to get the details from one-jar. It is working fine if I run directly in the project. But once the jar is created, it is not getting the classes. I know that the problem is with the one-jar as it packages the jar files in different folder. Is there any way to make the ClassUtil work with one-jar. My sample code with ClassUtil is given below.
The output I am getting is from OneJar classes. Output from running the above code is
The text was updated successfully, but these errors were encountered: