Skip to content

Commit

Permalink
1. 优化加载逻辑。减少插件加载运行时间
Browse files Browse the repository at this point in the history
2. 支持只在进行打包apk时启动replace插件。
3. 支持upload时自动机型clean。避免偶发性的数据污染现象
4. 支持双向动态替换依赖:project <==> local maven url
  • Loading branch information
yjfnypeu committed Oct 27, 2017
1 parent 5bb078e commit cd169fa
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 47 deletions.
50 changes: 45 additions & 5 deletions speedup/src/main/groovy/org/component/gradle/ReplacePlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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<Project>{
class ReplacePlugin implements Plugin<Project>{

@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
}

Expand All @@ -30,11 +36,45 @@ public class ReplacePlugin implements Plugin<Project>{
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<String> flatDependencies(String localRepo, String name) {
List<String> 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
}
}
36 changes: 29 additions & 7 deletions speedup/src/main/groovy/org/component/gradle/RootPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ import org.gradle.api.logging.Logger

public class RootPlugin implements Plugin<Project> {

static Project root;
Project root
boolean isAssemble

static Logger logger

@Override
void apply(Project project) {
if (project != project.rootProject) {
throw new RuntimeException("You must apply the plugin : 'speedup' on root build.gradle")
}
root = project.rootProject
logger = project.logger

parseTasks(project)
boolean enable
try {
enable = parseLocal()
Expand All @@ -32,21 +35,32 @@ public class RootPlugin implements Plugin<Project> {
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
}

Expand All @@ -62,4 +76,12 @@ public class RootPlugin implements Plugin<Project> {
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
}
}
}
}
67 changes: 32 additions & 35 deletions speedup/src/main/groovy/org/component/gradle/UploadPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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<Project>{
class UploadPlugin implements Plugin<Project>{

@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
}


}

0 comments on commit cd169fa

Please sign in to comment.