Search in sources :

Example 1 with ArtifactKey

use of org.eclipse.m2e.core.embedder.ArtifactKey in project bndtools by bndtools.

the class AbstractMavenRepository method guessBundleFile.

File guessBundleFile(IMavenProjectFacade projectFacade) {
    String buildDirectoryGuess;
    IProject project = projectFacade.getProject();
    IPath outputLocation = projectFacade.getOutputLocation();
    if (outputLocation.segment(0).equals(project.getFullPath().segment(0))) {
        outputLocation = outputLocation.removeFirstSegments(1);
    }
    IFolder folder = project.getFolder(outputLocation.toString());
    if (folder.exists()) {
        outputLocation = folder.getLocation();
    }
    if (outputLocation != null) {
        if (outputLocation.lastSegment().equals("classes")) {
            outputLocation = outputLocation.removeLastSegments(1);
        }
        buildDirectoryGuess = outputLocation.toOSString();
    } else {
        buildDirectoryGuess = projectFacade.getProject().getLocation().toOSString() + "/target";
    }
    ArtifactKey artifactKey = projectFacade.getArtifactKey();
    String finalNameGuess = buildDirectoryGuess + "/" + artifactKey.getArtifactId() + "-" + artifactKey.getVersion() + ".jar";
    return new File(finalNameGuess);
}
Also used : ArtifactKey(org.eclipse.m2e.core.embedder.ArtifactKey) IPath(org.eclipse.core.runtime.IPath) File(java.io.File) IProject(org.eclipse.core.resources.IProject) IFolder(org.eclipse.core.resources.IFolder)

Example 2 with ArtifactKey

use of org.eclipse.m2e.core.embedder.ArtifactKey in project eclipse.jdt.ls by eclipse.

the class DependencyUtil method getArtifact.

public static File getArtifact(String groupId, String artifactId, String version, String classifier) throws FileNotFoundException, CoreException {
    ArtifactKey key = new ArtifactKey(groupId, artifactId, version, classifier);
    File archive = getLocalArtifactFile(key);
    if (archive == null) {
        Artifact artifact = MavenPlugin.getMaven().resolve(key.getGroupId(), key.getArtifactId(), key.getVersion(), "jar", key.getClassifier(), null, new NullProgressMonitor());
        if (artifact == null) {
            throw new FileNotFoundException("Unable to find " + key);
        }
        archive = getLocalArtifactFile(key);
    }
    return archive;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ArtifactKey(org.eclipse.m2e.core.embedder.ArtifactKey) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact)

Example 3 with ArtifactKey

use of org.eclipse.m2e.core.embedder.ArtifactKey in project eclipse.jdt.ls by eclipse.

the class MavenSourceDownloader method discoverSource.

@Override
public void discoverSource(IClassFile classFile, IProgressMonitor monitor) throws CoreException {
    if (classFile == null) {
        return;
    }
    IJavaElement element = classFile;
    while (element.getParent() != null) {
        element = element.getParent();
        if (element instanceof IPackageFragmentRoot) {
            final IPackageFragmentRoot fragment = (IPackageFragmentRoot) element;
            IPath attachmentPath = fragment.getSourceAttachmentPath();
            if (attachmentPath != null && !attachmentPath.isEmpty() && attachmentPath.toFile().exists()) {
                break;
            }
            if (fragment.isArchive()) {
                IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(fragment.getPath());
                IPath path = file.getFullPath();
                if (path == null || !path.toFile().exists()) {
                    path = file.getLocation();
                    if (path == null) {
                        return;
                    }
                }
                Boolean downloaded = downloadRequestsCache.getIfPresent(path.toString());
                if (downloaded == null) {
                    downloadRequestsCache.put(path.toString(), true);
                    ArtifactKey artifact = new MavenPropertiesIdentifier().identify(path, monitor);
                    if (artifact == null) {
                        artifact = new MavenCentralIdentifier().identify(path, monitor);
                    }
                    if (artifact != null) {
                        if (!ProjectUtils.isMavenProject(element.getJavaProject().getProject())) {
                            // see https://github.com/eclipse-m2e/m2e-core/commit/b547ecc358c990e182a5eaf8d36f121e43f4a8c9#diff-3967743078be6a24ba1e3ec28bfc22bdf2c88a740695411f6d20e2444fef042fR943
                            long lastModified;
                            try {
                                File artifactFile = DependencyUtil.getArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getClassifier());
                                lastModified = artifactFile.lastModified();
                            } catch (FileNotFoundException | CoreException e1) {
                                lastModified = -1;
                            }
                            if (lastModified > -1) {
                                try {
                                    File sources = DependencyUtil.getSources(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
                                    sources.setLastModified(lastModified - 1);
                                } catch (FileNotFoundException | CoreException e) {
                                // ignore
                                }
                                try {
                                    File javadoc = DependencyUtil.getJavadoc(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
                                    javadoc.setLastModified(lastModified - 1);
                                } catch (FileNotFoundException | CoreException e) {
                                // ignore
                                }
                            }
                        }
                        BuildPathManager buildpathManager = (BuildPathManager) MavenJdtPlugin.getDefault().getBuildpathManager();
                        buildpathManager.scheduleDownload(fragment, artifact, true, true);
                        JobHelpers.waitForDownloadSourcesJobs(MAX_TIME_MILLIS);
                    }
                }
                break;
            }
        }
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IFile(org.eclipse.core.resources.IFile) ArtifactKey(org.eclipse.m2e.core.embedder.ArtifactKey) IPath(org.eclipse.core.runtime.IPath) FileNotFoundException(java.io.FileNotFoundException) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) CoreException(org.eclipse.core.runtime.CoreException) BuildPathManager(org.eclipse.m2e.jdt.internal.BuildPathManager) IClassFile(org.eclipse.jdt.core.IClassFile) File(java.io.File) IFile(org.eclipse.core.resources.IFile)

Example 4 with ArtifactKey

use of org.eclipse.m2e.core.embedder.ArtifactKey in project eclipse.jdt.ls by eclipse.

the class MavenCentralIdentifier method extractKey.

private ArtifactKey extractKey(JsonObject modelNode) {
    JsonObject response = modelNode.getAsJsonObject("response");
    if (response != null) {
        int num = response.get("numFound").getAsInt();
        if (num > 0) {
            JsonArray docs = response.getAsJsonArray("docs");
            String a = null, g = null, v = null;
            for (JsonElement d : docs) {
                JsonObject o = d.getAsJsonObject();
                a = o.get("a").getAsString();
                g = o.get("g").getAsString();
                v = o.get("v").getAsString();
                if (a != null && g != null && v != null) {
                    return new ArtifactKey(g, a, v, null);
                }
            }
        }
    }
    return null;
}
Also used : JsonArray(com.google.gson.JsonArray) ArtifactKey(org.eclipse.m2e.core.embedder.ArtifactKey) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject)

Example 5 with ArtifactKey

use of org.eclipse.m2e.core.embedder.ArtifactKey in project eclipse.jdt.ls by eclipse.

the class MavenPropertiesIdentifier method getArtifactFromPomProperties.

private ArtifactKey getArtifactFromPomProperties(ZipFile jar) throws IOException {
    // $NON-NLS-1$
    ZipEntry mavenEntry = jar.getEntry("META-INF/maven");
    if (mavenEntry == null) {
        return null;
    }
    String jarName = jar.getName();
    String entryName = mavenEntry.getName();
    Enumeration<? extends ZipEntry> zipEntries = jar.entries();
    ArtifactKey artifact = null;
    while (zipEntries.hasMoreElements()) {
        ZipEntry zipEntry = zipEntries.nextElement();
        if (zipEntry.getName().endsWith("pom.properties") && zipEntry.getName().startsWith(entryName)) {
            Properties props = new Properties();
            props.load(jar.getInputStream(zipEntry));
            String groupId = props.getProperty("groupId");
            String artifactId = props.getProperty("artifactId");
            String version = props.getProperty("version");
            String classifier = props.getProperty("classifier");
            if (groupId != null && artifactId != null && version != null) {
                ArtifactKey currentArtifact = new ArtifactKey(groupId, artifactId, version, classifier);
                if (artifact == null) {
                    artifact = currentArtifact;
                } else {
                    // eg. org.fusesource.jansi:jansi:1.6
                    if (jarName.contains(artifactId + "-" + version)) {
                        artifact = currentArtifact;
                    }
                }
            }
        }
    }
    return artifact;
}
Also used : ArtifactKey(org.eclipse.m2e.core.embedder.ArtifactKey) ZipEntry(java.util.zip.ZipEntry) Properties(java.util.Properties)

Aggregations

ArtifactKey (org.eclipse.m2e.core.embedder.ArtifactKey)5 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)2 IPath (org.eclipse.core.runtime.IPath)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 Properties (java.util.Properties)1 ZipEntry (java.util.zip.ZipEntry)1 Artifact (org.apache.maven.artifact.Artifact)1 IFile (org.eclipse.core.resources.IFile)1 IFolder (org.eclipse.core.resources.IFolder)1 IProject (org.eclipse.core.resources.IProject)1 CoreException (org.eclipse.core.runtime.CoreException)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 IClassFile (org.eclipse.jdt.core.IClassFile)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)1 BuildPathManager (org.eclipse.m2e.jdt.internal.BuildPathManager)1