Search in sources :

Example 21 with Version

use of aQute.bnd.version.Version in project bnd by bndtools.

the class ProjectBuilder method getBaselineJar.

/**
	 * This method attempts to find the baseline jar for the current project. It
	 * reads the -baseline property and treats it as instructions. These
	 * instructions are matched against the bsns of the jars (think sub
	 * builders!). If they match, the sub builder is selected.
	 * <p>
	 * The instruction can then specify the following options:
	 * 
	 * <pre>
	 *  version :
	 * baseline version from repository file : a file path
	 * </pre>
	 * 
	 * If neither is specified, the current version is used to find the highest
	 * version (without qualifier) that is below the current version. If a
	 * version is specified, we take the highest version with the same base
	 * version.
	 * <p>
	 * Since baselining is expensive and easily generates errors you must enable
	 * it. The easiest solution is to {@code -baseline: *}. This will match all
	 * sub builders and will calculate the version.
	 * 
	 * @return a Jar or null
	 */
public Jar getBaselineJar() throws Exception {
    String bl = getProperty(Constants.BASELINE);
    if (bl == null || Constants.NONE.equals(bl))
        return null;
    Instructions baselines = new Instructions(getProperty(Constants.BASELINE));
    if (baselines.isEmpty())
        // no baselining
        return null;
    RepositoryPlugin repo = getBaselineRepo();
    if (repo == null)
        // errors reported already
        return null;
    String bsn = getBsn();
    Version version = new Version(getVersion());
    SortedSet<Version> versions = removeStagedAndFilter(repo.versions(bsn), repo, bsn);
    if (versions.isEmpty()) {
        // We have a repo
        Version v = Version.parseVersion(getVersion()).getWithoutQualifier();
        if (v.compareTo(Version.ONE) > 0) {
            warning("There is no baseline for %s in the baseline repo %s. The build is for version %s, which is higher than 1.0.0 which suggests that there should be a prior version.", getBsn(), repo, v);
        }
        return null;
    }
    for (Entry<Instruction, Attrs> e : baselines.entrySet()) {
        if (e.getKey().matches(bsn)) {
            Attrs attrs = e.getValue();
            Version target;
            if (attrs.containsKey("version")) {
                // Specified version!
                String v = attrs.get("version");
                if (!Verifier.isVersion(v)) {
                    error("Not a valid version in %s %s", Constants.BASELINE, v);
                    return null;
                }
                Version base = new Version(v);
                SortedSet<Version> later = versions.tailSet(base);
                if (later.isEmpty()) {
                    error("For baselineing %s-%s, specified version %s not found", bsn, version, base);
                    return null;
                }
                // First element is equal or next to the base we desire
                target = later.first();
            // Now, we could end up with a higher version than our
            // current
            // project
            } else if (attrs.containsKey("file")) {
                // Can be useful to specify a file
                // for example when copying a bundle with a public api
                File f = getProject().getFile(attrs.get("file"));
                if (f != null && f.isFile()) {
                    Jar jar = new Jar(f);
                    addClose(jar);
                    return jar;
                }
                error("Specified file for baseline but could not find it %s", f);
                return null;
            } else {
                target = versions.last();
            }
            if (target.getWithoutQualifier().compareTo(version.getWithoutQualifier()) > 0) {
                error("The baseline version %s is higher than the current version %s for %s in %s", target, version, bsn, repo);
                return null;
            }
            if (target.getWithoutQualifier().compareTo(version.getWithoutQualifier()) == 0) {
                if (isPedantic()) {
                    warning("Baselining against jar");
                }
            }
            File file = repo.get(bsn, target, attrs);
            if (file == null || !file.isFile()) {
                error("Decided on version %s-%s but cannot get file from repo %s", bsn, version, repo);
                return null;
            }
            Jar jar = new Jar(file);
            addClose(jar);
            return jar;
        }
    }
    // Ignore, nothing matched
    return null;
}
Also used : Version(aQute.bnd.version.Version) Attrs(aQute.bnd.header.Attrs) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) Instructions(aQute.bnd.osgi.Instructions) Jar(aQute.bnd.osgi.Jar) Instruction(aQute.bnd.osgi.Instruction) File(java.io.File)

Example 22 with Version

use of aQute.bnd.version.Version in project bnd by bndtools.

the class PackageInfo method replace.

/*
	 * Replace a version in a file based on a pattern. We search the pattern and
	 * if found we replace group 1 with the new version. If the found version
	 * matches the new version we bail out early.
	 */
private boolean replace(File target, final Version newVersion, Pattern pattern) throws IOException {
    String content = IO.collect(target);
    Matcher m = pattern.matcher(content);
    if (!m.find()) {
        return false;
    }
    Version oldVersion = new Version(m.group(1));
    if (newVersion.compareTo(oldVersion) == 0) {
        return true;
    }
    return replace(newVersion, content, m, target);
}
Also used : Matcher(java.util.regex.Matcher) Version(aQute.bnd.version.Version)

Example 23 with Version

use of aQute.bnd.version.Version in project bnd by bndtools.

the class PackageInfo method getVersion.

/*
	 * Get the version from the file using the pattern
	 */
private Version getVersion(File source, Pattern pattern) throws IOException {
    if (!source.isFile())
        return null;
    String content = IO.collect(source);
    Matcher m = pattern.matcher(content);
    if (!m.find())
        return null;
    return new Version(m.group(1));
}
Also used : Matcher(java.util.regex.Matcher) Version(aQute.bnd.version.Version)

Example 24 with Version

use of aQute.bnd.version.Version in project bnd by bndtools.

the class PackageInfo method getPackageInfo.

/**
	 * Get the version for a package name. This traverse the source paths and
	 * will stop at the first source directory that has a packageinfo or
	 * 
	 * @param packageName
	 * @throws Exception
	 */
public Version getPackageInfo(String packageName) throws Exception {
    File target = getFile(packageName);
    if (target != null && target.isFile()) {
        Version v = getVersion(target, getPattern(target));
        if (v == null && isModern(target)) {
            target = new File(target.getParentFile(), PACKAGEINFO);
            v = getVersion(target, getPattern(target));
        }
        if (v != null)
            return v;
    }
    return Version.emptyVersion;
}
Also used : Version(aQute.bnd.version.Version) File(java.io.File)

Example 25 with Version

use of aQute.bnd.version.Version in project bnd by bndtools.

the class ResourceImpl method compareTo.

@Override
public int compareTo(Resource o) {
    IdentityCapability me = ResourceUtils.getIdentityCapability(this);
    IdentityCapability them = ResourceUtils.getIdentityCapability(o);
    String myName = me.osgi_identity();
    String theirName = them.osgi_identity();
    if (myName == theirName)
        return 0;
    if (myName == null)
        return -1;
    if (theirName == null)
        return 1;
    int n = myName.compareTo(theirName);
    if (n != 0)
        return n;
    Version myVersion = me.version();
    Version theirVersion = them.version();
    if (myVersion == theirVersion)
        return 0;
    if (myVersion == null)
        return -1;
    if (theirVersion == null)
        return 1;
    return myVersion.compareTo(theirVersion);
}
Also used : Version(aQute.bnd.version.Version) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)

Aggregations

Version (aQute.bnd.version.Version)168 File (java.io.File)64 RepositoryPlugin (aQute.bnd.service.RepositoryPlugin)27 Attrs (aQute.bnd.header.Attrs)19 MavenVersion (aQute.bnd.version.MavenVersion)19 ArrayList (java.util.ArrayList)19 HashMap (java.util.HashMap)18 IOException (java.io.IOException)17 Jar (aQute.bnd.osgi.Jar)16 Workspace (aQute.bnd.build.Workspace)13 Project (aQute.bnd.build.Project)12 RevisionRef (aQute.service.library.Library.RevisionRef)12 Matcher (java.util.regex.Matcher)12 Parameters (aQute.bnd.header.Parameters)11 VersionRange (aQute.bnd.version.VersionRange)11 SortedList (aQute.lib.collections.SortedList)9 Processor (aQute.bnd.osgi.Processor)8 ResourceDescriptor (aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor)8 FileNotFoundException (java.io.FileNotFoundException)8 LinkedHashMap (java.util.LinkedHashMap)8