Maven: configure parallel build in pom.xml

后端 未结 3 2197
走了就别回头了
走了就别回头了 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: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.

提交回复
热议问题