This repository has been archived by the owner on Feb 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
MavenSourceContainerResolver.java
85 lines (70 loc) · 3.4 KB
/
MavenSourceContainerResolver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*******************************************************************************
* Copyright (c) 2011-2016 Igor Fedorenko
* 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:
* Igor Fedorenko - initial API and implementation
*******************************************************************************/
package com.ifedorenko.m2e.sourcelookup.internal.launch;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.debug.core.sourcelookup.ISourceContainer;
import org.eclipse.debug.core.sourcelookup.containers.ExternalArchiveSourceContainer;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.launching.sourcelookup.containers.JavaProjectSourceContainer;
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.ArtifactKey;
import org.eclipse.m2e.core.embedder.IMaven;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.IMavenProjectRegistry;
import com.ifedorenko.m2e.sourcelookup.internal.jdt.ISourceContainerResolver;
public class MavenSourceContainerResolver implements ISourceContainerResolver {
private static final MavenArtifactIdentifierer INDENTIFIERER = new MavenArtifactIdentifierer();
@Override
public Collection<ISourceContainer> resolveSourceContainers(File classesLocation, IProgressMonitor monitor) {
Collection<ArtifactKey> classesArtifacts = INDENTIFIERER.identify(classesLocation, monitor);
if (classesArtifacts == null) {
return null;
}
List<ISourceContainer> result = new ArrayList<>();
for (ArtifactKey classesArtifact : classesArtifacts) {
ISourceContainer container = resovleSourceContainer(classesArtifact, monitor);
if (container != null) {
result.add(container);
}
}
return result;
}
protected ISourceContainer resovleSourceContainer(ArtifactKey artifact, IProgressMonitor monitor) {
String groupId = artifact.getGroupId();
String artifactId = artifact.getArtifactId();
String version = artifact.getVersion();
IMaven maven = MavenPlugin.getMaven();
IMavenProjectRegistry projectRegistry = MavenPlugin.getMavenProjectRegistry();
IMavenProjectFacade mavenProject = projectRegistry.getMavenProject(groupId, artifactId, version);
if (mavenProject != null) {
return new JavaProjectSourceContainer(JavaCore.create(mavenProject.getProject()));
}
try {
List<ArtifactRepository> repositories = new ArrayList<ArtifactRepository>();
repositories.addAll(maven.getArtifactRepositories());
repositories.addAll(maven.getPluginArtifactRepositories());
if (!maven.isUnavailable(groupId, artifactId, version, "jar", "sources", repositories)) {
Artifact resolve = maven.resolve(groupId, artifactId, version, "jar", "sources", null, monitor);
return new ExternalArchiveSourceContainer(resolve.getFile().getAbsolutePath(), true);
}
} catch (CoreException e) {
// TODO maybe log, ignore otherwise
}
return null;
}
}