Skip to content

Commit

Permalink
Issue 697 inject repositories (#855)
Browse files Browse the repository at this point in the history
Fix #697 - Failed to resolve dependencies with Tycho 2.7.0 for custom
repositories

Signed-off-by: Christoph Läubrich <laeubi@laeubi-soft.de>
Co-authored-by: Patrick Ziegler <Patrick.Ziegler@dsa.de>
  • Loading branch information
laeubi and ptziegler committed Apr 7, 2022
1 parent 2b79621 commit 9cfc954
Show file tree
Hide file tree
Showing 14 changed files with 228 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,44 @@ 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(MavenDependencyInjector::getId, Function.identity(), (a, b) -> a,
LinkedHashMap::new));
for (MavenArtifactRepositoryReference reference : repositoryReferences) {
String id = getId(reference);
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());
}
}
project.setRemoteArtifactRepositories(new ArrayList<ArtifactRepository>(repositoryMap.values()));
}
}

private static String getId(MavenArtifactRepositoryReference reference) {
String id = reference.getId();
if (id == null || id.isBlank()) {
return reference.getUrl();
}
return id;
}

private static String getId(ArtifactRepository repository) {
String id = repository.getId();
if (id == null || id.isBlank()) {
return repository.getUrl();
}
return id;
}

private final BundleReader bundleReader;
Expand Down
8 changes: 8 additions & 0 deletions tycho-its/projects/issue697/bundle1/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: bundle
Bundle-SymbolicName: bundle
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-11
Automatic-Module-Name: bundle
Require-Bundle: wrapped.de.monticore.monticore-runtime;bundle-version="7.2.0"
3 changes: 3 additions & 0 deletions tycho-its/projects/issue697/bundle1/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source.. = src/
bin.includes = META-INF/,\
.
13 changes: 13 additions & 0 deletions tycho-its/projects/issue697/bundle1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>tycho-its-project</groupId>
<artifactId>issue697</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>bundle</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>
</project>
8 changes: 8 additions & 0 deletions tycho-its/projects/issue697/bundle2/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: bundle2
Bundle-SymbolicName: bundle2
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-11
Automatic-Module-Name: bundle2
Require-Bundle: dsltool-api;bundle-version="0.0.1"
3 changes: 3 additions & 0 deletions tycho-its/projects/issue697/bundle2/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source.. = src/
bin.includes = META-INF/,\
.
13 changes: 13 additions & 0 deletions tycho-its/projects/issue697/bundle2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>tycho-its-project</groupId>
<artifactId>issue697</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>bundle2</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>
</project>
58 changes: 58 additions & 0 deletions tycho-its/projects/issue697/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>tycho-its-project</groupId>
<artifactId>issue697</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
<module>target-platform</module>
<module>bundle1</module>
<module>bundle2</module>
</modules>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tycho-version}</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-versions-plugin</artifactId>
<version>${tycho-version}</version>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<version>${tycho-version}</version>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-packaging-plugin</artifactId>
<version>${tycho-version}</version>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>target-platform-configuration</artifactId>
<version>${tycho-version}</version>
<configuration>
<target>
<artifact>
<groupId>tycho-its-project</groupId>
<artifactId>target-platform</artifactId>
<version>0.0.1-SNAPSHOT</version>
</artifact>
</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
13 changes: 13 additions & 0 deletions tycho-its/projects/issue697/target-platform/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>tycho-its-project</groupId>
<artifactId>issue697</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>target-platform</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>eclipse-target-definition</packaging>
</project>
28 changes: 28 additions & 0 deletions tycho-its/projects/issue697/target-platform/target-platform.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?pde version="3.8"?>
<target name="tp">
<locations>
<location includeDependencyDepth="infinite" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven">
<dependencies>
<dependency>
<groupId>de.monticore</groupId>
<artifactId>dsltool-api</artifactId>
<version>0.0.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>de.monticore</groupId>
<artifactId>monticore-runtime</artifactId>
<version>7.2.0</version>
<type>jar</type>
</dependency>
</dependencies>
<repositories>
<repository>
<id>rwth</id>
<url>https://nexus.se.rwth-aachen.de/repository/monticore/</url>
</repository>
</repositories>
</location>
</locations>
</target>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.eclipse.tycho.test.issue697;

import java.util.List;

import org.apache.maven.it.Verifier;
import org.eclipse.tycho.test.AbstractTychoIntegrationTest;
import org.junit.Test;

public class Issue697Test extends AbstractTychoIntegrationTest {

@Test
public void test() throws Exception {
Verifier verifier = getVerifier("issue697");

verifier.executeGoals(List.of("clean", "verify"));
verifier.verifyErrorFreeLog();
}
}
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 9cfc954

Please sign in to comment.