0

Because my incremental build number = the CI build number, I don't want to put the incremental build number into my development POMs. It should only have the major and minor version components.

  • Version in POM = e.g. 1.1-SNAPSHOT
  • Version of release = e.g. 1.1.23

However, another component within the same framework will want to say "if the SNAPSHOT version exists locally, use that, otherwise use the latest incremental version".

How can I specify this in my POM?

I tried this:

<dependencies>
    <dependency>
        <groupId>com.example.ajb.versionpoc</groupId>
        <artifactId>downstream</artifactId>
        <version>[1.1-SNAPSHOT],[1.1,2.0)</version>
    </dependency>
</dependencies>

But it doesn't work. It still resolves the version as e.g. 1.1.23. How can I tell Maven to prefer the SNAPSHOT version?

Adam Burley
  • 4,231
  • 2
  • 40
  • 62
  • I think the best way to achieve this would be via [Maven Profiles](http://maven.apache.org/guides/introduction/introduction-to-profiles.html) – DB5 Sep 30 '15 at 11:17
  • @DB5 I am familiar with Maven Profiles, but I don't think there's a way to trigger a profile based on the existence or not of a particular dependency? – Adam Burley Sep 30 '15 at 11:25

2 Answers2

0

I do not fuly understand what you are trying to do but here is the general deal:

  1. Use snapshots in development only. Never put them in ranges.
  2. Your range expression has to contain two arguments. You have two ranges with three.
  3. Provide ranges for stable releases only.
Michael-O
  • 17,130
  • 6
  • 51
  • 108
  • 1
    4. [Don't use ranges to begin with!](http://stackoverflow.com/q/30571#comment1287159_30571) – Tunaki Sep 30 '15 at 11:20
  • Hi Michael, (1) Yes I am using snapshots in development only. But if I don't put them in a range, how can I solve my issue? (2) A range expression can contain two arguments or one, and can also contain multiple ranges, see https://maven.apache.org/enforcer/enforcer-rules/versionRanges.html (3) Not sure what this means. I'm not trying to put a "range" as such for snapshot releases, I'm just trying to use the range syntax to achieve my goal. – Adam Burley Sep 30 '15 at 11:22
  • Hi @Tunaki, this depends on the frequency of updating the version. In my case, the version would have to be updated manually in the POM multiple times per day, as we are in a continuous delivery model. – Adam Burley Sep 30 '15 at 11:24
0

Based on @DB5's suggestion about profiles, I think you could declare one specific profile which includes the snapshot dependencies. The activation of such a profile can be done depending on the existence of that library in the local reposiory.

Add in the profiles section of your pom this fragment:

    <profile>
        <!--This profile will prefer the SNAPSHOT version if it exists locally-->
        <id>local</id>
        <activation>
            <file>
                <exists>${user.home}/.m2/repository/mygroup/myartifact/myversion-SNAPSHOT/myartifact-myversion-SNAPSHOT.jar</exists>
            </file>
        </activation>
        <dependencies>
            <dependency>
                <groupId>mygroup</groupId>
                <artifactId>myartifact</artifactId>
                <version>myversion-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </profile>

    <profile>
        <!--This profile will prefer the released version if the SNAPSHOT does not exist locally-->
        <id>released</id>
        <activation>
            <file>
                <missing>${user.home}/.m2/repository/mygroup/myartifact/myversion-SNAPSHOT/myartifact-myversion-SNAPSHOT.jar</missing>
            </file>
        </activation>
        <dependencies>
            <dependency>
                <groupId>mygroup</groupId>
                <artifactId>myartifact</artifactId>
                <version>myversion</version>
            </dependency>
        </dependencies>
    </profile>
Little Santi
  • 7,940
  • 2
  • 14
  • 40
  • I understand what you are trying to do, but this solution is a bit flaky...(1) what if the user has specified a different repository location (I have done this in the past myself due to downstream teams releasing multiple variants of the same version), (2) what if the code is being built from source in the IDE and doesn't actually exist in the repository (this is quite a normal use-case actually), (3) not sure if this works in a platform-independent way (Windows/UNIX path conventions) – Adam Burley Sep 30 '15 at 14:34
  • My answers: 1) You can specify repositories in a profile. 2) In that case, you can specify a scope=system path in the proper dependency. 3) Why not? The `exists` and `missing` paths are specified in Unix notation but I tested them myself in Windows, and it worked OK. Go and give it a try. – Little Santi Sep 30 '15 at 15:03
  • (Maybe I should have replied that your problem is a bit flaky itself, but, er... :-) – Little Santi Sep 30 '15 at 15:05