Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing mixed (maven,gradle,eclipse) projects #1532

Merged
merged 1 commit into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -33,16 +33,19 @@
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.ls.core.internal.AbstractProjectImporter;
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
for (IProject project : ProjectUtils.getAllProjects(false)) {
snjeza marked this conversation as resolved.
Show resolved Hide resolved
File projectFile = project.getLocation().toFile();
eclipseDetector.addExclusions(projectFile.getAbsolutePath());
}
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) {
snjeza marked this conversation as resolved.
Show resolved Hide resolved
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;
snjeza marked this conversation as resolved.
Show resolved Hide resolved
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
}
}
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>
48 changes: 48 additions & 0 deletions org.eclipse.jdt.ls.tests/projects/mixed/simple-gradle/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* This build file was auto generated by running the Gradle 'init' task
* by 'fbricon' at '24/10/16 11:28' with Gradle 3.0
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* user guide available at https://docs.gradle.org/3.0/userguide/tutorial_java_projects.html
*/

// Apply the java plugin to add support for Java
apply plugin: 'java'
apply plugin: 'eclipse'

sourceCompatibility = 1.7

// In this section you declare where to find the dependencies of your project
repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}

// In this section you declare the dependencies for your production and test code
dependencies {
// The production code uses the SLF4J logging API at compile time
compile 'org.slf4j:slf4j-api:1.7.21'

// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile 'junit:junit:4.12'
}

eclipse {
classpath {
defaultOutputDir = file('target')
file {
whenMerged {
cp -> cp.getEntries().forEach{
cpEntry -> if(cpEntry.kind=='src') {
cpEntry.output = cpEntry.output.replace('bin/', 'target/')
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#
Binary file not shown.
Loading