Is there a way to make Maven download snapshot versions automatically?

我们两清 提交于 2019-12-09 04:41:15

问题


So I have a project that depends on a snapshot version of another project. The dependency is:

<dependency>
  <groupId>org.oop</groupId>
  <artifactId>oop</artifactId>
  <version>0.9.9-SNAPSHOT</version>
</dependency>

For the oop project, I did do a 'mvn clean deploy', so the snapshot version should be somewhere in the maven central repository. But when I do a mvn clean install, the snapshot dependency above cannot be resolved and I get this:

Missing:

1) org.oop:oop:jar:0.9.9-SNAPSHOT

Try downloading the file manually from the project website.

Then, install it using the command: mvn install:install-file -DgroupId=org.oop -DartifactId=oop -Dversion=0.9.9-SNAPSHOT -Dpackaging=jar -Dfile=/path/to/file

Alternatively, if you host your own repository you can deploy the file there: mvn deploy:deploy-file -DgroupId=org.oop -DartifactId=oop -Dversion=0.9.9-SNAPSHOT -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]

Is there a way to make maven download the snapshot automatically? I must be missing something here.

EDIT1: On my settings.xml I have:

   <server>
      <id>sonatype-nexus-snapshots</id>
      <username>XXXXXX</username>
      <password>XXXXXX</password>
    </server>

    <server>
      <id>sonatype-nexus-staging</id>
      <username>XXXXXX</username>
      <password>XXXXXX</password>
    </server>

EDIT2:


回答1:


Just add this to your ~/.m2/settings.xml:

<profiles>
  <profile>
     <id>allow-snapshots</id>
        <activation><activeByDefault>true</activeByDefault></activation>
     <repositories>
       <repository>
         <id>snapshots-repo</id>
         <url>https://oss.sonatype.org/content/repositories/snapshots</url>
         <releases><enabled>false</enabled></releases>
         <snapshots><enabled>true</enabled></snapshots>
       </repository>
     </repositories>
   </profile>
</profiles>



回答2:


Maven would try to download the snapshot automatically and indeed it does (as your error indicates). By default, Maven will look for newer snapshot versions once a day, but you can change that interval in your snapshot repository config (e.g. in settings.xml):

<updatePolicy>interval:5</updatePolicy>

This will make maven check every 5 minutes (if you build that often). Alternatively, you could use the -U or --update-snapshots option, to force the check manually.

However, it can't find the dependency. Could you post your repo settings and artifact config for the snapshot dependency?

Is the org.oop:oop:jar:0.9.9-SNAPSHOT artifact in your repository?

... so the snapshot version should be somewhere in the maven central repository.

No it isn't. I tried to look it up, but couldn't find it. Afaik, there's some staging mechanism, so maybe your settings are just wrong. But normally, as the others already said, you'd go and use your own repository manager like Artifactory or Nexus.




回答3:


To update snapshots, try with the -U option

-U,--update-snapshots                  Forces a check for updated
                                       releases and snapshots on remote
                                       repositories

However, you said:

I did do a 'mvn clean deploy', so the snapshot version should be somewhere in the maven central repository.

This is just not possible, your snapshot is going somewhere else. If I do a mvn clean deploy without configuring my personal repository I get:

Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter

To enable deployment, there is some configuration to be added to pom.xml, like for instance:

<distributionManagement>

    <!-- Publish versioned releases here -->
    <repository>
        <id>myrepo</id>
        <name>My releases</name>
        <url>http://nexus.mycompany.com/nexus/content/repositories/releases</url>
    </repository>

    <!-- Publish snapshots here -->
    <snapshotRepository>
        <id>myrepo</id>
        <name>my snapshots</name>
        <url>http://nexus.mycompany.com/nexus/content/repositories/snapshots</url>
    </snapshotRepository>

</distributionManagement>

<repositories>
    <repository>
        <id>myrepo</id>
        <name>My Public Repository</name>
        <url>http://nexus.mycompany.com/nexus/content/groups/public</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>



回答4:


Does that dependency exists in your repository? (in pom.xml or settings.xml)?

Looks like not. By the way, take a look at your config, just you are not using -o (offline). Also you can use -U to refresh snapshots.




回答5:


You can either

  • use a parent project which builds all your snapshots, or
  • deploy your snapshots to your maven build server (nexus/archiva/..) using e.g., mvn:deploy



回答6:


Let's clear up terminology a bit to make sure there is no misunderstanding.

"Maven Central" (http://search.maven.org/) is a global site where you only find releases. Central doesn't accept snapshots so deploying there should give you an error.

You probably mean your local/company wide maven proxy/cache. These can also be configured to reject snapshot versions. In case of Nexus, you can also define more complex rules. In my case, I had an issue there which gave no error during mvn deploy but I could see an error in the server's logs.

Try to follow the data: Enable debug (mvn -X) to see where Maven uploads the data. Then check the server to see whether the artifacts were really uploaded. Check the server's logs for errors.

Also note that snapshot dependencies are only refreshed once a day; so this won't work:

PC #1: mvn install -> Error missing dependency PC #2: mvn deploy PC #1: mvn install -> Dependency is still missing because of "update once per day" policy

Try mvn install -U to force Maven to refresh its cached metadata.




回答7:


I hit the issue of snapshots not updating even when setting -U on the command line. For me the issue was my client was Maven 3 and the server was Maven 2, and in Maven 3 unique snapshots are no longer supported. We had to create a new repository with timestamped snapshots to support the 3.xx clients.



来源:https://stackoverflow.com/questions/7713996/is-there-a-way-to-make-maven-download-snapshot-versions-automatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!