|
| 1 | + |
| 2 | +<!DOCTYPE html> |
| 3 | +<html lang="zh"> |
| 4 | +<head> |
| 5 | + <meta charset="utf-8" /> |
| 6 | + <title>maven项目构建加速(一)命令参数篇 | Tom Xin's Blog</title> |
| 7 | + <meta name="author" content="Tom Xin" /> |
| 8 | + <meta name="description" content="相信光吗?" /> |
| 9 | + <meta name="keywords" content="java,sql,develop" /> |
| 10 | + <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" /> |
| 11 | + <link rel="icon" href="https://q.qlogo.cn/g?b=qq&nk=766781886&s=0" /> |
| 12 | + <link rel="preconnect" href="https://s4.zstatic.net" /> |
| 13 | +<script src="https://s4.zstatic.net/ajax/libs/vue/3.3.7/vue.global.prod.min.js"></script> |
| 14 | +<link rel="stylesheet" href="https://s4.zstatic.net/ajax/libs/font-awesome/6.4.2/css/all.min.css" /> |
| 15 | +<link rel="preconnect" href="https://fonts.loli.net" /> |
| 16 | +<link rel="preconnect" href="https://gstatic.loli.net" crossorigin /> |
| 17 | +<link rel="stylesheet" href="https://fonts.loli.net/css2?family=Fira+Code:wght@400;500;600;700&family=Lexend:wght@400;500;600;700;800;900&family=Noto+Sans+SC:wght@400;500;600;700;800;900&display=swap" /> |
| 18 | +<script> const mixins = {}; </script> |
| 19 | + |
| 20 | +<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script> |
| 21 | + |
| 22 | + |
| 23 | +<script src="https://s4.zstatic.net/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script> |
| 24 | +<script src="https://s4.zstatic.net/ajax/libs/highlightjs-line-numbers.js/2.8.0/highlightjs-line-numbers.min.js"></script> |
| 25 | +<link |
| 26 | + rel="stylesheet" |
| 27 | + href="https://s4.zstatic.net/ajax/libs/highlight.js/11.9.0/styles/github.min.css" |
| 28 | +/> |
| 29 | +<script src="/js/lib/highlight.js"></script> |
| 30 | + |
| 31 | + |
| 32 | +<script src="https://s4.zstatic.net/ajax/libs/KaTeX/0.16.9/katex.min.js"></script> |
| 33 | +<script src="https://s4.zstatic.net/ajax/libs/KaTeX/0.16.9/contrib/auto-render.min.js"></script> |
| 34 | +<link rel="stylesheet" href="https://s4.zstatic.net/ajax/libs/KaTeX/0.16.9/katex.min.css" /> |
| 35 | +<script src="/js/lib/math.js"></script> |
| 36 | + |
| 37 | + |
| 38 | +<script src="/js/lib/preview.js"></script> |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +<link rel="stylesheet" href="/css/main.css" /> |
| 49 | + |
| 50 | +<meta name="generator" content="Hexo 6.3.0"></head> |
| 51 | +<body> |
| 52 | + <div id="layout"> |
| 53 | + <transition name="fade"> |
| 54 | + <div id="loading" v-show="loading"> |
| 55 | + <div id="loading-circle"> |
| 56 | + <h2>LOADING</h2> |
| 57 | + <p>加载过慢请开启缓存 浏览器默认开启</p> |
| 58 | + <img src="/images/loading.gif" /> |
| 59 | + </div> |
| 60 | + </div> |
| 61 | + </transition> |
| 62 | + <div id="menu" :class="{ hidden: hiddenMenu, 'menu-color': menuColor}"> |
| 63 | + <nav id="desktop-menu"> |
| 64 | + <a class="title" href="/"> |
| 65 | + <span>TOM XIN'S BLOG</span> |
| 66 | + </a> |
| 67 | + |
| 68 | + <a href="/"> |
| 69 | + <i class="fa-solid fa-house fa-fw"></i> |
| 70 | + <span> Home</span> |
| 71 | + </a> |
| 72 | + |
| 73 | + <a href="/about"> |
| 74 | + <i class="fa-solid fa-id-card fa-fw"></i> |
| 75 | + <span> About</span> |
| 76 | + </a> |
| 77 | + |
| 78 | + <a href="/archives"> |
| 79 | + <i class="fa-solid fa-box-archive fa-fw"></i> |
| 80 | + <span> Archives</span> |
| 81 | + </a> |
| 82 | + |
| 83 | + <a href="/categories"> |
| 84 | + <i class="fa-solid fa-bookmark fa-fw"></i> |
| 85 | + <span> Categories</span> |
| 86 | + </a> |
| 87 | + |
| 88 | + <a href="/tags"> |
| 89 | + <i class="fa-solid fa-tags fa-fw"></i> |
| 90 | + <span> Tags</span> |
| 91 | + </a> |
| 92 | + |
| 93 | + </nav> |
| 94 | + <nav id="mobile-menu"> |
| 95 | + <div class="title" @click="showMenuItems = !showMenuItems"> |
| 96 | + <i class="fa-solid fa-bars fa-fw"></i> |
| 97 | + <span> TOM XIN'S BLOG</span> |
| 98 | + </div> |
| 99 | + <transition name="slide"> |
| 100 | + <div class="items" v-show="showMenuItems"> |
| 101 | + |
| 102 | + <a href="/"> |
| 103 | + <div class="item"> |
| 104 | + <div style="min-width: 20px; max-width: 50px; width: 10%"> |
| 105 | + <i class="fa-solid fa-house fa-fw"></i> |
| 106 | + </div> |
| 107 | + <div style="min-width: 100px; max-width: 150%; width: 20%">Home</div> |
| 108 | + </div> |
| 109 | + </a> |
| 110 | + |
| 111 | + <a href="/about"> |
| 112 | + <div class="item"> |
| 113 | + <div style="min-width: 20px; max-width: 50px; width: 10%"> |
| 114 | + <i class="fa-solid fa-id-card fa-fw"></i> |
| 115 | + </div> |
| 116 | + <div style="min-width: 100px; max-width: 150%; width: 20%">About</div> |
| 117 | + </div> |
| 118 | + </a> |
| 119 | + |
| 120 | + <a href="/archives"> |
| 121 | + <div class="item"> |
| 122 | + <div style="min-width: 20px; max-width: 50px; width: 10%"> |
| 123 | + <i class="fa-solid fa-box-archive fa-fw"></i> |
| 124 | + </div> |
| 125 | + <div style="min-width: 100px; max-width: 150%; width: 20%">Archives</div> |
| 126 | + </div> |
| 127 | + </a> |
| 128 | + |
| 129 | + <a href="/categories"> |
| 130 | + <div class="item"> |
| 131 | + <div style="min-width: 20px; max-width: 50px; width: 10%"> |
| 132 | + <i class="fa-solid fa-bookmark fa-fw"></i> |
| 133 | + </div> |
| 134 | + <div style="min-width: 100px; max-width: 150%; width: 20%">Categories</div> |
| 135 | + </div> |
| 136 | + </a> |
| 137 | + |
| 138 | + <a href="/tags"> |
| 139 | + <div class="item"> |
| 140 | + <div style="min-width: 20px; max-width: 50px; width: 10%"> |
| 141 | + <i class="fa-solid fa-tags fa-fw"></i> |
| 142 | + </div> |
| 143 | + <div style="min-width: 100px; max-width: 150%; width: 20%">Tags</div> |
| 144 | + </div> |
| 145 | + </a> |
| 146 | + |
| 147 | + </div> |
| 148 | + </transition> |
| 149 | + </nav> |
| 150 | +</div> |
| 151 | +<transition name="fade"> |
| 152 | + <div id="menu-curtain" @click="showMenuItems = !showMenuItems" v-show="showMenuItems"></div> |
| 153 | +</transition> |
| 154 | + |
| 155 | + <div id="main" :class="loading ? 'into-enter-from': 'into-enter-active'"> |
| 156 | + <div class="article"> |
| 157 | + <div> |
| 158 | + <h1>maven项目构建加速(一)命令参数篇</h1> |
| 159 | + </div> |
| 160 | + <div class="info"> |
| 161 | + <span class="date"> |
| 162 | + <span class="icon"> |
| 163 | + <i class="fa-solid fa-calendar fa-fw"></i> |
| 164 | + </span> |
| 165 | + 2022/9/12 |
| 166 | + </span> |
| 167 | + |
| 168 | + <span class="category"> |
| 169 | + <a href="/categories/%E6%95%99%E7%A8%8B/"> |
| 170 | + <span class="icon"> |
| 171 | + <i class="fa-solid fa-bookmark fa-fw"></i> |
| 172 | + </span> |
| 173 | + 教程 |
| 174 | + </a> |
| 175 | + </span> |
| 176 | + |
| 177 | + |
| 178 | + <span class="tags"> |
| 179 | + <span class="icon"> |
| 180 | + <i class="fa-solid fa-tags fa-fw"></i> |
| 181 | + </span> |
| 182 | + |
| 183 | + |
| 184 | + <span class="tag"> |
| 185 | + |
| 186 | + <a href="/tags/maven/" style="color: #03a9f4">maven</a> |
| 187 | + </span> |
| 188 | + |
| 189 | + </span> |
| 190 | + |
| 191 | + </div> |
| 192 | + |
| 193 | + <div class="content" v-pre> |
| 194 | + <p>比如我们的项目结构如下,下面将使用maven自带的命令参数对该项目构建进行优化。本教程中仅使用了部分用于加速构建的maven参数,更多命令参数请参考官方文档 <a target="_blank" rel="noopener" href="https://maven.apache.org/ref/3.8.6/maven-embedder/cli.html">Maven CLI Options Reference</a>。</p> |
| 195 | +<pre><code class="shell">parent |
| 196 | + ├─childA(model层) |
| 197 | + │ └─pom.xml(jar) |
| 198 | + ├─childB(service层) |
| 199 | + │ └─pom.xml(jar) |
| 200 | + ├─childC(web层) |
| 201 | + │ └─pom.xml(war) |
| 202 | + └─pom.xml(pom) |
| 203 | +</code></pre> |
| 204 | +<h1 id="一、指定模块构建"><a href="#一、指定模块构建" class="headerlink" title="一、指定模块构建"></a>一、指定模块构建</h1><p>maven构建命令如<code>maven install</code>默认会构建当前项目中的所有模块,参数<code>-pl</code>能指定要构建的模块,一般配合<code>-am</code>参数一起使用。参数的作用如下:</p> |
| 205 | +<table> |
| 206 | +<thead> |
| 207 | +<tr> |
| 208 | +<th align="left">参数</th> |
| 209 | +<th>作用</th> |
| 210 | +</tr> |
| 211 | +</thead> |
| 212 | +<tbody><tr> |
| 213 | +<td align="left"><code>-pl,--projects <arg></code></td> |
| 214 | +<td>手动选择需要构建的项目,项目间以逗号分隔,<arg>可以是[group]:artifactId形式,也可以是相对路径</td> |
| 215 | +</tr> |
| 216 | +<tr> |
| 217 | +<td align="left"><code>-am,--also-make</code></td> |
| 218 | +<td>构建指定模块,同时构建指定模块依赖的其他模块</td> |
| 219 | +</tr> |
| 220 | +</tbody></table> |
| 221 | +<h2 id="例子:"><a href="#例子:" class="headerlink" title="例子:"></a>例子:</h2><p>例子1:对parent、childB、childA进行构建,并安装到本地maven仓库中,childC将不被构建。</p> |
| 222 | +<pre><code class="shell">mvn clean install -pl childB -am |
| 223 | +</code></pre> |
| 224 | +<p>注意:在IntelliJ IDEA中,在子模块中点击右键-》maven-》Run Maven-》<code>install</code>,这个操作与使用命令<code>mvn install - pl <module> -am</code>效果不同。前者在还没有将整个项目install的情况下,可能会因找不到依赖而报错。</p> |
| 225 | +<h1 id="二、跳过测试"><a href="#二、跳过测试" class="headerlink" title="二、跳过测试"></a>二、跳过测试</h1><p>跳过测试有两种,<code>-DskipTests</code>和<code>-Dmaven.test.skip=true</code>区别如下:</p> |
| 226 | +<table> |
| 227 | +<thead> |
| 228 | +<tr> |
| 229 | +<th>参数</th> |
| 230 | +<th>作用</th> |
| 231 | +</tr> |
| 232 | +</thead> |
| 233 | +<tbody><tr> |
| 234 | +<td>-DskipTests</td> |
| 235 | +<td>不执行测试用例,但编译测试用例类生成相应的class文件至target/test-classes下</td> |
| 236 | +</tr> |
| 237 | +<tr> |
| 238 | +<td>-Dmaven.test.skip=true</td> |
| 239 | +<td>不执行测试用例,也不编译测试用例类</td> |
| 240 | +</tr> |
| 241 | +</tbody></table> |
| 242 | +<h2 id="例子:-1"><a href="#例子:-1" class="headerlink" title="例子:"></a>例子:</h2><p>例子1:将跳过当前项目所有模块中的的测试用例的执行</p> |
| 243 | +<pre><code class="shell">mvn clean install -DskipTests |
| 244 | +</code></pre> |
| 245 | +<p>例子2:跳过当前项目所有模块中的的测试用例的执行,并且不会编译测试用例的类</p> |
| 246 | +<pre><code class="shell">mvn clean install -Dmaven.test.skip=true |
| 247 | +</code></pre> |
| 248 | +<p>在IntelliJ IDEA中跳过测试,作用等同于<code>-DskipTests</code>参数</p> |
| 249 | +<img src="/2022/09/12/maven%E9%A1%B9%E7%9B%AE%E6%9E%84%E5%BB%BA%E5%8A%A0%E9%80%9F%EF%BC%88%E4%B8%80%EF%BC%89%E5%91%BD%E4%BB%A4%E5%8F%82%E6%95%B0%E7%AF%87/2022-09-12_140947.png" class="" title="idea跳过测试用例"> |
| 250 | + |
| 251 | +<p>如需跳过指定模块的测试用例,请参考官方文档 <a target="_blank" rel="noopener" href="https://maven.apache.org/plugins-archives/maven-surefire-plugin-2.12.4/examples/skipping-test.html">Skipping Tests</a></p> |
| 252 | +<h1 id="三、开启离线模式"><a href="#三、开启离线模式" class="headerlink" title="三、开启离线模式"></a>三、开启离线模式</h1><p>默认情况下,maven构建时会定期检查SNAPSHOT版本依赖在远程仓库中的有无更新,如果可以确定没有更新可以添加<code>-o</code>参数,来跳过此检查。参数作用如下:</p> |
| 253 | +<table> |
| 254 | +<thead> |
| 255 | +<tr> |
| 256 | +<th>参数</th> |
| 257 | +<th>作用</th> |
| 258 | +</tr> |
| 259 | +</thead> |
| 260 | +<tbody><tr> |
| 261 | +<td>-o,–offline</td> |
| 262 | +<td>运行offline模式,不联网进行依赖更新</td> |
| 263 | +</tr> |
| 264 | +</tbody></table> |
| 265 | +<h2 id="例子:-2"><a href="#例子:-2" class="headerlink" title="例子:"></a>例子:</h2><p>例子1:开启离线模式</p> |
| 266 | +<pre><code class="shell">mvn clean install -o |
| 267 | +</code></pre> |
| 268 | +<p>在IntelliJ IDEA中开启离线模式,作用等同于<code>-o</code>参数</p> |
| 269 | +<img src="/2022/09/12/maven%E9%A1%B9%E7%9B%AE%E6%9E%84%E5%BB%BA%E5%8A%A0%E9%80%9F%EF%BC%88%E4%B8%80%EF%BC%89%E5%91%BD%E4%BB%A4%E5%8F%82%E6%95%B0%E7%AF%87/2022-09-12_180555.png" class="" title="idea开启离线模式"> |
| 270 | + |
| 271 | + |
| 272 | +<h1 id="四、多线程构建"><a href="#四、多线程构建" class="headerlink" title="四、多线程构建"></a>四、多线程构建</h1><p>maven从3.x版本开始支持多线程构建项目,不过需要手动开启。官方宣称大约20-50% 的速度提升,具体还要看项目结构、项目规模等因素。开启多线程构建前,还应评估兼容性,maven核心的功能都是支持多线程的,但是个别第三方maven插件可能不是线程安全的,详情见<a target="_blank" rel="noopener" href="https://cwiki.apache.org/confluence/display/MAVEN/Parallel+builds+in+Maven+3">Parallel builds in Maven 3</a>。开启多线程的命令参数作用如下:</p> |
| 273 | +<table> |
| 274 | +<thead> |
| 275 | +<tr> |
| 276 | +<th>参数</th> |
| 277 | +<th>作用</th> |
| 278 | +</tr> |
| 279 | +</thead> |
| 280 | +<tbody><tr> |
| 281 | +<td>-Dmaven.compile.fork=true</td> |
| 282 | +<td>开启多线程编译</td> |
| 283 | +</tr> |
| 284 | +<tr> |
| 285 | +<td>-T,–threads <arg></td> |
| 286 | +<td>线程数,<br/>-T 4 是直接指定4线程<br/>-T 1C 表示CPU线程的倍数</td> |
| 287 | +</tr> |
| 288 | +</tbody></table> |
| 289 | +<h2 id="例子:-3"><a href="#例子:-3" class="headerlink" title="例子:"></a>例子:</h2><p>例子1:开启四线程进行并发构建</p> |
| 290 | +<pre><code class="shell">mvn clean install -Dmaven.compile.fork=true -T 4 |
| 291 | +</code></pre> |
| 292 | +<p>在IntelliJ IDEA中开启多线程构建,作用等同于<code>-Dmaven.compile.fork=true -T 4</code>参数</p> |
| 293 | +<img src="/2022/09/12/maven%E9%A1%B9%E7%9B%AE%E6%9E%84%E5%BB%BA%E5%8A%A0%E9%80%9F%EF%BC%88%E4%B8%80%EF%BC%89%E5%91%BD%E4%BB%A4%E5%8F%82%E6%95%B0%E7%AF%87/2022-09-12_181509.png" class="" title="idea开启多线程构建"> |
| 294 | + |
| 295 | + </div> |
| 296 | + |
| 297 | + |
| 298 | + |
| 299 | + |
| 300 | + |
| 301 | + |
| 302 | + |
| 303 | +</div> |
| 304 | + |
| 305 | + <footer id="footer"> |
| 306 | + <div id="footer-wrap"> |
| 307 | + <div> |
| 308 | + © |
| 309 | + 2022 - 2024 Tom Xin's Blog |
| 310 | + <span id="footer-icon"> |
| 311 | + <i class="fa-solid fa-font-awesome fa-fw"></i> |
| 312 | + </span> |
| 313 | + @Tom Xin |
| 314 | + </div> |
| 315 | + <div> |
| 316 | + Based on the <a target="_blank" rel="noopener" href="https://hexo.io">Hexo Engine</a> & |
| 317 | + <a target="_blank" rel="noopener" href="https://github.com/theme-particlex/hexo-theme-particlex">ParticleX Theme</a> |
| 318 | + </div> |
| 319 | + |
| 320 | + </div> |
| 321 | +</footer> |
| 322 | + |
| 323 | + </div> |
| 324 | + |
| 325 | + <transition name="fade"> |
| 326 | + <div id="preview" ref="preview" v-show="previewShow"> |
| 327 | + <img id="preview-content" ref="previewContent" /> |
| 328 | + </div> |
| 329 | + </transition> |
| 330 | + |
| 331 | + </div> |
| 332 | + <script src="/js/main.js"></script> |
| 333 | + |
| 334 | + |
| 335 | + |
| 336 | + |
| 337 | + |
| 338 | + |
| 339 | + |
| 340 | +</body> |
| 341 | +</html> |
0 commit comments