Compile gradle project with another project as a dependency

12 07 2014

imagesHaving project dependant on another project is common situation. How to configure gradle so that it will include your dependency project in build process?

There are two cases:

1. Your project is a root project and dependency is under its root

When dependand project is under root in a directory structure (dependency is not being shared with any other project except this one) you are doing it in recommended gradle strategy :)

To wrap up: you have directory structure like this:

Project
  |--build.gradle
  |--settings.gradle
  |--Dependency
  |    |--build.gradle

Then to add Dependency to Project, you need to have Project/settings.gradle content like this:

include ':Dependency'

and in a Project/build.gradle dependencies section you need to compile the dependent project by adding:

dependencies {
   compile project(':Dependency')
}

2. You have two independent projects and you need to use one of them as a dependency

When both project are on the same level in a directory structure you are not doing it as gradle team wish, but it is more real world situation in my opinion :) This is because you have your library project that can be easily used in several other projects as a dependency.

So you have directory structure like this:

Project
  |--build.gradle
  |--settings.gradle
Dependency
  |--build.gradle

To add Dependency to the Project, you need to include it, and show Dependency path manually. So the Project/settings.gradle content should be like this:

include ':Dependency'
project(':Dependency').projectDir = new File(settingsDir, '../Dependency')

and in a Project/build.gradle dependencies section you need to compile the dependent project by adding:

dependencies {
   compile project(':Dependency')
}

Notice that tis is build.gradle is exactly the same as in previous section.

Did I help you?
I manage this blog and share my knowledge for free, sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!

Advertisements




Incorporate Maven into Eclipse project

28 04 2012

Here is short instruction how to incorporate Maven into existing Eclipse projects from scratch, step by step. The goal of Maven introduction presented here is quite generic, however I installed Maven to manage dependencies (jars). Maven build, installs or clean functions are not used by me. If You need to know more about maven itself – this screencast is good start.

1. Install eclipse plugin for maven
I recommend m2eclipse as it is most popular and works quite well. It is supported by Eclipse: 3.6 Galileo and 3.7 Indigo. Install it by ‘Install new software‘ Eclipse option. Update site link you will find on this site.

This will install its own Maven instance, so you don’t need to install Maven yourself – m2eclipse will handle it. However manual Maven installation is necessary if You need another version that is provided in m2eclipse by default.

Some very good introduction about m2eclipse is here. If m2eclipse doesn’t suit you, then you can find another plugins comparison here. The best one decided on StackOverflow.com is however m2eclipse.

2. Update existing project with maven functionality
(If You are creating new project use Eclipse -> New Project and choose type: Maven. Params to enter are the same as described below)

To create pom.xml file and tell to m2eclipse to use this file:

Right click on project -> configure -> Convert to maven project

On the project configuration dialog You need to fill in these fields:

  • groupId – in fact this is main package name that is used in eclipse project. Param states that project belongs to some group, pointed by package name.
  • artifactId – project name that is used in Eclipse. This will be the name of the file that will be produced by Maven after compilation (e.g. name of external jar)
  • archetype – project type. This is similar to creating new project in Eclipse, where You choose on of the project types at the beginning (Android project / Java project / Dynamic Web Project etc.). Like Eclipse project types, Maven provides its archetypes. Choosing proper one, eases project configuration. Maven preconfigures its structure and pom.xml file.
  • dependencies – list of all jars or another workspace projects required for Mavenized project to run. Referenced workspace projects should also be maven ones.

3. Updating Maven project configuration
After making changes in Maven config (pom.xml file) you will have to update project configuration to reflect these changes in project.
Do it by:

Right Click on th project -> Maven -> Update project configuration

4. That’s all

On another computer, where this project is used it is enough to install m2eclipse (point 1.) and trigger ‘Update Project Configuration’ (point 3.).

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!








%d bloggers like this: