2
0
Fork 0
mirror of https://github.com/ethauvin/rife2-hello.git synced 2025-04-25 07:27:12 -07:00

Add support for publishing the fat jar

This commit adds compatibility with the Maven publish plugin, by
making sure that if the application is published, then the uber
jar is published as a classified artifact (`-uber`) in addition
to the regular jar.

A sample publishing configuration was added to the `app`, with
a "local" publishing repository in <rootproject>/build/repo.

For example, calling `./gradlew pAPTBR` will create the
`build/repo` directory with the corresponding Maven repository
structure after publishing.
This commit is contained in:
Cedric Champeau 2023-02-22 13:44:43 +01:00
parent c8b3cc7890
commit f6deafda3a
No known key found for this signature in database
GPG key ID: 825C06C827AF6B66
2 changed files with 61 additions and 8 deletions

View file

@ -4,17 +4,25 @@ import org.gradle.api.tasks.testing.logging.TestLogEvent
plugins {
application
id("com.uwyn.rife2")
`maven-publish`
}
base {
archivesName.set("hello")
version = 1.0
group = "com.example"
}
application {
mainClass.set("hello.App")
}
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
@ -44,3 +52,17 @@ tasks {
}
}
}
publishing {
repositories {
maven {
name = "Build"
url = uri(rootProject.layout.buildDirectory.dir("repo"))
}
}
publications {
create<MavenPublication>("maven") {
from(components["java"])
}
}
}

View file

@ -20,6 +20,11 @@ import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.artifacts.dsl.DependencyHandler;
import org.gradle.api.attributes.Attribute;
import org.gradle.api.attributes.AttributeContainer;
import org.gradle.api.attributes.Bundling;
import org.gradle.api.component.AdhocComponentWithVariants;
import org.gradle.api.component.ConfigurationVariantDetails;
import org.gradle.api.file.DuplicatesStrategy;
import org.gradle.api.plugins.BasePluginExtension;
import org.gradle.api.plugins.JavaApplication;
@ -60,8 +65,34 @@ public class Rife2Plugin implements Plugin<Project> {
createRife2DevelopmentOnlyConfiguration(project, configurations, dependencyHandler, precompileTemplates);
exposePrecompiledTemplatesToTestTask(project, configurations, dependencyHandler, precompileTemplates);
configureAgent(project, plugins, rife2Extension, rife2AgentClasspath);
registerUberJarTask(project, plugins, javaPluginExtension, rife2Extension, tasks, precompileTemplates);
TaskProvider<Jar> uberJarTask = registerUberJarTask(project, plugins, javaPluginExtension, rife2Extension, tasks, precompileTemplates);
bundlePrecompiledTemplatesIntoJarFile(tasks, precompileTemplates);
configureMavenPublishing(project, plugins, configurations, uberJarTask);
}
private static void configureMavenPublishing(Project project, PluginContainer plugins, ConfigurationContainer configurations, TaskProvider<Jar> uberJarTask) {
plugins.withId("maven-publish", unused -> {
Configuration rife2UberJarElements = configurations.create("rife2UberJarElements", conf -> {
conf.setDescription("Exposes the uber jar archive of the RIFE2 web application.");
conf.setCanBeResolved(false);
conf.setCanBeConsumed(true);
conf.getOutgoing().artifact(uberJarTask, artifact -> artifact.setClassifier("uber"));
AttributeContainer runtimeAttributes = configurations.getByName(JavaPlugin.RUNTIME_ELEMENTS_CONFIGURATION_NAME).getAttributes();
conf.attributes(attrs -> {
for (Attribute<?> attribute : runtimeAttributes.keySet()) {
Object value = runtimeAttributes.getAttribute(attribute);
//noinspection unchecked
if (Bundling.class.equals(attribute.getType())) {
attrs.attribute(Bundling.BUNDLING_ATTRIBUTE, project.getObjects().named(Bundling.class, Bundling.SHADOWED));
} else {
attrs.attribute((Attribute<Object>) attribute, value);
}
}
});
});
AdhocComponentWithVariants component = (AdhocComponentWithVariants) project.getComponents().getByName("java");
component.addVariantsFromConfiguration(rife2UberJarElements, ConfigurationVariantDetails::mapToOptional);
});
}
private static void exposePrecompiledTemplatesToTestTask(Project project, ConfigurationContainer configurations, DependencyHandler dependencyHandler, TaskProvider<PrecompileTemplates> precompileTemplates) {
@ -87,13 +118,13 @@ public class Rife2Plugin implements Plugin<Project> {
configurations.getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME).extendsFrom(rife2DevelopmentOnly);
}
private static void registerUberJarTask(Project project,
PluginContainer plugins,
JavaPluginExtension javaPluginExtension,
Rife2Extension rife2Extension,
TaskContainer tasks,
TaskProvider<PrecompileTemplates> precompileTemplatesTask) {
tasks.register("uberJar", Jar.class, jar -> {
private static TaskProvider<Jar> registerUberJarTask(Project project,
PluginContainer plugins,
JavaPluginExtension javaPluginExtension,
Rife2Extension rife2Extension,
TaskContainer tasks,
TaskProvider<PrecompileTemplates> precompileTemplatesTask) {
return tasks.register("uberJar", Jar.class, jar -> {
var base = project.getExtensions().getByType(BasePluginExtension.class);
jar.getArchiveBaseName().convention(project.provider(() -> base.getArchivesName().get() + "-uber"));
jar.setDuplicatesStrategy(DuplicatesStrategy.EXCLUDE);