天天看点

maven私服搭建后,将maven工程上传到私服,并在其他maven项目中引用

maven私服搭建可参考上一篇文章maven私服搭建:docker安装Sonatype Nexus以及寻找admin用户对应的随机初始密码

1,登录Nexus首页,点击左上角的齿轮按钮,通过左边的Repositories菜单栏,进入仓库创建页面。

maven私服搭建后,将maven工程上传到私服,并在其他maven项目中引用

2,点击Create Repository,选择maven2(hosted),进入仓库信息设置页面。

在红线1处填写仓库名,并且在version policy处的artifacts type选择为Release,记住这个选择,后续有用哦。

maven私服搭建后,将maven工程上传到私服,并在其他maven项目中引用

在红线2处,选择为Allow redeploy,默认是Disable redeploy,如果是后者,那么无法上传。选择好了,选择创建仓库即可。

maven私服搭建后,将maven工程上传到私服,并在其他maven项目中引用

3,找到maven的settings文件,在<servers>标签内增加如下配置。

<server>
        <id>cloudshop</id>
        <username>bighuan</username>
        <password>bighuan</password>
      </server>
           

<server>标签中的id在maven项目中会用到,而username、password则是在nexus中创建的用户,或者使用默认的用户admin也是可以的。

maven私服搭建后,将maven工程上传到私服,并在其他maven项目中引用

4,创建一个maven项目(SpringBoot工程)进行测试。

pom.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bighuan</groupId>
    <artifactId>my-maven-repos</artifactId>
   <!-- <version>0.0.1-SNAPSHOT</version>-->
    <version>0.0.1-RELEASE</version>
    <name>my-maven-repos</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!--注意限定版本一定为RELEASE,因为上传的对应仓库的存储类型为RELEASE -->
    <!--指定仓库地址 -->
    <distributionManagement>
        <repository>
            <!--此名称要和maven的settings.xml中设置的ID一致 -->
            <id>cloudshop</id>
            <url>http://192.168.219.131:8081/repository/cloudshop-release/</url>
        </repository>
    </distributionManagement>

    <build>
       <!-- <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>-->
        <plugins>
            <!--发布代码Jar插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
            </plugin>
            <!--发布源码插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
           

4.1 配置说明

a)因为在创建仓库(cloudshop-release)时,选择的是Release,所以pom中的<version>必须是0.0.1-Release,如果是SNAPSHOT则后续上传会出错。

b)在<distributionManagement>标签中配置了仓库的相关信息,注意的是,id必须要和settings.xml文件中配置的id一致(步骤三中配置的那个id)

c)在<build>标签配置了发布jar的插件和发布源码的插件

d)在IDEA的Terminal上执行命令:mvn deploy,将代码发布到maven私服。如果命令执行后build success,到nexus的search那里能看到上传的代码,那么上传项目到maven私服就成功了。

maven私服搭建后,将maven工程上传到私服,并在其他maven项目中引用

5,在新项目中配置一下,就可以获取依赖了

<dependencies>
		<dependency>
			<groupId>com.bighuan</groupId>
  			  <artifactId>my-maven-repos</artifactId>
    			<version>0.0.1-RELEASE</version>
		</dependency>
	</dependencies>

      	<repository>
            		<!--此名称要和maven的settings.xml中设置的ID一致 -->
            		<id>cloudshop</id>
            		<url>http://192.168.219.131:8081/repository/cloudshop-release/</url>
        	</repository>
           

6,如有错误,欢迎留言指出!(又写了一篇文章,加油!)

继续阅读