nexus私服搭建及maven配置使用

nexus私服搭建

參考資料
Nexus搭建開發組的私有倉庫
https://www.cnblogs.com/fanzhenyong/p/7709434.html
nexus/bin/nexus 下修改INSTALL4J_JAVA_HOME_OVERRIDE=java環境變量
/opt/nexus/etc/nexus-default.propertie s修改端口
/opt/nexus/bin/nexus.rc 修改運行配置

# vi /etc/systemd/system/nexus.service

# file content start
[Unit]
Description=nexus service
After=network.target

[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/opt/nexus/bin/nexus start
ExecStop=/opt/nexus/bin/nexus stop
User=root
Restart=on-abort

[Install]
WantedBy=multi-user.target
# file content end

# set file encode unix not dos
:set ff=unix
# 設置爲自啓服務
# systemctl  daemon-reload
# systemctl start nexus.service
# systemctl status nexus.service
# systemctl enable nexus.service
# 幾種倉庫類型

- hosted,本地倉庫,通常我們會部署自己的構件到這一類型的倉庫。比如公司的第二方庫。

- proxy,代理倉庫,它們被用來代理遠程的公共倉庫,如maven中央倉庫。

- group,倉庫組,用來合併多個hosted/proxy倉庫,當你的項目希望在多個repository使用資源時就不需要多次引用了,只需要引用一個group即可。

# 預置三個本地倉庫

- Releases:這裏存放我們自己項目中發佈的構建, 通常是Release版本的, 比如我們自己做了一個FTP Server的項目, 生成的構件爲ftpserver.war, 我們就可以把這個構建發佈到Nexus的Releases本地倉庫. 關於符合發佈後面會有介紹.

- Snapshots:這個倉庫非常的有用, 它的目的是讓我們可以發佈那些非release版本, 非穩定版本, 比如我們在trunk下開發一個項目,在正式release之前你可能需要臨時發佈一個版本給你的同伴使用, 因爲你的同伴正在依賴你的模塊開發, 那麼這個時候我們就可以發佈Snapshot版本到這個倉庫, 你的同伴就可以通過簡單的命令來獲取和使用這個臨時版本.

- 3rd Party:顧名思義, 第三方庫, 你可能會問不是有中央倉庫來管理第三方庫嘛,沒錯, 這裏的是指可以讓你添加自己的第三方庫, 比如有些構件在中央倉庫是不存在的. 比如你在中央倉庫找不到Oracle 的JDBC驅動, 這個時候我們就需要自己添加到3rdparty倉庫。


# 設置maven settings

<?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>D:/deploy/apache-maven-3.5.3/repository</localRepository>
    <!-- 配置鏡像 -->
    <mirrors>
        <mirror>
            <!-- 此鏡像一般用來作爲公司第三方引用基礎類庫鏡像,是所有倉庫的鏡像地址 -->
            <id>nexus</id>
            <!-- 爲*表示爲所有的倉庫做鏡像,有了這個配置,所有的構建都會包含public組,如果你想包含public-snapshots組,
                你必須添加public-snapshots這個Profile,通過在命令行使用如下的 -P 標誌:$ mvn -P public-snapshots clean install -->
            <mirrorOf>*</mirrorOf>
            <url>http://192.168.174.161:8081/repository/maven-public/</url>
        </mirror>
        <mirror>
            <!-- 此鏡像一般用來作爲公司內部開發的版本快照,作爲public-snapshots倉庫的鏡像地址 -->
            <!-- 鏡像的id,id用來區分不同的mirror元素。 -->
            <id>nexus-public-snapshots</id>
            <!-- 被鏡像的服務器的id。例如,如果我們要設置了一個Maven中央倉庫(http://repo1.maven.org/maven2)的鏡像,
                就需要將該元素設置成central。這必須和中央倉庫的id “central”完全一致。 -->
            <mirrorOf>public-snapshots</mirrorOf>
            <!-- 該鏡像的URL。 -->
            <url>http://192.168.174.161:8081/repository/maven-public/</url>
        </mirror>
    </mirrors>

    <!-- settings.xml中的profile元素是pom.xml中profile元素的裁剪版本。它包含了activation, repositories, pluginRepositories 和 properties元素。
         這裏的profile元素只包含這四個子元素是因爲這裏只關心構建系統這個整體(這正是settings.xml文件的角色定位),而非單獨的項目對象模型設置。
       如果一個settings中的profile被激活,它的值會覆蓋任何其它定義在POM中或者profile.xml中的帶有相同id的profile。 -->
    <profiles>
        <profile>
            <id>development</id>
            <!-- 倉庫。倉庫是Maven用來填充構建系統本地倉庫所使用的一組遠程項目。而Maven是從本地倉庫中使用其插件和依賴。
                不同的遠程倉庫可能含有不同的項目,而在某個激活的profile下,可能定義了一些倉庫來搜索需要的發佈版或快照版構件。有了Nexus,這些應該交由Nexus完成 -->
            <repositories>
                <repository>
                    <id>central</id>
                    <!-- 虛擬的URL形式,指向鏡像的URL,因爲所有的鏡像都是用的是nexus,這裏的central實際上指向的是http://repos.d.xxx.com/nexus/content/groups/public -->
                    <url>http://central</url>
                    <!-- 表示可以從這個倉庫下載releases版本的構件-->
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <!-- 表示可以從這個倉庫下載snapshot版本的構件 -->
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>

            <!-- 插件倉庫。倉庫是兩種主要構件的家。第一種構件被用作其它構件的依賴。這是中央倉庫中存儲大部分構件類型。
                另外一種構件類型是插件。Maven插件是一種特殊類型的構件。由於這個原因,插件倉庫獨立於其它倉庫。
               pluginRepositories元素的結構和repositories元素的結構類似。每個pluginRepository元素指定一個Maven可以用來尋找新插件的遠程地址。 -->
            <pluginRepositories>
                <pluginRepository>
                    <id>central</id>
                    <url>http://central</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>

        <profile>
            <!--this profile will allow snapshots to be searched when activated-->
            <id>public-snapshots</id>
            <repositories>
                <repository>
                    <id>public-snapshots</id>
                    <!-- 虛擬的URL形式,指向鏡像的URL,這裏指向的是http://repos.d.xxx.com/nexus/content/groups/public-snapshots -->
                    <url>http://public-snapshots</url>
                    <releases>
                        <enabled>false</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>public-snapshots</id>
                    <url>http://public-snapshots</url>
                    <releases>
                        <enabled>false</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>

    <!-- 激活的Profile。activation元素並不是激活profile的唯一方式。settings.xml文件中的activeProfile元素可以包含profile的id,
        任何在activeProfile中定義的profile id,不論環境設置如何,其對應的profile都會被激活。如果沒有匹配的profile,則什麼都不會發生。
      profile也可以通過在命令行,使用-P標記和逗號分隔的列表來顯式的激活(如,-P test)。
      要了解在某個特定的構建中哪些profile會激活,可以使用maven-help-plugin(mvn help:active-profiles)。 -->
    <activeProfiles>
        <!-- 沒有顯示激活public-snapshots -->
        <activeProfile>development</activeProfile>
        <activeProfile>public-snapshots</activeProfile>
    </activeProfiles>

    <!-- 發佈的服務器和密碼,暫時未限制權限 -->
    <servers>
        <!-- 發佈的位置在POM中配置,以ID爲關聯,有很多公用的信息需要配置在POM文件裏,最佳實踐是定義一個公司級別的root pom -->
        <server>
            <id>thirdparty</id>
            <username>admin</username>
            <password>admin123</password>
        </server>
        <server>
            <id>shsq-releases</id>
            <username>admin</username>
            <password>admin123</password>
        </server>
        <server>
            <id>shsq-snapshots</id>
            <username>admin</username>
            <password>admin123</password>
        </server>
    </servers>

</settings>

# pom本地發佈配置

<distributionManagement>
        <repository>
            <id>shsq-releases</id>
            <name>User Project Release</name>
            <url>http://192.168.174.161:8081/repository/maven-releases/</url>
        </repository>

        <snapshotRepository>
            <id>shsq-snapshots</id>
            <name>User Project SNAPSHOTS</name>
            <url>http://192.168.174.161:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章