Search in sources :

Example 1 with IP2Artifact

use of org.eclipse.tycho.p2.metadata.IP2Artifact 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 IP2Artifact

use of org.eclipse.tycho.p2.metadata.IP2Artifact in project tycho by eclipse.

the class BaselineValidator method validateAndReplace.

public Map<String, IP2Artifact> validateAndReplace(MavenProject project, MojoExecution execution, Map<String, IP2Artifact> reactorMetadata, List<Repository> baselineRepositories, BaselineMode baselineMode, BaselineReplace baselineReplace) throws IOException, MojoExecutionException {
    Map<String, IP2Artifact> result = reactorMetadata;
    if (baselineMode != disable && baselineRepositories != null && !baselineRepositories.isEmpty()) {
        List<MavenRepositoryLocation> _repositories = new ArrayList<>();
        for (Repository repository : baselineRepositories) {
            if (repository.getUrl() != null) {
                _repositories.add(new MavenRepositoryLocation(repository.getId(), repository.getUrl()));
            }
        }
        File baselineBasedir = new File(project.getBuild().getDirectory(), "baseline");
        BaselineService baselineService = getService(BaselineService.class);
        Map<String, IP2Artifact> baselineMetadata = baselineService.getProjectBaseline(_repositories, reactorMetadata, baselineBasedir);
        if (baselineMetadata != null) {
            CompoundArtifactDelta delta = getDelta(baselineService, baselineMetadata, reactorMetadata, execution);
            if (delta != null) {
                if (System.getProperties().containsKey("tycho.debug.artifactcomparator")) {
                    File logdir = new File(project.getBuild().getDirectory(), "artifactcomparison");
                    log.info("Artifact comparison detailed log directory " + logdir.getAbsolutePath());
                    for (Map.Entry<String, ArtifactDelta> classifier : delta.getMembers().entrySet()) {
                        if (classifier.getValue() instanceof CompoundArtifactDelta) {
                            ((CompoundArtifactDelta) classifier.getValue()).writeDetails(new File(logdir, classifier.getKey()));
                        }
                    }
                }
                if (baselineMode == fail || (baselineMode == failCommon && !isMissingOnlyDelta(delta))) {
                    throw new MojoExecutionException(delta.getDetailedMessage());
                } else {
                    log.warn(project.toString() + ": " + delta.getDetailedMessage());
                }
            }
            if (baselineReplace != none) {
                result = new LinkedHashMap<>();
                // replace reactor artifacts with baseline
                ArrayList<String> replaced = new ArrayList<>();
                for (Map.Entry<String, IP2Artifact> artifact : baselineMetadata.entrySet()) {
                    File baseLineFile = artifact.getValue().getLocation();
                    String classifier = artifact.getKey();
                    File reactorFile = reactorMetadata.get(classifier).getLocation();
                    if (baseLineFile.isFile() && baseLineFile.length() == 0L) {
                        // workaround for possibly corrupted download - bug 484003
                        log.error("baseline file " + baseLineFile.getAbsolutePath() + " is empty. Will not replace " + reactorFile);
                    } else {
                        FileUtils.copyFile(baseLineFile, reactorFile);
                        result.put(classifier, artifact.getValue());
                        if (classifier != null) {
                            replaced.add(classifier);
                        }
                    }
                }
                // un-attach and delete artifacts present in reactor but not in baseline
                ArrayList<String> removed = new ArrayList<>();
                ArrayList<String> inconsistent = new ArrayList<>();
                for (Map.Entry<String, IP2Artifact> entry : reactorMetadata.entrySet()) {
                    String classifier = entry.getKey();
                    IP2Artifact artifact = entry.getValue();
                    if (classifier == null || artifact == null) {
                        continue;
                    }
                    if (baselineReplace == all && !baselineMetadata.containsKey(classifier)) {
                        List<Artifact> attachedArtifacts = project.getAttachedArtifacts();
                        ListIterator<Artifact> iterator = attachedArtifacts.listIterator();
                        while (iterator.hasNext()) {
                            if (classifier.equals(iterator.next().getClassifier())) {
                                iterator.remove();
                                break;
                            }
                        }
                        artifact.getLocation().delete();
                        removed.add(classifier);
                    } else {
                        inconsistent.add(classifier);
                        result.put(classifier, artifact);
                    }
                }
                if (log.isInfoEnabled()) {
                    StringBuilder msg = new StringBuilder();
                    msg.append(project.toString());
                    msg.append("\n    The main artifact has been replaced with the baseline version.\n");
                    if (!replaced.isEmpty()) {
                        msg.append("    The following attached artifacts have been replaced with the baseline version: ");
                        msg.append(replaced.toString());
                        msg.append("\n");
                    }
                    if (!removed.isEmpty()) {
                        msg.append("    The following attached artifacts are not present in the baseline and have been removed: ");
                        msg.append(removed.toString());
                        msg.append("\n");
                    }
                    log.info(msg.toString());
                }
            }
        } else {
            log.info("No baseline version " + project);
        }
    }
    return result;
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArrayList(java.util.ArrayList) IP2Artifact(org.eclipse.tycho.p2.metadata.IP2Artifact) Artifact(org.apache.maven.artifact.Artifact) BaselineService(org.eclipse.tycho.p2.tools.baseline.facade.BaselineService) MavenRepositoryLocation(org.eclipse.tycho.core.resolver.shared.MavenRepositoryLocation) CompoundArtifactDelta(org.eclipse.tycho.zipcomparator.internal.CompoundArtifactDelta) IP2Artifact(org.eclipse.tycho.p2.metadata.IP2Artifact) ArtifactDelta(org.eclipse.tycho.artifactcomparator.ArtifactDelta) SimpleArtifactDelta(org.eclipse.tycho.zipcomparator.internal.SimpleArtifactDelta) CompoundArtifactDelta(org.eclipse.tycho.zipcomparator.internal.CompoundArtifactDelta) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with IP2Artifact

use of org.eclipse.tycho.p2.metadata.IP2Artifact in project tycho by eclipse.

the class BaselineValidator method getDelta.

private CompoundArtifactDelta getDelta(BaselineService baselineService, Map<String, IP2Artifact> baselineMetadata, Map<String, IP2Artifact> generatedMetadata, MojoExecution execution) throws IOException {
    Map<String, ArtifactDelta> result = new LinkedHashMap<>();
    // baseline never includes more artifacts
    for (Entry<String, IP2Artifact> classifierEntry : generatedMetadata.entrySet()) {
        // the following types of artifacts are produced/consumed by tycho as of 0.16
        // - bundle jar and jar.pack.gz artifacts
        // - feature jar artifacts
        // - feature rootfiles zip artifacts
        String classifier = classifierEntry.getKey();
        if (RepositoryLayoutHelper.PACK200_CLASSIFIER.equals(classifier)) {
            // but bundle jar files are the same, the build will silently use baseline pack200 file
            continue;
        }
        String deltaKey = classifier != null ? "classifier-" + classifier : "no-classifier";
        IP2Artifact baselineArtifact = baselineMetadata.get(classifier);
        IP2Artifact reactorArtifact = classifierEntry.getValue();
        if (baselineArtifact == null) {
            result.put(deltaKey, new MissingArtifactDelta());
            continue;
        }
        if (!baselineService.isMetadataEqual(baselineArtifact, reactorArtifact)) {
            result.put(deltaKey, new SimpleArtifactDelta("p2 metadata different"));
            continue;
        }
        try {
            ArtifactDelta delta = zipComparator.getDelta(baselineArtifact.getLocation(), reactorArtifact.getLocation(), execution);
            if (delta != null) {
                result.put(deltaKey, delta);
            }
        } catch (IOException e) {
            // do byte-to-byte comparison if jar comparison fails for whatever reason
            if (!FileUtils.contentEquals(baselineArtifact.getLocation(), reactorArtifact.getLocation())) {
                result.put(deltaKey, new SimpleArtifactDelta("different"));
            }
        }
    }
    return !result.isEmpty() ? new CompoundArtifactDelta("baseline and build artifacts have same version but different contents", result) : null;
}
Also used : CompoundArtifactDelta(org.eclipse.tycho.zipcomparator.internal.CompoundArtifactDelta) ArtifactDelta(org.eclipse.tycho.artifactcomparator.ArtifactDelta) SimpleArtifactDelta(org.eclipse.tycho.zipcomparator.internal.SimpleArtifactDelta) CompoundArtifactDelta(org.eclipse.tycho.zipcomparator.internal.CompoundArtifactDelta) IP2Artifact(org.eclipse.tycho.p2.metadata.IP2Artifact) IOException(java.io.IOException) SimpleArtifactDelta(org.eclipse.tycho.zipcomparator.internal.SimpleArtifactDelta) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with IP2Artifact

use of org.eclipse.tycho.p2.metadata.IP2Artifact 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 5 with IP2Artifact

use of org.eclipse.tycho.p2.metadata.IP2Artifact in project tycho by eclipse.

the class P2GeneratorImpl method persistMetadata.

@Override
public void persistMetadata(Map<String, IP2Artifact> metadata, File unitsXml, File artifactsXml) throws IOException {
    Set<IInstallableUnit> units = new LinkedHashSet<>();
    Set<IArtifactDescriptor> artifactDescriptors = new LinkedHashSet<>();
    for (IP2Artifact artifact : metadata.values()) {
        for (Object unit : artifact.getInstallableUnits()) {
            units.add((IInstallableUnit) unit);
        }
        artifactDescriptors.add((IArtifactDescriptor) artifact.getArtifactDescriptor());
    }
    new MetadataIO().writeXML(units, unitsXml);
    new ArtifactsIO().writeXML(artifactDescriptors, artifactsXml);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IArtifactDescriptor(org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor) MetadataIO(org.eclipse.tycho.p2.maven.repository.xmlio.MetadataIO) IP2Artifact(org.eclipse.tycho.p2.metadata.IP2Artifact) ArtifactsIO(org.eclipse.tycho.p2.maven.repository.xmlio.ArtifactsIO) IInstallableUnit(org.eclipse.equinox.p2.metadata.IInstallableUnit)

Aggregations

IP2Artifact (org.eclipse.tycho.p2.metadata.IP2Artifact)8 File (java.io.File)4 LinkedHashMap (java.util.LinkedHashMap)4 IArtifactDescriptor (org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 IInstallableUnit (org.eclipse.equinox.p2.metadata.IInstallableUnit)3 HashMap (java.util.HashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 Artifact (org.apache.maven.artifact.Artifact)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 ArtifactDelta (org.eclipse.tycho.artifactcomparator.ArtifactDelta)2 MavenRepositoryLocation (org.eclipse.tycho.core.resolver.shared.MavenRepositoryLocation)2 IArtifactFacade (org.eclipse.tycho.p2.metadata.IArtifactFacade)2 CompoundArtifactDelta (org.eclipse.tycho.zipcomparator.internal.CompoundArtifactDelta)2 SimpleArtifactDelta (org.eclipse.tycho.zipcomparator.internal.SimpleArtifactDelta)2 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1