1.私有maven仓库的搭建

下载安装包

  1. wget http://download.sonatype.com/nexus/3/nexus-3.7.1-02-unix.tar.gz

解压软件

  1. #解压软件
  2. mkdir -p /opt/nexus && tar xf nexus-3.7.1-02-unix.tar.gz -C /opt/nexus
  3. #添加系统配置文件
  4. vim /etc/profile
  5. export MAVEN_HOME=/opt/nexus/nexus-3.7.1-02
  6. export PATH=$PATH:$MAVEN_HOME/bin
  7. #刷新配置文件
  8. source /etc/profile

启动nexus

  1. nexus run
  2. #然后访问ip:port 默认的端口是8081,阿里云服务器记得要开启这个端口的访问。看到这个页面就表示,你的私有maven仓库搭建成功了。
  3. #登录进去(默认账号:admin,默认密码:admin123)可以查看repositories,配置用户啥的。

搭建Maven私有仓库 - 图1

  • 默认仓库说明

    maven-central:maven中央库,默认从https://repo1.maven.org/maven2/拉取jar
    maven-releases:私库发行版jar,初次安装请将Deployment policy设置为Allow redeploy
    maven-snapshots:私库快照(调试版本)jar
    maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml或项目pom.xml中使用

  • 仓库类型

    Group:这是一个仓库聚合的概念,用户仓库地址选择Group的地址,即可访问Group中配置的,用于方便开发人员自己设定的仓库。maven-public就是一个Group类型的仓库,内部设置了多个仓库,访问顺序取决于配置顺序,3.x默认Releases,Snapshots, Central,当然你也可以自己设置。
    Hosted:私有仓库,内部项目的发布仓库,专门用来存储我们自己生成的jar文件
    3rd party:未发布到公网的第三方jar (3.x去除了)
    Snapshots:本地项目的快照仓库
    Releases: 本地项目发布的正式版本
    Proxy:代理类型,从远程中央仓库中寻找数据的仓库(可以点击对应的仓库的Configuration页签下Remote Storage属性的值即被代理的远程仓库的路径),如可配置阿里云maven仓库
    Central:中央仓库
    Apache Snapshots:Apache专用快照仓库(3.x去除了)

增加新的代理源

第一步 按照步骤添加新的代理源

搭建Maven私有仓库 - 图2

第二步选择添加maven2的代理

搭建Maven私有仓库 - 图3

第三步添加代理(Cache统一设置为200天 288000)

搭建Maven私有仓库 - 图4搭建Maven私有仓库 - 图5

第四步逐个增加常用代理
  1. 1. aliyun
  2. http://maven.aliyun.com/nexus/content/groups/public
  3. 2. apache_snapshot
  4. https://repository.apache.org/content/repositories/snapshots/
  5. 3. apache_release
  6. https://repository.apache.org/content/repositories/releases/
  7. 4. atlassian
  8. https://maven.atlassian.com/content/repositories/atlassian-public/
  9. 5. central.maven.org
  10. http://central.maven.org/maven2/
  11. 6. datanucleus
  12. http://www.datanucleus.org/downloads/maven2
  13. 7. maven-central (安装后自带,仅需设置Cache有效期即可)
  14. https://repo1.maven.org/maven2/
  15. 8. nexus.axiomalaska.com
  16. http://nexus.axiomalaska.com/nexus/content/repositories/public
  17. 9. oss.sonatype.org
  18. https://oss.sonatype.org/content/repositories/snapshots
  19. 10.pentaho
  20. https://public.nexus.pentaho.org/content/groups/omni/

第五步 设置maven-public 将这些代理加入Group,最好将默认的maven库放到最底下

搭建Maven私有仓库 - 图6

搭建Maven私有仓库 - 图7

选择Configuration > Repository, 双击 maven-public, 在Group区域将aliyun移到右侧Members, 上移到maven-central的上面, 点击 Save。

第六步 设置私用仓库可重复发布

Nexus安装后自带maven-releases,maven-snapshots两个仓库,用于将生成的jar包发布在这两个仓库中,在实际开发中需要将maven-releases设置为可以重复发布
搭建Maven私有仓库 - 图8

Maven配置使用Nexus

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing,
  12. software distributed under the License is distributed on an
  13. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. KIND, either express or implied. See the License for the
  15. specific language governing permissions and limitations
  16. under the License.
  17. -->
  18. <!--
  19. | This is the configuration file for Maven. It can be specified at two levels:
  20. |
  21. | 1. User Level. This settings.xml file provides configuration for a single user,
  22. | and is normally provided in ${user.home}/.m2/settings.xml.
  23. |
  24. | NOTE: This location can be overridden with the CLI option:
  25. |
  26. | -s /path/to/user/settings.xml
  27. |
  28. | 2. Global Level. This settings.xml file provides configuration for all Maven
  29. | users on a machine (assuming they're all using the same Maven
  30. | installation). It's normally provided in
  31. | ${maven.conf}/settings.xml.
  32. |
  33. | NOTE: This location can be overridden with the CLI option:
  34. |
  35. | -gs /path/to/global/settings.xml
  36. |
  37. | The sections in this sample file are intended to give you a running start at
  38. | getting the most out of your Maven installation. Where appropriate, the default
  39. | values (values used when the setting is not specified) are provided.
  40. |
  41. |-->
  42. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  43. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44. xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  45. <!-- localRepository
  46. | The path to the local repository maven will use to store artifacts.
  47. |
  48. | Default: ${user.home}/.m2/repository
  49. -->
  50. <localRepository>${user.home}/.m2/repository</localRepository>
  51. <!-- interactiveMode
  52. | This will determine whether maven prompts you when it needs input. If set to false,
  53. | maven will use a sensible default value, perhaps based on some other setting, for
  54. | the parameter in question.
  55. |
  56. | Default: true
  57. <interactiveMode>true</interactiveMode>
  58. -->
  59. <!-- offline
  60. | Determines whether maven should attempt to connect to the network when executing a build.
  61. | This will have an effect on artifact downloads, artifact deployment, and others.
  62. |
  63. | Default: false
  64. <offline>false</offline>
  65. -->
  66. <!-- pluginGroups
  67. | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
  68. | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
  69. | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
  70. |-->
  71. <pluginGroups>
  72. <!-- pluginGroup
  73. | Specifies a further group identifier to use for plugin lookup.
  74. <pluginGroup>com.your.plugins</pluginGroup>
  75. -->
  76. </pluginGroups>
  77. <!-- proxies
  78. | This is a list of proxies which can be used on this machine to connect to the network.
  79. | Unless otherwise specified (by system property or command-line switch), the first proxy
  80. | specification in this list marked as active will be used.
  81. |-->
  82. <proxies>
  83. <!-- proxy
  84. | Specification for one proxy, to be used in connecting to the network.
  85. |
  86. <proxy>
  87. <id>optional</id>
  88. <active>true</active>
  89. <protocol>http</protocol>
  90. <username>proxyuser</username>
  91. <password>proxypass</password>
  92. <host>proxy.host.net</host>
  93. <port>80</port>
  94. <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
  95. </proxy>
  96. -->
  97. </proxies>
  98. <!-- servers
  99. | This is a list of authentication profiles, keyed by the server-id used within the system.
  100. | Authentication profiles can be used whenever maven must make a connection to a remote server.
  101. |-->
  102. <servers>
  103. <!-- server
  104. | Specifies the authentication information to use when connecting to a particular server, identified by
  105. | a unique name within the system (referred to by the 'id' attribute below).
  106. |
  107. | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
  108. | used together.
  109. |
  110. -->
  111. <server>
  112. <id>releases</id>
  113. <username>admin</username>
  114. <password>admin23</password>
  115. </server>
  116. <server>
  117. <id>snapshots</id>
  118. <username>admin</username>
  119. <password>admin123</password>
  120. </server>
  121. <!-- Another sample, using keys to authenticate.
  122. <server>
  123. <id>siteServer</id>
  124. <privateKey>/path/to/private/key</privateKey>
  125. <passphrase>optional; leave empty if not used.</passphrase>
  126. </server>
  127. -->
  128. </servers>
  129. <!-- mirrors
  130. | This is a list of mirrors to be used in downloading artifacts from remote repositories.
  131. |
  132. | It works like this: a POM may declare a repository to use in resolving certain artifacts.
  133. | However, this repository may have problems with heavy traffic at times, so people have mirrored
  134. | it to several places.
  135. |
  136. | That repository definition will have a unique id, so we can create a mirror reference for that
  137. | repository, to be used as an alternate download site. The mirror site will be the preferred
  138. | server for that repository.
  139. |-->
  140. <mirrors>
  141. <!-- mirror
  142. | Specifies a repository mirror site to use instead of a given repository. The repository that
  143. | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
  144. | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
  145. |
  146. -->
  147. <mirror>
  148. <id>HolliParkMirror</id>
  149. <mirrorOf>*</mirrorOf>
  150. <name>HolliPark Repository Mirror.</name>
  151. <url>http://localhost:8082/nexus/repository/maven-public/</url>
  152. </mirror>
  153. </mirrors>
  154. <!-- profiles
  155. | This is a list of profiles which can be activated in a variety of ways, and which can modify
  156. | the build process. Profiles provided in the settings.xml are intended to provide local machine-
  157. | specific paths and repository locations which allow the build to work in the local environment.
  158. |
  159. | For example, if you have an integration testing plugin - like cactus - that needs to know where
  160. | your Tomcat instance is installed, you can provide a variable here such that the variable is
  161. | dereferenced during the build process to configure the cactus plugin.
  162. |
  163. | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
  164. | section of this document (settings.xml) - will be discussed later. Another way essentially
  165. | relies on the detection of a system property, either matching a particular value for the property,
  166. | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
  167. | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
  168. | Finally, the list of active profiles can be specified directly from the command line.
  169. |
  170. | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
  171. | repositories, plugin repositories, and free-form properties to be used as configuration
  172. | variables for plugins in the POM.
  173. |
  174. |-->
  175. <profiles>
  176. <profile>
  177. <id>HolliPark</id>
  178. <repositories>
  179. <repository>
  180. <id>nexus</id>
  181. <name>Public Repositories</name>
  182. <url>http://localhost:8082/nexus/repository/maven-public/</url>
  183. <releases>
  184. <enabled>true</enabled>
  185. </releases>
  186. </repository>
  187. <repository>
  188. <id>central</id>
  189. <name>Central Repositories</name>
  190. <url>http://localhost:8082/nexus/repository/maven-central/</url>
  191. <releases>
  192. <enabled>true</enabled>
  193. </releases>
  194. <snapshots>
  195. <enabled>false</enabled>
  196. </snapshots>
  197. </repository>
  198. <repository>
  199. <id>release</id>
  200. <name>Release Repositories</name>
  201. <url>http://localhost:8082/nexus/repository/maven-releases/</url>
  202. <releases>
  203. <enabled>true</enabled>
  204. </releases>
  205. <snapshots>
  206. <enabled>false</enabled>
  207. </snapshots>
  208. </repository>
  209. <repository>
  210. <id>snapshots</id>
  211. <name>Snapshot Repositories</name>
  212. <url>http://localhost:8082/nexus/repository/maven-snapshots/</url>
  213. <releases>
  214. <enabled>true</enabled>
  215. </releases>
  216. <snapshots>
  217. <enabled>true</enabled>
  218. </snapshots>
  219. </repository>
  220. </repositories>
  221. <pluginRepositories>
  222. <pluginRepository>
  223. <id>plugins</id>
  224. <name>Plugin Repositories</name>
  225. <url>http://localhost:8082/nexus/repository/maven-public/</url>
  226. </pluginRepository>
  227. </pluginRepositories>
  228. </profile>
  229. <!-- profile
  230. | Specifies a set of introductions to the build process, to be activated using one or more of the
  231. | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
  232. | or the command line, profiles have to have an ID that is unique.
  233. |
  234. | An encouraged best practice for profile identification is to use a consistent naming convention
  235. | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
  236. | This will make it more intuitive to understand what the set of introduced profiles is attempting
  237. | to accomplish, particularly when you only have a list of profile id's for debug.
  238. |
  239. | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
  240. <profile>
  241. <id>jdk-1.4</id>
  242. <activation>
  243. <jdk>1.4</jdk>
  244. </activation>
  245. <repositories>
  246. <repository>
  247. <id>jdk14</id>
  248. <name>Repository for JDK 1.4 builds</name>
  249. <url>http://www.myhost.com/maven/jdk14</url>
  250. <layout>default</layout>
  251. <snapshotPolicy>always</snapshotPolicy>
  252. </repository>
  253. </repositories>
  254. </profile>
  255. -->
  256. <!--
  257. | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
  258. | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
  259. | might hypothetically look like:
  260. |
  261. | ...
  262. | <plugin>
  263. | <groupId>org.myco.myplugins</groupId>
  264. | <artifactId>myplugin</artifactId>
  265. |
  266. | <configuration>
  267. | <tomcatLocation>${tomcatPath}</tomcatLocation>
  268. | </configuration>
  269. | </plugin>
  270. | ...
  271. |
  272. | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
  273. | anything, you could just leave off the <value/> inside the activation-property.
  274. |
  275. <profile>
  276. <id>env-dev</id>
  277. <activation>
  278. <property>
  279. <name>target-env</name>
  280. <value>dev</value>
  281. </property>
  282. </activation>
  283. <properties>
  284. <tomcatPath>/path/to/tomcat/instance</tomcatPath>
  285. </properties>
  286. </profile>
  287. -->
  288. </profiles>
  289. <!-- activeProfiles
  290. | List of profiles that are active for all builds.
  291. |
  292. <activeProfiles>
  293. <activeProfile>alwaysActiveProfile</activeProfile>
  294. <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  295. </activeProfiles>
  296. -->
  297. <activeProfiles>
  298. <activeProfile>HolliPark</activeProfile>
  299. </activeProfiles>
  300. </settings>

创建私有公库

创建普通的maven项目

搭建Maven私有仓库 - 图9

修改项目的pom.xml

在pom文件中加入distributionManagement节点,注意:pom.xml中repository里的id需要和.m2中setting.xml里的server id名称保持一致

  1. <distributionManagement>
  2. <repository>
  3. <id>releases</id>
  4. <name>Nexus Release Repository</name>
  5. <url>http://localhost:8082/nexus/repository/maven-releases/</url>
  6. </repository>
  7. <snapshotRepository>
  8. <id>snapshots</id>
  9. <name>Nexus Snapshot Repository</name>
  10. <url>http://localhost:8082/nexus/repository/maven-snapshots/</url>
  11. </snapshotRepository>
  12. </distributionManagement>

发布私有公库

执行部署命令即可发布。

  1. mvn deploy

登录Nexus,查看对应的仓库已经有相关的依赖包了
搭建Maven私有仓库 - 图10

注意以下几点:

  • 若项目版本号末尾带有 -SNAPSHOT,则会发布到snapshots快照版本仓库
  • 若项目版本号末尾带有 -RELEASES 或什么都不带,则会发布到releases正式版本仓库

项目中配置连接

启动完成了,那怎么配置本地(你的开发环境)和 这个私有库的连接呢?
Step1: 找到你本地的maven安装目录,我的在F:\ProgramFiles\apache-maven-3.5.0。 然后呢,到conf目录下,打开setting.xml。 找到这个标签,在里面添加两个配置。

  1. <server>
  2. <id>nexus</id>
  3. <username>admin</username>
  4. <password>admin123</password>
  5. </server>
  6. <server>
  7. <id>nexus</id>
  8. <username>admin</username>
  9. <password>admin123</password>
  10. </server>

完了之后呢,打开你的Maven项目,在pom.xml里面加两段.

  1. #a.先看配置远程仓库地址
  2. <repositories>
  3. <!-- 配置nexus远程仓库 -->
  4. <repository>
  5. <id>nexus</id>
  6. <name>Nexus Snapshot Repository</name>
  7. <url>http://192.168.0.1:8081/repository/maven-public/</url>
  8. <releases>
  9. <enabled>true</enabled>
  10. </releases>
  11. <snapshots>
  12. <enabled>false</enabled>
  13. </snapshots>
  14. </repository>
  15. </repositories>
  16. b.打包时发布jar包到私有仓库
  17. <distributionManagement>
  18. <repository>
  19. <id>nexus</id>
  20. <name>Releases</name>
  21. <url>http://192.168.0.1:8081/repository/maven-releases</url>
  22. </repository>
  23. <snapshotRepository>
  24. <id>nexus</id>
  25. <name>Snapshot</name>
  26. <url>http://192.168.0.1:8081/repository/maven-snapshots</url>
  27. </snapshotRepository>
  28. </distributionManagement>