Skip to content

Commit

Permalink
eclipse-tycho#443 - reuse information from p2 iu
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Läubrich <laeubi@laeubi-soft.de>
  • Loading branch information
laeubi committed Jan 4, 2022
1 parent fba2b72 commit 7c8ab0f
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*******************************************************************************
* Copyright (c) 2022 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;

/**
* describes a dependency as with the maven Dependency class
*/
public interface DependencyDescriptor {

/**
* @return the unique id for an artifact
*/
String getArtifactId();

/**
* @return the classifier of the dependency
*/
String getClassifier();

/**
*
* @return group that produced the dependency
*/
String getGroupId();

/**
* @return indicates the dependency is optional
*/
boolean isOptional();

/**
* @return the scope of the dependency
*/
String getScope();

/**
* @return absolute system path for system scoped dependencies
*/
String getSystemPath();

/**
* @return the type of dependency
*/
String getType();

/**
* @return the version of the dependency
*/
String getVersion();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.Properties;

import org.eclipse.equinox.p2.core.ProvisionException;
import org.eclipse.tycho.ArtifactDescriptor;
import org.eclipse.tycho.DependencyDescriptor;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.core.shared.MavenContext;
import org.eclipse.tycho.core.shared.MavenContextImpl;
Expand Down Expand Up @@ -104,4 +106,9 @@ public TargetPlatformFactoryImpl getTargetPlatformFactoryImpl() {
public P2Resolver createResolver(MavenLogger logger) {
return new P2ResolverImpl(getTargetPlatformFactoryImpl(), mavenContext.getLogger());
}

@Override
public DependencyDescriptor getDependencyDescriptor(ArtifactDescriptor artifactDescriptor) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@
* Contributors:
* Sonatype Inc. - initial API and implementation
* Christoph Läubrich - Bug 567098 - pomDependencies=consider should wrap non-osgi jars
* Issue #443 - Use regular Maven coordinates -when possible- for dependencies
*******************************************************************************/
package org.eclipse.tycho.p2.resolver;

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

import org.eclipse.equinox.p2.core.ProvisionException;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.tycho.ArtifactDescriptor;
import org.eclipse.tycho.DependencyDescriptor;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.core.shared.MavenContext;
import org.eclipse.tycho.core.shared.MavenLogger;
import org.eclipse.tycho.p2.remote.RemoteAgentManager;
import org.eclipse.tycho.p2.repository.LocalRepositoryP2Indices;
import org.eclipse.tycho.p2.repository.LocalRepositoryReader;
import org.eclipse.tycho.p2.repository.RepositoryLayoutHelper;
import org.eclipse.tycho.p2.repository.RepositoryReader;
import org.eclipse.tycho.p2.resolver.facade.P2ResolverFactory;
import org.eclipse.tycho.p2.target.PomDependencyCollectorImpl;
Expand Down Expand Up @@ -103,4 +110,61 @@ public void setRemoteAgentManager(RemoteAgentManager remoteAgentManager) {
public void setTargetDefinitionResolverService(TargetDefinitionResolverService targetDefinitionResolverService) {
this.targetDefinitionResolverService = targetDefinitionResolverService;
}

@Override
public DependencyDescriptor getDependencyDescriptor(ArtifactDescriptor artifactDescriptor) {
return artifactDescriptor.getInstallableUnits().stream().filter(IInstallableUnit.class::isInstance)
.map(IInstallableUnit.class::cast).map(iu -> {
Map<String, String> properties = iu.getProperties();
String groupId = properties.get(RepositoryLayoutHelper.PROP_GROUP_ID);
String artifactId = properties.get(RepositoryLayoutHelper.PROP_ARTIFACT_ID);
String version = properties.get(RepositoryLayoutHelper.PROP_VERSION);
if (groupId == null || artifactId == null || version == null) {
//these properties are required!
return null;
}
return new DependencyDescriptor() {

@Override
public boolean isOptional() {
return false;
}

@Override
public String getVersion() {
return version;
}

@Override
public String getType() {
return properties.get(RepositoryLayoutHelper.PROP_EXTENSION);
}

@Override
public String getSystemPath() {
return null;
}

@Override
public String getScope() {
return null;
}

@Override
public String getGroupId() {
return groupId;
}

@Override
public String getClassifier() {
return properties.get(RepositoryLayoutHelper.PROP_CLASSIFIER);
}

@Override
public String getArtifactId() {
return artifactId;
}
};
}).filter(Objects::nonNull).findFirst().orElse(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2020 Sonatype Inc. and others.
* Copyright (c) 2008, 2022 Sonatype Inc. 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
Expand All @@ -10,9 +10,12 @@
* Contributors:
* Sonatype Inc. - initial API and implementation
* Christoph Läubrich - Bug 567098 - pomDependencies=consider should wrap non-osgi jars
* - Issue #443 - Use regular Maven coordinates -when possible- for dependencies
*******************************************************************************/
package org.eclipse.tycho.p2.resolver.facade;

import org.eclipse.tycho.ArtifactDescriptor;
import org.eclipse.tycho.DependencyDescriptor;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.core.shared.MavenLogger;
import org.eclipse.tycho.p2.target.facade.PomDependencyCollector;
Expand All @@ -30,4 +33,6 @@ public interface P2ResolverFactory {
public TargetPlatformFactory getTargetPlatformFactory();

public P2Resolver createResolver(MavenLogger logger);

DependencyDescriptor getDependencyDescriptor(ArtifactDescriptor artifactDescriptor);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2011 Sonatype Inc. and others.
* Copyright (c) 2008, 2022 Sonatype Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -35,7 +35,7 @@ public class MavenDependencyCollector extends ArtifactDependencyVisitor {
private final Logger logger;

public MavenDependencyCollector(MavenProject project, BundleReader bundleReader, Logger logger) {
this.injector = new MavenDependencyInjector(project, bundleReader, logger);
this.injector = new MavenDependencyInjector(project, bundleReader, null, logger);
this.logger = logger;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2011 Sonatype Inc. and others.
* Copyright (c) 2008, 2022 Sonatype Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -8,13 +8,15 @@
* Contributors:
* 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
*******************************************************************************/
package org.eclipse.tycho.core.maven;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Dependency;
Expand All @@ -24,6 +26,7 @@
import org.eclipse.tycho.ArtifactDescriptor;
import org.eclipse.tycho.ArtifactKey;
import org.eclipse.tycho.ArtifactType;
import org.eclipse.tycho.DependencyDescriptor;
import org.eclipse.tycho.PackagingType;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.artifacts.DependencyArtifacts;
Expand All @@ -44,8 +47,10 @@ public final class MavenDependencyInjector {
* The p2-resolved dependencies of the project.
*/
public static void injectMavenDependencies(MavenProject project, DependencyArtifacts dependencies,
DependencyArtifacts testDependencies, BundleReader bundleReader, Logger logger) {
MavenDependencyInjector generator = new MavenDependencyInjector(project, bundleReader, logger);
DependencyArtifacts testDependencies, BundleReader bundleReader,
Function<ArtifactDescriptor, DependencyDescriptor> descriptorMapping, Logger logger) {
MavenDependencyInjector generator = new MavenDependencyInjector(project, bundleReader, descriptorMapping,
logger);
for (ArtifactDescriptor artifact : dependencies.getArtifacts()) {
generator.addDependency(artifact, Artifact.SCOPE_COMPILE);
}
Expand All @@ -63,9 +68,13 @@ public static void injectMavenDependencies(MavenProject project, DependencyArtif

private final MavenProject project;

MavenDependencyInjector(MavenProject project, BundleReader bundleReader, Logger logger) {
private Function<ArtifactDescriptor, DependencyDescriptor> descriptorMapping;

MavenDependencyInjector(MavenProject project, BundleReader bundleReader,
Function<ArtifactDescriptor, DependencyDescriptor> descriptorMapping, Logger logger) {
this.project = project;
this.bundleReader = bundleReader;
this.descriptorMapping = descriptorMapping;
this.logger = logger;
}

Expand Down Expand Up @@ -98,13 +107,12 @@ private List<Dependency> newExternalDependencies(ArtifactDescriptor artifact) {
if (ArtifactType.TYPE_ECLIPSE_PLUGIN.equals(artifact.getKey().getType())) {
for (String classpathElement : getClasspathElements(location)) {
if (".".equals(classpathElement)) {
result.add(createSystemScopeDependency(artifact.getKey(), location));
result.add(createSystemScopeDependency(artifact, location));
} else {
File nestedJarOrDir = bundleReader.getEntry(location, classpathElement);
if (nestedJarOrDir != null) {
if (nestedJarOrDir.isFile()) {
Dependency nestedJarDependency = createSystemScopeDependency(artifact.getKey(),
nestedJarOrDir);
Dependency nestedJarDependency = createSystemScopeDependency(artifact, nestedJarOrDir);
nestedJarDependency.setClassifier(classpathElement);
result.add(nestedJarDependency);
} else if (nestedJarOrDir.isDirectory()) {
Expand All @@ -117,7 +125,7 @@ private List<Dependency> newExternalDependencies(ArtifactDescriptor artifact) {
}
}
} else {
result.add(createSystemScopeDependency(artifact.getKey(), location));
result.add(createSystemScopeDependency(artifact, location));
}
return result;
}
Expand All @@ -126,17 +134,41 @@ private String[] getClasspathElements(File bundleLocation) {
return bundleReader.loadManifest(bundleLocation).getBundleClasspath();
}

private Dependency createSystemScopeDependency(ArtifactKey artifactKey, File location) {
return createSystemScopeDependency(artifactKey, P2_GROUPID_PREFIX + artifactKey.getType(), location);
private Dependency createSystemScopeDependency(ArtifactDescriptor descriptor, File location) {
ArtifactKey artifactKey = descriptor.getKey();
return createSystemScopeDependency(descriptor, P2_GROUPID_PREFIX + artifactKey.getType(), location);
}

private Dependency createSystemScopeDependency(ArtifactKey artifactKey, String groupId, File location) {
private Dependency createSystemScopeDependency(ArtifactDescriptor descriptor, String groupId, File location) {
Dependency dependency = new Dependency();
dependency.setGroupId(groupId);
dependency.setArtifactId(artifactKey.getId());
dependency.setVersion(artifactKey.getVersion());
dependency.setScope(Artifact.SCOPE_SYSTEM);
dependency.setSystemPath(location.getAbsolutePath());
DependencyDescriptor dependencyDescriptor = descriptorMapping == null ? null
: descriptorMapping.apply(descriptor);
if (dependencyDescriptor != null) {
dependency.setGroupId(dependencyDescriptor.getGroupId());
dependency.setArtifactId(dependencyDescriptor.getArtifactId());
dependency.setVersion(dependencyDescriptor.getVersion());
dependency.setClassifier(dependency.getClassifier());
dependency.setType(dependencyDescriptor.getType());
String scope = dependencyDescriptor.getScope();
if (scope == null || scope.isBlank() || Artifact.SCOPE_SYSTEM.equalsIgnoreCase(scope)) {
dependency.setScope(Artifact.SCOPE_SYSTEM);
} else {
dependency.setScope(scope);
}
String systemPath = dependencyDescriptor.getSystemPath();
if (systemPath == null || systemPath.isBlank()) {
dependency.setSystemPath(location.getAbsolutePath());
} else {
dependency.setSystemPath(systemPath);
}
} else {
ArtifactKey artifactKey = descriptor.getKey();
dependency.setGroupId(groupId);
dependency.setArtifactId(artifactKey.getId());
dependency.setVersion(artifactKey.getVersion());
dependency.setScope(Artifact.SCOPE_SYSTEM);
dependency.setSystemPath(location.getAbsolutePath());
}
return dependency;
}

Expand All @@ -158,7 +190,7 @@ private List<Dependency> newProjectDependencies(ArtifactDescriptor artifact) {
// we can only add a system scope dependency for an existing (checked-in) jar file
// otherwise maven will throw a DependencyResolutionException
if (jar.isFile()) {
Dependency systemScopeDependency = createSystemScopeDependency(artifact.getKey(),
Dependency systemScopeDependency = createSystemScopeDependency(artifact,
artifact.getMavenProject().getGroupId(), jar);
systemScopeDependency.setClassifier(classpathElement);
result.add(systemScopeDependency);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,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, logger);
bundleReader, resolverFactory::getDependencyDescriptor, logger);
}
}

0 comments on commit 7c8ab0f

Please sign in to comment.