Skip to content

/flags/maven/mvn-threads-parallel-build

mvn -T 1C: build Maven modules in parallel threads

The commands on this page have not been executed by our verification runner yet.updated
tool
Maven
command
mvn
flag
-T, --threads N|NC
default
1 (modules are built one after another)

mvn -T 4 install (long form --threads) builds up to four modules of a multi-module project at the same time, respecting the dependency order between them. -T 1C uses one thread per CPU core, -T 2C two per core. On a reactor with many independent modules this can cut build times considerably; a single-module project gains nothing, because Maven parallelises across modules, not inside one.

Forms

ValueMeaning
-T 4Exactly 4 threads
-T 1COne thread per CPU core
-T 2C / -T 0.5CTwo threads per core / half as many threads as cores
-T 1Sequential build (the default)
.mvn/maven.config with -T1CApply it to every build of the project
bash
mvn -T 1C clean install
mvn -T 1C -pl web -am verify        # combine with module selection
mvn -T 4 -Dsurefire.printSummary=true test

How it schedules modules

Maven computes the reactor order as usual, then starts every module whose dependencies are already built. In a layout where api and worker both depend only on core, core is built first, then api and worker in parallel. The more independent branches the module graph has, the bigger the gain; a strict chain of modules stays sequential whatever the thread count.

Pitfalls

  • Plugins that are not thread-safe. Maven prints a warning block listing goals not marked @threadSafe. They usually still work but may corrupt shared state; update the plugins or run that build sequentially.
  • Interleaved logs. Output of parallel modules is mixed. Look at the Reactor Summary at the end to see which module failed, and resume with -rf :module.
  • Shared resources in tests. Integration tests of different modules that use the same port, database or temporary directory start failing randomly in parallel builds. Give each module its own resources, or skip those tests in the parallel run; skipTests vs maven.test.skip explains the two ways to skip.
  • Memory. Every thread runs compilers and test JVMs; raise the heap in MAVEN_OPTS or .mvn/jvm.config if the build starts swapping.
  • Test parallelism inside one module is a separate setting of Surefire (forkCount, parallel) and is independent from -T.

Selecting fewer modules is often an even bigger win than more threads: see mvn -pl -am.

FAQ

What does 1C mean in mvn -T 1C?

C multiplies by the number of CPU cores, so 1C is one thread per core and 2C two threads per core. A plain number such as -T 4 means exactly four threads.

Does mvn -T make a single-module project faster?

No. Parallelism is between modules: Maven builds independent modules of the reactor at the same time. A project with one module still runs its goals one after another.

Is it safe to use -T with every plugin?

Plugins have to be thread-safe. Maven prints a warning listing plugins whose goals are not marked thread-safe; most current plugins are, but check old or in-house ones before relying on parallel builds.

Sources