Search code examples
eclipsegitgradlegradle-eclipsebuildship

Do dependencies need to be added through both the Eclipse GUI and the Gradle configuration files?


Two Gradle Git projects need to be created, within the same repository, using the Eclipse IDE. Project A depends upon a third-party library (a JAR, source code, Javadoc and natives) and project B depends upon project A.

Without Gradle, both projects would be created and Git would be enabled with Project Explorer > [Project Name] > Team > Share Project. Then, dependencies would be added via Project Explorer > [Project Name] > Build Path > Configure Build Path, followed by Projects > Add and Libraries > Add External JARs (which would also allow the specification of the locations of related source code, Javadoc and natives). This would enable automatic code completion of Project A from Project B as well as source code and Javadoc integration, within Eclipse.

With Gradle, should the settings.gradle or build.gradle files be manually edited, without adding dependencies through the Eclipse GUI, or must both be used simultaneously? Additionally,how does this effect which hidden files should be indexed by Git (.project, .classpath, .settings, .gradle)?

How is this setup through Eclipse and Gradle?


Solution

  • When you use a build-tool like Gradle (or maven), it is kind of assumed that the build-tool is in charge of configuring stuff like project dependencies and classpaths.

    This kind of conflicts with the Eclipse UI which is kind of built to allow you to manage classpath / dependencies via its own ui.

    But the Eclipse UI only controls what eclipse uses as the classpath when the Eclipse JDT compiler compiles your code... inside Eclipse.

    So if you change things that way, then Gradle will not know about these dependencies and the build won't work.

    Yep, this is definitely confusing :-)

    The proper thing to do is manage your dependencies and project configuration entirely through gradle. So that means editing the build.gradle and settings.gradle.

    The tools (BuildShip or STS Gradle Tools) provide a 'bridge' to try and configure Eclipse projects in conformance with your build.

    E.g they may offer a 'Update Project' or 'Refresh Dependencies' command in project context menu.

    Even if you don't use Gradle tooling, this kind of holds true. You then would use Gradle's 'ecplise' plugin and run a command like

       gradle cleanEclipse eclipse
    

    To generate the eclipse project config from the build config. Then import the project into Eclipse as configured by gradle. Also in this case, it would be a bad idea to use Eclipse UI to make changes to the buildpath since ultimately it has the same issue, changes you make to those generated files may make things compile inside Eclipse, but Gradle doesn't know you changed anything. And the next time you run gradle cleanEclipse eclipse your changes are also blown away.

    As to your specific questions:

    should the settings.gradle or build.gradle files be manually edited ...

    Yes.

    ... without adding dependencies through the Eclipse GUI ...

    Yes.

    ... or must both be used simultaneously?

    No, only configure things in gradle. Then 'synch it up' to eclipse with some tool (BuildShip / STS Gradle / gradle cleanEclipse eclipse)

    How does this effect which hidden files should be indexed by Git (.project, .classpath, .settings, .gradle)?

    General rule of thumb. Only index the stuff that defines gradle's behaviour (there may be some exceptions, but in general try to minimize them only violate this rule if you have a good reason).

    So specifically don't put in git these 'eclipse metadata'

    • .settings
    • .project
    • .classpath

    Do put in git: the gradle wrapper and its properties file.

    Gradle also has a .gradle folder. It belongs to gradle, not eclipse, buts is caches and things which are 'transient'. You don't want those in git either.