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!");
}
}
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!
Cheers!
Top comments (7)
The answer the first question, the only thing you need for compiling and building a java app is the
java
plugin. You can have abuild.gradle
file with only the following line: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/....
I tried the first thing already, it throws an error, and I was advised to use:
Is this (not) correct?
Yes, this is correct. Both:
and
are importing the
java
plugin for your project. From there you can usegradle build
to build your project.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 toapply( )
?Thanks for your help so far.
Cheers.
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.Sorry I'm late to the party!
Let me give some
not requestedcontext 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
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:
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:
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 includesjava
plugin and also gives you the possibility of configure where yourmain
is located, once configured, you will have a new task for running thatmain
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!
Thanks for this.
I wanted to respond sooner but didn't find the time.
Have a wonderful day.
Cheers.