本地磁盘maven仓库的使用

本文介绍了如何将Gradle插件与AspectJ库打包并存入本地Maven仓库,以便在其他项目中引用。通过在模块的build.gradle中添加配置,执行特定任务生成库文件,然后在需要的项目中指定仓库位置和引用库包。以PluginDemo和AspectJPlugin为例,详细阐述了每个步骤,包括创建插件、定义AspectJ编译规则、生成和上传到本地仓库的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最后编辑于2019年5月4日

以之前插件化的例子PluginDemo做个开场。

Step1:在需要制作成库的module的build.gradle中添加

apply plugin: 'maven'
...
uploadArchives {
    repositories.mavenDeployer {
        //本地磁盘仓库路径,以放到项目根目录下的 repo 的文件夹为例
        repository(url: uri('../repo'))

        //groupId ,自行定义
        pom.groupId = 'com.example.xydzjnq'

        //artifactId
        pom.artifactId = 'mypluginlibrary'

        //插件版本号
        pom.version = '1.0.0'
    }
}

Step2:打开右侧Gradle窗口,执行对应task;执行完成后会在对应目录下生成对应库文件

Step3:在需要的module中指定maven仓库位置,并引用其中的库包,如:

 

 

再举一个gradle plugin的例子GradlePlugin

该例子使用了AspectJ提供的独特的ajc编译器对使用该插件的模块进行编译。ajc会在编译期将Aspect代码插入到PointCut中,从而达到AOP的目的。

Step1:新建plugin模块,修改模块的build.gradle(引入对应dependencies)为

apply plugin: 'groovy'

dependencies {
    implementation gradleApi()
    implementation localGroovy()

    implementation "org.aspectj:aspectjtools:1.9.0"
    implementation "org.aspectj:aspectjrt:1.9.0"
}
repositories {
    jcenter()
}

Step2:新建plugin模块,创建groovy目录,并新建一个实现Plugin<Project>接口的类(该类apply方法中会指定执行一些task,我会指定其在项目编译时执行ajc编译织入增强(Advice)代码)。

 对应AspectJPlugin代码如下:

public class AspectJPlugin implements Plugin<Project> {
    void apply(Project project) {
        final def log = project.logger

        project.dependencies {
            implementation 'org.aspectj:aspectjrt:1.8.10'
        }

        if (project.android.hasProperty('applicationVariants')) {
            project.android.applicationVariants.all { variant ->
                JavaCompile javaCompile = variant.javaCompile

                javaCompile.doLast {
                    String[] args = [
                            "-showWeaveInfo",
                            "-1.7",
                            "-inpath", javaCompile.destinationDir.toString(),
                            "-aspectpath", javaCompile.classpath.asPath,
                            "-d", javaCompile.destinationDir.toString(),
                            "-classpath", javaCompile.classpath.asPath,
                            "-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)
                    ]
                    MessageHandler handler = new MessageHandler(true);
                    new Main().run(args, handler);

                    for (IMessage message : handler.getMessages(null, true)) {
                        switch (message.getKind()) {
                            case IMessage.ABORT:
                            case IMessage.ERROR:
                            case IMessage.FAIL:
                                log.error message.message, message.thrown
                                break;
                            case IMessage.WARNING:
                                log.warn message.message, message.thrown
                                break;
                            case IMessage.INFO:
                                log.info message.message, message.thrown
                                break;
                            case IMessage.DEBUG:
                                log.debug message.message, message.thrown
                                break;
                        }
                    }
                }
            }
        }

        if (project.android.hasProperty('libraryVariants')) {
            project.android.libraryVariants.all { variant ->
                JavaCompile javaCompile = variant.javaCompile

                javaCompile.doLast {
                    String[] args = [
                            "-showWeaveInfo",
                            "-1.7",
                            "-inpath", javaCompile.destinationDir.toString(),
                            "-aspectpath", javaCompile.classpath.asPath,
                            "-d", javaCompile.destinationDir.toString(),
                            "-classpath", javaCompile.classpath.asPath,
                            "-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)
                    ]
                    MessageHandler handler = new MessageHandler(true);
                    new Main().run(args, handler);

                    for (IMessage message : handler.getMessages(null, true)) {
                        switch (message.getKind()) {
                            case IMessage.ABORT:
                            case IMessage.ERROR:
                            case IMessage.FAIL:
                                log.error message.message, message.thrown
                                break;
                            case IMessage.WARNING:
                                log.warn message.message, message.thrown
                                break;
                            case IMessage.INFO:
                                log.info message.message, message.thrown
                                break;
                            case IMessage.DEBUG:
                                log.debug message.message, message.thrown
                                break;
                        }
                    }
                }
            }
        }
    }
}

 Step3:新建properties文件
在resources/META-INF/grdle-plugins目录下创建一个aspectjplugin.properties文件,其中aspectjplugin就是以后引用的插件名。

Step4:修改模块的build.gradle,将该插件上传到本地库

apply plugin: 'groovy'
apply plugin: 'maven'

dependencies {
    implementation gradleApi()
    implementation localGroovy()

    implementation "org.aspectj:aspectjtools:1.9.0"
    implementation "org.aspectj:aspectjrt:1.9.0"
}
repositories {
    jcenter()
}

uploadArchives {
    repositories.mavenDeployer {
        //本地仓库路径,以放到项目根目录下的 repo 的文件夹为例
        repository(url: uri('../repo'))

        //groupId ,自行定义
        pom.groupId = 'com.example.xydzjnq'

        //artifactId
        pom.artifactId = 'autotrack'

        //插件版本号
        pom.version = '1.0.0'
    }
}

 Step5:打开右侧Gradle窗口,执行对应task;执行完成后会在对应目录下生成对应库文件

Step6:在需要使用该插件的project 的build.gradle中引入该插件

 Step7:在需要该插件编译的模块apply plugin:

 

 下面是一些开源的aspect插件:
Hugo

gradle-android-aspectj-plugin

gradle_plugin_android_aspectjx

 

待续。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值