|
| 1 | +import io.github.serpro69.semverkt.gradle.plugin.tasks.TagTask |
| 2 | +import org.gradle.accessors.dm.LibrariesForLibs |
| 3 | +import org.jetbrains.dokka.gradle.DokkaTask |
| 4 | +import java.util.Locale |
| 5 | + |
| 6 | +plugins { |
| 7 | + base |
| 8 | + kotlin("jvm") |
| 9 | + id("org.jetbrains.dokka") |
| 10 | + `maven-publish` |
| 11 | + signing |
| 12 | +} |
| 13 | + |
| 14 | +val libs = the<LibrariesForLibs>() |
| 15 | + |
| 16 | +/** |
| 17 | + * For additional providers, use a combination of rootProject and subproject names for artifact name and similar things. |
| 18 | + * i.e. kotlin-faker-books, kotlin-faker-movies, kotlin-faker-tv, ... |
| 19 | + * |
| 20 | + * The "core" lib retains the same name as before: kotlin-faker |
| 21 | + */ |
| 22 | +private val fullName: String = "${rootProject.name}-${project.name}" |
| 23 | + |
| 24 | +val isDev = provider { version.toString().startsWith("0.0.0") } |
| 25 | +val isSnapshot = provider { |
| 26 | + // QUESTION do we need to check if rootProject is also set to snapshot? |
| 27 | + // Likely not, since "isRelease" will not just check for the version, but also for the actual tag creation |
| 28 | + // rootProject.version.toString().endsWith("SNAPSHOT") && |
| 29 | + version.toString().endsWith("SNAPSHOT") |
| 30 | +} |
| 31 | +val isRelease = provider { |
| 32 | + val tag = project.tasks.getByName("tag", TagTask::class) |
| 33 | + /* all fakers have their own tags, so checking if tag.didWork is enough for them, |
| 34 | + ':core' shares the tag with 'root', ':bom' and ':cli-bot' modules, |
| 35 | + and hence the tag might already exist and didWork will return false for ':core' */ |
| 36 | + val tagCreated = if (project.name != "core") tag.didWork else tag.didWork || tag.tagExists |
| 37 | + !isDev.get() && !isSnapshot.get() && tagCreated |
| 38 | +} |
| 39 | + |
| 40 | +configurations { |
| 41 | + create("integrationImplementation") { extendsFrom(configurations.getByName("testImplementation")) } |
| 42 | + create("integrationRuntimeOnly") { |
| 43 | + extendsFrom( |
| 44 | + configurations.getByName("testRuntimeOnly"), |
| 45 | + ) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// configure sourceSets as extension since it's not available here as `sourceSets` is an extension on `Project` |
| 50 | +// https://docs.gradle.org/current/userguide/kotlin_dsl.html#project_extensions_and_conventions |
| 51 | +configure<SourceSetContainer> { |
| 52 | + create("integration") { |
| 53 | + resources.srcDir("src/integration/resources") |
| 54 | + compileClasspath += main.get().compileClasspath + test.get().compileClasspath |
| 55 | + runtimeClasspath += main.get().runtimeClasspath + test.get().runtimeClasspath |
| 56 | + } |
| 57 | + main { |
| 58 | + resources { |
| 59 | + this.srcDir("build/generated/src/main/resources") |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +dependencies { |
| 65 | + val implementation by configurations |
| 66 | + implementation(libs.bundles.kotlin) |
| 67 | +} |
| 68 | + |
| 69 | +val integrationTest by tasks.creating(Test::class) { |
| 70 | + testClassesDirs = sourceSets["integration"].output.classesDirs |
| 71 | + classpath = sourceSets["integration"].runtimeClasspath |
| 72 | + dependsOn(tasks.test) |
| 73 | +} |
| 74 | + |
| 75 | +tasks.withType<Jar> { |
| 76 | + archiveBaseName.set(fullName) |
| 77 | + |
| 78 | + manifest { |
| 79 | + attributes( |
| 80 | + mapOf( |
| 81 | + "Implementation-Title" to fullName, |
| 82 | + "Implementation-Version" to project.version, |
| 83 | + /* |
| 84 | + * We can't add this here because this resolves the configuration, |
| 85 | + * after which it effectively becomes read-only and we'll get an error |
| 86 | + * Cannot change dependencies of dependency configuration ':core:implementation' after it has been included in dependency resolution |
| 87 | + * if we try to add more dependencies in the module's build.gradle file directly |
| 88 | + */ |
| 89 | + // "Class-Path" to project.configurations.compileClasspath.get().joinToString(" ") { it.name } |
| 90 | + ) |
| 91 | + ) |
| 92 | + } |
| 93 | + |
| 94 | + dependsOn(integrationTest) |
| 95 | +} |
| 96 | + |
| 97 | +val sourcesJar by tasks.creating(Jar::class) { |
| 98 | + archiveClassifier.set("sources") |
| 99 | + from(sourceSets.getByName("main").allSource) |
| 100 | + from("${rootProject.rootDir.resolve("LICENSE.adoc")}") { |
| 101 | + into("META-INF") |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +val dokkaJavadocJar by tasks.creating(Jar::class) { |
| 106 | + archiveClassifier.set("javadoc") |
| 107 | + dependsOn(tasks.dokkaJavadoc) |
| 108 | + from(tasks.dokkaJavadoc.get().outputDirectory.orNull) |
| 109 | +} |
| 110 | + |
| 111 | +artifacts { |
| 112 | + archives(sourcesJar) |
| 113 | + archives(dokkaJavadocJar) |
| 114 | +} |
| 115 | + |
| 116 | +val publicationName = |
| 117 | + "faker${project.name.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }}" |
| 118 | + |
| 119 | +publishing { |
| 120 | + publications { |
| 121 | + create<MavenPublication>(publicationName) { |
| 122 | + groupId = project.group.toString() |
| 123 | + artifactId = fullName |
| 124 | + version = project.version.toString() |
| 125 | + from(components["java"]) |
| 126 | + artifact(sourcesJar) |
| 127 | + artifact(dokkaJavadocJar) //TODO configure dokka or use defaults? |
| 128 | + |
| 129 | + pom { |
| 130 | + packaging = "jar" |
| 131 | + name.set(fullName) |
| 132 | + description.set("Generate realistically looking fake data such as names, addresses, banking details, and many more, that can be used for testing and data anonymization purposes.") |
| 133 | + url.set("https://github.com/serpro69/kotlin-faker") |
| 134 | + scm { |
| 135 | + connection.set("scm:git:https://github.com/serpro69/kotlin-faker") |
| 136 | + developerConnection.set("scm:git:https://github.com/serpro69") |
| 137 | + url.set("https://github.com/serpro69/kotlin-faker") |
| 138 | + } |
| 139 | + issueManagement { |
| 140 | + url.set("https://github.com/serpro69/kotlin-faker/issues") |
| 141 | + } |
| 142 | + licenses { |
| 143 | + license { |
| 144 | + name.set("MIT") |
| 145 | + url.set("https://opensource.org/licenses/mit-license.php") |
| 146 | + } |
| 147 | + } |
| 148 | + developers { |
| 149 | + developer { |
| 150 | + id.set("serpro69") |
| 151 | + name.set("Serhii Prodan") |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +tasks { |
| 160 | + assemble { |
| 161 | + dependsOn(integrationTest) |
| 162 | + dependsOn(jar) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +signing { |
| 167 | + sign(publishing.publications[publicationName]) |
| 168 | +} |
| 169 | + |
| 170 | +tasks.withType<DokkaTask>().configureEach { |
| 171 | + onlyIf("Not dev") { !isDev.get() } |
| 172 | + onlyIf("Release or snapshot") { isRelease.get() || isSnapshot.get() } |
| 173 | +} |
| 174 | + |
| 175 | +tasks.withType<PublishToMavenRepository>().configureEach { |
| 176 | + dependsOn(project.tasks.getByName("tag")) |
| 177 | + dependsOn(project.tasks.withType(Sign::class.java)) |
| 178 | + onlyIf("Not dev") { !isDev.get() } |
| 179 | + onlyIf("Release or snapshot") { isRelease.get() || isSnapshot.get() } |
| 180 | +} |
| 181 | + |
| 182 | +tasks.withType<PublishToMavenLocal>().configureEach { |
| 183 | + onlyIf("In development") { isDev.get() || isSnapshot.get() } |
| 184 | +} |
| 185 | + |
| 186 | +tasks.withType<Sign>().configureEach { |
| 187 | + dependsOn(project.tasks.getByName("tag")) |
| 188 | + onlyIf("Not dev and snapshot") { !isDev.get() && !isSnapshot.get() } |
| 189 | + onlyIf("Release") { isRelease.get() } |
| 190 | +} |
0 commit comments