Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save walles/581c50278fdf9d50a482104366aaa82e to your computer and use it in GitHub Desktop.
Save walles/581c50278fdf9d50a482104366aaa82e 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')
CLASS_DIRS=$(find -L bazel-bin/external -type d -name '*_classes')
GENERATED_CLASS_DIRS=$(find -L bazel-bin/src -name '*_classes'|grep -v '/main/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; )
$( for CLASS_DIR in $CLASS_DIRS; do echo " <classpathentry kind=\"lib\" path=\"${CLASS_DIR}\" />"; done; )
$( for CLASS_DIR in $GENERATED_CLASS_DIRS; do echo " <classpathentry kind=\"lib\" path=\"${CLASS_DIR}\" />"; done; )
</classpath>
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment