Gradle编译工具:
Gradle是一款依赖工具,项目通过建立依赖关系实现组件化,让每个模块单独出来,降低耦合性。设想如果所有的模块都放在同一个项目里面,那么一个人改动的时候其他所有人都需要作出相应的改动来适配内容。通过依赖,可以实现代码上的解耦,每个模块独立开发。
项目依赖与模块依赖
项目在构建的过程中需要添加对于某些功能包的依赖,但是如果每次都打成一个jar包的形式,每次改动都需要重新打包,重新导入,很麻烦。
比如说我们在项目中有一些模块需要依赖外部工具模块,那么可以通过项目首先依赖该工具模块模块,然后自己的模块再对工具模块进行依赖即可。如果工具类有更新,我们也只需要在项目build.gradle里面进行文字修改即可更新自己的依赖。
- Project
- module1
- build.gradle #模块依赖配置
- module2
- build.gradle
- module3
- build.gradle
- gradle.properties
settings.gradle #如果本地模块需要在这里添加
build.gradle #项目依赖配置
自定义Gradle插件(plugin)
在Gradle配置项目的过程中,会执行Groovy逻辑,因此在gradle配置编码的过程中,我们可以通过修改Java字节码的方式像代码中插入代码-插桩。插桩会涉及到自定义Gradle插肩的技术,展开对自定义插件做说明
一个自定义插件的样式如下图所示
首先随便建立一个项目,然后把 /src 文件夹 以及build.gradle文件 之外的所有内容都删除
打开build.gradle添加内容
build.gradle
apply plugin: 'groovy' //这是一个groovy项目
apply plugin: 'maven' //会用到maven plugin
dependencies {
//gradle sdk compile gradleApi()
//groovy sdk compile localGroovy()
compile 'com.android.tools.build:gradle:3.1.3' #gradle library
compile 'org.javassist:javassist:3.27.0-GA' #javassist的library
}
//group和version在后面使用自定义插件的时候会用到
group='com.gavin.plugin' //本地发布的时候的组名
version='0.1.1' //本地发布的版本号
uploadArchives {
repositories {
mavenDeployer {
//提交到远程服务器:
// repository(url: "http://www.xxx.com/repos") {
// authentication(userName: "admin", password: "admin")
// }
//本地的Maven地址:当前工程下
// repository(url: uri('./my-plugin'))
repository(url: uri('/Users/ronghua/.m2/repository')) //maven发布的位置
}
}
}
gradle.properties
在项目路径新建gradle.properties文件
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
GROUP=com.gavin.plugin #上传到网络仓库的组名
POM_ARTIFACT_ID=project_name #项目名称
VERSION_NAME=0.0.29 # 版本号
#在引用的时候就是 implement 'com.gavin.plugin:project_name:0.0.29'
POM_NAME=cdc_auto
POM_DESCRIPTION=meituan check tool
POM_PACKAGING=jar
做完上面两个步骤之后,就可以开始添加文件了。当前module文件路径应该是这样的(我们的module名字叫testplugin)
- testplugin
- src
- main
-java
- build.gradle
- gradle.properties
然后我们就可以开始添加插件内容了
添加插件逻辑以及配置
首先在main文件夹新建一个groovy文件夹,并添加一个package,然后在路径下new一个文件 LifeCyclePlugintest.groovy
- main
- groovy
- com.ronghua.plugintest
- LifeCyclePlugintest.groovy
groovy文件
class LifeCyclePlugintest implements Plugin<Project>{
@Override
void apply (Project project){
System.out.println("Hello Gradle!")
}
}
在apply里面就可以添加自己需要的Gradle内容了,可以是复杂的功能:比如注册插桩需要的Transform,但这不在今天的讨论范围。
最后我们还需要添加一个文件。在main文件夹下新建./resources/META-INF/gradle-plugins路径,然后添加一个xxx.properties的文件,这里我们命名为myplugin.properties,看起来是这样的
- main
- groovy
- java
- resources
- META-INF.gradle-plugins
- myplugin.properties
# 其中myplugin.properties 的内容只有一行:
implementation-class=com.ronghua.plugintest.LifeCyclePlugin
不难理解,我们这个plugin使用的是com.ronghua.plugintest.LifeCyclePlugin这个文件
至此,我们的Gradle插件就完成啦!
应用plugin的时候需要在工程的build.gradle里面添加classpath
如果我们发送到的是本地的mavenLocal,那么在工程build.gradle的dependencies 里面添加classpath应该使用的是我们组件build.gradle里面定义的 组 跟 版本 classpath 'com.gavin.plugin:testplugin:0.1.1'
如果我们发送到的是某个网络Maven仓库,那么在工程build.gradle添加classpath使用的是在gradle.properties 因此classpath应该如下:classpath 'com.gavin.plugin:project_name:0.0.29'
添加完工程依赖之后,还要将插件应用到项目上。在需要我们插件的项目build.gradle里面添加 apply plugin: 'myplugin' 这个plugin名称取决于resourses文件夹里面的xxx.properties(xxx就是你plugin的名称)
P.S.
-
什么是plugin与library?一般来说添加依赖的时候,plugin的项目依赖需要在项目模块的build.gradle添加,而library的依赖可以在具体的module里面添加
- plugin一般来说是一些工具,在编译阶段我们希望通过plugin这个工具实现一些功能,例如往代码里面插入一些自定义内容,然后在电脑上编译解析生成新的代码 通常来说,plugin会是插桩,或者检查的工具,在编译的过程中进行内容模改
- library是别人写好的内容,我们只是想调用里面的类,方法,因此我们是完全不需要去编译,这些library已经是经过编译的考验,可以正常运行
-
Groovy代码没什么报错机制,因此很容易写错内容,要谨慎再谨慎!
-
resources里面的META-INF.gradle-plugins 路径名只能是这个,一个符号都不能差!
Maven
这里提到的Maven主要涉及到仓库的配置
一般来说Idea下载之后在路径下自带有Maven工具,当然也可以选择自己下载Maven工具。
MavenLocal()仓库的默认路径在Windows 与 Mac 下都是 ${user.home}/.m2/repository或 ~/.m2/repository
如果在使用MavenLocal的时候希望默认能寻找到.m2路径,需要确保.m2文件夹下有个settings.xml文件
Idea自带的settings.xml路径:
/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/conf/settings.xml
打开settings.xml,找到
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
我们可以看到默认的默认的地址是${user.home}/.m2/repository 我们将这个文件拷贝到${user.home}/.m2路径
然后修改
<localRepository>/path/to/local/repo<localRepository>
为
<localRepository>/{user.home}/.m2/repository/<localRepository>
那AndroidStudio 或者 IDEA怎么找到这个文件呢?
Android Studio MavenLocal的配置里是这样说的
* The location for the repository is determined as follows (in order of precedence):
* </p>
* <ol>
* <li>The value of system property 'maven.repo.local' if set;</li>
* <li>The value of element <localRepository>~/.m2/settings.xml</localRepository> if this file exists and element is set;</li>
* <li>The value of element <localRepository>$M2_HOME/conf/settings.xml</localRepository> (where <code>$M2_HOME</code> is the value of the environment variable with that name) if this file exists and element is set;</li>
* <li>The path ~/.m2/repository </li>
优先级从高到低
1、如果设置了系统property,就会使用 'maven.repo.local'
2、其次使用~/.m2/settings.xml里面配置的路径localRepository
所以我们配置了.m2/settings.xml之后mavenLocal就能正常找到默认.m2 的repository仓库了,同理如果希望将mavenLocal配置在其他地方,以后只要修改.m2的settings.xml文件内容就可以了
小结
关于Gradle与Maven的一些配置今天就聊到这里,后续关于插桩的问题再做总结