diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/publisher/FeatureRootfileArtifactRepository.java b/tycho-core/src/main/java/org/eclipse/tycho/core/publisher/FeatureRootfileArtifactRepository.java index 0257245a14..2139626840 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/publisher/FeatureRootfileArtifactRepository.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/publisher/FeatureRootfileArtifactRepository.java @@ -14,7 +14,6 @@ import java.io.BufferedOutputStream; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; @@ -23,8 +22,9 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; +import java.util.stream.Collectors; -import org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactDescriptor; import org.eclipse.equinox.p2.core.ProvisionException; import org.eclipse.equinox.p2.metadata.IArtifactKey; import org.eclipse.equinox.p2.metadata.IInstallableUnit; @@ -32,6 +32,7 @@ import org.eclipse.equinox.p2.publisher.PublisherInfo; import org.eclipse.equinox.p2.publisher.actions.IPropertyAdvice; import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor; +import org.eclipse.equinox.p2.repository.artifact.spi.ArtifactDescriptor; import org.eclipse.equinox.spi.p2.publisher.PublisherHelper; import org.eclipse.tycho.TychoConstants; import org.eclipse.tycho.p2.metadata.IP2Artifact; @@ -41,11 +42,15 @@ import org.eclipse.tycho.p2maven.advices.MavenPropertiesAdvice; public class FeatureRootfileArtifactRepository extends TransientArtifactRepository { + //for backward compatibility + @SuppressWarnings("deprecation") + private static final String PROP_EXTENSION = TychoConstants.PROP_EXTENSION; + private final File outputDirectory; private final PublisherInfo publisherInfo; - private Map publishedArtifacts = new HashMap<>(); + private Map artifactsToPublish = new HashMap<>(); public FeatureRootfileArtifactRepository(PublisherInfo publisherInfo, File outputDirectory) { this.publisherInfo = publisherInfo; @@ -57,6 +62,7 @@ public OutputStream getOutputStream(IArtifactDescriptor descriptor) throws Provi IArtifactKey artifactKey = descriptor.getArtifactKey(); if (artifactKey != null && PublisherHelper.BINARY_ARTIFACT_CLASSIFIER.equals(artifactKey.getClassifier())) { try { + addDescriptor(descriptor); return createRootfileOutputStream(artifactKey); } catch (IOException e) { throw new ProvisionException(e.getMessage(), e); @@ -66,50 +72,11 @@ public OutputStream getOutputStream(IArtifactDescriptor descriptor) throws Provi return super.getOutputStream(descriptor); } - private OutputStream createRootfileOutputStream(IArtifactKey artifactKey) throws ProvisionException, IOException { + private OutputStream createRootfileOutputStream(IArtifactKey artifactKey) throws IOException { File outputFile = new File(this.outputDirectory, artifactKey.getId() + "-" + artifactKey.getVersion() + "-" + TychoConstants.ROOTFILE_CLASSIFIER + "." + TychoConstants.ROOTFILE_EXTENSION); - - OutputStream target = null; - try { - SimpleArtifactDescriptor simpleArtifactDescriptor = (SimpleArtifactDescriptor) createArtifactDescriptor( - artifactKey); - - Collection 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(TychoConstants.PROP_CLASSIFIER, mavenArtifactClassifier); - //Type and extension are the same for rootfiles ... - simpleArtifactDescriptor.setProperty(TychoConstants.PROP_EXTENSION, TychoConstants.ROOTFILE_EXTENSION); - simpleArtifactDescriptor.setProperty(TychoConstants.PROP_TYPE, TychoConstants.ROOTFILE_EXTENSION); - - target = new BufferedOutputStream(new FileOutputStream(outputFile)); - - this.publishedArtifacts.put(mavenArtifactClassifier, - new P2Artifact(outputFile, Collections. emptySet(), simpleArtifactDescriptor)); - - descriptors.add(simpleArtifactDescriptor); - } catch (FileNotFoundException e) { - throw new ProvisionException(e.getMessage(), e); - } - - return target; + artifactsToPublish.put(outputFile, artifactKey); + return new BufferedOutputStream(new FileOutputStream(outputFile)); } String getRootFileArtifactClassifier(String artifactId) { @@ -131,6 +98,30 @@ String getRootFileArtifactClassifier(String artifactId) { } public Map getPublishedArtifacts() { - return publishedArtifacts; + return artifactsToPublish.entrySet().stream().map(entry -> { + File outputFile = entry.getKey(); + IArtifactKey artifactKey = entry.getValue(); + IArtifactDescriptor artifactDescriptor = PublisherHelper.createArtifactDescriptor(publisherInfo, + artifactKey, outputFile); + Collection advices = publisherInfo.getAdvice(null, false, + artifactDescriptor.getArtifactKey().getId(), artifactDescriptor.getArtifactKey().getVersion(), + IPropertyAdvice.class); + + boolean mavenPropAdviceExists = false; + for (IPropertyAdvice advice : advices) { + if (advice instanceof MavenPropertiesAdvice) { + advice.getArtifactProperties(null, artifactDescriptor); + } + } + String mavenArtifactClassifier = getRootFileArtifactClassifier(artifactDescriptor.getArtifactKey().getId()); + if (artifactDescriptor instanceof ArtifactDescriptor impl) { + impl.setProperty(TychoConstants.PROP_CLASSIFIER, mavenArtifactClassifier); + //Type and extension are the same for rootfiles ... + impl.setProperty(PROP_EXTENSION, TychoConstants.ROOTFILE_EXTENSION); + impl.setProperty(TychoConstants.PROP_TYPE, TychoConstants.ROOTFILE_EXTENSION); + } + return Map.entry(mavenArtifactClassifier, + new P2Artifact(outputFile, Collections. emptySet(), artifactDescriptor)); + }).collect(Collectors.toMap(Entry::getKey, Entry::getValue)); } }