New to gradle , please advise


(Vishal) #1

Hi All,

This is my first post . Apologies , if i sound stupid.

I have worked with make , ant and maven in past but now as it’s gradle craze everywhere . I am trying to learn it.

I have started with user-guide ( very big one ) and practicing in parallel . I am finding it difficult in terms of syntax and worried about retaining lot of information . Sometimes i feel discouraged to move ahead but then i muster my courage , as no way out :stuck_out_tongue:

No doubt , user guide is great masterpiece and quite interesting .

I am curious to know , how others learn and start with gradle ? Did you also find it tough in first attempt ?

I would be grateful , if you could please share your experiences and journey.

Thank you so much in advance .

P.S : Admins , could you please move my post to relevant forum , if this is not right place to ask for.


(Benjamin Muschko) #2

You will likely find the getting started guides very useful. Give it a shot!


(Vishal) #3

Thank you Benjamin!

First of all , apologies for delayed response!

i have made one-pass to user-guide somehow ( 600+ pages , God bless me! :slight_smile: ) to understand what gradle is all about. Trust me few chapters are very tricky and difficult specially “VI. THE SOFTWARE MODEL” etc.

Now have started going through guides ( and practicing) you referred above . Very useful .

Now stuck on section “1.4. Reacting to plugins” in https://guides.gradle.org/implementing-gradle-plugins/ . i tried searching web for help but no-luck . Do we have any more tutorials for this piece ?

Could you please suggest.


(Benjamin Muschko) #4

What exactly are you stuck on? The section provides a concrete example. I don’t think there’s any other “good” tutorial I could point you to.


(Vishal) #5

What have we achieved using below ( and how ) :

InhouseConventionJavaPlugin.java
public class InhouseConventionJavaPlugin implements Plugin {
public void apply(Project project) {
project.getPlugins().withType(JavaPlugin.class, new Action() {
public void execute(JavaPlugin javaPlugin) {
JavaPluginConvention javaConvention =
project.getConvention().getPlugin(JavaPluginConvention.class);
SourceSet main = javaConvention.getSourceSets().create(SourceSet.MAIN_SOURCE_SET_NAME);
main.getAllJava().setSrcDirs(Arrays.asList(“src”));
}
}
});
}

over

InhouseConventionJavaPlugin.java
public class InhouseConventionJavaPlugin implements Plugin {
public void apply(Project project) {
project.getPlugins().apply(JavaPlugin.class);
JavaPluginConvention javaConvention =
project.getConvention().getPlugin(JavaPluginConvention.class);
SourceSet main = javaConvention.getSourceSets().create(SourceSet.MAIN_SOURCE_SET_NAME);
main.getAllJava().setSrcDirs(Arrays.asList(“src”));
}
}

I am sorry for sounding stupid . I wish , this section had more explanation .


(Benjamin Muschko) #6

In the code example below you are applying the Java plugin - that means you will automatically introduce all conventions of the Java plugin to the project consuming your plugin. It’s possible that the consuming project isn’t even a Java plugin but you change the flavor of it by making the assumption. In most cases this can be avoided.

project.getPlugins().withType(JavaPlugin.class, ... says: If the consuming project already applies the Java plugin then configure it in a specific way. Otherwise don’t do it.


(Vishal) #7

Thank you again!

My bad! Though , syntax is very difficult :frowning: .

Now i get it , so here we don’t make consuming project --> a java plugin based one , “forcefully” . This code checks if it is a “java plugin based” or not and hence action accordingly.

Interestingly , so this way “I react” to java plugin :smiley: .


(Dimitar Dimitrov) #8

I would recommend that rather than trying to learn Gradle, your shift your goal to achieving something that has meaning in the context of the project.

One good exercise is finding a project with a complex Maven build and trying to replicate the results in Gradle. Then your questions will be a lot more concrete and your knowledge would stick to the practical problems you needed to solve.

Regarding the software model, I found it interesting about 2 years ago and read a bit through the docs to get the idea, but at the time it looked a bit half-baked, so I decided to wait for it to mature and accumulate a critical mass of plugins that I can look to for examples of best practices - I am still waiting :wink: