Search in sources :

Example 41 with Version

use of org.eclipse.equinox.p2.metadata.Version in project tycho by eclipse.

the class BaselineServiceImpl method getProjectBaseline.

@Override
public Map<String, IP2Artifact> getProjectBaseline(Collection<MavenRepositoryLocation> baselineLocations, Map<String, IP2Artifact> reactor, File target) {
    // baseline repository may contain artifacts with the same id/version but different contents
    // compared to what is installed (or cached) locally.
    // current local repository layout does not track per-repository artifacts and does not allow
    // multiple different artifacts with same id/version.
    CompositeMetadataRepository baselineUnits;
    CompositeArtifactRepository baselineArtifacts;
    try {
        IProvisioningAgent remoteAgent = remoteAgentManager.getProvisioningAgent();
        IRepositoryIdManager remoteRepositoryIdManager = (IRepositoryIdManager) remoteAgent.getService(IRepositoryIdManager.SERVICE_NAME);
        IMetadataRepositoryManager remoteMetadataRepositoryManager = (IMetadataRepositoryManager) remoteAgent.getService(IMetadataRepositoryManager.SERVICE_NAME);
        IArtifactRepositoryManager remoteArtifactRepositoryManager = (IArtifactRepositoryManager) remoteAgent.getService(IArtifactRepositoryManager.SERVICE_NAME);
        baselineUnits = CompositeMetadataRepository.createMemoryComposite(remoteAgent);
        baselineArtifacts = CompositeArtifactRepository.createMemoryComposite(remoteAgent);
        for (MavenRepositoryLocation location : baselineLocations) {
            URI url = location.getURL();
            try {
                remoteRepositoryIdManager.addMapping(location.getId(), url);
                // TODO offline mode https://bugs.eclipse.org/bugs/show_bug.cgi?id=337022
                // not strictly necessary, but makes sure metadata download is visible in the console/log
                remoteMetadataRepositoryManager.loadRepository(url, monitor);
                remoteArtifactRepositoryManager.loadRepository(url, monitor);
                baselineUnits.addChild(url);
                baselineArtifacts.addChild(url);
            } catch (ProvisionException e) {
                // baseline repository may not exist yet
                mavenContext.getLogger().warn(e.getMessage(), e);
            }
        }
    } catch (ProvisionException e) {
        throw new RuntimeException(e);
    }
    Map<String, IP2Artifact> result = new LinkedHashMap<>();
    for (Map.Entry<String, IP2Artifact> reactorArtifact : reactor.entrySet()) {
        IArtifactDescriptor descriptor = (IArtifactDescriptor) reactorArtifact.getValue().getArtifactDescriptor();
        IArtifactDescriptor baselineDescriptor = getBaselineDescriptor(baselineArtifacts, descriptor);
        if (baselineDescriptor == null) {
            continue;
        }
        IArtifactKey baslineKey = baselineDescriptor.getArtifactKey();
        String format = baselineDescriptor.getProperty(IArtifactDescriptor.FORMAT);
        File baselineArtifact = new File(target, baslineKey.getClassifier() + "/" + baslineKey.getId() + "/" + baslineKey.getVersion() + (format != null ? "." + format : ""));
        try {
            baselineArtifact.getParentFile().mkdirs();
            OutputStream os = new BufferedOutputStream(new FileOutputStream(baselineArtifact));
            try {
                IStatus status = baselineArtifacts.getRawArtifact(baselineDescriptor, os, monitor);
                if (status.matches(IStatus.ERROR | IStatus.CANCEL)) {
                    String repository = baselineDescriptor.getRepository().getLocation().toString();
                    String artifactId = baselineDescriptor.getArtifactKey().getId();
                    String artifactVersion = baselineDescriptor.getArtifactKey().getVersion().toString();
                    String statusMessage = StatusTool.toLogMessage(status);
                    throw new RuntimeException(String.format("Error trying to download %s version %s from %s:\n%s", artifactId, artifactVersion, repository, statusMessage), StatusTool.findException(status));
                }
            } finally {
                try {
                    os.close();
                } catch (IOException e) {
                // ignored
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        List<IInstallableUnit> units = new ArrayList<>();
        for (Object _unit : reactorArtifact.getValue().getInstallableUnits()) {
            IInstallableUnit unit = (IInstallableUnit) _unit;
            IInstallableUnit baselineUnit = getBaselineUnit(baselineUnits, unit.getId(), unit.getVersion());
            if (baselineUnit != null) {
                units.add(baselineUnit);
            }
        }
        result.put(reactorArtifact.getKey(), new P2Artifact(baselineArtifact, units, descriptor));
    }
    return !result.isEmpty() ? result : null;
}
Also used : IMetadataRepositoryManager(org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager) IStatus(org.eclipse.core.runtime.IStatus) CompositeArtifactRepository(org.eclipse.equinox.internal.p2.artifact.repository.CompositeArtifactRepository) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap) IRepositoryIdManager(org.eclipse.tycho.p2.remote.IRepositoryIdManager) ProvisionException(org.eclipse.equinox.p2.core.ProvisionException) IArtifactKey(org.eclipse.equinox.p2.metadata.IArtifactKey) CompositeMetadataRepository(org.eclipse.equinox.internal.p2.metadata.repository.CompositeMetadataRepository) IInstallableUnit(org.eclipse.equinox.p2.metadata.IInstallableUnit) BufferedOutputStream(java.io.BufferedOutputStream) IArtifactDescriptor(org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor) IProvisioningAgent(org.eclipse.equinox.p2.core.IProvisioningAgent) IOException(java.io.IOException) MavenRepositoryLocation(org.eclipse.tycho.core.resolver.shared.MavenRepositoryLocation) IArtifactRepositoryManager(org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager) FileOutputStream(java.io.FileOutputStream) IP2Artifact(org.eclipse.tycho.p2.metadata.IP2Artifact) IP2Artifact(org.eclipse.tycho.p2.metadata.IP2Artifact) P2Artifact(org.eclipse.tycho.p2.impl.publisher.P2Artifact) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) File(java.io.File)

Example 42 with Version

use of org.eclipse.equinox.p2.metadata.Version in project tycho by eclipse.

the class TargetPlatformBundlePublisher method attemptToPublishBundle.

/**
 * Generate p2 data for an artifact, if the artifact is an OSGI bundle.
 * <p>
 * The p2 metadata produced by this method is only determined by the artifact, and the function
 * used for this conversion must not change (significantly) even in future versions. This is
 * required because the resulting metadata can be included in p2 repositories built by Tycho,
 * and hence may be propagated into the p2 universe. Therefore the metadata generated by this
 * method shall fulfill the basic assumption of p2 that ID+version uniquely identifies a
 * unit/artifact. Assuming that distinct bundle artifacts specify unique ID+versions in their
 * manifest (which should be mostly true), and the p2 BundlesAction used in the implementation
 * doesn't change significantly (which can also be assumed), these conditions specified above a
 * met.
 * </p>
 * <p>
 * In slight deviation on the principles described in the previous paragraph, the implementation
 * adds GAV properties to the generated IU. This is justified by the potential benefits of
 * tracing the origin of artifact.
 * </p>
 *
 * @param mavenArtifact
 *            An artifact in local file system.
 * @return the p2 metadata of the artifact, or <code>null</code> if the artifact isn't a valid
 *         OSGi bundle.
 */
IInstallableUnit attemptToPublishBundle(IArtifactFacade mavenArtifact) {
    if (!isAvailableAsLocalFile(mavenArtifact)) {
        // this should have been ensured by the caller
        throw new IllegalArgumentException("Not an artifact file: " + mavenArtifact.getLocation());
    }
    if (isCertainlyNoBundle(mavenArtifact)) {
        return null;
    }
    PublisherRun publisherRun = new PublisherRun(mavenArtifact);
    IStatus status = publisherRun.execute();
    if (!status.isOK()) {
        /**
         * If publishing of a jar fails, it is simply not added to the resolution context. The
         * BundlesAction already ignores non-bundle JARs silently, so an error status here
         * indicates a caught exception that we at least want to see.
         */
        logger.warn(StatusTool.collectProblems(status), status.getException());
    }
    IInstallableUnit publishedIU = publisherRun.getPublishedUnitIfExists();
    if (publishedIU != null) {
        IArtifactDescriptor publishedDescriptor = publisherRun.getPublishedArtifactDescriptor();
        publishedArtifacts.addPublishedArtifact(publishedDescriptor, mavenArtifact);
    }
    return publishedIU;
}
Also used : IArtifactDescriptor(org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor) IStatus(org.eclipse.core.runtime.IStatus) IInstallableUnit(org.eclipse.equinox.p2.metadata.IInstallableUnit)

Example 43 with Version

use of org.eclipse.equinox.p2.metadata.Version in project tycho by eclipse.

the class StandardEEResolutionHints method newIU.

private static IInstallableUnit newIU(String id, Version version) {
    InstallableUnitDescription iud = new InstallableUnitDescription();
    iud.setId(id);
    iud.setVersion(version);
    iud.addProvidedCapabilities(Collections.singleton(MetadataFactory.createProvidedCapability(IInstallableUnit.NAMESPACE_IU_ID, id, version)));
    return MetadataFactory.createInstallableUnit(iud);
}
Also used : InstallableUnitDescription(org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitDescription)

Example 44 with Version

use of org.eclipse.equinox.p2.metadata.Version in project tycho by eclipse.

the class P2ResolverImpl method addDependency.

@Override
public void addDependency(String type, String id, String versionRange) throws IllegalArtifactReferenceException {
    final VersionRange parsedVersionRange;
    try {
        parsedVersionRange = new VersionRange(versionRange);
    } catch (IllegalArgumentException e) {
        throw new IllegalArtifactReferenceException("The string \"" + versionRange + "\" is not a valid OSGi version range");
    }
    additionalRequirements.add(ArtifactTypeHelper.createRequirementFor(type, id, parsedVersionRange));
}
Also used : IllegalArtifactReferenceException(org.eclipse.tycho.artifacts.IllegalArtifactReferenceException) VersionRange(org.eclipse.equinox.p2.metadata.VersionRange)

Example 45 with Version

use of org.eclipse.equinox.p2.metadata.Version in project tycho by eclipse.

the class ArtifactMatcher method resolveReference.

public static IInstallableUnit resolveReference(String type, String id, Version version, LinkedHashSet<IInstallableUnit> candidateUnits) throws IllegalArtifactReferenceException {
    if (id == null) {
        throw new IllegalArtifactReferenceException("ID is required");
    }
    VersionRange versionRange = getVersionRangeFromReference(version);
    IQuery<IInstallableUnit> query = QueryUtil.createLatestQuery(ArtifactTypeHelper.createQueryFor(type, id, versionRange));
    IQueryResult<IInstallableUnit> matchingIUs = query.perform(candidateUnits.iterator());
    if (matchingIUs.isEmpty()) {
        return null;
    } else {
        return matchingIUs.iterator().next();
    }
}
Also used : IllegalArtifactReferenceException(org.eclipse.tycho.artifacts.IllegalArtifactReferenceException) VersionRange(org.eclipse.equinox.p2.metadata.VersionRange) IInstallableUnit(org.eclipse.equinox.p2.metadata.IInstallableUnit)

Aggregations

IInstallableUnit (org.eclipse.equinox.p2.metadata.IInstallableUnit)25 Version (org.eclipse.equinox.p2.metadata.Version)15 Test (org.junit.Test)15 File (java.io.File)12 InstallableUnitDescription (org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitDescription)12 ArrayList (java.util.ArrayList)8 IRequirement (org.eclipse.equinox.p2.metadata.IRequirement)7 VersionRange (org.eclipse.equinox.p2.metadata.VersionRange)7 IStatus (org.eclipse.core.runtime.IStatus)5 IProvidedCapability (org.eclipse.equinox.p2.metadata.IProvidedCapability)5 IArtifactDescriptor (org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor)5 IArtifactKey (org.eclipse.equinox.p2.metadata.IArtifactKey)4 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 CoreException (org.eclipse.core.runtime.CoreException)3 ArtifactMock (org.eclipse.tycho.p2.impl.test.ArtifactMock)3 IOException (java.io.IOException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 HashMap (java.util.HashMap)2