Dylan
Dylan

Reputation: 59

Begin with gradle: Directory structure

I am a beginner with gradle and i need help please. If i want my own libs and extern libs (vendor) where shoud i put these libs (extern and personnal)? thank you for helping !

(example: a screenshot of my current dir structure) enter image description here

Upvotes: 1

Views: 2239

Answers (1)

Bjørn Vester
Bjørn Vester

Reputation: 7598

Gradle gives you a few different options for this.

In all cases, there is no fixed folder structure for local file dependencies, and you are free to chose what you like. libs seems fine to me.

All examples below are in Groovy DSL. Refer to the Gradle user guide for the Kotlin DSL variants.

File dependencies

To add dependencies from a local folder, you can use the Project.file() or Project.fileTree() methods. Both take a path that is resolved relative to the project directory.

dependencies {
    implementation files('libs/a.jar')                  // Single file
    implementation files('libs/b.jar', 'libs/c.jar')    // Multiple files
    implementation fileTree('libs')                     // All files in a folder
    implementation fileTree('libs') { include '*.jar' } // All jar files in a folder
}

See File dependencies in the user guide for more information.

Flat directory repository

You can also declare a flat directory repository. Here the path is relative to where you invoke Gradle from, so to make it consistent, you should make it absolute (using projectDir or rootDir).

repositories {
    flatDir name: 'local-libs', dirs: "$projectDir/libs" // The name is optional
}

You can then declare dependencies using the normal format with a group, name and version. However, as group is ignored so you can either write whatever you like, or just leave it out:

dependencies {
    implementation 'mysql:mysql-connector-java:5.1.49' // Real Maven coordinates
    implementation ':mysql-connector-java:5.1.49'      // Short-hand for local coordinates
}

See this section for more information.

Maven repositories and separate Gradle projects

Because the above two methods do not include module metadata, meaning you have to define and include all transitive dependencies manually, it can quickly get hairy to maintain.

If possible, try and use Maven repositories. Especially for third-party dependencies. You can even define a local repository to take advantage of metadata files (e.g. .pom files or .module) if you can't use remote ones like Maven Central or JCenter.

When depending on other Gradle projects, if they are related, you could possibly structure them as a multi-projects instead of building and putting them into a "libs" folder.

If they are not related, you could also publish them to a Maven repository (either local or remote), or you could look into composite builds, though this is a bit of an advanced topic.

A note on the MySQL Connector software license

Lastly, in case you are not aware, the community edition of MySQL Connector/J version 5.x is licensed under GPL v2. It means your own application also needs to be licensed under GPL v2.

Later versions like 8.x (I am unsure about 6 and 7) are licensed dually under GPL v2, but has a provision called The Universal FOSS Exception that allows you to link and use the library in your own application without affecting your own license.

If this is just a hobby project, no one will come knowing on your door if you breach the license. But if it is for a company, be careful or you might bring it into legal trouble. Oracle does audits from time to time and they are not known to let an opportunity slide for slapping you in the face with a big license fee.

Upvotes: 1

Related Questions