Elffuss BraidHub

kikocis/java-driver public

1cbac626deaf · 2.3 KiBRawPermalink
1/*
2 * Copyright (C) 2012-2015 DataStax Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.datastax.driver.osgi;
17
18import java.util.regex.Matcher;
19import java.util.regex.Pattern;
20
21import com.jcabi.manifests.Manifests;
22
23/**
24 * Attempts to resolve the project version from the Bundle manifest. If not present, will throw RuntimeException
25 * on initialization. If this happens, try building with 'mvn compile' to generate the Bundle manifest.
26 *
27 * In IntelliJ you can have compile run after make by right clicking on 'compile' in the 'Maven Projects' tool window.
28 */
29public class VersionProvider {
30
31 private static final Pattern versionPattern = Pattern.compile(("(\\d+.\\d+\\.\\d+)(.*)"));
32
33 private static final String PROJECT_VERSION;
34 static {
35 String bundleName = Manifests.read("Bundle-SymbolicName");
36 if (bundleName.equals("com.datastax.driver.osgi")) {
37 String bundleVersion = Manifests.read("Bundle-Version");
38 Matcher matcher = versionPattern.matcher(bundleVersion);
39 if(matcher.matches()) {
40 String majorVersion = matcher.group(1);
41 // Replace all instances of '.' after the main version with '-' to properly
42 // resolve the correct version.
43 String rest = matcher.group(2).replaceAll("\\.", "-");
44 PROJECT_VERSION = majorVersion + rest;
45 } else {
46 // This should never happen, but if we are using a non X.Y.Z version number
47 // we'll just back off to the bundle version.
48 PROJECT_VERSION = bundleVersion;
49 }
50 } else {
51 throw new RuntimeException("Couldn't resolve bundle manifest (try building with mvn compile)");
52 }
53 }
54
55 public static String projectVersion() {
56 return PROJECT_VERSION;
57 }
58}