Skip to content

Commit

Permalink
Fix eclipse-tycho#447 - Runtime dependencies not resolved from target…
Browse files Browse the repository at this point in the history
… file

Signed-off-by: Christoph Läubrich <[email protected]>
  • Loading branch information
laeubi committed Feb 5, 2022
1 parent 27d5e60 commit 5470783
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public interface MavenDependenciesResolver {
* the packaging type, might be <code>null</code> in witch case "jar" is assumed
* @param classifier
* the classifier or <code>null</code> if no classifier is desired
* @param dependencyScope
* @param collection
* optional dependency scope, if given it tries to resolve transitive dependencies of
* the given artifact as well
* @param additionalRepositories
Expand All @@ -53,14 +53,14 @@ public interface MavenDependenciesResolver {
* @throws DependencyResolutionException
*/
default Collection<? /* IArtifactFacade */> resolve(String groupId, String artifactId, String version,
String packaging, String classifier, String dependencyScope, int depth,
String packaging, String classifier, Collection<String> scopes, int depth,
Collection<MavenArtifactRepositoryReference> additionalRepositories) throws DependencyResolutionException {
return resolve(groupId, artifactId, version, packaging, classifier, dependencyScope, depth,
additionalRepositories, null);
return resolve(groupId, artifactId, version, packaging, classifier, scopes, depth, additionalRepositories,
null);
}

Collection<? /* IArtifactFacade */> resolve(String groupId, String artifactId, String version, String packaging,
String classifier, String dependencyScope, int depth,
String classifier, Collection<String> scopes, int depth,
Collection<MavenArtifactRepositoryReference> additionalRepositories,
Object/* MavenSession */ session) throws DependencyResolutionException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public TestResolverFactory(MavenLogger logger) {

@Override
public Collection<?> resolve(String groupId, String artifactId, String version, String packaging,
String classifier, String dependencyScope, int depth,
String classifier, Collection<String> dependencyScopes, int depth,
Collection<MavenArtifactRepositoryReference> additionalRepositories, Object session)
throws DependencyResolutionException {
GAV gav = new GAV(groupId, artifactId, version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ public MavenTargetDefinitionContent(MavenGAVLocation location, MavenDependencies
List<IInstallableUnit> locationSourceBundles = new ArrayList<>();
for (MavenDependency mavenDependency : location.getRoots()) {
int depth;
String scope = location.getIncludeDependencyScope();
DependencyDepth dependencyDepth = location.getIncludeDependencyDepth();
if (dependencyDepth == DependencyDepth.NONE
&& POM_PACKAGING_TYPE.equalsIgnoreCase(mavenDependency.getArtifactType())) {
Expand All @@ -131,8 +130,8 @@ public MavenTargetDefinitionContent(MavenGAVLocation location, MavenDependencies
try {
resolve = mavenDependenciesResolver.resolve(mavenDependency.getGroupId(),
mavenDependency.getArtifactId(), mavenDependency.getVersion(),
mavenDependency.getArtifactType(), mavenDependency.getClassifier(), scope, depth,
location.getRepositoryReferences());
mavenDependency.getArtifactType(), mavenDependency.getClassifier(),
location.getIncludeDependencyScopes(), depth, location.getRepositoryReferences());
} catch (DependencyResolutionException e1) {
throw new TargetDefinitionResolutionException("MavenDependency " + mavenDependency + " of location "
+ location + " could not be resolved", e1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ enum DependencyDepth {
NONE, DIRECT, INFINITE;
}

String getIncludeDependencyScope();
Collection<String> getIncludeDependencyScopes();

DependencyDepth getIncludeDependencyDepth();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public String getUri() {
private static class MavenLocation implements TargetDefinition.MavenGAVLocation {

private final Set<String> globalExcludes;
private final String includeDependencyScope;
private final Collection<String> includeDependencyScopes;
private final MissingManifestStrategy manifestStrategy;
private final boolean includeSource;
private final Collection<BNDInstructions> instructions;
Expand All @@ -172,12 +172,12 @@ private static class MavenLocation implements TargetDefinition.MavenGAVLocation
private final Collection<MavenArtifactRepositoryReference> repositoryReferences;
private final Element featureTemplate;

public MavenLocation(Collection<MavenDependency> roots, String includeDependencyScope,
public MavenLocation(Collection<MavenDependency> roots, Collection<String> includeDependencyScopes,
MissingManifestStrategy manifestStrategy, Set<String> globalExcludes, boolean includeSource,
Collection<BNDInstructions> instructions, DependencyDepth dependencyDepth,
Collection<MavenArtifactRepositoryReference> repositoryReferences, Element featureTemplate) {
this.roots = roots;
this.includeDependencyScope = includeDependencyScope;
this.includeDependencyScopes = includeDependencyScopes;
this.manifestStrategy = manifestStrategy;
this.globalExcludes = globalExcludes;
this.includeSource = includeSource;
Expand All @@ -187,9 +187,8 @@ public MavenLocation(Collection<MavenDependency> roots, String includeDependency
this.featureTemplate = featureTemplate == null ? null : (Element) featureTemplate.cloneNode(true);
}

@Override
public String getIncludeDependencyScope() {
return includeDependencyScope;
public Collection<String> getIncludeDependencyScopes() {
return includeDependencyScopes;
}

@Override
Expand All @@ -207,7 +206,7 @@ public String toString() {
StringBuilder builder = new StringBuilder("MavenDependencyRoots = ");
builder.append(getRoots());
builder.append(", IncludeDependencyScope = ");
builder.append(getIncludeDependencyScope());
builder.append(getIncludeDependencyScopes());
builder.append(", MissingManifestStrategy = ");
builder.append(getMissingManifestStrategy());
builder.append(", IncludeSource = ");
Expand Down Expand Up @@ -566,9 +565,37 @@ private static MavenLocation parseMavenLocation(Element dom) {
for (Element element : getChildren(dom, "exclude")) {
globalExcludes.add(element.getTextContent());
}
Collection<String> scopes = new ArrayList<>();
String scope = dom.getAttribute("includeDependencyScope");
if (dom.hasAttribute("includeDependencyScopes")) {
String scopesAttribute = dom.getAttribute("includeDependencyScopes");
for (String s : scopesAttribute.split(",")) {
scopes.add(s.strip());
}
} else {
//backward compat ...
String SCOPE_COMPILE = "compile";
String SCOPE_TEST = "test";
String SCOPE_RUNTIME = "runtime";
String SCOPE_PROVIDED = "provided";
String SCOPE_SYSTEM = "system";
if (scope == null || scope.isBlank() || SCOPE_COMPILE.equalsIgnoreCase(scope)) {
scopes.add(SCOPE_COMPILE);
} else if (SCOPE_PROVIDED.equalsIgnoreCase(scope)) {
scopes.add(SCOPE_PROVIDED);
scopes.add(SCOPE_COMPILE);
scopes.add(SCOPE_SYSTEM);
scopes.add(SCOPE_RUNTIME);
} else if (SCOPE_TEST.equalsIgnoreCase(scope)) {
scopes.add(SCOPE_TEST);
scopes.add(SCOPE_COMPILE);
scopes.add(SCOPE_PROVIDED);
scopes.add(SCOPE_SYSTEM);
scopes.add(SCOPE_RUNTIME);
}
}
Element featureTemplate = getChild(dom, "feature");
return new MavenLocation(parseRoots(dom, globalExcludes), scope, parseManifestStrategy(dom), globalExcludes,
return new MavenLocation(parseRoots(dom, globalExcludes), scopes, parseManifestStrategy(dom), globalExcludes,
Boolean.parseBoolean(dom.getAttribute("includeSource")), parseInstructions(dom),
parseDependencyDepth(dom, scope), parseRepositoryReferences(dom), featureTemplate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
package org.eclipse.tycho.osgi.configuration;

import static org.apache.maven.artifact.Artifact.SCOPE_COMPILE;
import static org.apache.maven.artifact.Artifact.SCOPE_PROVIDED;
import static org.apache.maven.artifact.Artifact.SCOPE_RUNTIME;
import static org.apache.maven.artifact.Artifact.SCOPE_SYSTEM;
import static org.apache.maven.artifact.Artifact.SCOPE_TEST;

import java.io.File;
import java.io.FileReader;
Expand Down Expand Up @@ -66,7 +62,7 @@ public class MavenDependenciesResolverConfigurer extends EquinoxLifecycleListene

@Override
public Collection<?> resolve(String groupId, String artifactId, String version, String packaging, String classifier,
String dependencyScope, int depth, Collection<MavenArtifactRepositoryReference> additionalRepositories,
Collection<String> scopes, int depth, Collection<MavenArtifactRepositoryReference> additionalRepositories,
Object session) throws DependencyResolutionException {
Artifact artifact;
if (classifier != null && !classifier.isEmpty()) {
Expand All @@ -87,11 +83,11 @@ public Collection<?> resolve(String groupId, String artifactId, String version,
public boolean include(Artifact a) {
List<String> trail = a.getDependencyTrail();
if (logger.isDebugEnabled()) {
logger.debug("[depth=" + trail.size() + ", scope matches =" + isValidScope(a, dependencyScope)
+ "][" + a + "][" + trail.stream().collect(Collectors.joining(" >> ")) + "]");
logger.debug("[depth=" + trail.size() + ", scope matches =" + isValidScope(a, scopes) + "][" + a
+ "][" + trail.stream().collect(Collectors.joining(" >> ")) + "]");
}
if (trail.size() <= depth) {
return isValidScope(a, dependencyScope);
return isValidScope(a, scopes);
}
return false;
}
Expand All @@ -117,24 +113,20 @@ public boolean include(Artifact a) {
.collect(Collectors.toList());
}

protected boolean isValidScope(Artifact artifact, String desiredScope) {
protected boolean isValidScope(Artifact artifact, Collection<String> scopes) {
String artifactScope = artifact.getScope();
if (artifactScope == null || artifactScope.isBlank()) {
return true;
}
//compile is the default scope if not specified see
// https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#dependency-scope
if (desiredScope == null || desiredScope.isBlank() || SCOPE_COMPILE.equalsIgnoreCase(desiredScope)) {
if (scopes == null || scopes.isEmpty()) {
return SCOPE_COMPILE.equalsIgnoreCase(artifactScope);
}
if (SCOPE_PROVIDED.equalsIgnoreCase(desiredScope)) {
return SCOPE_PROVIDED.equalsIgnoreCase(artifactScope) || SCOPE_COMPILE.equalsIgnoreCase(artifactScope)
|| SCOPE_SYSTEM.equalsIgnoreCase(artifactScope) || SCOPE_RUNTIME.equalsIgnoreCase(artifactScope);
}
if (SCOPE_TEST.equalsIgnoreCase(desiredScope)) {
return SCOPE_TEST.equalsIgnoreCase(artifactScope) || SCOPE_COMPILE.equalsIgnoreCase(artifactScope)
|| SCOPE_PROVIDED.equalsIgnoreCase(artifactScope) || SCOPE_SYSTEM.equalsIgnoreCase(artifactScope)
|| SCOPE_RUNTIME.equalsIgnoreCase(artifactScope);
for (String scope : scopes) {
if (artifactScope.equals(scope)) {
return true;
}
}
//invalid scope type
return false;
Expand Down
48 changes: 48 additions & 0 deletions tycho-its/projects/target.maven-scopes/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- - Copyright (c) 2015, 2020 SAP SE 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 - http://www.eclipse.org/legal/epl-v10.html - - Contributors:
- SAP SE - initial API and implementation -->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.eclipse.tycho.itests</groupId>
<artifactId>issue-447-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<tycho-version>2.7.0-SNAPSHOT</tycho-version>
</properties>

<modules>
<module>test.bundle</module>
</modules>

<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>target-platform-configuration</artifactId>
<version>${tycho-version}</version>
<configuration>
<target>
<file>../test.target</file>
</target>
</configuration>
</plugin>
</plugins>
</build>



</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Bundle
Bundle-SymbolicName: test.bundle
Bundle-Version: 0.0.1.qualifier
Automatic-Module-Name: test.bundle
Bundle-RequiredExecutionEnvironment: JavaSE-11
Require-Bundle: net.bytebuddy.byte-buddy;bundle-version="1.12.1",
net.bytebuddy.byte-buddy-agent;bundle-version="1.12.1",
org.mockito.mockito-core;bundle-version="4.1.0",
org.objenesis;bundle-version="3.2.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.
21 changes: 21 additions & 0 deletions tycho-its/projects/target.maven-scopes/test.bundle/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2012 SAP AG 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 http://www.eclipse.org/legal/epl-v10.html
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>test.bundle</artifactId>
<packaging>eclipse-plugin</packaging>

<parent>
<groupId>org.eclipse.tycho.itests</groupId>
<artifactId>issue-447-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*******************************************************************************
* 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 test.bundle;

public class TestClass {
public static void main(String[] args) {
}
}
16 changes: 16 additions & 0 deletions tycho-its/projects/target.maven-scopes/test.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?pde version="3.8"?>
<target name="test">
<locations>
<location includeDependencyDepth="infinite" includeDependencyScopes="compile,runtime" includeSource="false" label="org.mockito [compile+runtime]" missingManifest="error" type="Maven">
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.1.0</version>
<type>jar</type>
</dependency>
</dependencies>
</location>
</locations>
</target>
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ public void testMavenLocation() throws Exception {
verifier.verifyErrorFreeLog();
}

@Test
public void testMavenLocationScopes() throws Exception {
Verifier verifier = getVerifier("target.maven-scopes", false, true);
verifier.executeGoal("verify");
verifier.verifyErrorFreeLog();
}

@Test
public void testMavenArtifactHaveMavenRepoPath() throws Exception {
Verifier verifier = getVerifier("target.maven", false, true);
Expand Down

0 comments on commit 5470783

Please sign in to comment.