6

I'm currently using the Maven Failsafe Plugin to do forked execution of tests (running multiple in parallel in separate JVMs).

I'm manually setting the forkCount variable based on the number of cores my machine has, but I'd like for this to be automatically determined by Maven, allowing me to end up with something like:

<forkCount>${system.numCores}</forkCount>

Is this possible?

Thom Shutt
  • 449
  • 5
  • 17

2 Answers2

4

You could try using the build-helper:cpu-count plugin

http://www.mojohaus.org/build-helper-maven-plugin/cpu-count-mojo.html

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
      <execution>
        <id>get-cpu-count</id>
        <goals>
          <goal>cpu-count</goal>
        </goals>
        <configuration>
          <cpuCount>system.numCores</cpuCount>
        </configuration>
      </execution>
    </executions>
  </plugin>

That will place the available number of cores into the variable system.numCores.

You can also specify a projection factor in the config e.g. <factor>0.75</factor> if you want to use a fraction of the available cores. This will adjust the number of cores by the given factor and guarantees to return an integer value of at least 1 i.e. Using a factor Will not reduce the number of calculated cores below 1.

RobV
  • 28,022
  • 11
  • 77
  • 119
2

You can resolve the number before on your command line.

Linux: nproc
Mac OS: sysctl -n hw.ncpu
Windows: wmic cpu get NumberOfCores, NumberOfLogicalProcessors/Format:List or use environment variable NUMBER_OF_PROCESSORS (use with ${env.NUMBER_OF_PROCESSORS})

Note - for windows, you need to tune the output and get the number.

Then, pass the number as a parameter to maven: -Dsystem.numCores=$NUM or
-Dsystem.numCores=%NUM%

EDIT: You can also generate a file with the property (maven exec plugin) and then read it with the maven properties plugin. All done during the same build, at different stages

I hope this helps.

Allenaz
  • 1,013
  • 10
  • 14
Eldad Assis
  • 10,464
  • 11
  • 52
  • 78
  • Thanks, but I'm looking for something that I can include in the POM (so that e.g other developers don't have to know about passing in the arg) – Thom Shutt Aug 11 '15 at 13:35
  • Are you windows or linux? – Eldad Assis Aug 11 '15 at 13:47
  • You can also generate a file with the property (maven exec plugin) and then read it with the maven properties plugin. All done during the same build, at different stages. – Eldad Assis Aug 11 '15 at 13:54
  • @Eidad AK - Both operating systems ideally. Generating a file sounds a bit hacky but should work, thanks. – Thom Shutt Aug 11 '15 at 14:08