如何搭建Maven私服和使用Maven私服

一、我们为什么要使用私服?

主要有两个目的:第一个,避免直接从远程仓库下载我们在自己的项目中需要的构件;第二个,部署我们自己的公共构件到一个私有的仓库(我们自己的私服),方便在各个项目中共享。

二、私服环境搭建

这里我们基于Linux环境搭建。

第一步:下载nexus包(wget https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.11.2-03-bundle.tar.gz

);

第二步:解压(tar -xzvf nexus-2.11.2-03-bundle.tar.gz -C nexus);

第三步:启动(bin/nexus start);

第四步:访问(http://116.62.40.32:8081/nexus),启动后的默认端口是8081;注意,这里的访问地址直接用了IP地址,如果想通过域名访问,我们可以增加Nginx配置来实现。

server {

    listen       80;

    server_name  自己设置的域名;

    #charset koi8-r;

    access_log  访问日志目录  main;

    location / {

        proxy_redirect          off;

        proxy_set_header        Host            $host;

        proxy_set_header        X-Real-IP       $remote_addr;

        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

        client_max_body_size    64m;

        client_body_buffer_size 10M;

        proxy_buffers           32 4k;

        proxy_connect_timeout   3;

        proxy_send_timeout      30;

        proxy_read_timeout      30;

        proxy_pass http://127.0.0.1:8081;//代理的访问路径

    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html

    #

    error_page   500 502 503 504  /50x.html;

    location = /50x.html {

        root   /usr/share/nginx/html;

    }

}

三、私服使用

1.默认登录账号:用户名-admin,密码-admin123

2.创建自己的仓库;



 

 

3.让自己的每一个项目都使用私服下载构件——修改Maven的settings.xml文件(完整的配置)

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

<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>/Users/wuhoujian/Documents/m2/repository</localRepository>

<pluginGroups>

</pluginGroups>

<proxies>

</proxies>

<servers>

<server>

<id>jhzz-releases</id>

<username>xxx</username>

<password>xxx</password>

</server>

<server>

<id>jhzz-snapshots</id>

<username>xxx</username>

<password>xxx</password>

</server>

</servers>

<mirrors>

    <mirror>

        <id>jhzz-nexus</id>

        <mirrorOf>*</mirrorOf>

        <name>MyRepository</name>

        <!--私服仓库地址-->

        <url>自己的私服地址/nexus/content/groups/public/</url>

    </mirror>

  </mirrors>

<profiles>

<profile>

        <id>jhzz-nexus</id>

        <repositories>

            <repository>

                <id>central</id>

                <name>MyRepository</name>

                <url>http://central</url>

                <releases>

                     <enabled>true</enabled>

                     <updatePolicy>always</updatePolicy>

                </releases>

                <snapshots>

                     <enabled>true</enabled>

                     <updatePolicy>always</updatePolicy>

                </snapshots>

            </repository>

        </repositories>

        <pluginRepositories>

            <pluginRepository>

                <id>central</id>

                <url>http://central</url>

                <releases>

                     <enabled>true</enabled>

                     <updatePolicy>always</updatePolicy>

                </releases>

                <snapshots>

                     <enabled>true</enabled>

                     <updatePolicy>always</updatePolicy>

                </snapshots>

            </pluginRepository>

        </pluginRepositories>

</profile>

</profiles>

<activeProfiles>

<activeProfile>jhzz-nexus</activeProfile>

</activeProfiles>

</settings>

4.如何部署自己的构件到私服?

第一步:settings.xml添加权限认证配置,否则无法完成构件的部署;

第二步:修改项目的pom.xml文件;

<distributionManagement>

<repository>

<id>jhzz-releases</id>

<name>JHZZ Releases Repository</name>

<url>http://nexus.jihezhizao.com/nexus/content/repositories/jhzz-releases/</url>

</repository>

<snapshotRepository>

<id>jhzz-snapshots</id>

<name>JHZZ Snapshots Repository</name>

<url>http://nexus.jihezhizao.com/nexus/content/repositories/jhzz-snapshots/</url>

</snapshotRepository>

</distributionManagement>

第三步:部署构件到私服;

简单部署:mvn clean deploy

复杂部署:mvn clean source:jar deploy -D maven.test.skip=true(同时部署构件和对应的源码,这样我们在项目中引入自己部署的构件,便可以查看源码)

猜你喜欢

转载自williamwhj.iteye.com/blog/2394601
今日推荐