Nexus3仓库Docker Compose部署

2024-08-05 293 0

docker-compose.yaml

version: "3.3"
services:
  nexus:
    image: sonatype/nexus3:3.70.1
    container_name: nexus3
    restart: always
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 8081:8081
    volumes:
      - /data/app_data/nexus:/nexus-data

修改数据目录权限

mkdir -p /data/app_data/nexus/ && chown -R 200 /data/app_data/nexus/
docker compose up -d

需要等待一会,第一次启动没那么快

image-20240805182952019

查看admin密码

root@demo18:/data/docker_yaml/nexus# cat /data/app_data/nexus/admin.password 
371ce8d4-xxxxxxxx-4bd8-8e68-xxxxxxxx

默认的这几个仓库我解释一下:

  • maven-central:maven中央库,默认从https://repo1.maven.org/maven2/ 拉取jar
  • maven-releases:私库发行版jar,初次安装请将Deployment policy设置为Allow redeploy
  • maven-snapshots:私库快照(调试版本)jar
  • maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml中使用。

Nexus默认的仓库类型有以下四种:

  • group(仓库组类型):又叫组仓库,用于方便开发人员自己设定的仓库;
  • hosted(宿主类型):内部项目的发布仓库(内部开发人员,发布上去存放的仓库);
  • proxy(代理类型):从远程中央仓库中寻找数据的仓库(可以点击对应的仓库的Configuration页签下Remote Storage属性的值即被代理的远程仓库的路径);
  • virtual(虚拟类型):虚拟仓库(这个基本用不到,重点关注上面三个仓库的使用);
    Policy(策略): 表示该仓库为发布(Release)版本仓库还是快照(Snapshot)版本仓库;

首页加载慢

2024-08-05 18:41:52,083+0800 WARN  [pool-6-thread-2] *UNKNOWN com.sonatype.nexus.plugins.outreach.internal.outreach.SonatypeOutreach - Could not download page bundle
org.apache.http.conn.ConnectTimeoutException: Connect to sonatype-download.global.ssl.fastly.net:443 [sonatype-download.global.ssl.fastly.net/75.126.164.178] failed: connect timed out

image-20240805184350562

中央仓库代理

由于访问中央仓库有时候会比较慢,这里我添加一个阿里云的代理仓库,然后优先级放到默认中央库之前, 阿里云的maven仓库url为 http://maven.aliyun.com/nexus/content/groups/public

image-20240807130404102
然后在maven-public组加入aliyun-proxy仓库,排在maven-central之前即可。

image-20240807130521216

本地Maven使用私服

安装和配置好之后,在开发中如何使用呢。可在maven的默认配置settings.xml中修改如下:

<servers>
    <server>
        <id>releases</id>
        <username>admin</username>
        <password>admin123</password>
    </server>
    <server>
        <id>snapshots</id>
        <username>admin</username>
        <password>admin123</password>
    </server>
    <server>
        <id>nexus</id>
        <username>admin</username>
        <password>admin123</password>
    </server>  
</servers>

<mirrors>
    <mirror>
        <id>nexus</id>
        <mirrorOf>*</mirrorOf>
        <url>http://192.168.10.18:8081/repository/maven-public/</url>
    </mirror>
</mirrors>

<profiles>
    <profile>  
      <id>dev</id>
      <repositories>
        <repository>
          <id>Nexus</id>
          <url>http://192.168.10.18:8081/repository/maven-public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>
      </repositories>
      <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>
      </properties>
    </profile>
</profiles>
<activeProfiles>
    <activeProfile>dev</activeProfile>
</activeProfiles>

如果要发布自己的jar到私服,就需要修改工程的pom.xml,添加如下内容,否则什么都不用做:

<distributionManagement>
    <repository>
        <id>releases</id>
        <name>Releases</name>
        <url>http://192.168.10.18:8081/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
        <id>snapshots</id>
        <name>Snapshot</name>
        <url>http://192.168.10.18:8081/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

注意上面的repository的id值一定要跟settings.xml文件中配置的server一致。

# 安装到电脑本地仓库
mvn install
# 上传到远程仓库(即nexus)
mvn deploy

上传到Nexus上,使用 mvn deploy 即可,开发的时候请使用snapshot版本,也就是version的后缀必须是-SNAPSHOT。

<groupId>com.enzhico</groupId>
<artifactId>micro-pay-sdk</artifactId>
<version>1.2-SNAPSHOT</version>
<packaging>jar</packaging>

第三方Jar上传到Nexus

mvn deploy:deploy-file \
    -DgroupId=<group-id> \
    -DartifactId=<artifact-id> \
    -Dversion=<version> \
    -Dpackaging=<type-of-packaging> \
    -Dfile=<path-to-file> \
    -DrepositoryId=<server-id-settings.xml> \
    -Durl=<url-of-the-repository-to-deploy>

# 如第三方JAR包:aliyun-sdk-oss-2.2.3.jar
mvn deploy:deploy-file 
  -DgroupId=com.aliyun.oss 
  -DartifactId=aliyun-sdk-oss 
  -Dversion=2.2.3 
  -Dpackaging=jar 
  -Dfile=D:\aliyun-sdk-oss-2.2.3.jar 
  -Durl=http://192.168.10.18:8081/repository/maven-3rd/ 
  -DrepositoryId=nexus-releases

建议在上传第三方JAR包时,创建单独的第三方JAR包管理仓库,便于管理有维护。(maven-3rd)
-DrepositoryId的值即为在setttings.xml里面配置的server id。

上次不同JDK版本

pom.xml里面配置多个profile,其中一个默认的:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
            <configuration>
                <source>${jar.source}</source>
                <target>${jar.target}</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.8.2</version>
            <executions>
                <execution>
                    <id>deploy</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>deploy</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <id>default</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <jar.source>1.8</jar.source>
            <jar.target>1.8</jar.target>
        </properties>
    </profile>
    <profile>
        <id>jdk16</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                            <configuration>
                                <classifier>jdk16</classifier>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
        <properties>
            <jar.source>1.6</jar.source>
            <jar.target>1.6</jar.target>
        </properties>
    </profile>
</profiles>

上面我定义了两个profile,那么打包或者发布的时候可指定不同的JDK版本:

# 默认版本JDK1.8
mvn clean && mvn deploy
# JDK1.6版本
mvn clean && mvn deploy -P jdk16

第一条命令打包使用默认的profile,编译的版本是1.8,生成的文件是xxx-SNAPSHOT.jar; 而第二条命令打包指定使用jdk16这个profile,编译版本是1.6,生成的文件是xxx-SNAPSHOT-jdk16.jar。

项目中引用的时候可通过指定classifier:

<dependency>
    <groupId>com.enzhico</groupId>
    <artifactId>adm-traffic-common-model</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <classifier>jdk16</classifier>
</dependency>

发布源码和文档
如果你还想发布源码和javadoc,那么需要使用maven插件,我把插件配置列出来:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <configuration>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>3.0.1</version>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>2.10.4</version>
            <configuration>
                <encoding>UTF-8</encoding>
                <aggregate>true</aggregate>
                <charset>UTF-8</charset>
                <docencoding>UTF-8</docencoding>
            </configuration>
            <executions>
                <execution>
                    <id>attach-javadocs</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.8.2</version>
            <executions>
                <execution>
                    <id>deploy</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>deploy</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

发布javadoc的时候,每个方法注释必须遵循规范,比如参数、返回值、异常都应该有说明。

打包或发布的时候如果想跳过测试,加一个参数:

mvn clean && mvn deploy -DskipTests=true

报错

root@demo18:/data/docker_yaml/nexus# docker-compose logs
Unable to update instance pid: Unable to create directory /nexus-data/instances
mkdir: cannot create directory '../sonatype-work/nexus3/log': Permission denied
mkdir: cannot create directory '../sonatype-work/nexus3/tmp': Permission denied

解决:
mkdir -p /data/app_data/nexus/ && chown -R 200 /data/app_data/nexus/

https://segmentfault.com/a/1190000005966312

相关文章

Linux Sudo 权限配置
阿里云ECS云盘IOPS压测
nextcloud preview-generate 报错文件无权限解决
tcpdump 抓包使用小结
Linux Tcpdump抓包
Ubuntu 18 SSH禁用密码登陆失效

发布评论