搭建maven私服上传并使用Jar包

时间:2022-07-22
本文章向大家介绍搭建maven私服上传并使用Jar包,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

最近用dubbo做项目和第三方频繁RPC调用,如果对方改了接口参数或者新增接口,后台所有开发人员都需要把第三方提供的jar包打成pom文件引入本地环境。很累!!!

今天就给大家分享一下搭建Nexus3Maven私服以及上传并使用私服jar包

1、下载私服软件包:

1.1、官网下载:

地址:https://fr.sonatype.com/download-oss-sonatype

填写邮箱进行下载:

1.2、百度云盘下载(版本为:3.25):

下载链接: https://pan.baidu.com/s/1aOA8BVEmjDurgDfJ8EBpmQ 提取码: x65c

2、上传私服软件包到服务器:

3、解压私服软件包:

解压命令:
tar -zxvf nexus-3.25.1-04-unix.tar.gz

解压完之后(会有2个文件夹):

4、私服配置(可以忽略)

4.1、更改私服默认端口(8081):

//进入etc文件夹
cd  /DATA/nexus3/nexus-3.25.1-04/etc
//修改配置
vim nexus-default.properties
//更改端口号 port
application-port=12001

4.2、修改私服内存分配:

//进入bin目录
cd /DATA/nexus3/nexus-3.25.1-04/bin
//修改nexus.vmoptions 
vim nexus.vmoptions 

5、启动nexus3(必须有jdk环境哈,如果没有安装jdk请移步:https://blog.cnbuilder.cn/archives/linuxinstalljdk)

启动命令 &为后台启动:
./nexus run & 也可以改为./nexus start &

启动成功:

6、访问Nexus3 ip+端口

7、登录

7.1、寻找admin的登录密码

进入他给你说的目录,然后寻找密码(文件里面就是admin密码):

7.2、拿到密码进行登录:

7.3/修改新的密码:

这就是登录成功的系统:

7.4、可以新建私服用户以及新建角色:

8、创建仓库:

目前创建4个仓库(本地-快照仓库、本地-正式仓库、代理仓库、以及综合仓库) 本地-快照仓库:就是为发布的jar包,比如测试jar 本地-正式仓库:就是第三方给我们提供的jar包,不需要在修改的jar包。 代理仓库:代理华为云、阿里云的或者mavne总仓库 综合仓库:把上面合成一个仓库,都可以使用。

8.1、创建本地快照仓库-也就是测试jar包存放的仓库。

8.1.1、选择maven本地仓库:

8.1.2、创建本地快照仓库:

8.1.3、创建成功:

8.2、创建本地Release仓库:

8.2.1、创建release版本仓库:

8.2.2、创建成功:

8.3、创建代理仓库(比如阿里云、华为云。。。)

8.3.1、创建代理仓库:

8.3.2、创建代理仓库:

8.3.3、创建成功:

8.4、创建组合仓库:

8.4.1、选择组合仓库的其他仓库地址

8.4.2、创建成功:

9、发布jar到私服:

9.1、先在本地解压一个新的mavne环境

9.2、修改setting文件:

9.2.1、修改本地仓库地址:

 <!--自定义maven本地仓库地址-->
  <localRepository>D:hcrdevappstest_nexus3apache-maven-3.6.3-binapache-maven-3.6.3resp</localRepository>

9.2.2、新增servers的配置 指定发布版本账号密码

<servers>
	<server>  
		<id>kingyifan-releases</id>  
		<username>admin</username>  
		<password>admin123</password>  
	</server>
	<server>  
		<id>kingyifan-snapshots</id>  
		<username>admin</username>  
		<password>admin123</password>  
	</server>   
  </servers>

9.3、获取release仓库和快照仓库的地址:

9.4、代码pom文件新增:

  <!--打包上传maven私服-->
    <distributionManagement>
        <repository>
            <!--id的名字可以任意取,但是在setting文件中的属性<server>的ID与这里一致-->
            <id>kingyifan-releases</id>
            <!--指向仓库类型为host(宿主仓库)的储存类型为Release的仓库-->
            <url>http://192.168.189.129:12001/repository/kingyifan-hosted-release/</url>
        </repository>
        <snapshotRepository>
            <id>kingyifan-snapshots</id>
            <url>http://192.168.189.129:12001/repository/kingyifan-hosted-snapshot/</url>
        </snapshotRepository>
    </distributionManagement>

9.4、更改idea的maven仓库地址:

9.4、发布到私服。

命令:mvn deploy

或者用idea的打包工具:

自动就打到快照版本了。

Q: 为什么自动到快照版本而不是release版本呢? A: 因为我们创建项目的时候指定的版本号就是快照版本。

9.5、如果只有jar包怎么上传私服呢??别急听我继续吹~

9.5.1、选择上传的仓库地址(只能选择发布版本)

9.5.2、上传jar包并且命名

9.5.3、上传成功:

9.5.4、查看

10、本地使用私服环境并且从私服下载jar包。

10.1、配置maven的setting配置文件:

10.1.1、增加综合仓库的服务器配置

<server>
	  <id>nexus-kingyifan</id>
	  <username>admin</username>
	  <password>admin123</password>
	</server>	

10.1.2、增加私服的综合仓库地址:

<mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
	  <mirror>
        <id>nexus-kingyifan</id>
        <name>internal nexus repository</name>
        <!--镜像采用配置好的组的地址-->
        <url>http://192.168.189.129:12001/repository/kingyifan-group/</url>
        <mirrorOf>!internal.repo,*</mirrorOf>
    </mirror>
  </mirrors>

10.1.3、配置仓库列表:

<profiles>
	<profile>
		<id>jdk-1.8</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>
   <profile>
		  <id>nexus-kingyifan</id>
		   <!-- 远程仓库列表 -->
		  <repositories>
			<repository>
			  <id>nexus-kingyifan</id>
			  <name>Nexus Central</name>
			 <!-- 虚拟的URL形式,指向镜像的URL-->
			  <url>http://192.168.189.129:12001/repository/kingyifan-group/</url>
			  <layout>default</layout>
			 <!-- 表示可以从这个仓库下载releases版本的构件--> 
			  <releases>
				<enabled>true</enabled>
			  </releases>
			 <!-- 表示可以从这个仓库下载snapshot版本的构件 --> 
			  <snapshots>
				<enabled>true</enabled>
			  </snapshots>
			</repository>
		  </repositories>
		   <!-- 插件仓库列表 -->
		  <pluginRepositories>
			<pluginRepository>
			  <id>nexus-kingyifan</id>
			  <name>Nexus Central</name>
			  <url>http://192.168.189.129:12001/repository/kingyifan-group/</url>
			  <layout>default</layout>
			  <snapshots>
				<enabled>true</enabled>
			  </snapshots>
			  <releases>
				 <enabled>true</enabled>
			  </releases>
			</pluginRepository>
		  </pluginRepositories>
	</profile>
  </profiles>
  <activeProfiles>
     <!--需要激活 <profile>中的ID才生效--> 
    <activeProfile>nexus-kingyifan</activeProfile>
    <activeProfile>jdk-1.8</activeProfile>
  </activeProfiles>

来给大家甩一个完整的maven的setting配置文件:

<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user,
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in
 |                 ${maven.conf}/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<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
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
  
   <!--自定义maven本地仓库地址-->
  <localRepository>D:hcrdevappstest_nexus3apache-maven-3.6.3-binapache-maven-3.6.3resp</localRepository>

  <!-- interactiveMode
   | This will determine whether maven prompts you when it needs input. If set to false,
   | maven will use a sensible default value, perhaps based on some other setting, for
   | the parameter in question.
   |
   | Default: true
  <interactiveMode>true</interactiveMode>
  -->

  <!-- offline
   | Determines whether maven should attempt to connect to the network when executing a build.
   | This will have an effect on artifact downloads, artifact deployment, and others.
   |
   | Default: false
  <offline>false</offline>
  -->

  <!-- pluginGroups
   | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
   | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
   | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
   |-->
  <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>

  <!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->
  <proxies>
    <!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    -->
  </proxies>

  <!-- servers
   | This is a list of authentication profiles, keyed by the server-id used within the system.
   | Authentication profiles can be used whenever maven must make a connection to a remote server.
   |-->
  <servers>
	<server>  
		<id>kingyifan-releases</id>  
		<username>admin</username>  
		<password>admin123</password>  
	</server>
	<server>  
		<id>kingyifan-snapshots</id>  
		<username>admin</username>  
		<password>admin123</password>  
	</server>
	<server>
	  <id>nexus-kingyifan</id>
	  <username>admin</username>
	  <password>admin123</password>
	</server>	
  </servers>

  <!-- mirrors
   | This is a list of mirrors to be used in downloading artifacts from remote repositories.
   |
   | It works like this: a POM may declare a repository to use in resolving certain artifacts.
   | However, this repository may have problems with heavy traffic at times, so people have mirrored
   | it to several places.
   |
   | That repository definition will have a unique id, so we can create a mirror reference for that
   | repository, to be used as an alternate download site. The mirror site will be the preferred
   | server for that repository.
   |-->
 <mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
	  <mirror>
        <id>nexus-kingyifan</id>
        <name>internal nexus repository</name>
        <!--镜像采用配置好的组的地址-->
        <url>http://192.168.189.129:12001/repository/kingyifan-group/</url>
        <mirrorOf>!internal.repo,*</mirrorOf>
    </mirror>
  </mirrors>

  <!-- profiles
   | This is a list of profiles which can be activated in a variety of ways, and which can modify
   | the build process. Profiles provided in the settings.xml are intended to provide local machine-
   | specific paths and repository locations which allow the build to work in the local environment.
   |
   | For example, if you have an integration testing plugin - like cactus - that needs to know where
   | your Tomcat instance is installed, you can provide a variable here such that the variable is
   | dereferenced during the build process to configure the cactus plugin.
   |
   | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
   | section of this document (settings.xml) - will be discussed later. Another way essentially
   | relies on the detection of a system property, either matching a particular value for the property,
   | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
   | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
   | Finally, the list of active profiles can be specified directly from the command line.
   |
   | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
   |       repositories, plugin repositories, and free-form properties to be used as configuration
   |       variables for plugins in the POM.
   |
   |-->
  <profiles>
	<profile>
		<id>jdk-1.8</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>
   <profile>
		  <id>nexus-kingyifan</id>
		   <!-- 远程仓库列表 -->
		  <repositories>
			<repository>
			  <id>nexus-kingyifan</id>
			  <name>Nexus Central</name>
			 <!-- 虚拟的URL形式,指向镜像的URL-->
			  <url>http://192.168.189.129:12001/repository/kingyifan-group/</url>
			  <layout>default</layout>
			 <!-- 表示可以从这个仓库下载releases版本的构件--> 
			  <releases>
				<enabled>true</enabled>
			  </releases>
			 <!-- 表示可以从这个仓库下载snapshot版本的构件 --> 
			  <snapshots>
				<enabled>true</enabled>
			  </snapshots>
			</repository>
		  </repositories>
		   <!-- 插件仓库列表 -->
		  <pluginRepositories>
			<pluginRepository>
			  <id>nexus-kingyifan</id>
			  <name>Nexus Central</name>
			  <url>http://192.168.189.129:12001/repository/kingyifan-group/</url>
			  <layout>default</layout>
			  <snapshots>
				<enabled>true</enabled>
			  </snapshots>
			  <releases>
				 <enabled>true</enabled>
			  </releases>
			</pluginRepository>
		  </pluginRepositories>
	</profile>
  </profiles>
  <activeProfiles>
     <!--需要激活 <profile>中的ID才生效--> 
    <activeProfile>nexus-kingyifan</activeProfile>
    <activeProfile>jdk-1.8</activeProfile>
  </activeProfiles>

  <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->
</settings>

10.1.4、敲黑板:IDEAmaven环境一定更换为刚刚修改的setting配置文件!!!

10.1.5、引入pom包:

自己就会去私服下载jar包

我们看一下本地maven仓库也是有对应的jar包的:

11、私服-下载jar包路径: 本地仓库===》私服发布版本===》私服正式版本===》私服代理仓库===》直到寻找结束。

终、、,以上centos7搭建私服,上传并使用私服jar包。

感谢一路支持我的人。。。。。

Love me and hold me
QQ:69673804(16年老号)
EMAIL:itw@tom.com
友链交换
如果有兴趣和本博客交换友链的话,请按照下面的格式在评论区进行评论,我会尽快添加上你的链接。

网站名称:猿码优创
网站地址:http://blog.cnbuilder.cn
网站描述:年少是你未醒的梦话,风华是燃烬的彼岸花。
网站Logo/头像: [头像地址](https://blog.cnbuilder.cn/upload/2018/7/avatar20180720144536200.jpg)

欢迎关注猿码优创(联系小优优进内部群哦,新鲜技术优先更新):