在完成前面几讲之后,后面的貌似就没有那么轻松了,本讲讲结合java插件来介绍gradle在java中的使用
1.添加java插件
2.添加依赖仓库
3.添加编译引用
4.添加自定义jar包的引用
5.定制MANIFEST.MF文件
6.jar包的发布
1.添加java插件
apply plugin: 'java'
就这一句话,我们就已经将java插件引入了,输入:wudideMacBook-Pro:gradle ***$ gradle -q tasks
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
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'.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles classes 'test'.
Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]
Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.
Help tasks
----------
components - Displays the components produced by root project 'gradle'. [incubating]
dependencies - Displays all dependencies declared in root project 'gradle'.
dependencyInsight - Displays the insight into a specific dependency in root project 'gradle'.
help - Displays a help message.
model - Displays the configuration model of root project 'gradle'. [incubating]
projects - Displays the sub-projects of root project 'gradle'.
properties - Displays the properties of root project 'gradle'.
tasks - Displays the tasks runnable from root project 'gradle'.
Verification tasks
------------------
check - Runs all checks.
clean - Deletes the build directory.
test - Runs the unit tests.
Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.
以上的所有task启示都是java插件定义好的,我们可以直接使用就可以了
2.添加依赖仓库
repositories {
mavenCentral()
}
这里是添加了Maven仓库,我们在使用的时候,就可以在这里进行下载查找了
http://repo1.maven.org/maven2/
3.添加编译引用
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
这个就是添加依赖文件,这个可不是本地的依赖啊,而是去我们之前添加的依赖仓库中去查找,也就是说需要在build的时候,联网的,输入:wudideMacBook-Pro:gradle ***$ gradle build
:compileJava
Download https://repo1.maven.org/maven2/commons-collections/commons-collections/3.2/commons-collections-3.2.pom
Download https://repo1.maven.org/maven2/commons-collections/commons-collections/3.2/commons-collections-3.2.jar
> Building 0% > :compileJava > 47 KB/557 KB downloaded ....
但只需要下载一次,下载过之后,以后就会用本地的了。
4.添加自定义jar包的引用
dependencies {
compile files('libs/dex.jar')
}
5.定制MANIFEST.MF文件
sourceCompatibility = 1.7
version = '3.0'
jar {
manifest{
attributes 'Implementation-Title':'Gradle Quickstart','Implementation-Version':version
}
}
sourceCompatibility 是设置我们的java编译版本,当电脑中有多个版本时,我们可以指定
manifest中就很简单了,就是设置manifest中的值
version这个是制定当时编译出来的版本,如果是3.0 则编译出来的jar包就叫:gradle-3.0.jar
再来看一下我们生成jar包的manifest文件吧
6.jar包的发布
关于jar包的发布,我们每次编译完成自动在build文件中就会生成jar包,但这里介绍另外一种
uploadArchives {
repositories{
flatDir {
dirs 'out'
}
}
}
注意,这个方法名字是固定的,但是dirs这个是制定我们的输出目录,这个名字是可以随便取的,最后编译完成生成以下文件:
wudideMacBook-Pro:out ***$ ls
gradle-3.0.jar gradle-3.0.jar.sha1 ivy-3.0.xml ivy-3.0.xml.sha1
当然你还有可以将生成的jar包直接发布到远程的地点,这里就不介绍了。