Skip to content

Commit

Permalink
Fix eclipse-tycho#697 - Failed to resolve dependencies with Tycho 2.7…
Browse files Browse the repository at this point in the history
….0 for custom

repositories
  • Loading branch information
laeubi committed Apr 6, 2022
1 parent 755d2dd commit 66cbb4f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public interface TychoConstants {

// static final String CTX_TARGET_PLATFORM -> moved to TargetPlatform.FINAL_TARGET_PLATFORM_KEY;
static final String CTX_DEPENDENCY_ARTIFACTS = CTX_BASENAME + "/dependencyArtifacts";
static final String CTX_REPOSITORY_REFERENCE = CTX_BASENAME + "/repositoryReference";
/**
* Stores test-specific dependencies (usually derived from .classpath)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,30 @@
*
* Contributors:
* SAP SE - initial API and implementation
* Christoph Läubrich - Adjust to new API
* Christoph Läubrich - Issue #697 - Failed to resolve dependencies with Tycho 2.7.0 for custom repositories
*******************************************************************************/
package org.eclipse.tycho.p2.manager;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.equinox.internal.p2.core.helpers.FileUtils;
import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.equinox.p2.core.IProvisioningAgentProvider;
import org.eclipse.equinox.p2.core.ProvisionException;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.ReactorProjectIdentities;
import org.eclipse.tycho.TychoConstants;
import org.eclipse.tycho.artifacts.TargetPlatform;
import org.eclipse.tycho.core.ee.shared.ExecutionEnvironmentConfiguration;
import org.eclipse.tycho.core.shared.MavenArtifactRepositoryReference;
import org.eclipse.tycho.p2.resolver.facade.P2ResolverFactory;
import org.eclipse.tycho.p2.target.PreliminaryTargetPlatformImpl;
import org.eclipse.tycho.p2.target.facade.PomDependencyCollector;
import org.eclipse.tycho.p2.target.facade.TargetDefinition.MavenGAVLocation;
import org.eclipse.tycho.p2.target.facade.TargetPlatformConfigurationStub;
import org.eclipse.tycho.p2.target.facade.TargetPlatformFactory;
import org.eclipse.tycho.repository.module.PublishingRepositoryImpl;
Expand Down Expand Up @@ -85,6 +89,12 @@ public TargetPlatform computePreliminaryTargetPlatform(ReactorProject project,
// at this point, there is only incomplete ("dependency-only") metadata for the reactor projects
TargetPlatform result = tpFactory.createTargetPlatform(tpConfiguration, eeConfiguration, reactorProjects);
project.setContextValue(PRELIMINARY_TARGET_PLATFORM_KEY, result);

List<MavenArtifactRepositoryReference> repositoryReferences = tpConfiguration.getTargetDefinitions().stream()
.flatMap(definition -> definition.getLocations().stream()).filter(MavenGAVLocation.class::isInstance)
.map(MavenGAVLocation.class::cast).flatMap(location -> location.getRepositoryReferences().stream())
.collect(Collectors.toList());
project.setContextValue(TychoConstants.CTX_REPOSITORY_REFERENCE, repositoryReferences);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,29 @@
* Sonatype Inc. - initial API and implementation
* SAP AG - inject nested class path elements into maven model (TYCHO-483)
* Christoph Läubrich - Issue #443 - Use regular Maven coordinates -when possible- for dependencies
* - Issue #581 - Use the correct scope when injecting dependencies into the maven model
* - Issue #581 - Use the correct scope when injecting dependencies into the maven model
* - Issue #697 - Failed to resolve dependencies with Tycho 2.7.0 for custom repositories
*******************************************************************************/
package org.eclipse.tycho.core.maven;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.plexus.logging.Logger;
import org.eclipse.tycho.ArtifactDescriptor;
import org.eclipse.tycho.ArtifactKey;
Expand All @@ -35,6 +42,8 @@
import org.eclipse.tycho.TychoConstants;
import org.eclipse.tycho.artifacts.DependencyArtifacts;
import org.eclipse.tycho.core.osgitools.BundleReader;
import org.eclipse.tycho.core.osgitools.DefaultReactorProject;
import org.eclipse.tycho.core.shared.MavenArtifactRepositoryReference;

public final class MavenDependencyInjector {

Expand All @@ -49,7 +58,8 @@ public final class MavenDependencyInjector {
*/
public static void injectMavenDependencies(MavenProject project, DependencyArtifacts dependencies,
DependencyArtifacts testDependencies, BundleReader bundleReader,
Function<ArtifactDescriptor, MavenDependencyDescriptor> descriptorMapping, Logger logger) {
Function<ArtifactDescriptor, MavenDependencyDescriptor> descriptorMapping, Logger logger,
RepositorySystem repositorySystem) {
MavenDependencyInjector generator = new MavenDependencyInjector(project, bundleReader, descriptorMapping,
logger);
for (ArtifactDescriptor artifact : dependencies.getArtifacts()) {
Expand All @@ -60,6 +70,26 @@ public static void injectMavenDependencies(MavenProject project, DependencyArtif
.filter(testDep -> dependencies.getArtifact(testDep.getKey()) == null) //
.forEach(descriptor -> generator.addDependency(descriptor, Artifact.SCOPE_TEST));
}
ReactorProject reactorProject = DefaultReactorProject.adapt(project);
@SuppressWarnings("unchecked")
Collection<MavenArtifactRepositoryReference> repositoryReferences = (Collection<MavenArtifactRepositoryReference>) reactorProject
.getContextValue(TychoConstants.CTX_REPOSITORY_REFERENCE);
if (repositoryReferences != null && !repositoryReferences.isEmpty()) {
Map<String, ArtifactRepository> repositoryMap = project.getRemoteArtifactRepositories().stream().collect(
Collectors.toMap(ArtifactRepository::getId, Function.identity(), (a, b) -> a, LinkedHashMap::new));
for (MavenArtifactRepositoryReference reference : repositoryReferences) {
String id = reference.getId();
ArtifactRepository artifactRepository = repositoryMap.get(id);
if (artifactRepository == null) {
repositoryMap.put(id,
repositorySystem.createArtifactRepository(id, reference.getUrl(), null, null, null));
} else if (!artifactRepository.getUrl().equals(reference.getUrl())) {
logger.warn("Target defines an artifact repository with the ID " + id
+ " but there is already a repository for that ID mapped to a different URL! (target URL = "
+ reference.getUrl() + ", existing URL = " + artifactRepository.getUrl());
}
}
}
}

private final BundleReader bundleReader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,6 @@ public void initialize() throws InitializationException {
public void injectDependenciesIntoMavenModel(MavenProject project, AbstractTychoProject projectType,
DependencyArtifacts dependencyArtifacts, DependencyArtifacts testDependencyArtifacts, Logger logger) {
MavenDependencyInjector.injectMavenDependencies(project, dependencyArtifacts, testDependencyArtifacts,
bundleReader, resolverFactory::resolveDependencyDescriptor, logger);
bundleReader, resolverFactory::resolveDependencyDescriptor, logger, repositorySystem);
}
}

0 comments on commit 66cbb4f

Please sign in to comment.