Skip to content

Instantly share code, notes, and snippets.

@enriched
Last active November 3, 2020 14:16
Show Gist options
  • Save enriched/b0a685e124a2abff3ac92bf3a5e63630 to your computer and use it in GitHub Desktop.
Save enriched/b0a685e124a2abff3ac92bf3a5e63630 to your computer and use it in GitHub Desktop.
Generate Eclipse project for Bazel workspace (works with vscode-java)
#!/usr/bin/env bash
set -euo pipefail
cd $(bazel info workspace)
WORKSPACE_NAME=$(basename $PWD)
cat << EOF > ./.project
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<projectDescription>
<name>${WORKSPACE_NAME}</name>
<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>
EOF
JARS=$(find ./bazel-${WORKSPACE_NAME}/external/maven/v1 -iname '*.jar')
SRC_PATHS=$(find . -type d -path '*/src/main/java' -o -path '*/src/test/java')
cat << EOF > ./.classpath
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
$( for PATH in $SRC_PATHS; do echo " <classpathentry kind=\"src\" path=\"${PATH:2}\" />"; done; )
<classpathentry kind="output" path="java-bin"/>
$( for JAR in $JARS; do echo " <classpathentry kind=\"lib\" path=\"${JAR:2}\" />"; done; )
</classpath>
EOF
@walles
Copy link

walles commented Nov 3, 2020

I made some changes here: https://gist.github.com/walles/581c50278fdf9d50a482104366aaa82e

Changes include:

  • Add non-Maven dependencies to .classpath
  • Add generated code dependencies to .classpath
  • Fix shellcheck complaints

After my changes I was able to successfully open https://github.com/bazelbuild/bazel-buildfarm in vscode.

If this was a project I'd make a PR, not sure how to work with Gists.

And really nice work Rich! Looks like you found the MIT advice :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment