Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle + Buildship - Switch dependency between JAR and projects

I'm having some trouble configuring Buildship for Eclipse the way I want. I currently have > 50 projects always open in Eclipse, but I want to move to having only the projects I am actively working on in Eclipse, and the other projects would use a Maven repository to resolve their dependency.

Lets say ProjectA (which contains a main) depends on ProjectB (a library project). If ProjectB is opened in Eclipse, I would like ProjectA to use ProjectB directly. A change in the code in ProjectB would be noticeable when running ProjectA. However, if ProjectB is closed, I would like ProjectA to use the ProjectB's JAR located in the Maven repository mentioned in the build.gradle file.

The behavior that I am talking about is detailed here.

Is there a way to do that using Buildship? Or should I use another Gradle Eclipse plugin?

like image 260
GammaOmega Avatar asked Sep 16 '15 12:09

GammaOmega


People also ask

How do you add dependency from one project to another in Gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build. gradle file. This declares a dependency on an Android library module named "mylibrary" (this name must match the library name defined with an include: in your settings.

How does Gradle resolve transitive dependencies?

Transitive dependencyBy default, Gradle resolves transitive dependencies automatically. The version selection for transitive dependencies can be influenced by declaring dependency constraints.

What is subprojects in Gradle?

The subproject producer defines a task named buildInfo that generates a properties file containing build information e.g. the project version. You can then map the task provider to its output file and Gradle will automatically establish a task dependency.


2 Answers

  1. There's a new composite build support feature added in Gradle 3.1. This feature is a game changer and makes it simple to work on more than one project at once.

  2. You can use dependency substitution rules to swap out repository dependencies with local project dependencies.

  3. If each project is within it's own separate git/subversion repository you can use prezi pride to manage the 'pride' of projects. You could import the (dynamically generated) multi module build into buildship.

  4. If you wanted to use the eclipse plugin instead of buildship you use the whenMerged or withXml hooks to tweak the generated .classpath files to point to the projects within your workspace (note eclipse will now build differently to gradle command line).

like image 103
lance-java Avatar answered Oct 14 '22 19:10

lance-java


For the sake of completion, I ended up going with the dependency substitution as suggested by Lance Java. This approach has the following advantages:

  • Does not require a third party software.
  • Is IDE independent. We're not tinkering with the .classpath file directly, we let the Eclipse plugin (or any other IDE plugin) handle this.
  • Other plugins can access the actual dependency we want to use.

However, there are some gotchas with that approach:

  • Dependency substitution cannot be done inside a task. It needs to be done either in the build.gradle file or in a method that is called from the build.gradle file.
  • You need to refresh the workspace.
like image 41
GammaOmega Avatar answered Oct 14 '22 17:10

GammaOmega