Skip to content

Commit

Permalink
Check if the about to be injected maven coordinates can be resolved
Browse files Browse the repository at this point in the history
Fix #2625
  • Loading branch information
laeubi committed Jul 21, 2023
1 parent 6aff1f9 commit ff1f753
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ public enum LocalArtifactHandling {
ignore;
}

public enum InjectP2MavenMetadataHandling {
/**
* ignores P2 maven metadata
*/
ignore,
/**
* inject if found in P2 metadata
*/
inject,
/**
* inject if found in P2 metadata, but validate if it is actually valid for the current
* build
*/
validate;
}

private String resolver;

private List<TargetEnvironment> environments = new ArrayList<>();
Expand Down Expand Up @@ -99,6 +115,9 @@ public enum LocalArtifactHandling {
private LocalArtifactHandling localArtifactHandling;

private boolean requireEagerResolve;

private InjectP2MavenMetadataHandling p2MavenMetadataHandling;

/**
* Returns the list of configured target environments, or the running environment if no
* environments have been specified explicitly.
Expand Down Expand Up @@ -277,6 +296,17 @@ public LocalArtifactHandling getIgnoreLocalArtifacts() {
return localArtifactHandling;
}

public InjectP2MavenMetadataHandling getP2MetadataHandling() {
if (p2MavenMetadataHandling == null) {
return InjectP2MavenMetadataHandling.validate;
}
return p2MavenMetadataHandling;
}

public void setP2MavenMetadataHandling(InjectP2MavenMetadataHandling p2MavenMetadataHandling) {
this.p2MavenMetadataHandling = p2MavenMetadataHandling;
}

public void setLocalArtifactHandling(LocalArtifactHandling localArtifactHandling) {
this.localArtifactHandling = localArtifactHandling;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.function.Supplier;

import org.apache.maven.artifact.repository.ArtifactRepository;
Expand All @@ -44,8 +45,10 @@
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.IRequirement;
import org.eclipse.tycho.ArtifactDescriptor;
import org.eclipse.tycho.ArtifactKey;
import org.eclipse.tycho.BuildFailureException;
import org.eclipse.tycho.BuildProperties;
Expand All @@ -56,6 +59,7 @@
import org.eclipse.tycho.IDependencyMetadata;
import org.eclipse.tycho.IDependencyMetadata.DependencyMetadataType;
import org.eclipse.tycho.IllegalArtifactReferenceException;
import org.eclipse.tycho.MavenDependencyDescriptor;
import org.eclipse.tycho.MavenRepositoryLocation;
import org.eclipse.tycho.OptionalResolutionAction;
import org.eclipse.tycho.ReactorProject;
Expand All @@ -65,8 +69,10 @@
import org.eclipse.tycho.core.DependencyResolver;
import org.eclipse.tycho.core.DependencyResolverConfiguration;
import org.eclipse.tycho.core.TargetPlatformConfiguration;
import org.eclipse.tycho.core.TargetPlatformConfiguration.InjectP2MavenMetadataHandling;
import org.eclipse.tycho.core.TargetPlatformConfiguration.LocalArtifactHandling;
import org.eclipse.tycho.core.TychoProjectManager;
import org.eclipse.tycho.core.maven.MavenDependenciesResolver;
import org.eclipse.tycho.core.maven.MavenDependencyInjector;
import org.eclipse.tycho.core.osgitools.AbstractTychoProject;
import org.eclipse.tycho.core.osgitools.BundleReader;
Expand Down Expand Up @@ -131,6 +137,9 @@ public class P2DependencyResolver extends AbstractLogEnabled implements Dependen
@Requirement
private PomUnits pomUnits;

@Requirement
private MavenDependenciesResolver dependenciesResolver;

@Override
public void setupProjects(final MavenSession session, final MavenProject project,
final ReactorProject reactorProject) {
Expand Down Expand Up @@ -387,8 +396,28 @@ public void initialize() throws InitializationException {
@Override
public void injectDependenciesIntoMavenModel(MavenProject project, AbstractTychoProject projectType,
DependencyArtifacts dependencyArtifacts, DependencyArtifacts testDependencyArtifacts, Logger logger) {
Function<ArtifactDescriptor, MavenDependencyDescriptor> descriptorMapping;
TargetPlatformConfiguration configuration = projectManager.getTargetPlatformConfiguration(project);
if (configuration.getP2MetadataHandling() == InjectP2MavenMetadataHandling.inject) {
descriptorMapping = resolverFactory::resolveDependencyDescriptor;
} else if (configuration.getP2MetadataHandling() == InjectP2MavenMetadataHandling.validate) {
descriptorMapping = descriptor -> {
MavenDependencyDescriptor result = resolverFactory.resolveDependencyDescriptor(descriptor);
if (result != null) {
try {
dependenciesResolver.resolveArtifact(project, context.getSession(), result.getGroupId(),
result.getArtifactId(), result.getVersion());
} catch (ArtifactResolutionException e) {
logger.warn("Mapping P2 > Maven Coordinates failed: " + e.getMessage());
}
}
return null;
};
} else {
descriptorMapping = null;
}
MavenDependencyInjector.injectMavenDependencies(project, dependencyArtifacts, testDependencyArtifacts,
bundleReader, resolverFactory::resolveDependencyDescriptor, logger, repositorySystem,
context.getSession().getSettings(), buildPropertiesParser);
bundleReader, descriptorMapping, logger, repositorySystem, context.getSession().getSettings(),
buildPropertiesParser);
}
}

0 comments on commit ff1f753

Please sign in to comment.