Skip to content

Commit

Permalink
[backport] Fix MavenLocation scope filtering
Browse files Browse the repository at this point in the history
Use a CollectionFilter for scopes, as scope is not
as accurate when using the ResolutionFilter (eg case of commons-io with
test deps reported as compile scope)
  • Loading branch information
mickaelistria authored and laeubi committed Aug 27, 2022
1 parent 01549a4 commit 546f97b
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 8 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Expand Up @@ -7,6 +7,7 @@ This page describes the noteworthy improvements provided by each release of Ecli
Fixes:

- [reverted] Not all (direct) requirements of a feature are considered when building an update-site
- [backport] Fix MavenLocation scope filtering

## 2.7.4

Expand Down
7 changes: 7 additions & 0 deletions tycho-core/pom.xml
Expand Up @@ -228,6 +228,13 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<!-- See MavenDependenciesResolverTest -->
<dependency>
<groupId>io.takari.aether</groupId>
<artifactId>aether-connector-okhttp</artifactId>
<version>0.17.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.annotation</artifactId>
Expand Down
Expand Up @@ -12,8 +12,6 @@
*******************************************************************************/
package org.eclipse.tycho.osgi.configuration;

import static org.apache.maven.artifact.Artifact.SCOPE_COMPILE;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
Expand Down Expand Up @@ -77,19 +75,16 @@ public Collection<?> resolve(String groupId, String artifactId, String version,
MavenSession mavenSession = getMavenSession(session);
request.setResolveRoot(true);
request.setOffline(mavenSession.isOffline());
request.setCollectionFilter(a -> isValidScope(a, scopes));
request.setResolutionFilter(new ArtifactFilter() {

@Override
public boolean include(Artifact a) {
List<String> trail = a.getDependencyTrail();
if (logger.isDebugEnabled()) {
logger.debug("[depth=" + trail.size() + ", scope matches =" + isValidScope(a, scopes) + "][" + a
+ "][" + trail.stream().collect(Collectors.joining(" >> ")) + "]");
}
if (trail.size() <= depth) {
return isValidScope(a, scopes);
}
return false;
return trail.size() <= depth && isValidScope(a, scopes);
}
});
request.setLocalRepository(mavenSession.getLocalRepository());
Expand Down Expand Up @@ -121,7 +116,7 @@ protected boolean isValidScope(Artifact artifact, Collection<String> scopes) {
//compile is the default scope if not specified see
// https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#dependency-scope
if (scopes == null || scopes.isEmpty()) {
return SCOPE_COMPILE.equalsIgnoreCase(artifactScope);
return Artifact.SCOPE_COMPILE.equalsIgnoreCase(artifactScope);
}
for (String scope : scopes) {
if (artifactScope.equals(scope)) {
Expand Down
@@ -0,0 +1,68 @@
/*******************************************************************************
* Copyright (c) 2022 Red Hat, 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
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.tycho.core.maven;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;
import org.apache.maven.execution.DefaultMavenExecutionRequestPopulator;
import org.apache.maven.execution.MavenExecutionRequestPopulationException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.LegacySupport;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.PlexusContainerException;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.eclipse.sisu.equinox.embedder.EquinoxLifecycleListener;
import org.eclipse.tycho.core.shared.DependencyResolutionException;
import org.eclipse.tycho.core.shared.MavenArtifactRepositoryReference;
import org.eclipse.tycho.osgi.configuration.MavenDependenciesResolverConfigurer;
import org.junit.Test;

public class MavenDependenciesResolverTest extends AbstractMojoTestCase {

@Test
public void testResolveCommonsIO() throws DependencyResolutionException, PlexusContainerException,
ComponentLookupException, MavenExecutionRequestPopulationException, IOException {
MavenSession session = newMavenSession(new MavenProject());
File localRepo = Files.createTempDirectory("testResolveCommonsIO").toFile();
session.getRequest().setLocalRepositoryPath(localRepo);
DefaultMavenExecutionRequestPopulator populator = getContainer()
.lookup(DefaultMavenExecutionRequestPopulator.class);
populator.populateDefaults(session.getRequest());
getContainer().lookup(LegacySupport.class).setSession(session);
MavenDependenciesResolverConfigurer resolver = (MavenDependenciesResolverConfigurer) getContainer()
.lookup(EquinoxLifecycleListener.class, "MavenDependenciesResolver");
// the artifact must be pre-existing in the local repo, so the dep is added to pom.xml for this module. Test could be enhanced to fetch the artifact.
Collection<?> deps = resolver.resolve("commons-io", "commons-io", "2.11.0", "jar", null, List.of(),
Integer.MAX_VALUE, session.getRequest().getRemoteRepositories().stream()
.map(repo -> new MavenArtifactRepositoryReference() {

@Override
public String getUrl() {
return repo.getUrl();
}

@Override
public String getId() {
return repo.getId();
}
}).map(MavenArtifactRepositoryReference.class::cast) //
.collect(Collectors.toList()),
session);
assertEquals(deps.toString(), 1, deps.size());
FileUtils.deleteDirectory(localRepo);
}
}
6 changes: 6 additions & 0 deletions tycho-its/projects/target.maven-deps/META-INF/MANIFEST.MF
@@ -0,0 +1,6 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: test.bundle
Bundle-Version: 0.0.1.qualifier
Automatic-Module-Name: test.bundle
Require-Bundle: org.apache.commons.commons-io
4 changes: 4 additions & 0 deletions tycho-its/projects/target.maven-deps/build.properties
@@ -0,0 +1,4 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.
38 changes: 38 additions & 0 deletions tycho-its/projects/target.maven-deps/pom.xml
@@ -0,0 +1,38 @@
<?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 https://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>

<groupId>issue-1133</groupId>
<artifactId>test.bundle</artifactId>
<packaging>eclipse-plugin</packaging>
<version>0.0.1-SNAPSHOT</version>

<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>
@@ -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) {
}
}
15 changes: 15 additions & 0 deletions tycho-its/projects/target.maven-deps/test.target
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?pde version="3.8"?>
<target name="test">
<locations>
<location includeDependencyDepth="infinite" includeDependencyScopes="compile" includeSource="false" missingManifest="error" type="Maven">
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
</location>
</locations>
</target>
Expand Up @@ -122,4 +122,10 @@ public void testMavenLocationAutogeneratedFeature() throws Exception {
verifier.verifyErrorFreeLog();
}

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

0 comments on commit 546f97b

Please sign in to comment.