Search in sources :

Example 1 with P2Artifact

use of org.eclipse.tycho.p2.impl.publisher.P2Artifact in project tycho by eclipse.

the class P2GeneratorImpl method generateMetadata.

@Override
public Map<String, IP2Artifact> generateMetadata(List<IArtifactFacade> artifacts, final File targetDir) throws IOException {
    Map<String, IP2Artifact> result = new LinkedHashMap<>();
    for (IArtifactFacade artifact : artifacts) {
        PublisherInfo publisherInfo = new PublisherInfo();
        DependencyMetadata metadata;
        // meta data handling for root files
        if (PackagingType.TYPE_ECLIPSE_FEATURE.equals(artifact.getPackagingType())) {
            publisherInfo.setArtifactOptions(IPublisherInfo.A_INDEX | IPublisherInfo.A_PUBLISH | IPublisherInfo.A_NO_MD5);
            FeatureRootfileArtifactRepository artifactsRepository = new FeatureRootfileArtifactRepository(publisherInfo, targetDir);
            publisherInfo.setArtifactRepository(artifactsRepository);
            metadata = super.generateMetadata(artifact, null, publisherInfo, null);
            result.putAll(artifactsRepository.getPublishedArtifacts());
        } else if (PackagingType.TYPE_P2_IU.equals(artifact.getPackagingType())) {
            TransientArtifactRepository artifactsRepository = new TransientArtifactRepository();
            publisherInfo.setArtifactRepository(artifactsRepository);
            final IArtifactFacade currentArtifact = artifact;
            IArtifactFacade targetDirAsArtifact = new IArtifactFacade() {

                @Override
                public String getVersion() {
                    return currentArtifact.getVersion();
                }

                @Override
                public String getPackagingType() {
                    return currentArtifact.getPackagingType();
                }

                @Override
                public File getLocation() {
                    return targetDir;
                }

                @Override
                public String getGroupId() {
                    return currentArtifact.getGroupId();
                }

                @Override
                public String getClassifier() {
                    return currentArtifact.getClassifier();
                }

                @Override
                public String getArtifactId() {
                    return currentArtifact.getArtifactId();
                }
            };
            metadata = super.generateMetadata(targetDirAsArtifact, null, publisherInfo, null);
        } else {
            publisherInfo.setArtifactOptions(IPublisherInfo.A_PUBLISH | IPublisherInfo.A_NO_MD5);
            TransientArtifactRepository artifactsRepository = new TransientArtifactRepository();
            publisherInfo.setArtifactRepository(artifactsRepository);
            metadata = super.generateMetadata(artifact, null, publisherInfo, null);
        }
        // secondary metadata is meant to represent installable units that are provided by this project
        // but do not affect dependencies of the project itself. generateMetadata is called at the end
        // of project build lifecycle, and primary/secondary metadata separation is irrelevant at this point
        P2Artifact p2artifact = new P2Artifact(artifact.getLocation(), metadata.getInstallableUnits(), getCanonicalArtifact(artifact.getClassifier(), metadata.getArtifactDescriptors()));
        result.put(artifact.getClassifier(), p2artifact);
        IArtifactDescriptor packed = getPackedArtifactDescriptor(metadata.getArtifactDescriptors());
        if (packed != null) {
            File packedLocation = new File(artifact.getLocation().getAbsolutePath() + ".pack.gz");
            if (!packedLocation.canRead()) {
                throw new IllegalArgumentException("Could not find packed artifact " + packed + " at " + packedLocation);
            }
            if (result.containsKey(RepositoryLayoutHelper.PACK200_CLASSIFIER)) {
                throw new IllegalArgumentException();
            }
            // workaround for bug 412497
            Map<String, String> additionalProperties = new HashMap<>(5);
            additionalProperties.put(RepositoryLayoutHelper.PROP_GROUP_ID, artifact.getGroupId());
            additionalProperties.put(RepositoryLayoutHelper.PROP_ARTIFACT_ID, artifact.getArtifactId());
            additionalProperties.put(RepositoryLayoutHelper.PROP_VERSION, artifact.getVersion());
            additionalProperties.put(RepositoryLayoutHelper.PROP_CLASSIFIER, RepositoryLayoutHelper.PACK200_CLASSIFIER);
            additionalProperties.put(RepositoryLayoutHelper.PROP_EXTENSION, RepositoryLayoutHelper.PACK200_EXTENSION);
            ((ArtifactDescriptor) packed).addProperties(additionalProperties);
            result.put(RepositoryLayoutHelper.PACK200_CLASSIFIER, new P2Artifact(packedLocation, Collections.<IInstallableUnit>emptySet(), packed));
        }
    }
    return result;
}
Also used : IArtifactDescriptor(org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor) IArtifactFacade(org.eclipse.tycho.p2.metadata.IArtifactFacade) IPublisherInfo(org.eclipse.equinox.p2.publisher.IPublisherInfo) PublisherInfo(org.eclipse.equinox.p2.publisher.PublisherInfo) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FeatureRootfileArtifactRepository(org.eclipse.tycho.p2.impl.publisher.repo.FeatureRootfileArtifactRepository) TransientArtifactRepository(org.eclipse.tycho.p2.impl.publisher.repo.TransientArtifactRepository) LinkedHashMap(java.util.LinkedHashMap) ArtifactDescriptor(org.eclipse.equinox.p2.repository.artifact.spi.ArtifactDescriptor) IArtifactDescriptor(org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor) IP2Artifact(org.eclipse.tycho.p2.metadata.IP2Artifact) IP2Artifact(org.eclipse.tycho.p2.metadata.IP2Artifact) IInstallableUnit(org.eclipse.equinox.p2.metadata.IInstallableUnit) File(java.io.File)

Example 2 with P2Artifact

use of org.eclipse.tycho.p2.impl.publisher.P2Artifact in project tycho by eclipse.

the class P2MetadataMojo method attachP2Metadata.

protected void attachP2Metadata() throws MojoExecutionException {
    if (!attachP2Metadata || !supportedProjectTypes.contains(project.getPackaging())) {
        return;
    }
    File file = project.getArtifact().getFile();
    if (file == null || !file.canRead()) {
        throw new IllegalStateException();
    }
    File targetDir = new File(project.getBuild().getDirectory());
    ArtifactFacade projectDefaultArtifact = new ArtifactFacade(project.getArtifact());
    try {
        List<IArtifactFacade> artifacts = new ArrayList<>();
        artifacts.add(projectDefaultArtifact);
        for (Artifact attachedArtifact : project.getAttachedArtifacts()) {
            if (attachedArtifact.getFile() != null && (attachedArtifact.getFile().getName().endsWith(".jar") || (attachedArtifact.getFile().getName().endsWith(".zip") && project.getPackaging().equals(ArtifactType.TYPE_INSTALLABLE_UNIT)))) {
                artifacts.add(new ArtifactFacade(attachedArtifact));
            }
        }
        P2Generator p2generator = getService(P2Generator.class);
        Map<String, IP2Artifact> generatedMetadata = p2generator.generateMetadata(artifacts, targetDir);
        if (baselineMode != BaselineMode.disable) {
            generatedMetadata = baselineValidator.validateAndReplace(project, execution, generatedMetadata, baselineRepositories, baselineMode, baselineReplace);
        }
        File contentsXml = new File(targetDir, FILE_NAME_P2_METADATA);
        File artifactsXml = new File(targetDir, FILE_NAME_P2_ARTIFACTS);
        p2generator.persistMetadata(generatedMetadata, contentsXml, artifactsXml);
        projectHelper.attachArtifact(project, EXTENSION_P2_METADATA, CLASSIFIER_P2_METADATA, contentsXml);
        projectHelper.attachArtifact(project, EXTENSION_P2_ARTIFACTS, CLASSIFIER_P2_ARTIFACTS, artifactsXml);
        ReactorProject reactorProject = DefaultReactorProject.adapt(project);
        Set<Object> installableUnits = new LinkedHashSet<>();
        for (Map.Entry<String, IP2Artifact> entry : generatedMetadata.entrySet()) {
            String classifier = entry.getKey();
            IP2Artifact p2artifact = entry.getValue();
            installableUnits.addAll(p2artifact.getInstallableUnits());
            // attach any new classified artifacts, like feature root files for example
            if (classifier != null && !hasAttachedArtifact(project, classifier)) {
                projectHelper.attachArtifact(project, getExtension(p2artifact.getLocation()), classifier, p2artifact.getLocation());
            }
        }
        // TODO 353889 distinguish between dependency resolution seed units ("primary") and other units of the project
        reactorProject.setDependencyMetadata(true, installableUnits);
        reactorProject.setDependencyMetadata(false, Collections.emptySet());
    } catch (IOException e) {
        throw new MojoExecutionException("Could not generate P2 metadata", e);
    }
    File localArtifactsFile = new File(project.getBuild().getDirectory(), FILE_NAME_LOCAL_ARTIFACTS);
    writeArtifactLocations(localArtifactsFile, getAllProjectArtifacts(project));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IArtifactFacade(org.eclipse.tycho.p2.metadata.IArtifactFacade) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArrayList(java.util.ArrayList) DefaultReactorProject(org.eclipse.tycho.core.osgitools.DefaultReactorProject) ReactorProject(org.eclipse.tycho.ReactorProject) ArtifactFacade(org.eclipse.tycho.p2.facade.internal.ArtifactFacade) IArtifactFacade(org.eclipse.tycho.p2.metadata.IArtifactFacade) IOException(java.io.IOException) P2Generator(org.eclipse.tycho.p2.metadata.P2Generator) IP2Artifact(org.eclipse.tycho.p2.metadata.IP2Artifact) Artifact(org.apache.maven.artifact.Artifact) IP2Artifact(org.eclipse.tycho.p2.metadata.IP2Artifact) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with P2Artifact

use of org.eclipse.tycho.p2.impl.publisher.P2Artifact in project tycho by eclipse.

the class FeatureRootfileArtifactRepository method createRootfileOutputStream.

private OutputStream createRootfileOutputStream(IArtifactKey artifactKey) throws ProvisionException, IOException {
    File outputFile = new File(this.outputDirectory, artifactKey.getId() + "-" + artifactKey.getVersion() + "-" + ROOTFILE_CLASSIFIER + "." + ROOTFILE_EXTENSION);
    OutputStream target = null;
    try {
        SimpleArtifactDescriptor simpleArtifactDescriptor = (SimpleArtifactDescriptor) createArtifactDescriptor(artifactKey);
        Collection<IPropertyAdvice> advices = publisherInfo.getAdvice(null, false, simpleArtifactDescriptor.getArtifactKey().getId(), simpleArtifactDescriptor.getArtifactKey().getVersion(), IPropertyAdvice.class);
        boolean mavenPropAdviceExists = false;
        for (IPropertyAdvice entry : advices) {
            if (entry instanceof MavenPropertiesAdvice) {
                mavenPropAdviceExists = true;
                entry.getArtifactProperties(null, simpleArtifactDescriptor);
            }
        }
        if (!mavenPropAdviceExists) {
            throw new ProvisionException("MavenPropertiesAdvice does not exist for artifact: " + simpleArtifactDescriptor);
        }
        String mavenArtifactClassifier = getRootFileArtifactClassifier(simpleArtifactDescriptor.getArtifactKey().getId());
        simpleArtifactDescriptor.setProperty(RepositoryLayoutHelper.PROP_CLASSIFIER, mavenArtifactClassifier);
        simpleArtifactDescriptor.setProperty(RepositoryLayoutHelper.PROP_EXTENSION, ROOTFILE_EXTENSION);
        target = new BufferedOutputStream(new FileOutputStream(outputFile));
        this.publishedArtifacts.put(mavenArtifactClassifier, new P2Artifact(outputFile, Collections.<IInstallableUnit>emptySet(), simpleArtifactDescriptor));
        descriptors.add(simpleArtifactDescriptor);
    } catch (FileNotFoundException e) {
        throw new ProvisionException(e.getMessage(), e);
    }
    return target;
}
Also used : BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IPropertyAdvice(org.eclipse.equinox.p2.publisher.actions.IPropertyAdvice) MavenPropertiesAdvice(org.eclipse.tycho.p2.impl.publisher.MavenPropertiesAdvice) FileNotFoundException(java.io.FileNotFoundException) ProvisionException(org.eclipse.equinox.p2.core.ProvisionException) FileOutputStream(java.io.FileOutputStream) IP2Artifact(org.eclipse.tycho.p2.metadata.IP2Artifact) P2Artifact(org.eclipse.tycho.p2.impl.publisher.P2Artifact) IInstallableUnit(org.eclipse.equinox.p2.metadata.IInstallableUnit) SimpleArtifactDescriptor(org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactDescriptor) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 4 with P2Artifact

use of org.eclipse.tycho.p2.impl.publisher.P2Artifact 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)

Aggregations

File (java.io.File)4 IP2Artifact (org.eclipse.tycho.p2.metadata.IP2Artifact)4 IInstallableUnit (org.eclipse.equinox.p2.metadata.IInstallableUnit)3 BufferedOutputStream (java.io.BufferedOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ProvisionException (org.eclipse.equinox.p2.core.ProvisionException)2 IArtifactDescriptor (org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor)2 P2Artifact (org.eclipse.tycho.p2.impl.publisher.P2Artifact)2 IArtifactFacade (org.eclipse.tycho.p2.metadata.IArtifactFacade)2 FileNotFoundException (java.io.FileNotFoundException)1 URI (java.net.URI)1 LinkedHashSet (java.util.LinkedHashSet)1 Artifact (org.apache.maven.artifact.Artifact)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1