How to find unused sbt dependencies? - scala

My build.sbt has a lot of dependencies now. How do I know which dependencies are actually being used?
Maven seems to have dependency:analyse http://maven.apache.org/plugins/maven-dependency-plugin/
Is there something similar for sbt?

There is the sbt-explicit-dependencies plugin, which has been developed recently. It has direct commands in the SBT console to:
Enforce explicit direct declaration of dependencies, thus disallowing transitive dependencies.
Detect and remove unneeded dependencies.

you can use sbt-dependency-graph plugin. it shows dependencies in different graphical representations. also you can try to use tattletale, but it's not integrated with sbt. it'll require you to copy managed dependencies (retrieveManaged := true). this tool not only shows dependency graph, but analyzes class usage and can display unused dependencies (including transitive)

Related

How to identify source of specific dependency in SBT

When running an SBT compile, I can see a specific dependency with a specific version coming in in the build log:
[info] Resolving junit#junit;4.12 ...
This dependency is not mentioned explicity in any of the build.sbt files, so it is either coming in as a transitive dependency or maybe from the Play framework itself.
Any idea how I can trace the exact source of this particular Junit version dependency? I have already tried to get it from the SBT dependency tree. However, this includes only the same "Resolving..." msg, but not where it's been resolved from.
Thanks!
Before there was a plugin for inspecting dependencies: sbt-dependency-graph. Now (since sbt 1.4) it's part of the core sbt and can be enabled by adding addDependencyTreePlugin to your project/plugins.sbt file. Then you can use commands like whatDependsOn:
whatDependsOn <organization> <module> <revision>?: Find out what depends on an artifact. Shows a reverse dependency tree for the selected module. The <revision> argument is optional.
Or, if it doesn't help, run dependencyBrowseGraph or dependencyBrowseTree, and inspect the graph/tree (there is a search field).

How to use SBT's externalPom() command

I have a Maven POM file that the deployment engineers need to deploy the system in the enterprise. I have developers using SBT for a Scala project. They use SBT targets that just aren't supported in Maven. We'd like to use the Maven POM file to define the dependencies, slurp in those dependencies in SBT, and define SBT development targets there.
According to the SBT documentation, the externalPom() command is the way to do that. But even with the simplest POM file (two developers have tried this with two different simple POM files that defined different dependencies), the externalPom() command seems to half work. The SBT targets clearly recognize the dependency defined in the POM, but can't resolve it. This error arises:
Cannot add dependency 'commons-collections#commons-collections;3.2.2'
to configuration 'default' of module
default#maven-sbt$sources_javadoc;0.1-SNAPSHOT because this
configuration doesn't exist!
When the externalPom() command is commented out and the equivalent dependency added directly in the build.sbt file everything goes swimmingly. The dependency comes directly from Maven Central in both cases; one from copying the dependency from the Maven tab and one from copying the dependency from the SBT tab. Once again, two developers are seeing exactly the same thing, from two different dependencies. The thing that's common is that both developers have reduced the build.sbt file down to a single statement. In the "slurp from POM" case, that statement is externalPom(). In the "plain old SBT" case, that statement is the dependency copied from Maven Central. The POM file is a dependency list with a single dependency (as simple as we can make it and still test externalPom()).
We suspect that we need something else in the build.sbt to make the externalPom() command work but we don't know what it is. Any help with that would be greatly appreciated.
I did some experimentation with this, and was able to replicate your error in my experiments.
I'm still a bit of a Scala / SBT newbie, but I created a build.sbt file that looks like so:
val Default = config("default")
lazy val root = (project in file(".")).
configs(Default).
settings(
externalPom()
)
This did compile for me!
One non-obvious catch: I had to make sure to include the scala-library in my POM file as a dependency

Why does Eclipse turn a maven runtime dependency into a compile dependency?

I have a maven project imported into Eclipse Oxygen. Eclipse reports no compile issues (Alt + F5). When I run maven from the command line I get
[ERROR] /home/dean/src/TAP3UIs/TAP3Desktop/src/main/java/com/ms/tap3/controller/RequestAccessController.java:[8,30] package com.google.common.base does not exist
That package does exist in my .m2/repository in guava-15.0.jar. I can also see it in Eclipse mvn dependencies. When I check the mvn dependency:tree for the project I see
[INFO] | | | +- com.google.guava:guava:jar:15.0:runtime
It is a runtime transitive dependency on the command line, which explains why it doesn't compile on the command line. Somehow Eclipse has turned a transitive dependency from runtime to compile.
Does anyone know why this happens and how I make Eclispe m2e respect the scope of the transitive dependencies?
Currently, neither JDT nor m2e support multiple classpaths per project which is required to support different scopes.
See: Eclipse bug 486035 - Different classpath containers for different scopes
Update:
Since Eclipse Photon (4.8) which was released in June 2018 this is now supported. See Eclipse bug 526858 and my video showing this in action.
The main point is this: If you import external classes in your source code, you must set them as compile dependencies, and never trust the fact that they might already be transitive dependencies (because, since they are transitive, you have not direct control over them, so in a future version they might diseappear as well).
What is happening is this:
You need some classes from com.google.common.base package, so you need to set com.google.guava:guava:jar:15.0 as a dependency.
Instead, you didn't because you realised it was already a transitive dependency, but you missed the fact that is a runtime dependency.
Eclipse M2 does not distinguish the different Maven's standard classpaths, so it treats all dependencies as if they were of "compile" scope. So Eclipse includes guava-15.0.jar in the compilation and the project is compiled OK.
Maven, instead, won't include a runtime dependency in the compilation phase, so a compile error is raised.
In brief: You should include guava-15.0 (as well as any other artifact your code needs) as a direct dependency (with compile scope) in your pom file.

How to find the parent of a Maven transitive dependency

I am building a Java (web) application with Maven and Eclipse.
When I look inside my .war file I can see the following logging libraries there:
log4j-1.2.14.jar
log4j-1.2.17.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
I did not declared these libraries in my pom.xml, so they probably are transitive dependencies (i.e. dependencies of my dependencies).
How can I find out which of my dependencies depend on these libraries?
I tried to use the mvn dependency:tree plugin, but it does not show any of these .jars.
In Eclipse, the Java Resources > Libraries > Maven Dependencies node does not show them either. Though, curiously, it shows other transitive dependencies of my project.
If you want to rely on maven only you may want to take a closer look on the dependency plugin, here are two examples:
mvn dependency:tree -Dverbose will display more detailed information - especially for example if a artifact will be omitted for conflicting with another artifacts version (e.g. convergence issue). It will also display you the hirachy with all the transitive dependencies.
To have a specific artefact analyzed (to for example find who delivers a specific transitive dependency) you can specify like so:
mvn dependency:tree -Dincludes=com.my.group.id:my-artefact-id:jar:1.0.1 -Dverbose
(Where you obviously need to adjust the artefact, packaging type and version according to your needs)
Open pom.xml in Eclipse and go to Dependency Hierarchy tab
![enter image description here][1]
It should show you the dependency tree in the Dependency Hierarchy tab. Evgeniy Dorofeev is right.
Something similar to the screenshot attached
you would probably have to expand all.
EDIT: Refined the answer.
Thats a little weird. But here is what i found.
If you go to http://mvnrepository.com/artifact/log4j/log4j/1.2.17
it will show you which dependencies are used and which does the jar file depend on.
The springframework dependency for web-mvc 3.2.4 would download the spring-core and the spring-core dependency uses log4j.

How to enforce SBT to always fetch sources for project dependencies?

Just getting started with SBT, and I would like to setup it up to always get sources for dependencies it downloads. Having to add to remember to type withSources() for every dependency declared is only mildly annoying, but the real killer is the dependencies of dependencies problem.
Anybody know a simple switch to turn this on?
try this https://github.com/OlegYch/sbt-sources-plugin