发布时间:2025-06-24 20:14:42  作者:北方职教升学中心  阅读量:169


另外,在Nexus第一次启动时,会在数据卷中创建初始密码文件,我们需要查看该文件,获得初始密码我们才能进行进一步的Nexus初始化工作。Ubuntu下使用VisualStudioCode进行Java开发

你需要Nexus

Java应用编译构建的一种主流方式就是通过Maven, Maven可以很方便的管理Java应用的各种依赖包。Ubuntu下Docker环境安装
三、

vian@txzq1899-ubuntu:/media/vian/Work/Workspace/hello-world$ mvn clean install[INFO]Scanning forprojects...[INFO][INFO]------------------------<com.txzq:hello-world >------------------------[INFO]Building hello-world 0.0.1-SNAPSHOT[INFO]from pom.xml[INFO]--------------------------------[jar ]---------------------------------[INFO][INFO]--- clean:3.3.2:clean (default-clean)@ hello-world ---[INFO]Deleting /media/vian/Work/Workspace/hello-world/target[INFO][INFO]--- resources:3.3.1:resources (default-resources)@ hello-world ---[INFO]Copying 1resource from src/main/resources to target/classes[INFO]Copying 0resource from src/main/resources to target/classes[INFO][INFO]--- compiler:3.11.0:compile (default-compile)@ hello-world ---[INFO]Changes detected - recompiling the module!:source[INFO]Compiling 1sourcefilewith javac [debug release 17]to target/classes[INFO][INFO]--- resources:3.3.1:testResources (default-testResources)@ hello-world ---[INFO]skip non existing resourceDirectory /media/vian/Work/Workspace/hello-world/src/test/resources[INFO][INFO]--- compiler:3.11.0:testCompile (default-testCompile)@ hello-world ---[INFO]Changes detected - recompiling the module!:dependency[INFO]Compiling 1sourcefilewith javac [debug release 17]to target/test-classes[INFO][INFO]--- surefire:3.1.2:test (default-test)@ hello-world ---[INFO]Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider[INFO][INFO]-------------------------------------------------------[INFO]T E S T S[INFO]-------------------------------------------------------[INFO]Running com.txzq.helloworld.HelloWorldApplicationTests19:28:21.256 [main]INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils -- Could not detect default configuration classes fortestclass [com.txzq.helloworld.HelloWorldApplicationTests]: HelloWorldApplicationTests does not declareany static, non-private, non-final, nested classes annotated with @Configuration.19:28:21.413 [main]INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper -- Found @SpringBootConfiguration com.txzq.helloworld.HelloWorldApplication fortestclass com.txzq.helloworld.HelloWorldApplicationTests  .____          _            __ _ _ /\\/ ___'_ __ _ _(_)_ __  __ _ \ \ \ \( ( )\___ | '_ |'_| | '_ \/ _` |\\\\\\/  ___)||_)|||||||(_||))))'  |____|.__|_||_|_||_\__, |/ / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot ::                (v3.2.4)2024-04-17T19:28:21.903+08:00  INFO 183809--- [hello-world][main]c.t.h.HelloWorldApplicationTests         :Starting HelloWorldApplicationTests using Java 17.0.9 with PID 183809(started by vian in/media/vian/Work/Workspace/hello-world)2024-04-17T19:28:21.905+08:00  INFO 183809--- [hello-world][main]c.t.h.HelloWorldApplicationTests         :No active profile set, falling back to 1default profile: "default"2024-04-17T19:28:23.656+08:00  INFO 183809--- [hello-world][main]c.t.h.HelloWorldApplicationTests         :Started HelloWorldApplicationTests in2.067seconds (process running for3.35)Java HotSpot(TM)64-Bit Server VM warning: Sharing is only supported forboot loader classes because bootstrap classpath has been appended[INFO]Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.697s -- incom.txzq.helloworld.HelloWorldApplicationTests[INFO][INFO]Results:[INFO][INFO]Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO][INFO][INFO]--- jar:3.3.0:jar (default-jar)@ hello-world ---[INFO]Building jar: /media/vian/Work/Workspace/hello-world/target/hello-world-0.0.1-SNAPSHOT.jar[INFO][INFO]--- spring-boot:3.2.4:repackage (repackage)@ hello-world ---Downloading from nexus: http://localhost:8081/nexus/repository/maven-public/org/springframework/boot/spring-boot-buildpack-platform/3.2.4/spring-boot-buildpack-platform-3.2.4.pomDownloaded from nexus: http://localhost:8081/nexus/repository/maven-public/org/springframework/boot/spring-boot-buildpack-platform/3.2.4/spring-boot-buildpack-platform-3.2.4.pom (3.2kB at 2.7kB/s)Downloading from nexus: http://localhost:8081/nexus/repository/maven-public/org/springframework/boot/spring-boot-loader-tools/3.2.4/spring-boot-loader-tools-3.2.4.pomDownloaded from nexus: http://localhost:8081/nexus/repository/maven-public/org/springframework/boot/spring-boot-loader-tools/3.2.4/spring-boot-loader-tools-3.2.4.pom (2.2kB at 4.7kB/s)Downloading from nexus: http://localhost:8081/nexus/repository/maven-public/org/springframework/boot/spring-boot-buildpack-platform/3.2.4/spring-boot-buildpack-platform-3.2.4.jarDownloaded from nexus: http://localhost:8081/nexus/repository/maven-public/org/springframework/boot/spring-boot-buildpack-platform/3.2.4/spring-boot-buildpack-platform-3.2.4.jar (272kB at 331kB/s)Downloading from nexus: http://localhost:8081/nexus/repository/maven-public/org/springframework/boot/spring-boot-loader-tools/3.2.4/spring-boot-loader-tools-3.2.4.jarDownloaded from nexus: http://localhost:8081/nexus/repository/maven-public/org/springframework/boot/spring-boot-loader-tools/3.2.4/spring-boot-loader-tools-3.2.4.jar (434kB at 732kB/s)[INFO]Replacing main artifact /media/vian/Work/Workspace/hello-world/target/hello-world-0.0.1-SNAPSHOT.jar with repackaged archive, adding nested dependencies inBOOT-INF/.[INFO]The original artifact has been renamed to /media/vian/Work/Workspace/hello-world/target/hello-world-0.0.1-SNAPSHOT.jar.original[INFO][INFO]--- install:3.1.1:install (default-install)@ hello-world ---[INFO]Installing /media/vian/Work/Workspace/hello-world/pom.xml to /home/vian/Dev_Environment/maven_repository/com/txzq/hello-world/0.0.1-SNAPSHOT/hello-world-0.0.1-SNAPSHOT.pom[INFO]Installing /media/vian/Work/Workspace/hello-world/target/hello-world-0.0.1-SNAPSHOT.jar to /home/vian/Dev_Environment/maven_repository/com/txzq/hello-world/0.0.1-SNAPSHOT/hello-world-0.0.1-SNAPSHOT.jar[INFO]------------------------------------------------------------------------[INFO]BUILD SUCCESS[INFO]------------------------------------------------------------------------[INFO]Total time:  12.542s[INFO]Finished at: 2024-04-17T19:28:29+08:00[INFO]------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?><settingsxmlns="http://maven.apache.org/SETTINGS/1.2.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd"><localRepository>/home/vian/Dev_Environment/maven_repository</localRepository><servers><server><id>maven-releases</id><username>nexus-user</username><password>hello-world</password></server><server><id>maven-snapshots</id><username>nexus-user</username><password>hello-world</password></server><server><id>nexus</id><username>nexus-user</username><password>hello-world</password></server><server><id>maven-central</id><username>nexus-user</username><password>hello-world</password></server></servers><mirrors><mirror><id>maven-default-http-blocker</id><mirrorOf>external:http:*</mirrorOf><name>Pseudo repository to mirror external repositories initially using HTTP.</name><url>http://0.0.0.0/</url><blocked>true</blocked></mirror><mirror><id>nexus</id><name>本地Nexus私有仓库镜像</name><url>http://localhost:8081/nexus/repository/maven-public/</url><mirrorOf>*,!spring-milestones</mirrorOf></mirror></mirrors><profiles><profile><id>txzq-dev</id><repositories><repository><id>nexus</id><name>Public Repositories</name><url>http://localhost:8081/nexus/repository/maven-public/</url><releases><enabled>true</enabled></releases></repository><repository><id>maven-central</id><name>Central Repositories</name><url>http://localhost:8081/nexus/repository/maven-central/</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></repository><repository><id>maven-releases</id><name>Release Repositories</name><url>http://localhost:8081/nexus/repository/maven-releases/</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></repository><repository><id>maven-snapshots</id><name>Snapshot Repositories</name><url>http://localhost:8081/nexus/repository/maven-snapshots/</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository></repositories></profile></profiles><activeProfiles><activeProfile>txzq-dev</activeProfile></activeProfiles></settings>

我们通过Visual Studio Code 创建一个SpringBoot 项目,并找开该项目。但是在默认情况下,maven在进行java应用编译的时候,会从maven远程仓库下载相应的依赖包。

拉取最新Nexus镜像

dockerpull sonatype/nexus3dockerimagesREPOSITORY                    TAG            IMAGE ID       CREATED         SIZEprom/prometheus-linux-amd64   latest         051cb67876a6   6days ago      262MBapache/apisix                 3.9.0-debian   5e90db4294cd   2weeks ago     326MBsonatype/nexus3               latest         b630de26992b   4months ago    570MBhello-world                   latest         d2c94e258dcb   11months ago   13.3kBbitnami/etcd                  3.5.7          985a23c47893   12months ago   146MB

我们可以看到 sonatype/nexus3这个镜像,570MB大小,所以下载的时候,需要一些时间,请大家耐心等待。#当我们看到 Started Sonatype Nexus OSS 3.63.0-01 #就表示Nexus 已经启动成功了。

  • 点Save进行保存
    在这里插入图片描述
  • 测试验证

    到这里本地的Nexus 私有库就已经基本上搭建好了。

    创建并运行Nexus容器

    • –name nexus3
      • 指定容器名称为 nexus3
    • -e NEXUS_CONTEXT=nexus
      • 指定访问上下文,即通过http://localhost:8081/nexus访问
    • INSTALL4J_ADD_VM_PARAMS
      • 调整JVM参数
    • -v nexus3:/nexus-data
      • 映射数据卷
    dockerrun -d-p8081:8081 \--namenexus3 -eNEXUS_CONTEXT=nexus \-eINSTALL4J_ADD_VM_PARAMS="-Xms1024m -Xmx1024m -XX:MaxDirectMemorySize=1024m"\-vnexus3:/nexus-data \sonatype/nexus3 

    查看容器日志

    #Nexus 初次启动会比较慢,我们可以查看nexus3的容器日志。

    0-1开始Java语言编程之路
    一、

    创建nexus数据卷

    Nexus需要从远程仓库同步依赖库,所以需要持久化存储这些依赖库到本地,因此我们需要选创建一个数据卷。

    通过Docker 快速搭建Nexus私有仓库

    Nexus是一款开源的私有仓库,他们也提供了官方的docker镜像,使用docker部署起来非常的方便,不过也有一些小细节需要我们注意,下面我们一步一步来搭建这个本地私用库吧。Maven公共仓库是在国外的,网络不好的情况下,依赖包的下载会相当的耗时,这时私有库就派上了用场。dockerlogs -fnexus3

    2024-04-17 08:34:21,619+0000 INFO  [jetty-main-1]*SYSTEM org.sonatype.nexus.siesta.SiestaServlet - JAX-RS RuntimeDelegate: org.sonatype.nexus.siesta.internal.resteasy.SisuResteasyProviderFactory@2cba0cc42024-04-17 08:34:21,663+0000 INFO  [jetty-main-1]*SYSTEM org.jboss.resteasy.plugins.validation.i18n - RESTEASY008550: Unable to findCDI supporting ValidatorFactory. Using default ValidatorFactory2024-04-17 08:34:23,205+0000 INFO  [jetty-main-1]*SYSTEM org.sonatype.nexus.siesta.SiestaServlet - Initialized2024-04-17 08:34:23,215+0000 INFO  [jetty-main-1]*SYSTEM org.sonatype.nexus.repository.httpbridge.internal.ViewServlet - Initialized2024-04-17 08:34:23,240+0000 INFO  [FelixStartLevel]*SYSTEM org.sonatype.nexus.extender.NexusLifecycleManager - Start TASKS2024-04-17 08:34:23,279+0000 INFO  [jetty-main-1]*SYSTEM org.eclipse.jetty.server.handler.ContextHandler - Started o.e.j.w.WebAppContext@1705e4a6{Sonatype Nexus,/nexus,file:///opt/sonatype/nexus/public/,AVAILABLE}2024-04-17 08:34:23,404+0000 INFO  [jetty-main-1]*SYSTEM org.eclipse.jetty.server.AbstractConnector - Started ServerConnector@195ab70a{HTTP/1.1, (http/1.1)}{0.0.0.0:8081}2024-04-17 08:34:23,405+0000 INFO  [jetty-main-1]*SYSTEM org.eclipse.jetty.server.Server - Started @81852ms2024-04-17 08:34:23,407+0000 INFO  [jetty-main-1]*SYSTEM org.sonatype.nexus.bootstrap.jetty.JettyServer - -------------------------------------------------Started Sonatype Nexus OSS 3.63.0-01-------------------------------------------------2024-04-17 08:34:23,413+0000 INFO  [FelixStartLevel]*SYSTEM org.sonatype.nexus.quartz.internal.task.QuartzTaskInfo - Task 'Metric aggregation'[content.usage.aggregation]:state=WAITING2024-04-17 08:34:23,498+0000 INFO  [FelixStartLevel]*SYSTEM org.sonatype.nexus.scheduling.TaskSchedulerImpl - Task 'Metric aggregation'[content.usage.aggregation]scheduled: cron

    访问Nexus

    • 先打开admin.password文件,复制初始密码

    打开本地Nexus:
    Local Nexus3

    第一次登录Nexus
    第一次登录后,会提示修改管理员密码

    配置阿里云代理

    进到Repository设置页面,点Create Rrepository,Repository 管理

    这里我们选择《maven2 proxy》选择maven2 proxy

    • 输入Name : aliyun
    • Proxy 设置,输入Remote storage :
      • http://maven.aliyun.com/nexus/content/groups/public

    保存配置
    在这里插入图片描述

    配置maven-public(group)

    回到Repository管理页面,找到maven-public,并点击 maven-public进入编辑页面

    • 在Member repositories 左则 Available List中的aliyun 添加右则的 Members中,并将aliyun 上移至第一的位置。Ubuntu下Java语言环境搭建
      二、
      在这里插入图片描述
      打开一个终端,通过Maven进行一次编译

      mvn clean install

      查看编译过程,我们可以看到在repackage阶段,已经从本地nexus下载依赖包了。

      #创建一个名为 nexus-data的数据卷dockervolume create --namenexus3#查看数据卷信息dockervolume inspect nexus3[{"CreatedAt":"2024-04-17T16:32:30+08:00",        "Driver":"local",        "Labels":null,        "Mountpoint":"/var/lib/docker/volumes/nexus3/_data",        "Name":"nexus3",        "Options":null,        "Scope":"local"}]root@txzq1899-ubuntu: cd/var/lib/docker/volumes/nexus3/_dataroot@txzq1899-ubuntu:/var/lib/docker/volumes/nexus3/_data# pwd/var/lib/docker/volumes/nexus3/_dataroot@txzq1899-ubuntu:/var/lib/docker/volumes/nexus3/_data# ll总计 80drwxr-xr-x  14200200409641716:34 ./drwx-----x   3root root  409641716:32 ../-rw-r--r--   12002003641716:33 admin.passworddrwxr-xr-x   3200200409641716:34 blobs/drwxr-xr-x 3362002001228841716:33 cache/drwxr-xr-x   7200200409641716:33 db/drwxr-xr-x   3200200409641716:34 elasticsearch/drwxr-xr-x   3200200409641716:33 etc/drwxr-xr-x   2200200409641716:33 generated-bundles/drwxr-xr-x   2200200409641716:33 instances/-rw-r--r--   1200200141716:33 karaf.piddrwxr-xr-x   3200200409641716:33 keystores/-rw-r--r--   12002001441716:33 lockdrwxr-xr-x   3200200409641716:34 log/drwxr-xr-x   2200200409641716:33 orient/-rw-r--r--   1200200541716:33 portdrwxr-xr-x   2200200409641716:33 restore-from-backup/drwxr-xr-x   7200200409641716:34 tmp/root@txzq1899-ubuntu:/var/lib/docker/volumes/nexus3/_data# 

      这里要注意docker默认将数据卷存放在
      /var/lib/docker/volumes 目录,而这个目录需要root权限才能访问。使用Docker搭建本地Nexus Maven私有仓库
      四、
      在使用之前,首先要在maven的配置文件中使用我们本地的私有仓库

      通常在~/.m2/目录下,有一个 setting.xml 文件,如果没有,请创建。