Java

How to create java project in gradle

By March 23, 2016 November 9th, 2016 No Comments

This page gives an introduction to gradle java project, include java plugin in your gradle configuration file, which adds all required tasks by default.

  1. apply plugin: ‘java’

Folder structure should be similar to maven java project folder structure.

  1. src/main/java
    src/test/java
    src/main/resources
    src/test/resources
    build – all output files will be created here.
    build/lib

Add below configuration to define external dependencies using maven.

      1. repositories {
2.  mavenCentral()
3.   }

4. dependencies {
5. compile group: ‘commons-collections’, name: ‘commons-collections’, version: ‘3.2’
6.   testCompile group: ‘junit’, name: ‘junit’, version: ‘4.+’
7. }

The Java plugin by default adds many properties to your project. You are allowed to change these values. Here is an example to customize your MANIFEST.MF file:

  1. sourceCompatibility = 1.5
  2. version = ‘1.0’
  3. jar {
  4.   manifest {
  5.                 attributes ‘Implementation-Title’: ‘Gradle Quickstart’,
  6.                                     ‘Implementation-Version’: version
  7.         }
  8. }

Here is the complete sample java project configuration using gradle: 

  1. apply plugin: ‘java’
  2. apply plugin: ‘eclipse’
  3. sourceCompatibility = 1.5
  4. version = ‘1.0’
  5. jar {
  6.         manifest {
  7.                 attributes ‘Implementation-Title’: ‘Gradle Quickstart’,
  8.                            ‘Implementation-Version’: version
  9.                 }
  10. }
  11. repositories {
  12.         mavenCentral()
  13. }
  14. dependencies {
  15.         compile group: ‘commons-collections’, name: ‘commons-collections’, version: ‘3.2’
  16.         testCompile group: ‘junit’, name: ‘junit’, version: ‘4.+’
  17. }
  18. test {
  19.         systemProperties ‘property’: ‘value’
  20. }
  21. uploadArchives {
  22.         repositories {
  23.                 flatDir {
  24.                         dirs ‘repos’
  25.                 }
  26.         }
  27. }

Leave a Reply