Maven: configure parallel build in pom.xml

后端 未结 3 2198
走了就别回头了
走了就别回头了 2021-02-19 04:14

Maven has a capability to perform parallel builds: https://cwiki.apache.org/confluence/display/MAVEN/Parallel+builds+in+Maven+3

mvn -T 4 clean install # Builds w         


        
相关标签:
3条回答
  • 2021-02-19 04:26

    I could not find a way to configure this in the pom.xml or settings.xml There is a good solution on unix systems, edit your .bashrc and add an alias.

    alias mvnp='mvn -T 4'
    

    now from the terminal run maven using mvnp

    mvnp clean install
    
    0 讨论(0)
  • 2021-02-19 04:28

    This solution is a bit of a hack but worked for me. It involves specifying a new environment variable, assigning the value -T3 to it and adding this variable to the Maven launch script.

    For Windows (Linux in parens):

    1. Open the Environment Variables window: Computer -> Properties -> Advanced System settings -> Environment Variables
    2. Add the property MAVEN_CMD_LINE_OPTS with your desired value. In my case -T 3 as I want Maven to use 3 threads to build in parallel.
    3. Edit the mvn.cmd file (In Linux: the mvn file). Find the part where the Java command is actually executed, the line starting with %MAVEN_JAVA_EXE% (In Linux: generally after the line defining the main class: org.codehaus.plexus.classworlds.launcher.Launcher)

    4. Add %MAVEN_CMD_LINE_OPTS% to the end of the line (In Linux: $MAVEN_CMD_LINE_OPTS)

    When you run mvn compile on a Maven project you will now see the following line:

    Using the MultiThreadedBuilder implementation with a thread count of 3

    This has the advantage of the user being able to 'override' this value. So if the user executes mvn -T4 compile, then 4 threads are used instead of the default 3.

    Note:

    1. I tried this on Maven 3.3.9 but the concept should work on any Maven version.
    2. Multi-threaded builds can suffer from issues where plugins especially custom plugins are not thread safe. So use with care and consider disabling this as a fix in case of issues.
    0 讨论(0)
  • 2021-02-19 04:35

    You are able to specify the option in the MAVEN_OPTS environment variable (see http://maven.apache.org/guides/mini/guide-configuring-maven.html). Once this is done, you don't have to repeat it. Configuring the environment variable depends on your system. However this will affect all maven runs within your environment. Maybe it's possible for you to enable different environments, so that only the project you actually want to build in parallel is running in such an environment.

    0 讨论(0)
提交回复
热议问题