Skip to content

Commit

Permalink
Importing mixed (maven,gradle,eclipse) projects
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <[email protected]>
  • Loading branch information
snjeza committed Aug 28, 2020
1 parent 45363e4 commit 2472e55
Show file tree
Hide file tree
Showing 28 changed files with 578 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@
package org.eclipse.jdt.ls.core.internal;

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

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;

public abstract class AbstractProjectImporter implements IProjectImporter {

protected File rootFolder;
protected Collection<java.nio.file.Path> directories;

@Override
public void initialize(File rootFolder) {
Expand All @@ -34,10 +37,19 @@ public void initialize(File rootFolder) {
@Override
public abstract boolean applies(IProgressMonitor monitor) throws OperationCanceledException, CoreException;

@Override
public boolean isResolved(File folder) throws OperationCanceledException, CoreException {
return directories != null && directories.contains(folder.toPath());
};

@Override
public abstract void importToWorkspace(IProgressMonitor monitor) throws OperationCanceledException, CoreException;

@Override
public abstract void reset();

protected static Preferences getPreferences() {
return (JavaLanguageServerPlugin.getPreferencesManager() == null || JavaLanguageServerPlugin.getPreferencesManager().getPreferences() == null) ? new Preferences() : JavaLanguageServerPlugin.getPreferencesManager().getPreferences();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public interface IProjectImporter {

boolean applies(IProgressMonitor monitor) throws OperationCanceledException, CoreException;

default boolean isResolved(File folder) throws OperationCanceledException, CoreException {
return false;
};

void importToWorkspace(IProgressMonitor monitor) throws OperationCanceledException, CoreException;

void reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ public static IProject[] getAllProjects() {
return ResourcesPlugin.getWorkspace().getRoot().getProjects();
}

public static IProject[] getAllProjects(boolean includeInvisibleProjects) {
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
if (includeInvisibleProjects) {
return projects;
} else {
return Stream.of(projects).filter(p -> isVisibleProject(p)).collect(Collectors.toList()).toArray(new IProject[0]);
}
}

public static IProject getProject(String projectName) {
if (StringUtils.isBlank(projectName)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public Collection<Path> scan(IProgressMonitor monitor) throws CoreException {
}

private void scanDir(Path dir, final IProgressMonitor monitor) throws IOException {
FileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {
FileVisitor<Path> visitor = new SimpleFileVisitor<>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
if (monitor.isCanceled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
import static org.eclipse.core.resources.IProjectDescription.DESCRIPTION_FILE_NAME;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.core.resources.IProject;
Expand All @@ -32,17 +35,32 @@
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.ls.core.internal.AbstractProjectImporter;
import org.eclipse.jdt.ls.core.internal.IProjectImporter;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.ProjectUtils;

public class EclipseProjectImporter extends AbstractProjectImporter {

private Collection<java.nio.file.Path> directories;

@Override
public boolean applies(IProgressMonitor monitor) throws CoreException {
if (directories == null) {
BasicFileDetector eclipseDetector = new BasicFileDetector(rootFolder.toPath(), DESCRIPTION_FILE_NAME)
.addExclusions("**/bin");//default Eclipse build dir
List<IProjectImporter> importers = new ArrayList<>(ProjectsManager.importers());
for (IProject project : ProjectUtils.getAllProjects(false)) {
File projectFile = project.getLocation().toFile();
Iterator<IProjectImporter> iter = importers.iterator();
while (iter.hasNext()) {
IProjectImporter importer = iter.next();
if (!(importer instanceof EclipseProjectImporter)) {
importer.initialize(projectFile);
if (importer.isResolved(rootFolder) || importer.applies(monitor)) {
eclipseDetector.addExclusions(projectFile.getAbsolutePath());
break;
}
}
}
}
directories = eclipseDetector.scan(monitor);
}
directories = directories.stream().filter(path -> (new File(path.toFile(), IJavaProject.CLASSPATH_FILE_NAME).exists())).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.io.FilenameFilter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -79,8 +78,6 @@ public class GradleProjectImporter extends AbstractProjectImporter {
.replaceAll("\n", System.lineSeparator());
//@formatter:on

private Collection<Path> directories;

/* (non-Javadoc)
* @see org.eclipse.jdt.ls.core.internal.managers.IProjectImporter#applies(org.eclipse.core.runtime.IProgressMonitor)
*/
Expand All @@ -98,6 +95,12 @@ public boolean applies(IProgressMonitor monitor) throws CoreException {
.includeNested(false)
.addExclusions("**/build")//default gradle build dir
.addExclusions("**/bin");
for (IProject project : ProjectUtils.getAllProjects()) {
if (!ProjectUtils.isGradleProject(project)) {
String path = project.getLocation().toOSString();
gradleDetector.addExclusions(path);
}
}
directories = gradleDetector.scan(monitor);
}
return !directories.isEmpty();
Expand Down Expand Up @@ -322,8 +325,4 @@ public boolean accept(File dir, String name) {
public void reset() {
}

private static Preferences getPreferences() {
return (JavaLanguageServerPlugin.getPreferencesManager() == null || JavaLanguageServerPlugin.getPreferencesManager().getPreferences() == null) ? new Preferences() : JavaLanguageServerPlugin.getPreferencesManager().getPreferences();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,5 @@ public static IPath tryResolveSourceDirectory(IPath javaFile, IPath rootPath, St
String packageName = getPackageName(javaFile, rootPath, potentialSrcPrefixes);
return inferSourceDirectory(javaFile.toFile().toPath(), packageName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,18 @@ public boolean applies(IProgressMonitor monitor) throws OperationCanceledExcepti
return false;
}
Set<MavenProjectInfo> files = getMavenProjectInfo(monitor);
directories = new ArrayList<>();
if (files != null) {
Iterator<MavenProjectInfo> iter = files.iterator();
while (iter.hasNext()) {
MavenProjectInfo projectInfo = iter.next();
File dir = projectInfo.getPomFile() == null ? null : projectInfo.getPomFile().getParentFile();
if (dir != null && exclude(dir.toPath())) {
iter.remove();
continue;
}
if (dir != null) {
directories.add(dir.toPath());
}
}
}
Expand All @@ -113,6 +118,19 @@ private boolean exclude(java.nio.file.Path path) {
}
}
}
if (!excluded) {
for (IProject project : ProjectUtils.getAllProjects()) {
if (ProjectUtils.isMavenProject(project)) {
continue;
}
String pattern = project.getLocation().toOSString();
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
if (matcher.matches(path)) {
excluded = true;
break;
}
}
}
return excluded;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,14 @@ protected void importProjects(Collection<IPath> rootPaths, IProgressMonitor moni
SubMonitor subMonitor = SubMonitor.convert(monitor, rootPaths.size() * 100);
for (IPath rootPath : rootPaths) {
File rootFolder = rootPath.toFile();
IProjectImporter importer = getImporter(rootFolder, subMonitor.split(30));
if (importer != null) {
importer.importToWorkspace(subMonitor.split(70));
for (IProjectImporter importer : importers()) {
importer.initialize(rootFolder);
if (importer.applies(subMonitor.split(1))) {
importer.importToWorkspace(subMonitor.split(70));
if (importer.isResolved(rootFolder)) {
break;
}
}
}
}
}
Expand Down Expand Up @@ -266,23 +271,11 @@ public boolean isBuildLikeFileName(String fileName) {
return buildSupports().filter(bs -> bs.isBuildLikeFileName(fileName)).findAny().isPresent();
}

private IProjectImporter getImporter(File rootFolder, IProgressMonitor monitor) throws OperationCanceledException, CoreException {
Collection<IProjectImporter> importers = importers();
SubMonitor subMonitor = SubMonitor.convert(monitor, importers.size());
for (IProjectImporter importer : importers) {
importer.initialize(rootFolder);
if (importer.applies(subMonitor.split(1))) {
return importer;
}
}
return null;
}

public static IProject getDefaultProject() {
return getWorkspaceRoot().getProject(DEFAULT_PROJECT_NAME);
}

private Collection<IProjectImporter> importers() {
public static Collection<IProjectImporter> importers() {
Map<Integer, IProjectImporter> importers = new TreeMap<>();
IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(IConstants.PLUGIN_ID, "importers");
IConfigurationElement[] configs = extensionPoint.getConfigurationElements();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public void registerWatchers(boolean runInJob) {

@Override
public List<FileSystemWatcher> registerWatchers() {
logInfo(">> registerFeature 'workspace/didChangeWatchedFiles'");
logInfo(">> registerWatchers'");
if (preferenceManager.getClientPreferences().isWorkspaceChangeWatchedFilesDynamicRegistered()) {
Set<String> patterns = new LinkedHashSet<>(basicWatchers);
buildSupports().forEach(e -> e.getWatchPatterns().forEach(patterns::add));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>salut-java13</name>
<name>salut-java14</name>
<comment></comment>
<projects>
</projects>
Expand Down
6 changes: 6 additions & 0 deletions org.eclipse.jdt.ls.tests/projects/mixed/hello/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions org.eclipse.jdt.ls.tests/projects/mixed/hello/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>hello</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Empty file.
26 changes: 26 additions & 0 deletions org.eclipse.jdt.ls.tests/projects/mixed/salut/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<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>foo.bar</groupId>
<artifactId>salut</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.sample;

/**
* This is Bar
*/
public class Bar {

public static void main(String[] args) {
System.out.print( "Hello world! from "+Bar.class);
}

public static interface MyInterface {

void foo();
}

public static class MyClass {

void bar() {}
}

public static final String EMPTY = "";


public enum Foo {
Bar, Zoo
}
}
Empty file.
23 changes: 23 additions & 0 deletions org.eclipse.jdt.ls.tests/projects/mixed/simple-gradle/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>simple-gradle</name>
<comment>Project simple-gradle created by Buildship.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
</projectDescription>
Loading

0 comments on commit 2472e55

Please sign in to comment.