使用Maven内部仓库,怎样在版本号不变的情况下让客户端更新Artifacts?

最近开始在公司内使用内部远程仓库,目的是抽取通用代码减少开发中的重复部分。
但遇到一个问题,每个Maven都有本地缓存,当我部署的一个Artifact被别人下载时,对方的Maven就会在他本地缓存这个Artifact,而我还要对这个Artifact进行修改。再次部署改动的时候,如果版本号不变,则对方的Maven不会更新这个Artifact;如果版本号变了,对方也得跟着重写版本号。这样如果变动次数很多,就会非常麻烦。
请问有什么比较好的解决方案吗。目前看到了强制清空本地缓存的方法,可这样会清空所有本地缓存。有没有更合适点的方案呢?

够呛。Maven机制就是先读本地仓库,本地仓库没得话才去从远程拉。

我从stackOverFlow上面找到了这样一种说法:

Snapshot artifacts can be force-updated by using -U option with mvn when building a project with the dependency. Maven will take the newest snapshot available of the specified version. You have to deploy the fixed snapshot artifact to the repository before. The newest snapshot is determined by the timestamp attached to the file name of the jar.

However, release versions are not updated. Once a release artifact has been downloaded and verified, you must remove it manually if you replaced it on a remote repository. Generally, you should never replace release version artifacts. Rather you should always release a new version (and possibly delete the erroneous version from the repository) and change the pom.xml files of projects which use this artifact.

For the structure of the local/remote repository, see links below.

看起来可行,我应该把正在开发的版本标上Snapshot,这样的话在IDEA里新增一个RUN/DEBUG configuration,里面带上-u参数,每次Artifact更新的时候先执行一下这个就好。我试试

1 个赞

还有这种操作,你研究出来了说一下,学习学习。

基本搞明白了,这个方式是可行的,而且不用加 -U ,配置好仓库信息就可以。(但公司的仓库不是我配的,我不知道这个仓库具体设置)
总结的说SNAPSHOT就是Maven为这种小版本更新考虑的一个特性,开发Artifact的一方向远程仓库部署版本后缀为 -SNAPSHOT的Artifact,远程仓库在目录结构上就跟其他的版本不一样:
image
如图,SNAPSHOT在执行Deploy后是一个文件夹,里面的小版本是有时间标记的。
在使用该快照版本时,需要在Maven setting里或者项目POM里对SnapshotRepository进行配置才能够正确访问,如:

<repositories>
        <repository>
            <id>deployment-repo</id>
            <name>internal-repo</name>
<!-- 这里的maven-public,不是一个具体的文件夹,而是代理了maven-release、maven-snapshots的一个虚拟目录(大概这意思)-->
            <url>http://nexus.******.com/repository/maven-public/</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </snapshots>
        </repository>
    </repositories>

如此,在该快照版本有更新时,只要Maven执行一下刷新,就能获得最新的依赖。

开发Artifact时也需要对snapshot做相关配置,才能正确上传到远程仓库的snapshot库中,不然默认的release库会拒绝snapshot版本的部署。

  <distributionManagement>
    <repository>
      <id>deployment-repo</id>
      <name>release-repository</name>
      <url>http://nexus.******.com/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
      <id>deployment-repo</id>
      <name>dev-repository</name>
      <url>http://nexus.*******.com/repository/maven-snapshots/</url>
    </snapshotRepository>
  </distributionManagement>

1 个赞

自动更新的关键是这句,如果没做这句的话还是不会自动更新