maven集成nexus伺服服务实现项目快速自动化构建与发布

Posted 北溟溟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了maven集成nexus伺服服务实现项目快速自动化构建与发布相关的知识,希望对你有一定的参考价值。

前言

在前面的博客centos7案例实战——Nexus3伺服仓库服务器搭建中已经详细讲解了如何在本地搭建自己的maven伺服仓库nexus,本节内容是关于如何在本地使用maven集成nexus伺服服务从而实现项目快速自动化构建与发布。

正文

  • 修改本地maven配置文件settings.xml,加入nexus配置
<?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.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
  <!-- 本地仓库存储地址 -->
  <localRepository>E:\\maven_repo</localRepository>

  <!-- nexus服务账号信息,用于本地jar发布到伺服服务器使用-->
  <servers>
	<server>
      <id>releases</id>
      <username>admin</username>
      <password>admin</password>
    </server>

    <server>
        <id>snapshots</id>
        <username>admin</username>
        <password>admin</password>
    </server>
  </servers>

  <!-- nexus伺服仓库镜像地址,用于获取maven依赖包 -->
  <mirrors>
	<mirror>
		<id>public</id>
		<name>public</name>
		<mirrorOf>*</mirrorOf>
		<url>http://192.168.110.42:8081/repository/maven-public/</url>
	</mirror>

	<mirror>
		<id>releases</id>
		<name>releases</name>
		<mirrorOf>releases</mirrorOf>
		<url>http://192.168.110.42:8081/repository/maven-releases/</url>
	</mirror>

	<mirror>
		<id>snapshots</id>
		<name>snapshots</name>
		<mirrorOf>snapshots</mirrorOf>
		<url>http://192.168.110.42:8081/repository/maven-snapshots/</url>
	</mirror>
  </mirrors> 

  <!-- maven环境-->
  <profiles>
	<profile>
		<id>ht</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> 
			<encoding>UTF-8</encoding> 
		</properties> 
        <!--工件仓库配置属性-->
        <repositories>
            <repository>
                <id>ht</id>
                <name>ht nexus Repository</name>
                <url>http://192.168.110.42:8081/repository/maven-public/</url>
                <layout>default</layout>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
                <releases>
                    <enabled>true</enabled>
                </releases>
            </repository>
        </repositories>
        <!--插件仓库配置属性-->
        <pluginRepositories>
            <pluginRepository>
                <id>ht</id>
                <name>ht nexus plugin Repository</name>
                <url>http://192.168.110.42:8081/repository/maven-public/</url>
                <layout>default</layout>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
                <releases>
                    <enabled>true</enabled>
                </releases>
            </pluginRepository>
		</pluginRepositories>
	</profile>	
  </profiles>

  <!-- 激活maven使用的环境 -->
  <activeProfiles>
	<activeProfile>ht</activeProfile>
  </activeProfiles>
</settings>

  • idea集成maven配置,使用配置伺服环境的maven

  • 本地maven项目直接使用伺服仓库地址获取项目依赖包,增加此处配置会优先使用此处配置获取项目依赖包 
<repositories>
        <repository>
            <id>public</id>
            <name>public</name>
            <url>http://192.168.110.42:8081/repository/maven-public/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
</repositories>

  • 本地项目配置伺服地址,实现本地项目发布到伺服仓库,供第三方使用

ps:此处配置的伺服仓库id要与maven配置中settings.xml的serverid一致,保证能通伺服仓库的授权验证

<!--伺服发布地址-->
<distributionManagement>
        <repository>
            <id>releases</id>
            <name>releases</name>
            <url>http://192.168.110.42:8081/repository/maven-releases/</url>
        </repository>

        <snapshotRepository>
            <id>snapshots</id>
            <name>snapshots</name>
            <url>http://192.168.110.42:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
</distributionManagement>

  • 使用idea发布本地maven项目到nexus伺服仓库

  •  查看伺服仓库是否有本地打包发布后的项目工程文件

PS: pom版本号包含SNAPSHOT,例如0.0.1-SNAPSHOT,会将包打包到snapshots仓库,并且每次都会生成一个新的包,如果版本号不包含SNAPSHOT,则会打包到releases仓库,只会有一个包。​​​​​​​​​​​​​​

结语

本节内容到这里就结束了,我们下期见。。。

以上是关于maven集成nexus伺服服务实现项目快速自动化构建与发布的主要内容,如果未能解决你的问题,请参考以下文章

centos7案例实战——Nexus3伺服仓库服务器搭建

centos7案例实战——Nexus3伺服仓库服务器搭建

Jenkins+Maven+Gitlab+Nexus持续集成环境搭建

[Maven]Nexus自建伺服切换地址之后遇到的问题

剑指架构师系列-持续集成之Maven+Nexus+Jenkins+git+Spring boot

maven+jenkins+nexus+git 持续集成