= Modules
:page-aliases: Modules.adoc

`mill.Module` serves two main purposes:

1. As ``object``s, they serve as namespaces that let you group related ``Task``s
   together to keep things neat and organized.

2. As ``trait``s, they are re-usable templates that let you replicate groups
   of related ``Task``s and sub-``Module``s while allowing customizations

Mill comes with built-in modules such as `mill.scalalib.ScalaModule` and
`mill.scalalib.CrossSbtModule`, but you can also define your own modules to do
things that are not built-in to Mill.

include::partial$example/fundamentals/modules/6-modules.adoc[]

== Root Modules

include::partial$example/fundamentals/modules/7-root-module.adoc[]

== Use Case: DIY Java Modules

include::partial$example/fundamentals/modules/8-diy-java-modules.adoc[]

== Default Tasks

include::partial$example/fundamentals/modules/12-default-tasks.adoc[]

== Backticked Names

include::partial$example/fundamentals/modules/9-backticked-names.adoc[]


== External Modules

Libraries for use in Mill can define ``ExternalModule``s: ``Module``s which are
shared between all builds which use that library:

[source,scala]
----
package foo
import mill.*

object Bar extends mill.api.ExternalModule {
  def baz = Task { 1 }
  def qux() = Task.Command { println(baz() + 1) }

  lazy val millDiscover = Discover[this.type]
}
----

In the above example, `Bar` is an `ExternalModule` living within the `foo`
Java package, containing the `baz` task and `qux` command. Those can be run
from the command line via:

[source,console]
----
> mill foo.Bar/baz
> mill foo.Bar/qux
----

``ExternalModule``s are useful for someone providing a library for use with Mill
that is shared by the entire build: for example,
`mill.scalalib.JvmWorkerApi/jvmWorker` provides a shared Scala compilation
service & cache that is shared between all ``ScalaModule``s, and
`mill.scalalib.GenIdea/idea` lets you generate IntelliJ projects without
needing to define your own `Task.Command` in your `build.mill` file

=== Aliasing External Modules

include::partial$example/fundamentals/modules/10-external-module-aliases.adoc[]

== Abstract Modules References

include::partial$example/fundamentals/modules/11-abstract-module-ref.adoc[]
