Nexus3 For Docker 安裝配置

Nexus3 For Docker 安裝配置教程

安裝前準備

安裝 nexus3:

下載&安裝

$ docker pull sonatype/nexus3
# 創建掛載目錄
$ mkdir -p /home/nexus/nexus-data
# 授權掛載目錄
$ chown -R 200 /home/nexus/nexus-data
# 運行 nexus
$ docker run --name nexus \
    --restart=unless-stopped \
    -v /home/nexus/nexus-data:/nexus-data \
    -p 10000:8081 \
    -d sonatype/nexus3
# -v 掛載目錄,防止 docker 停止導致數據丟失
# -p 端口映射, 使外部可以訪問

查看日誌

$ docker logs -f nexus

登錄 nexus

# 獲取 admin 登錄密碼
$ cat /home/nexus/nexus-data/admin.password

# 瀏覽器輸入 ip:10000
# 點擊右上角 sign in
# 輸入用戶名密碼,然後按提示操作 

創建角色: 開發者最小權限集請看(騷操作)

創建用戶

添加 aliyun maven 倉庫

# http://maven.aliyun.com/nexus/content/groups/public/

配置 maven -> setting.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>/root/.m2/repository</localRepository>

  <pluginGroups>
  </pluginGroups>

  <proxies>
  </proxies>

  <servers>
    <!-- 配置服務,其實只要一個就可以,但是爲了權限區分,所以使用多個-->
	<server>
      <id>nexus</id>
      <username>project-developer</username>
      <password>developer123</password>
    </server>
	<server>
      <id>nexus-releases</id>
      <username>project-developer</username>
      <password>developer123</password>
    </server>
	<server>
      <id>nexus-snapshots</id>
      <username>project-developer</username>
      <password>developer123</password>
    </server>
  </servers>

  <mirrors>
    <!-- 定義鏡像地址 -->
    <!-- 私有鏡像庫 id 要和 servers 中的 server 中的 id 一致, 纔會用對應的信息驗證權限-->
    <!-- mirrorOf 直接用 * 就可以 -->
    <mirror>
	  <id>nexus</id>
	  <name>myself maven</name>
	  <mirrorOf>*</mirrorOf>
	  <url>http://192.168.220.50:10000/repository/maven-public/</url>
	</mirror>
	<mirror>
	  <id>alimaven</id>
	  <name>aliyun maven</name>
	  <mirrorOf>central</mirrorOf>
	  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
	</mirror> 
	<mirror>  
      <id>defmaven</id>
	  <name>maven repository</name>
      <mirrorOf>central</mirrorOf>
      <url>http://repo2.maven.org/maven2/</url>
    </mirror>
  </mirrors>

  <profiles>
    <!-- 如果該處定義的 profile 都在 activeProfiles 中激活的話, 會按這邊的順序後面的覆蓋前面的-->
	<!-- override maven center releases and snapshots -->
    <profile>
      <id>central</id>
      <repositories>
          <repository>
              <id>central</id>
              <url>http://central</url>
              <releases><enabled>true</enabled></releases>
              <snapshots><enabled>true</enabled></snapshots>
          </repository>
      </repositories>
      <pluginRepositories>
          <pluginRepository>
              <id>central</id>
              <url>http://central</url>
              <releases><enabled>true</enabled></releases>
              <snapshots><enabled>true</enabled></snapshots>
          </pluginRepository>
      </pluginRepositories>
    </profile>
    <!-- 定義私庫 -->
    <profile>
        <id>nexus</id>
        <repositories>
          <repository>
            <id>nexus</id>
            <name>Nexus</name>
            <url>http://192.168.220.50:10000/repository/maven-public/</url>
            <releases><enabled>true</enabled></releases>
            <snapshots><enabled>true</enabled></snapshots>
          </repository>
        </repositories>
        <pluginRepositories>
          <pluginRepository>
            <id>nexus</id>
            <name>Nexus</name>
            <url>http://192.168.220.50:10000/repository/maven-public/</url>
            <releases><enabled>true</enabled></releases>
            <snapshots><enabled>true</enabled></snapshots>
          </pluginRepository>
        </pluginRepositories>
    </profile>
  </profiles>
 
  <!-- active last profile which definition in profiles -->
  <activeProfiles>
    <activeProfile>central</activeProfile>
	<activeProfile>nexus</activeProfile>
  </activeProfiles>
</settings>

配置項目 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.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.texous.test</groupId>
    <artifactId>demo-gitlabci</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-gitlabci</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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <distributionManagement>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <url>http://192.168.220.50:10000/repository/maven-snapshots/</url>
        </snapshotRepository>
        <repository>
            <id>nexus-releases</id>
            <url>http://192.168.220.50:10000/repository/maven-releases/</url>
        </repository>
    </distributionManagement>

</project>

騷操作

nexus 圖形界面介紹

nexus3 開發者最小權限集

# nx-repository-view-*-*-browse
# nx-repository-view-*-*-edit
# nx-repository-view-*-*-read

maven docker 鏡像配置本地倉庫

  • maven 官方鏡像說明
  • maven 的鏡像默認配置文件地址 /root/.m2
  • 所以實現自定義 settings.xml 只需要將配置好的 settings.xml 拷貝到 /root/.m2 的掛載目錄下
  • 比如 docker run -v /data/.m2:/root/.m2 那麼只要將 settings.xml 拷貝到 /data/.m2/ 就可以了
  • 注意: settings.xml 中的 localRepository 需要配鏡像內的地址,而不是掛載地址. 例: /root/.m2/repository

可優化

  • nexus3 具有 docker 鏡像倉庫的功能, 後期可以從 gitlab 自帶倉庫中遷移

參考文獻

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章