Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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: enter image description here

like image 682
chrisapotek Avatar asked Oct 10 '11 14:10

chrisapotek


People also ask

How often does Maven check for snapshot updates?

Snapshot versions are not enabled by default. daily (default value) — check for a newer version once a day. interval:mm — check for a newer version based on an interval set in minutes. never — never try to get a newer version (compared to the one we already have locally)

Why is it best practice not to release snapshot?

Rule #3 Never release using the Time-Stamped snapshot The release plugin will still fail if you do this because it correctly understands you have SNAPSHOT dependencies. The plugin has a flag to allow bypass this check and people unfortunately use it far too often.

What is the difference between snapshot and version in Maven?

A Maven snapshot is a special version of a Maven package that refers to the latest production branch code. It is a development version that precedes the final release version. You can identify a snapshot version of a Maven package by the suffix SNAPSHOT that is appended to the package version.

What is snapshot dependency?

In Maven, a SNAPSHOT version is a version of the project/dependency that has not been released. This is indicated by suffixing SNAPSHOT to the version number. Here's an example: <version>1.0-SNAPSHOT</version> Copy.

What is the difference between release and snapshot?

By definition, snapshots are mutable, releases are immutable. This is why Nexus makes you store them separately because usually you don't care if you lose snapshots, but you will care if you lose releases. It makes snapshot cleanup much easier to deal with that way.


2 Answers

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>
like image 67
JohnPristine Avatar answered Oct 30 '22 09:10

JohnPristine


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>
like image 44
stivlo Avatar answered Oct 30 '22 09:10

stivlo