解疑惑----maven中snapshot相关jar无法拉取问题

本文解决Maven中无法正确拉取snapshot版本的jar包问题。通过调整setting-main.xml配置,增加对snapshot版本的支持,确保开发过程中的顺利进行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这段时间maven中经常不能拉取snapshot相关jar的问题一直困扰着,今天把相关解决方案整理下。
使用的是局域网中的Snaptype Nexus服务器的,maven的setting-main.xml相关配置如下:

<!-- <mirrors> ... </mirrors> -->

</profile>  
     <profile>
      <id>redstar-nexus</id>
      <repositories>
        <repository>
          <id>central</id>
          <url>http://nexus.******.com/nexus/content/groups/public/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
          <updatePolicy>always</updatePolicy>

        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://nexus.******.com/nexus/content/groups/public/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
          <updatePolicy>always</updatePolicy>
        </pluginRepository>
      </pluginRepositories>
</profile>

<activeProfiles>
    <activeProfile>redstar-nexus</activeProfile>
</activeProfiles>

项目中有相关pom.xml引入的jar配置:

<!-- 引入父jar包 -->
<parent>
    <artifactId>parent</artifactId>
    <groupId>com.*****</groupId>
    <version>1.3.0.RELEASE</version>
</parent>

<!-- 引入服务的snapshot包-->
<dependencies>
  <dependency>
    <groupId>com.*******</groupId>
    <artifactId>show-tools</artifactId>
    <version>1.0.2-SNAPSHOT</version>
  </dependency>
</dependencies>

在使用项目的clean命令时,报错。因为默认是从maven的中央仓库中,更新jar报,但是报找不到parent中的引入jar包的。为了解决问题。在setting-main.xml文件中添加 <mirror>的相关配置:

<mirror>
            <id>private-mirror</id>
            <mirrorOf>*</mirrorOf>
            <name>private-mirror</name>
            <url>http://nexus.*****.com/nexus/content/groups/public/</url>
        </mirror>

这样只解决了仓库引入的问题。snapshot的jar引入依然没有解决。
但是mirror的配置,只对release有效,对于snapshot无效的。所以我们添加个对snapshot有效的:

<mirrors>
    <mirror>
        <id>private-mirror</id>
        <mirrorOf>*</mirrorOf>
        <name>private-mirror</name>
        <url>http://nexus.corp.rs.com/nexus/content/groups/public/</url>
    </mirror>

    <mirror>
        <id>private-snapshots</id>
        <url>http://nexus.****.com/nexus/content/repositories/snapshots/</url>
        <mirrorOf>public-snapshots</mirrorOf>
    </mirror>
</mirrors>

<profile>
    <id>public-snapshots</id>
    <repositories>  
        <repository>
            <id>nexus_snapshot_repository</id>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <url>http://nexus.*****.com/nexus/content/repositories/snapshots/</url>
            <layout>default</layout>
        </repository>

    </repositories>

    <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://nexus.*****.com/nexus/content/groups/public/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
          <updatePolicy>always</updatePolicy>
        </pluginRepository>
    </pluginRepositories>

</profile>

<activeProfiles>
    <activeProfile>public-snapshots</activeProfile>
</activeProfiles>

问题解决。这个配置的有些繁琐的,应该有更方便的方式。后续更新优化的。

snapshot的出现是解决release版本有变动要升级,在开发过程中造成频繁升级的问题的。maven会自动从仓库中检查引入服务的snapshot版本的最新版本,发现有要更新的时候进行下载的。默认每天一更新。通过updatePolicy控制(默认值是daily,表示Maven每天检查一次。其他可用的值包括:never-从不检查更新;always-每次构建都检查更新;interval:X-每隔X分钟检查一次更新(X为任意整数)

### 下载 Maven Archetype Plugin JAR 文件 为了获 `maven-archetype-plugin` 的 JAR 文件,通常不需要手动下载该文件。Maven 会自动处理依赖关系并从中央仓库下载所需的插件及其版本。 然而,如果确实需要手动操作或遇到网络连接问题阻止了正常下载,则可以采以下方法: #### 方法一:通过 Maven 自动安装 确保本地环境配置正确,并且能够访问互联网上的 Maven 中央仓库。接着,在命令行工具中输入如下指令来创建一个新的项目结构,这将触发 Maven 安装必要的插件和库: ```bash mvn archetype:generate \ -DgroupId=com.example \ -DartifactId=my-web-DarchetypeArtifactId=maven-archetype-webapp \ -Dversion=1.0-SNAPSHOT \ -DinteractiveMode=false ``` 此过程会自动最新的 `maven-archetype-plugin` 及其关联的 JAR 文件[^2]。 #### 方法二:直接从 Maven Central Repository 获无法通过上述方式获得所需资源时,可以直接前往[Maven Central](https://search.maven.org/)网站搜索 `maven-archetype-plugin` 并找到特定版本对应的链接进行下载。例如对于版本3.0.1, URL可能是这样的形式: > https://repo1.maven.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/3.0.1/ 在这个页面上可以选择不同格式的打文件(如 `.pom`, `.jar`),根据需求选择合适的文件类型点击下载即可[^3]。 另外需要注意的是,如果遇到了类似于 "Connection to http://repo.maven.apache.org refused." 这样的错误提示,可能是因为防火墙设置或其他原因导致无法连接至官方镜像源。此时建议修改 `settings.xml` 配置文件中的 `<mirrors>` 节点指向国内可用的速度较快的镜像站点,比如阿里云提供的公共镜像服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值