What exactly is a Maven Snapshot and why do we need it?

前端 未结 13 2308
名媛妹妹
名媛妹妹 2020-11-22 07:07

I am a bit confused about the meaning of a Maven Snapshot and why we build one?

相关标签:
13条回答
  • 2020-11-22 07:17

    A "release" is the final build for a version which does not change.

    A "snapshot" is a build which can be replaced by another build which has the same name. It implies that the build could change at any time and is still under active development.

    You have different artifacts for different builds based on the same code. E.g. you might have one with debugging and one without. One for Java 5.0 and one for Java 6. Generally its simpler to have one build which does everything you need. ;)

    0 讨论(0)
  • 2020-11-22 07:20

    A snapshot version in Maven is one that has not been released.

    The idea is that before a 1.0 release (or any other release) is done, there exists a 1.0-SNAPSHOT. That version is what might become 1.0. It's basically "1.0 under development". This might be close to a real 1.0 release, or pretty far (right after the 0.9 release, for example).

    The difference between a "real" version and a snapshot version is that snapshots might get updates. That means that downloading 1.0-SNAPSHOT today might give a different file than downloading it yesterday or tomorrow.

    Usually, snapshot dependencies should only exist during development and no released version (i.e. no non-snapshot) should have a dependency on a snapshot version.

    0 讨论(0)
  • 2020-11-22 07:21

    Maven versions can contain a string literal "SNAPSHOT" to signify that a project is currently under active development.

    For example, if your project has a version of “1.0-SNAPSHOT” and you deploy this project’s artifacts to a Maven repository, Maven would expand this version to “1.0-20080207-230803-1” if you were to deploy a release at 11:08 PM on February 7th, 2008 UTC. In other words, when you deploy a snapshot, you are not making a release of a software component; you are releasing a snapshot of a component at a specific time.

    So mainly snapshot versions are used for projects under active development. If your project depends on a software component that is under active development, you can depend on a snapshot release, and Maven will periodically attempt to download the latest snapshot from a repository when you run a build. Similarly, if the next release of your system is going to have a version “1.8,” your project would have a “1.8-SNAPSHOT” version until it was formally released.

    For example , the following dependency would always download the latest 1.8 development JAR of spring:

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>1.8-SNAPSHOT”</version>
        </dependency>
    

    Maven

    An example of maven release process

    0 讨论(0)
  • 2020-11-22 07:22

    simply snapshot means it is the version which is not stable one.

    when version includes snapshot like 1.0.0 -SNAPSHOT means it is not stable version and look for remote repository to resolve dependencies

    0 讨论(0)
  • 2020-11-22 07:23

    This is how a snapshot looks like for a repository and in this case is not enabled, which means that the repository referred in here is stable and there's no need for updates.

    <project>
        ...
        <repositories>
            <repository>
                <id>lds-main</id>
                <name>LDS Main Repo</name>
                <url>http://code.lds.org/nexus/content/groups/main-repo</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>
    </project>
    

    Another case would be for:

    <snapshots>
            <enabled>true</enabled>
    </snapshots>
    

    which means that Maven will look for updates for this repository. You can also specify an interval for the updates with tag.

    0 讨论(0)
  • 2020-11-22 07:25

    The three others answers provide you a good vision of what a -SNAPSHOT version is. I just wanted to add some information regarding the behavior of Maven when it finds a SNAPSHOT dependency.

    When you build an application, Maven will search for dependencies in the local repository. If a stable version is not found there, it will search the remote repositories (defined in settings.xml or pom.xml) to retrieve this dependency. Then, it will copy it into the local repository, to make it available for the next builds.

    For example, a foo-1.0.jar library is considered as a stable version, and if Maven finds it in the local repository, it will use this one for the current build.

    Now, if you need a foo-1.0-SNAPSHOT.jar library, Maven will know that this version is not stable and is subject to changes. That's why Maven will try to find a newer version in the remote repositories, even if a version of this library is found on the local repository. However, this check is made only once per day. That means that if you have a foo-1.0-20110506.110000-1.jar (i.e. this library has been generated on 2011/05/06 at 11:00:00) in your local repository, and if you run the Maven build again the same day, Maven will not check the repositories for a newer version.

    Maven provides you a way to change this update policy in your repository definition:

    <repository>
        <id>foo-repository</id>
        <url>...</url>
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>XXX</updatePolicy>
        </snapshots>
    </repository>
    

    where XXX can be:

    • always: Maven will check for a newer version on every build;
    • daily, the default value;
    • interval:XXX: an interval in minutes (XXX)
    • never: Maven will never try to retrieve another version. It will do that only if it doesn't exist locally. With the configuration, SNAPSHOT version will be handled as the stable libraries.

    (model of the settings.xml can be found here)

    0 讨论(0)
提交回复
热议问题