服务于Java平台构建、依赖管理、项目管理,通过xml格式配置文件管理

下载安装

官网:https://archive.apache.org/dist/maven/maven-3/

  1. #上传tar包
  2. tar -zxvf apache-maven-3.3.9-bin.tar.gz && mv apache-maven-3.3.9 /usr/local
  3. #创建软链接
  4. ln -s /usr/local/apache-maven-3.3.9 /usr/local/maven
  5. #添加maven环境变量
  6. echo 'export PATH=/usr/local/maven/bin/:$PATH' >> /etc/profile && source /etc/profile

常用操作

  1. mvn -v #查看版本号
  2. mvn clean #清理上次的编译结果
  3. mvn package #打包
  4. mvn validate(验证):验证项目正确,并且所有必要信息可用。
  5. mvn compile(编译):编译项目源码
  6. mvn test(测试):使用合适的单元测试框架测试编译后的源码。
  7. mvn integration-test(集成测试):如果有需要,把包处理并部署到可以运行集成测试的环境中去。
  8. mvn verify(验证):进行各种测试来验证包是否有效并且符合质量标准。
  9. mvn install(安装):把包安装到本地仓库,使该包可以作为其他本地项目的依赖。
  10. mvn deploy(部署):在集成或发布环境中完成,将最终软件包复制到远程存储库,以与其他开发人员和项目共享。

配置阿里云服务器

mirror标签指向阿里云

/maven/conf/settings.xml

  1. #打开配置文件找到相应的mirror标签,加入对应的配置
  2. <mirror>
  3. <id>aliyun</id>
  4. <name>aliyun</name>
  5. <mirrorOf>*</mirrorOf>
  6. <url>https://maven.aliyun.com/repository/public</url>
  7. </mirror>
  8. #项目中测试
  9. mvn package

maven nexus私服自动打包 - 图1

创建maven私服Nexus

官网:https://www.sonatype.com/download-oss-sonatype
默认端口:8081
项目下的pom.xml配置只生效于当前项目
在maven配置全局生效

安装jdk和nexus

  1. 上传jdknexus安装包
  2. 安装jdk,并配置jdk的环境变量 /etc/profile
  3. tar -zxvf nexus-3.17.0-01-unix.tar.gz && mv nexus-3.17.0-01 /usr/local
  4. ln -s /usr/local/nexus-3.17.0-01 /usr/local/nexus
  5. cd /usr/local/nexus/bin && ./nexus start
  6. ps:这里启动需要一点时间,请耐心等待~
  7. #查看默认的nexus密码
  8. cat /usr/local/sonatype-work/nexus3/admin.password
  9. #用admin用户和默认密码登录系统

nexus后台配置全局代理

maven nexus私服自动打包 - 图2

修改maven 配置文件

/usr/local/maven/conf/settings.xml
修改 servers标签 mirrors标签 profiles标签 activeProfiles标签

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  5. <pluginGroups>
  6. <!-- pluginGroup
  7. | Specifies a further group identifier to use for plugin lookup.
  8. <pluginGroup>com.your.plugins</pluginGroup>
  9. -->
  10. </pluginGroups>
  11. <proxies>
  12. <!-- proxy
  13. | Specification for one proxy, to be used in connecting to the network.
  14. |
  15. <proxy>
  16. <id>optional</id>
  17. <active>true</active>
  18. <protocol>http</protocol>
  19. <username>proxyuser</username>
  20. <password>proxypass</password>
  21. <host>proxy.host.net</host>
  22. <port>80</port>
  23. <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
  24. </proxy>
  25. -->
  26. </proxies>
  27. <servers>
  28. <server>
  29. <id>nexus-releases</id>
  30. <username>admin</username>
  31. <password>admin</password>
  32. </server>
  33. <server>
  34. <id>nexus-snapshots</id>
  35. <username>admin</username>
  36. <password>admin</password>
  37. </server>
  38. </servers>
  39. <mirrors>
  40. <mirror>
  41. <id>nexus</id>
  42. <name>nexus maven</name>
  43. <url>http://192.168.31.238:8081/repository/maven-public/</url>
  44. <mirrorOf>*</mirrorOf>
  45. </mirror>
  46. <mirror>
  47. <id>alimaven</id>
  48. <name>aliyun maven</name>
  49. <url>https://maven.aliyun.com/repository/public</url>
  50. <mirrorOf>central</mirrorOf>
  51. </mirror>
  52. <mirror>
  53. <id>repo2</id>
  54. <mirrorOf>central</mirrorOf>
  55. <name>Human Readable Name for this Mirror.</name>
  56. <url>http://repo2.maven.org/maven2/</url>
  57. </mirror>
  58. </mirrors>
  59. <profiles>
  60. <profile>
  61. <id>nexus</id>
  62. <repositories>
  63. <repository>
  64. <id>nexus</id>
  65. <url>https://maven.aliyun.com/repository/public</url>
  66. <releases><enabled>true</enabled></releases>
  67. <snapshots><enabled>true</enabled></snapshots>
  68. </repository>
  69. </repositories>
  70. <pluginRepositories>
  71. <pluginRepository>
  72. <id>nexus</id>
  73. <url>http://192.168.31.238:8081/repository/maven-public/</url>
  74. <releases>
  75. <enabled>true</enabled>
  76. </releases>
  77. <snapshots>
  78. <enabled>true</enabled>
  79. </snapshots>
  80. </pluginRepository>
  81. </pluginRepositories>
  82. </profile>
  83. <profile>
  84. <!--profile的id-->
  85. <id>nexus-releases</id>
  86. <repositories>
  87. <repository>
  88. <!--仓库id,repositories可以配置多个仓库,保证id不重复-->
  89. <id>nexus-releases</id>
  90. <!--仓库地址,即nexus仓库组的地址-->
  91. <url>http://192.168.31.238:8081/repository/maven-releases/</url>
  92. <!--是否下载releases构件-->
  93. <releases><enabled>true</enabled></releases>
  94. <!--是否下载snapshots构件-->
  95. <snapshots><enabled>true</enabled></snapshots>
  96. </repository>
  97. </repositories>
  98. <pluginRepositories>
  99. <!-- 插件仓库,maven的运行依赖插件,也需要从私服下载插件 -->
  100. <pluginRepository>
  101. <!-- 插件仓库的id不允许重复,如果重复后边配置会覆盖前边 -->
  102. <id>nexus-releases</id>
  103. <url>http://192.168.31.238:8081/repository/maven-releases/</url>
  104. <releases>
  105. <enabled>true</enabled>
  106. </releases>
  107. <snapshots>
  108. <enabled>true</enabled>
  109. </snapshots>
  110. </pluginRepository>
  111. </pluginRepositories>
  112. </profile>
  113. <profile>
  114. <id>nexus-snapshots</id>
  115. <repositories>
  116. <repository>
  117. <id>nexus-snapshots</id>
  118. <url>http://192.168.31.238:8081/repository/maven-snapshots/</url>
  119. <releases><enabled>true</enabled></releases>
  120. <snapshots><enabled>true</enabled></snapshots>
  121. </repository>
  122. </repositories>
  123. <pluginRepositories>
  124. <pluginRepository>
  125. <id>nexus-snapshots</id>
  126. <url>http://192.168.31.238:8081/repository/maven-snapshots/</url>
  127. <releases>
  128. <enabled>true</enabled>
  129. </releases>
  130. <snapshots>
  131. <enabled>true</enabled>
  132. </snapshots>
  133. </pluginRepository>
  134. </pluginRepositories>
  135. </profile>
  136. </profiles>
  137. <activeProfiles>
  138. <activeProfile>nexus</activeProfile>
  139. <activeProfile>nexus-releases</activeProfile>
  140. <activeProfile>nexus-snapshots</activeProfile>
  141. </activeProfiles>
  142. </settings>

maven nexus私服自动打包 - 图3