DEV Community

Calin Baenen
Calin Baenen

Posted on

Two Gradle questions I have.

Being new to Gradle (and build tools in general), I have a few specific questions centered around my first experience, and how to navigate this world.




What is the minimum required Groovy is required to make a test build.gradle?


What is the minimum amount needed in a build.gradle to run a test? I have:

import java.lang.*;

public final class Test {
    public static void main(String[] args) {
        System.out.println("Test!");
    }
}
Enter fullscreen mode Exit fullscreen mode

but what do I actually do to build this?


What are some guides that break things down piece by piece (1:1)?


I like how sites like MDN break things down in HTML and JS, and (basically) have tutorials for each method/keyword/language concept, and would like to know, is there any site/guide/tutorial that does this for Gradle?

Thanks!
Cheers!

Top comments (7)

Collapse
 
ehudon profile image
ehudon

The answer the first question, the only thing you need for compiling and building a java app is the java plugin. You can have a build.gradle file with only the following line:

apply plugin: 'java'
Enter fullscreen mode Exit fullscreen mode

From here, you can decide to add other plugins for packaging, for a specific IDE, etc.

For the second question, the gradle documentation is well done. I would start here docs.gradle.org/current/userguide/....

Collapse
 
baenencalin profile image
Calin Baenen

I tried the first thing already, it throws an error, and I was advised to use:

plugins {
    id "java"
}
Enter fullscreen mode Exit fullscreen mode

Is this (not) correct?

Collapse
 
ehudon profile image
ehudon

Yes, this is correct. Both:

apply plugin: 'java'
Enter fullscreen mode Exit fullscreen mode

and

plugins {
    id "java"
}
Enter fullscreen mode Exit fullscreen mode

are importing the java plugin for your project. From there you can use gradle build to build your project.

Thread Thread
 
baenencalin profile image
Calin Baenen

Do I need both, or just the bottom one (since the first one gives me an error (Why does it do that?))?
How do I structure it, the plugins block, then the call to apply( )?

Thanks for your help so far.
Cheers.

Thread Thread
 
ehudon profile image
ehudon • Edited

No what you have above is enough, you only need the lines above in a file named build.gradle. You will need to use gradle conventions which are the same as maven. I created a gist for you here gist.github.com/ehudon/7379f19f33b.... You can copy this file in your project and it should work if you have the correct project layout.

Collapse
 
sierisimo profile image
Sinuhe Jaime Valencia

Sorry I'm late to the party!

Let me give some not requested context and then, we move to your question.

Gradle is a build tool of "general purpose" which means it doesn't have an specific way for build stuff, so how does it build things? with plugins! Each plugin has their own set of definitions and tasks that helps you build stuff, officially Gradle has support for

  • Java (and some JVM languages like Groovy, Kotlin and Scala)
  • C++
  • Swift
  • Partial support for NodeJS

So for any kind of thing you want to build, you will need a plugin or define a task that gives gradle context/idea on how to build things.

Using plugins

For adding an official plugin in your project, you have 2 ways of doing it:

The old way:

apply plugin: NAME_OF_THE_PLUGIN

The fancy and new way:

plugins {
   java
}
Enter fullscreen mode Exit fullscreen mode

The main difference is that the old way allows you to apply more things than just plugins (other build files for example) while the new way only support applying plugins already present and defined in the classpath (if you don't understand what it is, just leave as "official plugins")

You have the option for adding plugins and leverage the build/test process to them. In your case you have 2 options:

  • java
  • application

For the case of the java plugin, this is the simplest plugin for java projects, it requires a lot of manual configurations and gives you the basics of building java.

application is a fancier plugin, that includes java plugin and also gives you the possibility of configure where your main is located, once configured, you will have a new task for running that main method using:

gralde run

Is a more suitable plugin as it adds some useful tasks for testing and running java code.

You can add multiple plugins to your build process, and even create your own, there's also a plugin search page and the official docs that sometime are a little hard to navigate...

Hope this helps!

Collapse
 
baenencalin profile image
Calin Baenen

Thanks for this.
I wanted to respond sooner but didn't find the time.

Have a wonderful day.
Cheers.