Windows下maven私服Nexus搭建及使用

项目即将开始进行重构,准备从之前的单体应用从新重构为基于spring cloud的微服务应用。那么问题来了,若是将各类工具类都冗余到每一个微服务中,后期维护就特别难受,想着把工具类、业务模块、数据库访问等抽出来成独立的模块,每一个服务去引入便可。基于maven的架构,小编首先想到了nexus,今天就跟着小编一块儿来玩玩Nexus的搭建及简单实用吧!spring

Nexus实际上是商业收费的,可是它其实也有开源的版本,咱们下载Nexus Repository Manager OSS 3.x数据库

1、Nexus下载及安装

  • 一、下载地址:https://www.sonatype.com/download-oss-sonatype

下载完成以后大概就是这个样子的,我们以windows为例,以下图:apache

)

  • 二、修改Nexus默认端口号

移步至etc/目录下,会有一个叫default-nexus.properties文件,修改其配置项的端口号便可,默认端口号是8081,配置以下:windows

application-port=8084浏览器

  • 三、安装Nexus

Nexus能够在命令行直接启动,也能够被安装成一个服务,cmd移步至安装目录下,输入nexus.exe -help,回车以下:bash

其中install,uninstall两个为服务安装与卸载命令,startstop为服务启停命令,咱们能够键入nexus.exe install将nexus安装成为一个windows服务,而后nexus.exe start将其启动便可。网络

  • 四、打开首页进行配置

若是你的端口没有改动,则默认是8081端口,在浏览器输入http://ip:port/便可进入首页。以下图:架构

点击右上角的sign in去登陆,Nexus默认有一个帐号是admin,密码是admin123,进入以后能够修改密码。app

登陆成功以后,进去这个页面,将中央仓库的远程代理地址改成阿里云镜像地址。maven

为何要修改远程代理地址呢?由于默认的远程仓库地址是maven中央仓库,若是遇到网络很差会很是的慢,再者是由于咱们搭建了私服,后面项目引入jar包的原理就变成:[项目]->[私服地址]->[阿里云镜像地址]

问题:是否是项目引入jar文件的时候都会走[阿里云镜像地址]?答案是否认的,只有当你的私服不存在项目须要的jar文件时,私服才会去你配置的代理地址去下载项目须要jar文件。固然了,nexus会备份从远程仓库拉取的包,保存在本身的center仓库中。

2、项目maven配置

之前咱们项目没有链接私服的时候,咱们本地的settings.xml文件可能会加上这个配置,提升速度。

<mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>        
</mirror>
复制代码

那么,若是配置了,如今请你把它删掉,换成下面的配置:

<mirror>
	  <id>public</id>      
	  <mirrorOf>central</mirrorOf>
	  <name>central repository</name>
	  <url>http://192.168.1.116:8084/repository/maven-public/</url>
</mirror>
复制代码

这样表示会从私服拉取jar文件,固然啦,咱们上面在私服配置了远程代理地址,本质上若是私服没有你想要的jar文件的话,仍是会去阿里云上下载的。

接着在settings.xml文件中增长下面配置:

<profile>
    <id>nexusProfile</id>
    <repositories>
        <repository>
            <id>nexus</id>
            <name>nexus</name>
            <url>http://192.168.1.116:8084/repository/maven-central/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
</profile>

<activeProfiles>
    <activeProfile>nexusProfile</activeProfile>
</activeProfiles>

<server>
    <id>releases</id> <!-- 记为   id1-->
    <username>admin</username>
    <password>******</password>
</server>
<server>
    <id>snapshots</id>  <!-- 记为   id2-->
    <username>admin</username>
    <password>******</password>
</server>
复制代码

最后在项目的pom.xml文件中,新增下面配置:

<distributionManagement>
    <repository>
    	<id>releases</id>
    	<name>User Project Release</name>
    	<url>http://192.168.1.116:8084/repository/maven-releases/</url>
    </repository>
    
    <snapshotRepository>
    	<id>snapshots</id>
    	<name>User Project SNAPSHOTS</name>
    	<url>http://192.168.1.116:8084/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>
复制代码

此处2个id要和上面的id一、id2保持一致。再运行mvn deploy -e便可完成将包上传至私服。

3、maven打包插件

<build>
    	<plugins>
    		<plugin>
    			<groupId>org.apache.maven.plugins</groupId>
    			<artifactId>maven-jar-plugin</artifactId>
    			<version>2.4</version>
    			<configuration>
    				<archive>
    					<!-- 生成的jar中,包含pom.xml和pom.properties这两个文件 -->
    					<addMavenDescriptor>true</addMavenDescriptor>
    					<manifest>
    						<!-- 为依赖包添加路径, 这些路径会写在MANIFEST文件的Class-Path下 -->
    						<addClasspath>true</addClasspath>
    						<!-- 这个jar所依赖的jar包添加classPath的时候的前缀,若是这个 jar自己和依赖包在同一级目录,则不须要添加 -->
    						<classpathPrefix>lib/</classpathPrefix>
    						<!-- jar启动入口类 -->
    						<mainClass>com.dl.middleware.bocimmid.BocimApplication</mainClass>
    					</manifest>
    				</archive>
    				<!-- jar包的位置 -->
    				<outputDirectory>${project.build.directory}</outputDirectory>
    				<includes>
    					<!-- 打jar包时,只打包class文件 -->
    					<!-- 有时候可能须要一些其余文件,这边能够配置,包括剔除的文件等等 -->
    					<include>**/*.class</include>
    				</includes>
    			</configuration>
    		</plugin>
    		<plugin>
    			<artifactId>maven-source-plugin</artifactId>
    			<version>2.1</version>
    			<configuration>
    				<attach>true</attach>
    			</configuration>
    			<executions>
    				<execution>
    					<phase>compile</phase>
    					<goals>
    						<goal>jar</goal>
    					</goals>
    				</execution>
    			</executions>
    		</plugin>
    	</plugins>
</build>    
复制代码

4、技巧

  • 一、maven子模块<parent>标签必须指定<version>,那么每次子模块版本更新岂不是很麻烦,可使用mvn versions:set -DnewVersion=0.0.2-SNAPSHOT进行版本控制
相关文章
相关标签/搜索