Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating multi release jar #110

Merged
merged 2 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ That's because different parts of the source code target different Java versions
and Gradle requires the exact JDK version (not higher) for each.

Be sure that your default Java version (which Gradle should use automatically) is at
least 16!
least 17!

If you are building from the official source *release* (not from source that you
got from Git), `gradle/wrapper/gradle-wrapper.jar` is missing from that, and you
Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import java.io.FileOutputStream
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.util.*
import java.util.Properties
import java.util.stream.Collectors

plugins {
`freemarker-root`
`maven-publish`
signing
id("biz.aQute.bnd.builder") version "6.1.0"
id("biz.aQute.bnd.builder") version "7.0.0"
id("eclipse")
}

Expand Down
5 changes: 4 additions & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ dependencies {
implementation(gradleApi())

implementation("org.apache.freemarker.docgen:freemarker-docgen-core:0.0.2-SNAPSHOT")
implementation("org.nosphere.apache:creadur-rat-gradle:0.7.0")
implementation("org.nosphere.apache:creadur-rat-gradle:0.8.1")
// Xalan dependencies are required by rat even though it does not declare a dependency on them.
implementation("xalan:xalan:2.7.3")
implementation("xalan:serializer:2.7.3")
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package freemarker.build

import java.util.concurrent.atomic.AtomicBoolean
import org.gradle.api.NamedDomainObjectProvider
import org.gradle.api.Project
import org.gradle.api.artifacts.VersionCatalogsExtension
Expand All @@ -33,13 +34,17 @@ import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.bundling.Jar
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.api.tasks.javadoc.Javadoc
import org.gradle.api.tasks.testing.Test
import org.gradle.jvm.toolchain.JavaLanguageVersion
import org.gradle.jvm.toolchain.JavaToolchainService
import org.gradle.kotlin.dsl.*
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.named
import org.gradle.kotlin.dsl.register
import org.gradle.kotlin.dsl.setProperty
import org.gradle.kotlin.dsl.the
import org.gradle.language.base.plugins.LifecycleBasePlugin
import org.gradle.language.jvm.tasks.ProcessResources
import org.gradle.testing.base.TestingExtension
import java.util.concurrent.atomic.AtomicBoolean

private const val TEST_UTILS_SOURCE_SET_NAME = "test-utils"

Expand Down Expand Up @@ -150,15 +155,54 @@ class FreemarkerModuleDef internal constructor(
fun enableTests(testJavaVersion: String = ext.testJavaVersion) =
configureTests(JavaLanguageVersion.of(testJavaVersion))

private fun testWithJarTaskName(sourceSetName: String): String {
if (sourceSetName == SourceSet.MAIN_SOURCE_SET_NAME) {
return "testWithJar"
}
return "test${sourceSetName.replaceFirstChar { it.uppercaseChar() }}WithJar"
}

private fun registerTestWithJar(testSuiteRef: Provider<JvmTestSuite>, testJavaVersion: JavaLanguageVersion) {
val tasks = context.project.tasks
val testWithJarTaskRef = context.project.tasks.register<Test>(testWithJarTaskName(sourceSetName)) {
group = LifecycleBasePlugin.VERIFICATION_GROUP
description = "Runs the tests in ${sourceSetName} with the jar as the dependency."

val testSourceSet = testSuiteRef.get().sources
testClassesDirs = testSourceSet.output.classesDirs

val jarClasspath = project.objects.fileCollection()
jarClasspath.from(tasks.named(JavaPlugin.JAR_TASK_NAME))
jarClasspath.from(testSourceSet.output)
jarClasspath.from(testUtils().output)
// Filtering out directories is strictly speaking incorrect.
// Our intent is to filter out the compiled classes that are declared as dependencies.
// The correct solution would be to split the configurations to separately
// track the external and internal dependencies. However, that would be a considerable
// complication of the build, and this solution should be good given that our external
// dependencies are always files (jars).
jarClasspath.from(testSourceSet.runtimeClasspath.filter { it.isFile })
classpath = jarClasspath

javaLauncher.set(context.javaToolchains.launcherFor {
languageVersion.set(testJavaVersion)
})
}
tasks.named(LifecycleBasePlugin.CHECK_TASK_NAME) { dependsOn(testWithJarTaskRef) }
}

private fun configureTests(testJavaVersion: JavaLanguageVersion): NamedDomainObjectProvider<JvmTestSuite> {
val testSuitRef = getOrCreateTestSuiteRef()
testSuitRef.configure {
val testSuiteRef = getOrCreateTestSuiteRef()
testSuiteRef.configure {
useJUnit(context.version("junit"))

configureSources(sources, testJavaVersion)
targets.all { configureTarget(this, sources, testJavaVersion) }
}
return testSuitRef

registerTestWithJar(testSuiteRef, testJavaVersion)

return testSuiteRef
}

private fun getOrCreateTestSuiteRef(): NamedDomainObjectProvider<JvmTestSuite> {
Expand Down Expand Up @@ -332,7 +376,8 @@ class FreemarkerRootExtension constructor(

allConfiguredSourceSetNamesRef.add(sourceSetName)

FreemarkerModuleDef(context, this, generated, sourceSetName, JavaLanguageVersion.of(sourceSetVersion)).apply {
val parsedSourceSetVersion = JavaLanguageVersion.of(sourceSetVersion)
FreemarkerModuleDef(context, this, generated, sourceSetName, parsedSourceSetVersion).apply {
sourceSet.apply {
if (generated) {
java.setSrcDirs(emptyList<String>())
Expand All @@ -350,7 +395,13 @@ class FreemarkerRootExtension constructor(

tasks.apply {
named<Jar>(mainSourceSet.sourcesJarTaskName) { from(sourceSet.allSource) }
named<Jar>(JavaPlugin.JAR_TASK_NAME) { from(sourceSet.output) }
named<Jar>(JavaPlugin.JAR_TASK_NAME) {
from(sourceSet.output) {
if (parsedSourceSetVersion.compareTo(JavaLanguageVersion.of(javaVersion)) > 0) {
into("META-INF/versions/${parsedSourceSetVersion.asInt()}")
}
}
}
named<Javadoc>(JavaPlugin.JAVADOC_TASK_NAME) { source(sourceSet.java) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ import org.gradle.api.publish.maven.tasks.AbstractPublishToMaven
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.bundling.Jar
import org.gradle.jvm.toolchain.JavaLanguageVersion
import org.gradle.kotlin.dsl.*
import org.gradle.kotlin.dsl.attributes
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.filter
import org.gradle.kotlin.dsl.named
import org.gradle.kotlin.dsl.the
import org.gradle.kotlin.dsl.withType
import org.gradle.language.base.plugins.LifecycleBasePlugin
import org.gradle.language.jvm.tasks.ProcessResources

Expand Down Expand Up @@ -66,6 +71,9 @@ open class FreemarkerRootPlugin : Plugin<Project> {

named<Jar>(mainSourceSet.sourcesJarTaskName) {
from(resourceTemplatesDir)
manifest.attributes(
"Multi-Release" to "true"
)
}

withType<org.nosphere.apache.rat.RatTask>() {
Expand Down
Loading