I'm new to Scala and sbt and I can't understand what the differences are between:
libraryDependencies ++= Seq(...)
and
libraryDependencies += ...
Most of the time, you can simply list your dependencies in the setting libraryDependencies . It's also possible to write a Maven POM file or Ivy configuration file to externally configure your dependencies, and have sbt use those external configuration files.
Apache Ivy is a transitive package manager. It is a sub-project of the Apache Ant project, with which Ivy works to resolve project dependencies. An external XML file defines project dependencies and lists the resources necessary to build a project.
By default, sbt uses the standard Ivy home directory location ${user. home}/. ivy2/ . This can be configured machine-wide, for use by both the sbt launcher and by projects, by setting the system property sbt.
The sbt-assembly Plugin In that case, we can mark that dependency as “provided” in our build. sbt file. The “provided” keyword indicates that the dependency is provided by the runtime, so there's no need to include it in the JAR file.
See Appending to previous values: += and ++=:
Assignment with
:=
is the simplest transformation, but keys have other methods as well. If theT
inSettingKey[T]
is a sequence, i.e. the key’s value type is a sequence, you can append to the sequence rather than replacing it.
+=
will append a single element to the sequence.++=
will concatenate another sequence.For example, the key
sourceDirectories in Compile
has aSeq[File]
as its value.
It is += dep
or ++= Seq(dep, dep2, dep3)
: "Of course, you can also use ++= to add a list of (read: multiple) dependencies all at once".
See collections.Seq for the +
("append an item") and ++
("append a sequence") operators.
One way to strip this down to a simpler problem is to look at the two parts in separation.
First, there's the ...=
part. You're probably familiar with this syntax from Java and other languages, but it performs the ...
operator on both the left and right operands, and stores it back into the left operand - in this case, it stores it back into libraryDependencies.
Second, there's the choice of either ++
or +
. If you take a look at the Seq Scaladoc then you'll find these two operators. The difference here is that:
+
takes a single element on the left-hand side, and prepends it to a sequence, returning a new sequence as the result.++
takes a sequence on the left and a sequence on the right and returns a new sequence of both.In practice, this means that you will get different results from the two.
List(1, 2, 3) ++ List(4, 5, 6)
will return List(1, 2, 3, 4, 5, 6)
, but...List(1, 2, 3) + List(4, 5, 6)
will return List(List(1, 2, 3), 4, 5, 6)
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With