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

Preventing .classpath overwrite #285

Open
abarisain opened this issue Aug 23, 2017 · 8 comments
Open

Preventing .classpath overwrite #285

abarisain opened this issue Aug 23, 2017 · 8 comments

Comments

@abarisain
Copy link

abarisain commented Aug 23, 2017

Hello,

While I'm fully aware that Android projects are unsupported, manually crafting .classpath files provide quite satisfying results - with the caveat that they don't support env vars, so you have to hardcode the path to your Android SDK.
For example, with a classpath that looks like:

<?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/"/>
	<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
	<classpathentry kind="output" path="bin"/>
	<classpathentry kind="src" path="src/main/java"/>
	<classpathentry kind="lib" path="/Users/arnaud/dev/android-sdk/platforms/android-26/android.jar" />
</classpath>

I'm able, after indexing, to get completion for my project's classes, but also Android's.
Libraries need a little more work, but I don't mind doing everything manually, as both the platform version and libraries don't change often.

The problem is that vscode-java notices that this is a gradle project, and will overwrite.
I have to rewrite the .classpath with my version every time I reopen vscode.

I'd like to have a setting to tell the plugin never to overwrite .classpath on its own.

Environment
  • Operating System: macOS & Windows
  • JDK version: 1.8
  • Visual Studio Code version: 1.15.1
  • Java extension version: Latest as of writing
Steps To Reproduce
  1. Open a
  2. Patch the .classpath yourself. Make sure you back it up somewhere else
  3. Restart vscode
Current Result

.classpath was overwritten with an empty one

Expected Result

.classpath should have not been overwritten

Additional Informations

I tried setting " "java.configuration.updateBuildConfiguration": "disabled" ", but that does not help

@fbricon
Copy link
Collaborator

fbricon commented Aug 24, 2017

It seems Buildship always overwrites .classpath files on Gradle project import. @donat is there any way (API) to prevent that?

@fbricon
Copy link
Collaborator

fbricon commented Aug 24, 2017

@abarisain have you tried getting gradle to generate a customized eclipse .classpath? See https://docs.gradle.org/4.1/dsl/org.gradle.plugins.ide.eclipse.model.EclipseClasspath.html

@abarisain
Copy link
Author

Hello, thanks for the fast answer!

I've tried to use a Gradle plugin that generates a classpath by reading the Android project, but it fails on some of my projects.

I did not think of using the plugin you've linked. I'll give it a shot, thanks!

@fbricon
Copy link
Collaborator

fbricon commented Aug 29, 2017

Please comment your findings here, that might help other people

@abarisain
Copy link
Author

For now, I can't seem to get this to work. The eclipse plugin throws a null pointer when defining a "file" section, and does not recognize any of the configurations. I also tried a gradle android plugin, which throws an NPE

Note that I'm running the beta Android tools, which work really differently than the old ones. I'm not surprised that it breaks.

I'll try removing write permissions on .classpath. It should work around my problem for the time being.

Thanks for the support!

@donat
Copy link

donat commented Sep 5, 2017

@fbricon Sorry for the late response, I was away for a few weeks.

Buildship always overrides the classpath when a project is refreshed. However, the content of the classpath file can be customized in the eclipse.classpath.file.whenMerged { ... } block. Here are a few examples.

@abarisain
Copy link
Author

abarisain commented Sep 5, 2017

I tried adding a eclipse.classpath.file section again, and this time the NPE is gone. I'm not sure what happened back then.

Anyway, I'll override everything I need in the classpath in the gradle file.

While I still think that it would be handy to force vscode-java to use the .classpath I provide and ignore Gradle, this solution works for me!
#286 mentions that Gradle overwrites the classpath on build, but my Gradle CLI builds don't do that and make their own classpath (Android tools are quite different from standard Gradle stuff).
I also don't need the extension to build my project, I "just" need it to provide code completion.
Discussion might be more appropriate on that issue, but as the build.gradle eclipse plugin solution works for me, I think resources are best spent elsewhere.

Anybody who really wants to prevent .classpath to be overwritten can also use their native OS' features to do so. It worked for me.

I believe this issue can be closed.
Thanks.

@bigtfromaz
Copy link

I was experiencing an issue similar to this and I managed to work around the problem. I had used Spring to generate a basic Rest API app and it created both Maven and Gradle constructs. I am partial to Maven so I updated pom.xml to add some dependencies. Everything seemed fine but, not sure what happened, without warning my local classpath-based compiles stopped working. The dependencies I had added to the classpath had disappeared. In fact the classpath no longer had any Maven references at all, just Gradle. "mvn, clean package" worked at all times.

I searched extensively and could not find a reason this would have occurred. I finally found these two settings for Red Hat Developer and set them as follows in my workspace settings.

"java.import.gradle.enabled": false,
"java.import.maven.enabled": true

Made an innocuous change to the pom.xml file, saved it, and the build returned to normal.

It would appear that the two features are replacing the classpath.

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

No branches or pull requests

4 participants