From cd169fab35f0337ea22a2d7a57610ea4bdba9e9c Mon Sep 17 00:00:00 2001 From: haoge <470368500@qq.com> Date: Fri, 27 Oct 2017 11:10:41 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E4=BC=98=E5=8C=96=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E3=80=82=E5=87=8F=E5=B0=91=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E8=BF=90=E8=A1=8C=E6=97=B6=E9=97=B4=202.=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=8F=AA=E5=9C=A8=E8=BF=9B=E8=A1=8C=E6=89=93?= =?UTF-8?q?=E5=8C=85apk=E6=97=B6=E5=90=AF=E5=8A=A8replace=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E3=80=82=203.=20=E6=94=AF=E6=8C=81upload=E6=97=B6?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=9C=BA=E5=9E=8Bclean=E3=80=82=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E5=81=B6=E5=8F=91=E6=80=A7=E7=9A=84=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=B1=A1=E6=9F=93=E7=8E=B0=E8=B1=A1=204.=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=8F=8C=E5=90=91=E5=8A=A8=E6=80=81=E6=9B=BF=E6=8D=A2=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=EF=BC=9Aproject=20<=3D=3D>=20local=20maven=20url?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/component/gradle/ReplacePlugin.groovy | 50 ++++++++++++-- .../org/component/gradle/RootPlugin.groovy | 36 ++++++++-- .../org/component/gradle/UploadPlugin.groovy | 67 +++++++++---------- 3 files changed, 106 insertions(+), 47 deletions(-) diff --git a/speedup/src/main/groovy/org/component/gradle/ReplacePlugin.groovy b/speedup/src/main/groovy/org/component/gradle/ReplacePlugin.groovy index cb73985..089166f 100644 --- a/speedup/src/main/groovy/org/component/gradle/ReplacePlugin.groovy +++ b/speedup/src/main/groovy/org/component/gradle/ReplacePlugin.groovy @@ -2,24 +2,30 @@ package org.component.gradle import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.api.artifacts.Configuration +import org.gradle.api.artifacts.Dependency import org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency import org.gradle.api.internal.artifacts.dependencies.DefaultProjectDependency import org.gradle.api.logging.LogLevel -public class ReplacePlugin implements Plugin{ +class ReplacePlugin implements Plugin{ @Override void apply(Project project) { - project.configurations.all { configuration -> + RootPlugin.log(LogLevel.LIFECYCLE, "Apply Replace Plugin for ${project.path}") + project.configurations.all { Configuration configuration -> if (configuration.dependencies.size() == 0) { return } + + String[] excludes = project.rootProject.excludes + // compare and replace from project to remote url configuration.dependencies.all { dependency -> if (dependency instanceof DefaultProjectDependency) { - // compare and replace from project to remote url + String name = dependency.dependencyProject.path - if (project.rootProject.excludes.contains(name)) { + if (excludes.contains(name)) { return } @@ -30,11 +36,45 @@ public class ReplacePlugin implements Plugin{ return } configuration.dependencies.remove(dependency) - configuration.dependencies.add(new DefaultExternalModuleDependency("com.local.maven", name, "local")) + Dependency projectModule = new DefaultExternalModuleDependency("com.local.maven", name, "local") + def subs = flatDependencies(new File(project.rootProject.localMaven, "com/local/maven").absolutePath, name) + excludes.each { + if (subs.contains(it)) { + projectModule.exclude group : 'com.local.maven', module: it.replaceAll(':', '-') + project.dependencies.add(configuration.name, project.project(it)) + } + } + configuration.dependencies.add(projectModule) RootPlugin.log("$project.name Replace dependency $dependency.name with local name [com.local.maven:$name:local] successful!") } } } + } + + static List flatDependencies(String localRepo, String name) { + List list = new ArrayList<>() + File repoDir = new File(localRepo, "$name") + if (!repoDir.exists()) { + return list + } + + try { + File file = new File(localRepo, "${name}/local/${name}-local.pom") + Node project = new XmlParser().parse(file) + project.dependencies.dependency.each { + if ('com.local.maven' == it.groupId[0].text() + && 'local' == it.version[0].text()) { + String artifactId = it.artifactId[0].text() + list.add(artifactId.replaceAll('-', ':')) + list.addAll(flatDependencies(localRepo, artifactId)) + } + } + } catch (Exception e) { + // ignore + RootPlugin.log(LogLevel.ERROR, e.getMessage()) + e.printStackTrace() + } + return list } } diff --git a/speedup/src/main/groovy/org/component/gradle/RootPlugin.groovy b/speedup/src/main/groovy/org/component/gradle/RootPlugin.groovy index 970d54b..8241afd 100644 --- a/speedup/src/main/groovy/org/component/gradle/RootPlugin.groovy +++ b/speedup/src/main/groovy/org/component/gradle/RootPlugin.groovy @@ -7,8 +7,11 @@ import org.gradle.api.logging.Logger public class RootPlugin implements Plugin { - static Project root; + Project root + boolean isAssemble + static Logger logger + @Override void apply(Project project) { if (project != project.rootProject) { @@ -16,7 +19,7 @@ public class RootPlugin implements Plugin { } root = project.rootProject logger = project.logger - + parseTasks(project) boolean enable try { enable = parseLocal() @@ -32,21 +35,32 @@ public class RootPlugin implements Plugin { log("exclude module name $it") } - // create uploadAll task - root.tasks.create(name:'uploadAll', group: 'speedup') + // create upload tasks + def uploadAll = root.tasks.create(name:'uploadAll', group: 'speedup') + def uploadForClean = root.tasks.create(name:'uploadForClean', group: 'speedup').doLast { + log(LogLevel.LIFECYCLE, 'clean for upload successful!') + } + uploadAll.dependsOn uploadForClean project.subprojects { - it.plugins.apply(UploadPlugin) + it.afterEvaluate { + it.plugins.apply(UploadPlugin) + if (isAssemble) { + it.plugins.apply(ReplacePlugin) + } + } } } - static boolean parseLocal() { + private boolean parseLocal() { // read local.properties Properties local = new Properties() local.load(root.file('local.properties').newInputStream()) File file = new File(local.get("localRepo", '_repo') as String) + String excludeModules = local.get("excludeModules", "") as String + excludeModules = excludeModules.replaceAll(' ','') root.ext { - excludes = (local.get("excludeModules", "") as String).replaceAll(' ', '').split(',') + excludes = excludeModules.length() == 0 ? [] as String[] : excludeModules.split(',') localMaven = file.absolutePath } @@ -62,4 +76,12 @@ public class RootPlugin implements Plugin { static void log(LogLevel level = LogLevel.DEBUG, String message) { logger.log(level, "[Speedup] $message") } + + private void parseTasks(Project project){ + project.gradle.startParameter.taskNames.each { + if (it.contains("assemble")) { + isAssemble = true + } + } + } } diff --git a/speedup/src/main/groovy/org/component/gradle/UploadPlugin.groovy b/speedup/src/main/groovy/org/component/gradle/UploadPlugin.groovy index d29cbcc..3b5e909 100644 --- a/speedup/src/main/groovy/org/component/gradle/UploadPlugin.groovy +++ b/speedup/src/main/groovy/org/component/gradle/UploadPlugin.groovy @@ -5,50 +5,47 @@ import org.gradle.api.Project import org.gradle.api.logging.LogLevel import org.gradle.api.plugins.MavenPlugin -public class UploadPlugin implements Plugin{ +class UploadPlugin implements Plugin{ + @Override void apply(Project project) { def root = project.rootProject def uploadAll = root.tasks.getByName("uploadAll") + def uploadForClean = root.tasks.getByName("uploadForClean") + String[] excludes = root.excludes + + if (project == root + || project.plugins.hasPlugin('com.android.application')) { + return + } project.plugins.apply(MavenPlugin) - project.afterEvaluate { - String[] excludes = root.excludes - project.plugins.apply(ReplacePlugin) - if (project == root - || excludes.contains(project.path) - || project.plugins.hasPlugin('com.android.application')) { - RootPlugin.log("Filter module $project.path") - return - } + project.group = 'com.local.maven' + project.version = 'local' - // create upload tasks - String name = project.path.replaceAll(":", "") - def upload = project.rootProject.tasks.create(name: "upload$name", group: 'speedup', dependsOn: "${project.path}:uploadArchives") - upload.doLast { - RootPlugin.log(LogLevel.LIFECYCLE, "upload ${project.path} to local maven successful!") - } - uploadAll.dependsOn upload - - project.uploadArchives { - repositories { - mavenDeployer { - pom.groupId = "com.local.maven" - pom.artifactId = project.path.replaceAll(':','-') - pom.version = "local" - repository(url: project.uri(project.rootProject.localMaven)) - - pom.whenConfigured { pom -> - pom.dependencies.forEach { dep -> - if (dep.getVersion() == "unspecified") { - dep.setGroupId("com.local.maven") - dep.setVersion("local") - } - } - } - } + project.uploadArchives { + repositories { + mavenDeployer { + pom.artifactId = project.path.replaceAll(':','-') + repository(url: project.uri(project.rootProject.localMaven)) } } } + + // filter exclude modules + if (excludes.contains(project.path)) { + return + } + + // create upload tasks + String name = project.path.replaceAll(":", "") + def upload = project.rootProject.tasks.create(name: "upload$name", group: 'speedup', dependsOn: "${project.path}:uploadArchives") + upload.doLast { + RootPlugin.log(LogLevel.LIFECYCLE, "upload ${project.path} to local maven successful!") + } + uploadForClean.dependsOn "${project.path}:clean" + uploadAll.dependsOn upload } + + }