Skip to content

Commit

Permalink
Return the maven location for missing junit bundles
Browse files Browse the repository at this point in the history
Currently we only publish the meta-data, what works for most of the time
if a junit bundle is missing, but for the case where the bundle name
does not match the artifact key (e.g. junit4/hamcrest) this leads to
Tycho not find the file and injecting a dummy file that can confuse
maven-surefire-plugin.

This now adds a new IRawArtifactFileProvider provider for such missing
bundles that is asked as last resort if no other part of the system can
answer for a file.
  • Loading branch information
laeubi committed Sep 28, 2023
1 parent 57a6a43 commit fb32453
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactDescriptor;
import org.eclipse.equinox.p2.metadata.IArtifactKey;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.publisher.IPublisherAdvice;
import org.eclipse.equinox.p2.publisher.IPublisherInfo;
import org.eclipse.equinox.p2.publisher.PublisherInfo;
import org.eclipse.equinox.p2.publisher.actions.IPropertyAdvice;
Expand All @@ -37,13 +38,17 @@ private BundlePublisher(BundleDescription bundleDescription) {
super(new BundleDescription[] { bundleDescription });
}

public static Optional<IInstallableUnit> getBundleIU(File bundleLocation) throws IOException, BundleException {
public static Optional<IInstallableUnit> getBundleIU(File bundleLocation, IPublisherAdvice... advices)
throws IOException, BundleException {
BundleDescription bundleDescription = BundlesAction.createBundleDescription(bundleLocation);
if (bundleDescription == null) {
//seems it is not a bundle
return Optional.empty();
}
PublisherInfo publisherInfo = new PublisherInfo();
for (IPublisherAdvice advice : advices) {
publisherInfo.addAdvice(advice);
}
publisherInfo.setArtifactOptions(IPublisherInfo.A_INDEX);
String symbolicName = bundleDescription.getSymbolicName();
if (symbolicName == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*******************************************************************************
* Copyright (c) 2023 Christoph Läubrich and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.tycho.p2resolver;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.p2.metadata.IArtifactKey;
import org.eclipse.equinox.p2.query.IQuery;
import org.eclipse.equinox.p2.query.IQueryResult;
import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor;
import org.eclipse.tycho.ArtifactSinkException;
import org.eclipse.tycho.IArtifactSink;
import org.eclipse.tycho.IRawArtifactFileProvider;
import org.eclipse.tycho.IRawArtifactSink;

class MissingBundlesArtifactFileProvider implements IRawArtifactFileProvider {

private Map<IArtifactKey, File> mappedFiles = new HashMap<>();

@Override
public File getArtifactFile(IArtifactKey key) {
return mappedFiles.get(key);
}

@Override
public boolean isFileAlreadyAvailable(IArtifactKey artifactKey) {
return contains(artifactKey);
}

@Override
public boolean contains(IArtifactKey key) {
return mappedFiles.containsKey(key);
}

@Override
public IStatus getArtifact(IArtifactSink sink, IProgressMonitor monitor) throws ArtifactSinkException {
return Status.CANCEL_STATUS;
}

@Override
public IQueryResult<IArtifactKey> query(IQuery<IArtifactKey> query, IProgressMonitor monitor) {
return query.perform(mappedFiles.keySet().iterator());
}

@Override
public IArtifactDescriptor[] getArtifactDescriptors(IArtifactKey key) {
return new IArtifactDescriptor[0];
}

@Override
public boolean contains(IArtifactDescriptor descriptor) {
return false;
}

@Override
public IStatus getRawArtifact(IRawArtifactSink sink, IProgressMonitor monitor) throws ArtifactSinkException {
return Status.CANCEL_STATUS;
}

@Override
public File getArtifactFile(IArtifactDescriptor descriptor) {

return getArtifactFile(descriptor.getArtifactKey());
}

void add(IArtifactKey key, File file) {
mappedFiles.put(key, file);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.URIUtil;
import org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactDescriptor;
import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.equinox.p2.core.ProvisionException;
import org.eclipse.equinox.p2.metadata.IArtifactKey;
Expand Down Expand Up @@ -107,6 +108,7 @@
import org.eclipse.tycho.p2.target.facade.TargetPlatformConfigurationStub;
import org.eclipse.tycho.p2.target.facade.TargetPlatformFactory;
import org.eclipse.tycho.p2maven.ListCompositeArtifactRepository;
import org.eclipse.tycho.p2maven.advices.MavenPropertiesAdvice;
import org.eclipse.tycho.targetplatform.P2TargetPlatform;
import org.eclipse.tycho.targetplatform.TargetDefinition;
import org.eclipse.tycho.targetplatform.TargetDefinitionContent;
Expand Down Expand Up @@ -224,12 +226,16 @@ public P2TargetPlatform createTargetPlatform(TargetPlatformConfigurationStub tpC

//add maven junit bundles...
List<MavenArtifactKey> junitBundles = getMissingJunitBundles(project, externalUIs);
MissingBundlesArtifactFileProvider extraMavenBundles = new MissingBundlesArtifactFileProvider();
for (MavenArtifactKey mavenArtifactKey : junitBundles) {
Optional<ResolvedArtifactKey> mavenBundle = mavenBundleResolver.resolveMavenBundle(
project.adapt(MavenProject.class), project.adapt(MavenSession.class), mavenArtifactKey);
mavenBundle.map(ResolvedArtifactKey::getLocation).flatMap(bundleFile -> {
mavenBundle.flatMap(key -> {
File bundleFile = key.getLocation();
try {
Optional<IInstallableUnit> iu = BundlePublisher.getBundleIU(bundleFile);
MavenPropertiesAdvice advice = new MavenPropertiesAdvice(mavenArtifactKey.getGroupId(),
mavenArtifactKey.getArtifactId(), key.getVersion());
Optional<IInstallableUnit> iu = BundlePublisher.getBundleIU(bundleFile, advice);
IInstallableUnit unit = iu.orElse(null);
if (unit != null) {
InstallableUnitDescription description = new InstallableUnitDescription();
Expand All @@ -242,13 +248,17 @@ public P2TargetPlatform createTargetPlatform(TargetPlatformConfigurationStub tpC
"org.eclipse.equinox.p2.iu", mavenArtifactKey.getId(), unit.getVersion());
description.addProvidedCapabilities(List.of(cap));
}
description.setArtifacts(unit.getArtifacts().toArray(IArtifactKey[]::new));
IArtifactKey[] artifactKeys = unit.getArtifacts().toArray(IArtifactKey[]::new);
description.setArtifacts(artifactKeys);
for (IArtifactKey mavenkey : artifactKeys) {
extraMavenBundles.add(mavenkey, bundleFile);
}
return Optional.of(MetadataFactory.createInstallableUnit(description));
}
} catch (IOException e) {
} catch (BundleException e) {
}
return null;
return Optional.empty();
}).ifPresent(externalUIs::add);
}

Expand All @@ -266,7 +276,7 @@ public P2TargetPlatform createTargetPlatform(TargetPlatformConfigurationStub tpC
shadowed);

IRawArtifactFileProvider externalArtifactFileProvider = createExternalArtifactProvider(artifactRepositories,
targetFileContent);
targetFileContent, extraMavenBundles);
PreliminaryTargetPlatformImpl targetPlatform = new PreliminaryTargetPlatformImpl(reactorProjectUIs, //
externalUIs, //
eeResolutionHandler.getResolutionHints(), //
Expand All @@ -276,7 +286,6 @@ public P2TargetPlatform createTargetPlatform(TargetPlatformConfigurationStub tpC
localArtifactRepository, //
includeLocalMavenRepo, //
logger, shadowed);

eeResolutionHandler.readFullSpecification(targetPlatform.getInstallableUnits());

return targetPlatform;
Expand Down Expand Up @@ -456,7 +465,7 @@ public static SortedRepositories sort(List<IArtifactRepository> repositories) {
* Provider for all target platform artifacts from outside the reactor.
*/
private IRawArtifactFileProvider createExternalArtifactProvider(Set<URI> completeRepositories,
List<TargetDefinitionContent> targetDefinitionsContent) {
List<TargetDefinitionContent> targetDefinitionsContent, IRawArtifactFileProvider extraMavenBundles) {
SortedRepositories repos = SortedRepositories
.sort(targetDefinitionsContent.stream().map(TargetDefinitionContent::getArtifactRepository).toList());
RepositoryArtifactProvider remoteArtifactProvider = createRemoteArtifactProvider(completeRepositories,
Expand All @@ -467,7 +476,7 @@ private IRawArtifactFileProvider createExternalArtifactProvider(Set<URI> complet
return new CompositeArtifactProvider(
new FileRepositoryArtifactProvider(repos.localRepositories,
ArtifactTransferPolicies.forLocalArtifacts()), //
remoteArtifactProviderWithCache);
remoteArtifactProviderWithCache, extraMavenBundles);
}

/**
Expand Down

0 comments on commit fb32453

Please sign in to comment.