Last active
July 6, 2020 10:13
-
-
Save Karlatemp/6a8fa53cd3641c09f32e0ddc09391fe8 to your computer and use it in GitHub Desktop.
Gradle pom generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2018-2020 Karlatemp. All rights reserved. | |
* @author Karlatemp <karlatemp@vip.qq.com> <https://github.com/Karlatemp> | |
* @create 2020/07/06 17:15:01 | |
* | |
* gradient/gradient/pom.gradle | |
*/ | |
/* | |
How to use: | |
({ | |
def file = new File("G:/KarlatempRepo/mkpom.gradle") | |
if (file.isFile()) { | |
apply from: file | |
} | |
project.extensions.add("pomFilter", ((Predicate<Dependency>) { d -> | |
true | |
})) | |
}).call() | |
*/ | |
import java.security.MessageDigest | |
import java.util.function.Predicate | |
class Util { | |
static char[] HEX_DIGITS = "01234567890abcdef".toCharArray() | |
static fastMD(byte[] array) { | |
def md = MessageDigest.getInstance("sha1") | |
md.update(array) | |
return md.digest() | |
} | |
static toHex(byte[] array) { | |
def builder = new StringBuilder() | |
for (int b : array) { | |
builder.append(HEX_DIGITS[b << 4 & 0xF]) | |
builder.append(HEX_DIGITS[b & 0xF]) | |
} | |
return builder.toString() | |
} | |
static fastSha1(File file) { | |
new File("${file.path}.sha1").write( | |
toHex(fastMD(file.readBytes())) | |
) | |
} | |
static copyTo(InputStream input, OutputStream output) { | |
def buffer = new byte[1024] | |
int length | |
while ((length = input.read(buffer)) > 0) { | |
output.write(buffer, 0, length) | |
} | |
} | |
} | |
class PomTask extends DefaultTask { | |
@TaskAction | |
def invoke() { | |
project.configurations.named("runtimeClasspath").configure { config -> | |
def filter = project.extensions.getByName("pomFilter") as Predicate<Dependency> | |
def builder = new StringBuilder() | |
builder.append("""<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>${project.group}</groupId> | |
<artifactId>${project.name}</artifactId> | |
<version>${project.version}</version> | |
<dependencies> | |
""") | |
def runtimeCP = new LinkedList<String>() | |
for (dep in config.allDependencies) { | |
if (!filter.test(dep)) break | |
runtimeCP.add("${dep.group}:${dep.name}:${dep.version}") | |
builder.append(""" <dependency> | |
<groupId>${dep.group}</groupId> | |
<artifactId>${dep.name}</artifactId> | |
<version>${dep.version}</version> | |
<scope>runtime</scope> | |
</dependency> | |
""") | |
} | |
project.configurations.named("compileClasspath").get().with { compileConfig -> | |
for (dep in config.allDependencies) { | |
if (runtimeCP.contains("${dep.group}:${dep.name}:${dep.version}")) continue | |
if (!filter.test(dep)) break | |
builder.append(""" <dependency> | |
<groupId>${dep.group}</groupId> | |
<artifactId>${dep.name}</artifactId> | |
<version>${dep.version}</version> | |
<scope>compile</scope> | |
</dependency> | |
""") | |
} | |
} | |
builder.append(" </dependencies>\n</project>") | |
def pom = builder.toString() | |
println(pom) | |
// TODO: Replace your local location here | |
def rootRepo = new File("G:/KarlatempRepo") | |
def location = new File(rootRepo, "${project.group.toString().replace('.', '/')}/${project.name}/${project.version}") | |
location.mkdirs() | |
def target = new File(project.buildDir, "libs/${project.name}-${project.version}.jar") | |
if (!target.isFile()) | |
throw new FileNotFoundException(target.toString()) | |
new File(location, "${project.name}-${project.version}.jar").with { file -> | |
file.withOutputStream { output -> | |
target.withInputStream { inp -> | |
Util.copyTo(inp, output) | |
} | |
} | |
Util.fastSha1(file) | |
} | |
new File(location, "${project.name}-${project.version}.pom").with { file -> | |
file.setBytes(pom.getBytes("utf-8")) | |
Util.fastSha1(file) | |
} | |
} | |
} | |
} | |
tasks.create("publishToMavenLocal", PomTask.class) { | |
it.group = "karlatemp" | |
} | |
println("MKPom done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment