Transcript Gradle
Specially in android studio
Gradle
combines the power and flexibility of
Ant with the dependency management and
conventions of Maven into a more effective
way to build.
download
the ZIP file to your machine. You
should have a file named gradle-2.0-all.zip.
Expand it to a folder of your choice.
Groovy as you know is one of the most popular and
powerful languages available on the JVM
It is less verbose
It is very flexible
It lets you configure and dictate things the way you want
Groovy plays a big role in making the above points happen.
Unlike XML that is typically used in Ant and Maven, Groovy
is a high level language that not only cuts down on the
verbosity with its intuitive and productive syntax but it
also gives you full programmatic power to tweak / specify
things
for all purposes, you can use various build.gradle
templates that we shall look at and while it contains the
Groovy DSL, you need not know much about it or for all
you care, not know anything about it.
Maven
<!-- The smallest possible Maven POM.xml -->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.tricode.academy.samples</groupId>
<artifactId>sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>
Gradle
apply plugin: 'java'
gradle
-q help
gradle -q tasks
gradle properties
we
will essentially work
with build.gradle file to create / use all
sorts of plugins/tasks that will help us
compile, build , test and run our Java
applications.
task compileTask << { System.out.println
"compiling..." }
Gradle
gradle <task>
gradle -q compileTask
compiling...
defaultTasks
'buildTask‘
task compileTask << { System.out.println
"compiling..." }
task buildTask (dependsOn:compileTask) <<
{ System.out.println "building..." }
A plugin is a mechanism by which we can
extend the capabilities of Gradle. Gradle
need to know how to build everything and
anything for us. But we can make that
possible if we know a certain process and can
create pre-built tasks that it can then
perform for us. This is the role of a plugin.
we will use great Gradle plugins that are
available to us to do our job. These plugins
will add tasks that we can invoke
straightaway.
this
plugin adds the following capabilities to
a project:
compilation
testing
Bundling
The bundling typically would mean a JAR file in most
cases.
Any
plugin that you need to use should be
added to the build.gradle file via the
following statement:
apply
plugin: <plugin-name>
apply plugin: "java“
he
plugin will add a bunch of tasks that know
how to deal with typical Java projects.
Build
tasks -----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this
project and all projects that depend on it.
buildNeeded - Assembles and tests this project
and all projects it depends on.
classes - Assembles classes 'main'.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main
classes.
testClasses - Assembles classes 'test'.
It identies the source i.e. grouping for your source
files that need to be compiled and built together,
some sort of a logical grouping or component.
he Java plugin defines two standard source sets,
called main and test.
The main source set contains your production source
code, which is compiled and assembled into a JAR file.
The test source set contains your unit test source code,
which is compiled and executed using JUnit or TestNG.
Put all your Java files (full package,etc) into
the src/main/java folder
Put all your Test files (JUnit stuff) into
the src/main/test folder
What
JAR files are needed to compile the
code ?
Where to find those JAR files ?
Enter
the world of Repositories in Gradle. A
Repository in Gradle is a location where
Gradle can locate the JAR files in our case.
Gradle supports popular public repositories
like Maven Central, Ivy and even your local
repositories if you want.
add
the following element to
your build.gradle file:
repositories { mavenCentral() }
This will make Gradle look for any dependency (JAR
files) that you specify in the Maven Central Repository.
Each
file when published to Maven Central or
any repository for that matter will have the
following characteristics:
A Group Id (group)
An Artifact Id (name)
A Version (version)
A
Configuration is simply a named set of
dependencies. You can use them to declare
the external dependencies of your project.
apply plugin: 'java' version = '1.0-FINAL'
repositories { mavenCentral() }
dependencies { compile group:
'org.apache.commons', name: 'commons-lang3',
version: '3.3.2' }
Each
dependency is defined separately.
You specify first the configuration name
i.e. compile in our case. Then you mention
the library via its group, name and version as
earlier mentioned.
Currently there is only one compile time
dependency shown. But we can add more
dependencies too. Just add them on each
line.
There
is also a short form of specifying the
dependency library. Instead of using
group:value,name:value,version:value, you
can just give the value in the form ‘groupvalue:name-value:version-value’
Example:
dependencies { compile
'org.apache.commons:commons-lang3:3.3.2' }
you
will have multiple Java projects that
could be dependent on each other. For e.g.
You could have a library project where you
write some utility classes and another Java
project that depends on it.
Example:
javaprojects
|- api
|- common
|- app
common: This project contains some utility code
and hence it will not depend on any of the other
projects. Do note that this does not mean that
common does not have dependencies on external
libraries for compilation. So that concept that
we saw in the earlier chapter still remains. It
may depend on 3rd party JARs.
api : This project contains some API code and it
depends on common project
app : This project contains the application code
and it depends on api and common projects
The
first thing you should think of is a
central place to control the overall build for
all the 3 projects. Gradle makes this easy for
you by asking you to create
asettings.gradle file in the root folder.
The
settings.gradle file is shown below:
include ":api", ":common", ":app"
subprojects {
apply plugin: "java"
repositories { mavenCentral()
}
dependencies {
testCompile "junit:junit:4+"
}
}
//Common Project specific stuff
project(':common') {
dependencies {
compile 'org.apache.commons:commonslang3:3.3.2'
}
}
//API Project specific stuff
project(':api') {
dependencies {
compile project(':common')
compile 'org.apache.commons:commonslang3:3.3.2‘
compile 'log4j:log4j:1.2.17'
}
}
//App Project specific stuff
project(':app') {
dependencies {
compile project(':common'),project(':api')
compile 'log4j:log4j:1.2.17'
}
}
create
a single large build.gradle file in the
root folder that will:
Apply common code within
the subprojects closure
Contain individual project(<projectname>) closu
res that will define tasks specific to that project,
its own dependencies, etc.
to
break them into
multiple build.gradle files and each of these
specific build.gradle files will be present in
the respective root folders of the 3 projects
i.e. app, common and api.
http://rominirani.com/2014/07/28/gradle-
tutorial-series-an-overview/
http://www.gradle.org/