1818import java .util .logging .Logger ;
1919
2020import org .apache .commons .lang3 .StringUtils ;
21+ import org .eclipse .core .resources .IBuildConfiguration ;
2122import org .eclipse .core .resources .IMarker ;
2223import org .eclipse .core .resources .IProject ;
2324import org .eclipse .core .resources .IResource ;
25+ import org .eclipse .core .resources .IResourceStatus ;
2426import org .eclipse .core .resources .IWorkspaceRoot ;
2527import org .eclipse .core .resources .IncrementalProjectBuilder ;
2628import org .eclipse .core .resources .ResourcesPlugin ;
4042public class Compile {
4143 private static final Logger logger = Logger .getLogger (Configuration .LOGGER_NAME );
4244
43- public static BuildWorkspaceStatus compile (CompileParams params , IProgressMonitor monitor ) {
45+ private static final int GRADLE_BS_COMPILATION_ERROR = 100 ;
46+
47+ public static Object compile (CompileParams params , IProgressMonitor monitor ) {
48+ IProject mainProject = params == null ? null : ProjectUtils .getProject (params .getProjectName ());
49+ if (mainProject == null ) {
50+ try {
51+ // Q: is infer project by main class name necessary? perf impact?
52+ List <IJavaProject > javaProjects = ResolveClasspathsHandler .getJavaProjectFromType (params .getMainClass ());
53+ if (javaProjects .size () == 1 ) {
54+ mainProject = javaProjects .get (0 ).getProject ();
55+ }
56+ } catch (CoreException e ) {
57+ JavaLanguageServerPlugin .logException ("Failed to resolve project from main class name." , e );
58+ }
59+ }
60+
61+ if (isBspProject (mainProject )) {
62+ // Just need to trigger a build for the target project, the Gradle build server will
63+ // handle the build dependencies for us.
64+ try {
65+ ResourcesPlugin .getWorkspace ().build (
66+ new IBuildConfiguration []{mainProject .getActiveBuildConfig ()},
67+ IncrementalProjectBuilder .INCREMENTAL_BUILD ,
68+ false /*buildReference*/ ,
69+ monitor
70+ );
71+ } catch (CoreException e ) {
72+ if (e .getStatus ().getCode () == IResourceStatus .BUILD_FAILED ) {
73+ return GRADLE_BS_COMPILATION_ERROR ;
74+ } else {
75+ return BuildWorkspaceStatus .FAILED ;
76+ }
77+ }
78+ return BuildWorkspaceStatus .SUCCEED ;
79+ }
80+
4481 try {
4582 if (monitor .isCanceled ()) {
4683 return BuildWorkspaceStatus .CANCELLED ;
@@ -55,7 +92,6 @@ public static BuildWorkspaceStatus compile(CompileParams params, IProgressMonito
5592 }
5693 logger .info ("Time cost for ECJ: " + (System .currentTimeMillis () - compileAt ) + "ms" );
5794
58- IProject mainProject = params == null ? null : ProjectUtils .getProject (params .getProjectName ());
5995 IResource currentResource = mainProject ;
6096 if (isUnmanagedFolder (mainProject ) && StringUtils .isNotBlank (params .getMainClass ())) {
6197 IType mainType = ProjectUtils .getJavaProject (mainProject ).findType (params .getMainClass ());
@@ -117,6 +153,11 @@ private static boolean isUnmanagedFolder(IProject project) {
117153 && ProjectUtils .isJavaProject (project );
118154 }
119155
156+ private static boolean isBspProject (IProject project ) {
157+ return project != null && ProjectUtils .isJavaProject (project )
158+ && ProjectUtils .hasNature (project , "com.microsoft.gradle.bs.importer.GradleBuildServerProjectNature" );
159+ }
160+
120161 private static IProject getDefaultProject () {
121162 return getWorkspaceRoot ().getProject (ProjectsManager .DEFAULT_PROJECT_NAME );
122163 }
0 commit comments