Search in sources :

Example 1 with ArtifactDelta

use of org.eclipse.tycho.artifactcomparator.ArtifactDelta in project tycho by eclipse.

the class PropertiesComparator method getDelta.

@Override
public ArtifactDelta getDelta(InputStream baseline, InputStream reactor, MojoExecution mojo) throws IOException {
    TreeMap<String, ArtifactDelta> result = new TreeMap<>();
    Properties props = new Properties();
    props.load(baseline);
    Properties props2 = new Properties();
    props2.load(reactor);
    Set<String> names = new LinkedHashSet<>();
    addAll(names, props);
    addAll(names, props2);
    for (String name : names) {
        String value = props.getProperty(name);
        if (value == null) {
            result.put(name, new SimpleArtifactDelta("not present in baseline version"));
            continue;
        }
        String value2 = props2.getProperty(name);
        if (value2 == null) {
            result.put(name, new SimpleArtifactDelta("present in baseline version only"));
            continue;
        }
        if (!value.equals(value2)) {
            result.put(name, new SimpleArtifactDelta("baseline='" + value + "' != reactor='" + value2 + "'"));
        }
    }
    return !result.isEmpty() ? new CompoundArtifactDelta("properties files differ", result) : null;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArtifactDelta(org.eclipse.tycho.artifactcomparator.ArtifactDelta) TreeMap(java.util.TreeMap) Properties(java.util.Properties)

Example 2 with ArtifactDelta

use of org.eclipse.tycho.artifactcomparator.ArtifactDelta in project tycho by eclipse.

the class ZipComparatorImpl method getDelta.

@Override
public CompoundArtifactDelta getDelta(File baseline, File reactor, MojoExecution execution) throws IOException {
    Map<String, ArtifactDelta> result = new LinkedHashMap<>();
    Collection<String> ignoredPatterns = new HashSet<>(IGNORED_PATTERNS);
    if (execution != null) {
        Xpp3Dom pluginConfiguration = (Xpp3Dom) execution.getPlugin().getConfiguration();
        if (pluginConfiguration != null) {
            Xpp3Dom ignoredPatternsNode = pluginConfiguration.getChild("ignoredPatterns");
            if (ignoredPatternsNode != null) {
                for (Xpp3Dom node : ignoredPatternsNode.getChildren()) {
                    ignoredPatterns.add(node.getValue());
                }
            }
        }
    }
    ZipFile jar = new ZipFile(baseline);
    try {
        ZipFile jar2 = new ZipFile(reactor);
        try {
            Map<String, ZipEntry> entries = toEntryMap(jar, ignoredPatterns);
            Map<String, ZipEntry> entries2 = toEntryMap(jar2, ignoredPatterns);
            Set<String> names = new TreeSet<>();
            names.addAll(entries.keySet());
            names.addAll(entries2.keySet());
            for (String name : names) {
                ZipEntry entry = entries.get(name);
                if (entry == null) {
                    result.put(name, new SimpleArtifactDelta("not present in baseline"));
                    continue;
                }
                ZipEntry entry2 = entries2.get(name);
                if (entry2 == null) {
                    result.put(name, new SimpleArtifactDelta("present in baseline only"));
                    continue;
                }
                InputStream is = jar.getInputStream(entry);
                try {
                    InputStream is2 = jar2.getInputStream(entry2);
                    try {
                        ContentsComparator comparator = comparators.get(getContentType(name));
                        ArtifactDelta differences = comparator.getDelta(is, is2, execution);
                        if (differences != null) {
                            result.put(name, differences);
                            continue;
                        }
                    } finally {
                        IOUtil.close(is2);
                    }
                } finally {
                    IOUtil.close(is);
                }
            }
        } finally {
            try {
                jar2.close();
            } catch (IOException e) {
            // too bad
            }
        }
    } finally {
        try {
            jar.close();
        } catch (IOException e) {
        // ouch
        }
    }
    return !result.isEmpty() ? new CompoundArtifactDelta("different", result) : null;
}
Also used : Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) ZipFile(java.util.zip.ZipFile) TreeSet(java.util.TreeSet) ArtifactDelta(org.eclipse.tycho.artifactcomparator.ArtifactDelta) HashSet(java.util.HashSet)

Example 3 with ArtifactDelta

use of org.eclipse.tycho.artifactcomparator.ArtifactDelta in project tycho by eclipse.

the class CompoundArtifactDeltaTest method testGetDetailedMessage.

@Test
public void testGetDetailedMessage() {
    Map<String, ArtifactDelta> manifest = new TreeMap<>();
    manifest.put("name1", new SimpleArtifactDelta("present in baseline only"));
    Map<String, ArtifactDelta> main = new TreeMap<>();
    main.put("path/file1", new SimpleArtifactDelta("different"));
    main.put("path/file2", new SimpleArtifactDelta("not present in baseline"));
    main.put("META-INF/MANIFEST.MF", new CompoundArtifactDelta("different", manifest));
    Map<String, ArtifactDelta> delta = new TreeMap<>();
    delta.put("<main>", new CompoundArtifactDelta("different", main));
    delta.put("sources", new SimpleArtifactDelta("different"));
    ArtifactDelta subject = new CompoundArtifactDelta("Baseline and reactor artifacts have the same version but different contents", delta);
    String expected = // 
    "Baseline and reactor artifacts have the same version but different contents\n" + // 
    "   <main>: different\n" + // 
    "      META-INF/MANIFEST.MF: different\n" + // 
    "         name1: present in baseline only\n" + // 
    "      path/file1: different\n" + // 
    "      path/file2: not present in baseline\n" + "   sources: different\n";
    Assert.assertEquals(expected, subject.getDetailedMessage());
}
Also used : CompoundArtifactDelta(org.eclipse.tycho.zipcomparator.internal.CompoundArtifactDelta) SimpleArtifactDelta(org.eclipse.tycho.zipcomparator.internal.SimpleArtifactDelta) ArtifactDelta(org.eclipse.tycho.artifactcomparator.ArtifactDelta) CompoundArtifactDelta(org.eclipse.tycho.zipcomparator.internal.CompoundArtifactDelta) TreeMap(java.util.TreeMap) SimpleArtifactDelta(org.eclipse.tycho.zipcomparator.internal.SimpleArtifactDelta) Test(org.junit.Test)

Example 4 with ArtifactDelta

use of org.eclipse.tycho.artifactcomparator.ArtifactDelta 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 5 with ArtifactDelta

use of org.eclipse.tycho.artifactcomparator.ArtifactDelta 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)

Aggregations

ArtifactDelta (org.eclipse.tycho.artifactcomparator.ArtifactDelta)6 LinkedHashMap (java.util.LinkedHashMap)3 TreeMap (java.util.TreeMap)3 CompoundArtifactDelta (org.eclipse.tycho.zipcomparator.internal.CompoundArtifactDelta)3 SimpleArtifactDelta (org.eclipse.tycho.zipcomparator.internal.SimpleArtifactDelta)3 IOException (java.io.IOException)2 LinkedHashSet (java.util.LinkedHashSet)2 IP2Artifact (org.eclipse.tycho.p2.metadata.IP2Artifact)2 File (java.io.File)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Properties (java.util.Properties)1 TreeSet (java.util.TreeSet)1 Attributes (java.util.jar.Attributes)1 Name (java.util.jar.Attributes.Name)1 Manifest (java.util.jar.Manifest)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1