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 24, 2020
1 parent 86fb065 commit 53fa063
Show file tree
Hide file tree
Showing 25 changed files with 538 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@
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 static Preferences getPreferences() {
return (JavaLanguageServerPlugin.getPreferencesManager() == null || JavaLanguageServerPlugin.getPreferencesManager().getPreferences() == null) ? new Preferences() : JavaLanguageServerPlugin.getPreferencesManager().getPreferences();
}

protected File rootFolder;

@Override
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,6 +33,8 @@
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;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;

public class EclipseProjectImporter extends AbstractProjectImporter {

Expand All @@ -43,6 +45,13 @@ 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
Preferences preferences = getPreferences();
for (IProject project : ProjectUtils.getAllProjects()) {
if ((preferences.isImportGradleEnabled() && ProjectUtils.isGradleProject(project)) || (preferences.isImportMavenEnabled() && ProjectUtils.isMavenProject(project))) {
String path = project.getLocation().toOSString();
eclipseDetector.addExclusions(path);
}
}
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 @@ -98,6 +98,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 +328,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 @@ -113,6 +113,18 @@ 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;
}
}
}
return excluded;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,12 @@ 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) {
List<IProjectImporter> importers = new ArrayList<>(importers());
IProjectImporter importer = getImporter(rootFolder, importers, subMonitor.split(30));
while (importer != null) {
importer.importToWorkspace(subMonitor.split(70));
importers.remove(importer);
importer = getImporter(rootFolder, importers, subMonitor.split(30));
}
}
}
Expand Down Expand Up @@ -266,8 +269,8 @@ 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();
private IProjectImporter getImporter(File rootFolder, List<IProjectImporter> importers, IProgressMonitor monitor) throws OperationCanceledException, CoreException {
// Collection<IProjectImporter> importers = importers();
SubMonitor subMonitor = SubMonitor.convert(monitor, importers.size());
for (IProjectImporter importer : importers) {
importer.initialize(rootFolder);
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>
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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 53fa063

Please sign in to comment.