如今我们构建一个项目需要用到很多第三方的类库,如写一个使用spring的Web项目就需要引入大量的jar包。一个项目Jar包的数量之多往往让我们瞠目结舌,并且Jar包之间的关系错综复杂,一个Jar包往往又会引用其他Jar包,缺少任何一个Jar包都会导致项目编译失败。 以往开发项目时,程序员往往需要花较多的精力在引用Jar包搭建项目环境上,而这一项工作尤为艰难,少一个Jar包、多一个Jar包往往会报一些让人摸不着头脑的异常。 而Maven就是一款帮助程序员构建项目的工具,我们只需要告诉Maven需要哪些Jar 包,它会帮助我们下载所有的Jar,极大提升开发效率。
命令 内置变量
内置属性分为6种:
内置属性:如${basedir}表示项目根目录,${version}表示项目版本
POM属性:用户可以引用pom文件中对应的值。如:
${basedir} 项目根目录
${project.build.directory} 构建目录,缺省为target
${project.build.outputDirectory} 构建过程输出目录,缺省为target/classes
${project.build.finalName} 产出物名称,缺省为${project.artifactId}-${project.version}
${project.packaging} 打包类型,缺省为jar
${project.xxx} 当前pom文件的任意节点的内容
自定义属性:用户可以在pom的元素下自定义maven属性。
setting属性:用户可以使用以settings开头的属性引用settings.xml中xml元素的值,如${settings.localRepository}指向用户本地仓库的地址。
java系统属性:maven可以使用当前java系统的属性,如${user.home}指向了用户目录。
环境变量属性:所有环境变量都可以使用以env.开头的属性。如:${env.JAVA_HOE}。
私服仓库 镜像 代理 公司需要设置代理才能上网,而运行Maven时需要下载依赖的库。怎么办呢?原来Maven也像IE一样,可以设置代理的。
步骤如下:
编辑 ~/.m2/setting.xml 文件。如果该目录下没有该文件,复制 $M2_HOME/conf/setting.xml 找到 节点。去掉相应的注释,设置代理信息。
1 2 3 4 5 6 7 8 9 10 <proxy > <id > optional</id > <active > true</active > <protocol > http</protocol > <host > 10.50.135.250</host > <port > 8080</port > <nonProxyHosts > local.net|some.host.com</nonProxyHosts > </proxy >
id:代理的名称(随便设,XYZ也行)
active:表示该代理是否激活
protocol:代理协议,这个不用改
username:当代理需要认证时的用户名
password:当代理需要认证时的密码
host:代理的IP地址
port:代理的端口号
nonProxyHost:指定不需要使用代理的主机,可不设置。如果有多个,用 | 分隔
如果代理不需要用户认证,username 和 password 两个节点可注释掉)再打开Maven试试,需要下载依赖库时是不是就顺畅了?当然,要确认你的代理是可用的。
多模块 多模块打包 版本号
maven顶级pom和子pom的版本号批量修改
当一个版本发布,新起一个版本时,我们只需要手动修改一下项目中pom.xml的版本号就可以了。但是如果这个maven项目有很多的子模块项目,那么一个个手动的去改就显得费时费力又繁琐了。
还好,maven为我们提供了以下三个命令(需要进入顶级pom所在的目录)来帮助我们解决这个问题。
1、设置新的版本号mvn versions:set -DnewVersion=1.1.3-RELEASE
2、当新版本号设置不正确时可以撤销新版本号的设置mvn versions:revert
3、确认新版本号无误后提交新版本号的设置mvn versions:commit
命令 1、 mvn install -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
跳过测试,跳过Javadoc编译安装本地 2、 mvn deploy -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
跳过测试,跳过Javadoc发布
插件 依赖排查 Maven开发笔记(二)—— Maven中的可选依赖(Optional Dependencie… - 简书https://www.jianshu.com/p/3173a0154102
pom详解 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 <?xml version="1.0" encoding="UTF-8" ?> <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <parent > <artifactId /> <groupId /> <version /> <relativePath /> </parent > <modelVersion > 4.0.0</modelVersion > <groupId > cn.erhuowang</groupId > <artifactId > erhuowang-maven2</artifactId > <packaging > war</packaging > <version > 1.0-SNAPSHOT</version > <name > erhuo-maven</name > <url > http://erhuowang.cn</url > <description > A maven project to study maven.</description > <prerequisites > <maven /> </prerequisites > <issueManagement > <system > erhuowang</system > <url > http://erhuowang.cn</url > </issueManagement > <ciManagement > <system /> <url /> <notifiers > <notifier > <type /> <sendOnError /> <sendOnFailure /> <sendOnSuccess /> <sendOnWarning /> <address /> <configuration /> </notifier > </notifiers > </ciManagement > <inceptionYear /> <mailingLists > <mailingList > <name > Demo</name > <post > chaibozhou@163.com</post > <subscribe > chaibozhou@163.com</subscribe > <unsubscribe > chaibozhou@163.com</unsubscribe > <archive > chaibozhou@163.com</archive > </mailingList > </mailingLists > <developers > <developer > <id > HELLO WORLD</id > <name > chaimm</name > <email > chaibozhou@163.com</email > <url /> <roles > <role > Project Manager</role > <role > Architect</role > </roles > <organization > demo</organization > <organizationUrl > http://erhuowang.cn</organizationUrl > <properties > <dept > No</dept > </properties > <timezone > -5</timezone > </developer > </developers > <contributors > <contributor > <name /> <email /> <url /> <organization /> <organizationUrl /> <roles /> <timezone /> <properties /> </contributor > </contributors > <licenses > <license > <name > Apache 2</name > <url > http://www.baidu.com/erhuwoang/LICENSE-2.0.txt</url > <distribution > repo</distribution > <comments > A business-friendly OSS license</comments > </license > </licenses > <scm > <connection > scm:svn:http://svn.baidu.com/banseon/maven/banseon/banseon-maven2-trunk(dao-trunk) </connection > <developerConnection > scm:svn:http://svn.baidu.com/banseon/maven/banseon/dao-trunk </developerConnection > <tag /> <url > http://svn.baidu.com/banseon</url > </scm > <organization > <name > demo</name > <url > http://www.erhuowang.cn</url > </organization > <build > <sourceDirectory /> <scriptSourceDirectory /> <testSourceDirectory /> <outputDirectory /> <testOutputDirectory /> <extensions > <extension > <groupId /> <artifactId /> <version /> </extension > </extensions > <defaultGoal /> <resources > <resource > <targetPath /> <filtering /> <directory /> <includes /> <excludes /> </resource > </resources > <testResources > <testResource > <targetPath /> <filtering /> <directory /> <includes /> <excludes /> </testResource > </testResources > <directory /> <finalName /> <filters /> <pluginManagement > <plugins > <plugin > <groupId /> <artifactId /> <version /> <extensions /> <executions > <execution > <id /> <phase /> <goals /> <inherited /> <configuration /> </execution > </executions > <dependencies > <dependency > ...... </dependency > </dependencies > <inherited /> <configuration /> </plugin > </plugins > </pluginManagement > <plugins > <plugin > <groupId /> <artifactId /> <version /> <extensions /> <executions > <execution > <id /> <phase /> <goals /> <inherited /> <configuration /> </execution > </executions > <dependencies > <dependency > ...... </dependency > </dependencies > <goals /> <inherited /> <configuration /> </plugin > </plugins > </build > <profiles > <profile > <id /> <activation > <activeByDefault /> <jdk /> <os > <name > Windows XP</name > <family > Windows</family > <arch > x86</arch > <version > 5.1.2600</version > </os > <property > <name > mavenVersion</name > <value > 2.0.3</value > </property > <file > <exists > /usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/</exists > <missing > /usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/</missing > </file > </activation > <build > <defaultGoal /> <resources > <resource > <targetPath /> <filtering /> <directory /> <includes /> <excludes /> </resource > </resources > <testResources > <testResource > <targetPath /> <filtering /> <directory /> <includes /> <excludes /> </testResource > </testResources > <directory /> <finalName /> <filters /> <pluginManagement > <plugins > <plugin > <groupId /> <artifactId /> <version /> <extensions /> <executions > <execution > <id /> <phase /> <goals /> <inherited /> <configuration /> </execution > </executions > <dependencies > <dependency > ...... </dependency > </dependencies > <goals /> <inherited /> <configuration /> </plugin > </plugins > </pluginManagement > <plugins > <plugin > <groupId /> <artifactId /> <version /> <extensions /> <executions > <execution > <id /> <phase /> <goals /> <inherited /> <configuration /> </execution > </executions > <dependencies > <dependency > ...... </dependency > </dependencies > <goals /> <inherited /> <configuration /> </plugin > </plugins > </build > <modules /> <repositories > <repository > <releases > <enabled /> <updatePolicy /> <checksumPolicy /> </releases > <snapshots > <enabled /> <updatePolicy /> <checksumPolicy /> </snapshots > <id /> <name /> <url /> <layout /> </repository > </repositories > <pluginRepositories > <pluginRepository > <releases > <enabled /> <updatePolicy /> <checksumPolicy /> </releases > <snapshots > <enabled /> <updatePolicy /> <checksumPolicy /> </snapshots > <id /> <name /> <url /> <layout /> </pluginRepository > </pluginRepositories > <dependencies > <dependency > ...... </dependency > </dependencies > <reports /> <reporting > ...... </reporting > <dependencyManagement > <dependencies > <dependency > ...... </dependency > </dependencies > </dependencyManagement > <distributionManagement > ...... </distributionManagement > <properties /> </profile > </profiles > <modules /> <repositories > <repository > <releases > <enabled /> <updatePolicy /> <checksumPolicy /> </releases > <snapshots > <enabled /> <updatePolicy /> <checksumPolicy /> </snapshots > <id > banseon-repository-proxy</id > <name > banseon-repository-proxy</name > <url > http://192.168.1.169:9999/repository/</url > <layout > default</layout > </repository > </repositories > <pluginRepositories > <pluginRepository > ...... </pluginRepository > </pluginRepositories > <dependencies > <dependency > <groupId > org.apache.maven</groupId > <artifactId > maven-artifact</artifactId > <version > 3.8.1</version > <type > jar</type > <classifier > </classifier > <scope > test</scope > <systemPath > </systemPath > <exclusions > <exclusion > <artifactId > spring-core</artifactId > <groupId > org.springframework</groupId > </exclusion > </exclusions > <optional > true</optional > </dependency > </dependencies > <reports > </reports > <reporting > <excludeDefaults /> <outputDirectory /> <plugins > <plugin > <groupId /> <artifactId /> <version /> <inherited /> <configuration /> <reportSets > <reportSet > <id /> <configuration /> <inherited /> <reports /> </reportSet > </reportSets > </plugin > </plugins > </reporting > <dependencyManagement > <dependencies > <dependency > ...... </dependency > </dependencies > </dependencyManagement > <distributionManagement > <repository > <uniqueVersion /> <id > banseon-maven2</id > <name > banseon maven2</name > <url > file://${basedir}/target/deploy</url > <layout /> </repository > <snapshotRepository > <uniqueVersion /> <id > banseon-maven2</id > <name > Banseon-maven2 Snapshot Repository</name > <url > scp://svn.baidu.com/banseon:/usr/local/maven-snapshot</url > <layout /> </snapshotRepository > <site > <id > banseon-site</id > <name > business api website</name > <url > scp://svn.baidu.com/banseon:/var/www/localhost/banseon-web </url > </site > <downloadUrl /> <relocation > <groupId /> <artifactId /> <version /> <message /> </relocation > <status /> </distributionManagement > <properties /> </project >
配置例子参考 配置发布私服、配置阿里云为镜像、配置代理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 <?xml version="1.0" encoding="UTF-8" ?> <settings xmlns ="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd" > <localRepository > D:/maven_repos</localRepository > <pluginGroups /> <proxies > <proxy > <id > optional</id > <active > true</active > <protocol > http</protocol > <host > 10.50.135.250</host > <port > 8080</port > <nonProxyHosts > local.net|some.host.com</nonProxyHosts > </proxy > </proxies > <servers > <server > <id > nexus</id > <username > admin</username > <password > admin123</password > </server > <server > <id > skyheng-releases</id > <username > admin</username > <password > admin123</password > </server > <server > <id > skyheng-snapshots</id > <username > admin</username > <password > admin123</password > </server > </servers > <mirrors > <mirror > <id > nexus</id > <mirrorOf > *</mirrorOf > <name > central repository</name > <url > https://maven.aliyun.com/nexus/content/groups/public/</url > </mirror > </mirrors > <profiles > <profile > <id > nexus</id > <repositories > <repository > <id > nexus</id > <name > Nexus Release Snapshot Repository</name > <url > https://maven.aliyun.com/nexus/content/groups/public/</url > <releases > <enabled > true</enabled > </releases > <snapshots > <enabled > true</enabled > </snapshots > </repository > </repositories > <pluginRepositories > <pluginRepository > <id > nexus</id > <name > Nexus Release Snapshot Repository</name > <url > https://maven.aliyun.com/nexus/content/groups/public/</url > <releases > <enabled > true</enabled > </releases > <snapshots > <enabled > true</enabled > </snapshots > </pluginRepository > </pluginRepositories > </profile > <profile > <id > jdk18</id > <activation > <activeByDefault > true</activeByDefault > <jdk > 1.8</jdk > </activation > <properties > <maven.compiler.source > 1.8</maven.compiler.source > <maven.compiler.target > 1.8</maven.compiler.target > <maven.compiler.compilerVersion > 1.8</maven.compiler.compilerVersion > </properties > </profile > </profiles > <activeProfiles > <activeProfile > nexus</activeProfile > </activeProfiles > </settings >
资料参考:
常见错误 1 2 3 4 5 6 7 8 9 10 11 12 13 14 [WARNING] The requested profile "mvn" could not be activated because it does not exist. [WARNING] The requested profile "clean" could not be activated because it does not exist. [WARNING] The requested profile "install" could not be activated because it does not exist. [ERROR] No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test , prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoGoalSpecifiedException
资料
Maven开发笔记(四)—— Maven中plugins和pluginManagement - 简书https://www.jianshu.com/p/49acf1246eff
maven实战笔记 - 专题 - 简书https://www.jianshu.com/c/67fa0ed1f27a
个人微信公众号 技术交流QQ群