Skip to content

Commit

Permalink
Merge pull request #195 from laeubi/issue_194
Browse files Browse the repository at this point in the history
Fix #194 Support additional repositories defined in the maven-target location
  • Loading branch information
laeubi authored Jul 28, 2021
2 parents be529ae + 6d9e02d commit 4043bcb
Show file tree
Hide file tree
Showing 16 changed files with 326 additions and 26 deletions.
8 changes: 6 additions & 2 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

This page describes the noteworthy improvements provided by each release of Eclipse Tycho.

### Next release...
## Next release...

### Support for new m2e-pde features

Tycho supports the new m2e-pde features regarding [multiple dependencies per target](https://github.com/eclipse-m2e/m2e-core/blob/master/RELEASE_NOTES.md#the-m2e-pde-editor-now-supports-adding-more-than-one-dependency-per-target-location) and specifying [extra repositories in the target](https://github.com/eclipse-m2e/m2e-core/blob/master/RELEASE_NOTES.md#the-m2e-pde-editor-now-supports-adding-additional-maven-repoistories-for-a-target-location).

## 2.4.0

### [Support resolving of JUnit Classpath Container](https://bugs.eclipse.org/bugs/show_bug.cgi?id=572602)
It is now possible to resolve the JDT 'JUnit Classpath Container', for this do the follwoing:
It is now possible to resolve the JDT 'JUnit Classpath Container', for this do the following:

- add the 'JUnit Classpath Container' to the classpath of your eclipse project
- make sure you check in the .classpath file
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*******************************************************************************
* Copyright (c) 2021 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.core.shared;

public interface MavenArtifactRepositoryReference {

String getId();

String getUrl();

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 Christoph Läubrich and others.
* Copyright (c) 2020, 2021 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
Expand All @@ -14,6 +14,7 @@

import java.io.File;
import java.util.Collection;
import java.util.Collections;

public interface MavenDependenciesResolver {

Expand All @@ -35,8 +36,15 @@ public interface MavenDependenciesResolver {
* the given artifact as well
* @return
*/
default Collection<? /* IArtifactFacade */> resolve(String groupId, String artifactId, String version,
String packaging, String classifier, String dependencyScope) {
return resolve(groupId, artifactId, version, packaging, classifier, dependencyScope, Collections.emptyList());
}

Collection<? /* IArtifactFacade */> resolve(String groupId, String artifactId, String version, String packaging,
String classifier, String dependencyScope);
String classifier, String dependencyScope,
Collection<MavenArtifactRepositoryReference> additionalRepositories);

File getRepositoryRoot();

}
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,41 @@ public MavenTargetDefinitionContent(MavenGAVLocation location, MavenDependencies
Collection<?> resolve = mavenDependenciesResolver.resolve(mavenDependency.getGroupId(),
mavenDependency.getArtifactId(), mavenDependency.getVersion(),
mavenDependency.getArtifactType(), mavenDependency.getClassifier(),
location.getIncludeDependencyScope());
location.getIncludeDependencyScope(), location.getRepositoryReferences());

Iterator<IArtifactFacade> resolvedArtifacts = resolve.stream().filter(IArtifactFacade.class::isInstance)
.map(IArtifactFacade.class::cast).iterator();
Properties defaultProperties = WrappedArtifact.createPropertiesForPrefix("wrapped");
while (resolvedArtifacts.hasNext()) {
IArtifactFacade mavenArtifact = resolvedArtifacts.next();
if (mavenDependency.isIgnored(mavenArtifact)) {
logger.debug("Skipp ignored " + mavenArtifact + "...");
continue;
}
logger.debug("Resolved " + mavenArtifact + "...");
String symbolicName;
String bundleVersion;
try {
File bundleLocation = mavenArtifact.getLocation();
BundleDescription bundleDescription = BundlesAction.createBundleDescription(bundleLocation);
if (bundleDescription == null) {
if (logger.isDebugEnabled()) {
logger.debug("Bundle Location: " + bundleLocation + " (Filesize "
+ (bundleLocation != null ? bundleLocation.length() : -1) + ")");
boolean isFile = bundleLocation != null && bundleLocation.isFile();
logger.debug("File isFile: " + isFile);
if (isFile) {
try (JarFile jarFile = new JarFile(bundleLocation)) {
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry jarEntry = entries.nextElement();
logger.debug(" Entry: " + jarEntry.getName());
}
} catch (Exception e) {
logger.debug("Reading as jar failed: " + e);
}
}
}
throw new TargetDefinitionResolutionException("Artifact " + mavenArtifact + " of location "
+ location + " is not a valid jar file");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* [Bug 568729] - Support new "Maven" Target location
* [Bug 569481] - Support for maven target location includeSource="true" attribute
* [Issue 189] - Support multiple maven-dependencies for one target location
* [Issue 194] - Support additional repositories defined in the maven-target location #
*******************************************************************************/
package org.eclipse.tycho.p2.target.facade;

Expand All @@ -21,6 +22,9 @@
import java.util.List;
import java.util.Properties;

import org.eclipse.tycho.core.shared.MavenArtifactRepositoryReference;
import org.eclipse.tycho.p2.metadata.IArtifactFacade;

// TODO javadoc
public interface TargetDefinition {

Expand Down Expand Up @@ -85,6 +89,8 @@ enum MissingManifestStrategy {

Collection<MavenDependency> getRoots();

Collection<MavenArtifactRepositoryReference> getRepositoryReferences();

boolean includeSource();
}

Expand Down Expand Up @@ -179,6 +185,8 @@ public interface MavenDependency {
String getArtifactType();

String getClassifier();

boolean isIgnored(IArtifactFacade artifact);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* - [Bug 568729] - Support new "Maven" Target location
* - [Bug 569481] - Support for maven target location includeSource="true" attribute
* - [Issue 189] - Support multiple maven-dependencies for one target location
* - [Issue 194] - Support additional repositories defined in the maven-target location #
*******************************************************************************/
package org.eclipse.tycho.core.ee;

Expand All @@ -34,11 +35,15 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.tycho.core.shared.MavenArtifactRepositoryReference;
import org.eclipse.tycho.p2.metadata.IArtifactFacade;
import org.eclipse.tycho.p2.target.facade.TargetDefinition;
import org.eclipse.tycho.p2.target.facade.TargetDefinitionSyntaxException;

Expand Down Expand Up @@ -135,8 +140,14 @@ public class MavenLocation implements TargetDefinition.MavenGAVLocation {

private Element dom;

private Set<String> globalExcludes = new HashSet<>();

public MavenLocation(Element dom) {
this.dom = dom;
List<Element> children = dom.getChildren("exclude");
for (Element element : children) {
globalExcludes.add(element.getNormalizedText());
}
}

@Override
Expand Down Expand Up @@ -214,57 +225,74 @@ public Collection<MavenDependency> getRoots() {
for (Element dependencies : dom.getChildren("dependencies")) {
List<MavenDependency> roots = new ArrayList<>();
for (Element dependency : dependencies.getChildren("dependency")) {
roots.add(new MavenDependencyRoot(dependency));
roots.add(new MavenDependencyRoot(dependency, this));
}
return roots;
}
//backward compatibility for old format...
return Collections.singleton(new MavenDependencyRoot(dom));
return Collections.singleton(new MavenDependencyRoot(dom, this));
}

@Override
public Collection<MavenArtifactRepositoryReference> getRepositoryReferences() {
for (Element dependencies : dom.getChildren("repositories")) {
List<MavenArtifactRepositoryReference> list = new ArrayList<MavenArtifactRepositoryReference>();
for (Element repository : dependencies.getChildren("repository")) {
list.add(new MavenArtifactRepositoryReference() {

@Override
public String getId() {
return getTextFromChild(repository, "id",
String.valueOf(System.identityHashCode(repository)));
}

@Override
public String getUrl() {
return getTextFromChild(repository, "url", null);
}

});
}
return list;
}
return Collections.emptyList();
}

}

private static final class MavenDependencyRoot implements MavenDependency {

private Element dom;
private MavenLocation parent;

public MavenDependencyRoot(Element dom) {
public MavenDependencyRoot(Element dom, MavenLocation parent) {
this.dom = dom;
this.parent = parent;
}

@Override
public String getGroupId() {
return getTextFromChild("groupId", null);
return getTextFromChild(dom, "groupId", null);
}

@Override
public String getArtifactId() {
return getTextFromChild("artifactId", null);
return getTextFromChild(dom, "artifactId", null);
}

@Override
public String getVersion() {
return getTextFromChild("version", null);
return getTextFromChild(dom, "version", null);
}

@Override
public String getArtifactType() {
return getTextFromChild("type", "jar");
return getTextFromChild(dom, "type", "jar");
}

@Override
public String getClassifier() {
return getTextFromChild("classifier", "");
}

private String getTextFromChild(String childName, String defaultValue) {
for (Element element : dom.getChildren(childName)) {
return element.getNormalizedText();
}
if (defaultValue != null) {
return defaultValue;
}
throw new TargetDefinitionSyntaxException("Missing child element '" + childName + "'");
return getTextFromChild(dom, "classifier", "");
}

@Override
Expand All @@ -282,6 +310,34 @@ public String toString() {
return builder.toString();
}

@Override
public boolean isIgnored(IArtifactFacade artifact) {
return parent.globalExcludes.contains(getKey(artifact));
}

}

private static String getTextFromChild(Element dom, String childName, String defaultValue) {
for (Element element : dom.getChildren(childName)) {
return element.getNormalizedText();
}
if (defaultValue != null) {
return defaultValue;
}
throw new TargetDefinitionSyntaxException("Missing child element '" + childName + "'");
}

private static String getKey(IArtifactFacade artifact) {
if (artifact == null) {
return "";
}
String key = artifact.getGroupId() + ":" + artifact.getArtifactId();
String classifier = artifact.getClassifier();
if (classifier != null && !classifier.isBlank()) {
key += ":" + classifier;
}
key += ":" + artifact.getVersion();
return key;
}

public class IULocation implements TargetDefinition.InstallableUnitLocation {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 Christoph Läubrich and others.
* Copyright (c) 2020, 2021 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
Expand All @@ -15,9 +15,11 @@
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.execution.MavenSession;
Expand All @@ -29,6 +31,7 @@
import org.eclipse.sisu.equinox.embedder.EmbeddedEquinox;
import org.eclipse.sisu.equinox.embedder.EquinoxLifecycleListener;
import org.eclipse.tycho.core.maven.MavenArtifactFacade;
import org.eclipse.tycho.core.shared.MavenArtifactRepositoryReference;
import org.eclipse.tycho.core.shared.MavenDependenciesResolver;
import org.eclipse.tycho.p2.metadata.IArtifactFacade;

Expand All @@ -46,7 +49,7 @@ public class MavenDependenciesResolverConfigurer extends EquinoxLifecycleListene

@Override
public Collection<?> resolve(String groupId, String artifactId, String version, String packaging, String classifier,
String dependencyScope) {
String dependencyScope, Collection<MavenArtifactRepositoryReference> additionalRepositories) {
Artifact artifact;
if (classifier != null && !classifier.isEmpty()) {
artifact = repositorySystem.createArtifactWithClassifier(groupId, artifactId, version, packaging,
Expand All @@ -61,7 +64,13 @@ public Collection<?> resolve(String groupId, String artifactId, String version,
request.setOffline(session.isOffline());
request.setLocalRepository(session.getLocalRepository());
request.setResolveTransitively(dependencyScope != null && !dependencyScope.isEmpty());
request.setRemoteRepositories(session.getCurrentProject().getRemoteArtifactRepositories());
List<ArtifactRepository> repositories = new ArrayList<>(
session.getCurrentProject().getRemoteArtifactRepositories());
for (MavenArtifactRepositoryReference reference : additionalRepositories) {
repositories.add(
repositorySystem.createArtifactRepository(reference.getId(), reference.getUrl(), null, null, null));
}
request.setRemoteRepositories(repositories);
ArtifactResolutionResult result = repositorySystem.resolve(request);
Set<Artifact> artifacts = result.getArtifacts();
ArrayList<IArtifactFacade> list = new ArrayList<IArtifactFacade>();
Expand Down
Loading

0 comments on commit 4043bcb

Please sign in to comment.