Skip to content
Open
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 build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
subprojects {
apply(plugin = "maven-publish")

apply(plugin = "java-library")
group = "net.onelitefeather"

plugins.withType<JavaPlugin> {
Expand Down
4 changes: 4 additions & 0 deletions lasius-base/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
plugins {
`java-gradle-plugin`
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package net.onelitefeather.lasius;

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.publish.PublishingExtension;
import org.gradle.api.publish.maven.MavenPublication;
import org.gradle.api.tasks.testing.Test;
import org.gradle.api.tasks.testing.logging.TestLogEvent;
import org.gradle.testing.jacoco.tasks.JacocoReport;
import org.jetbrains.annotations.NotNull;

import java.util.Set;

/**
* Shared base Gradle plugin that configures common behavior for both Java and Kotlin projects.
*/
public abstract class LasiusBasePlugin implements Plugin<@NotNull Project> {

@Override
public void apply(@NotNull Project project) {
applyCommonPlugins(project);
configureTesting(project);
configurePublishing(project);
Comment on lines +21 to +23

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add conditions here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you design such conditions? Just using properties? I’m not very experienced in writing plugins, so I can’t suggest the best solution

}

private void applyCommonPlugins(@NotNull Project project) {
project.getPluginManager().apply("jacoco");
project.getPluginManager().apply("maven-publish");
}

/**
* Configures JUnit 5, Jacoco, and test logging.
*/
private void configureTesting(@NotNull Project project) {
project.getTasks().withType(Test.class).configureEach(test -> {
test.useJUnitPlatform();
test.getTestLogging().setEvents(Set.of(
TestLogEvent.PASSED,
TestLogEvent.SKIPPED,
TestLogEvent.FAILED
));
test.finalizedBy(project.getTasks().named("jacocoTestReport"));
});

project.getTasks().withType(JacocoReport.class).configureEach(jacoco -> {
jacoco.dependsOn(project.getTasks().withType(Test.class));
jacoco.getReports().getXml().getRequired().set(true);
jacoco.getReports().getHtml().getRequired().set(true);
jacoco.getReports().getCsv().getRequired().set(false);
});
}

/**
* Configures Maven publishing with credentials and repository selection based on version.
*/
private void configurePublishing(@NotNull Project project) {
project.afterEvaluate(p -> {
var publishing = p.getExtensions().findByType(PublishingExtension.class);
if (publishing == null) {
return;
}

publishing.publications(publications ->
publications.create("maven", MavenPublication.class, publication -> {
var component = p.getComponents().findByName("java");
if (component != null) {
publication.from(component);
}
})
);

publishing.repositories(repos -> repos.maven(maven -> {
String version = p.getVersion().toString();
String baseUrl = version.contains("SNAPSHOT")
? "https://repo.onelitefeather.dev/onelitefeather-snapshots"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a property for gradle or system property

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a separate pull request and isn’t really related to this one

: "https://repo.onelitefeather.dev/onelitefeather-releases";

maven.setName("OneLiteFeatherRepository");
maven.setUrl(p.uri(baseUrl));

String username = System.getenv("ONELITEFEATHER_MAVEN_USERNAME");
String password = System.getenv("ONELITEFEATHER_MAVEN_PASSWORD");

maven.credentials(passwordCredentials -> {
passwordCredentials.setUsername(username);
passwordCredentials.setPassword(password);
});
}));

project.getLogger().lifecycle("Configured publishing for {}", project.getName());
});
}
}
1 change: 1 addition & 0 deletions lasius-java/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
version = "1.0-SNAPSHOT"

dependencies {
implementation(project(":lasius-base"))
testImplementation(platform(libs.junit.bom))
testImplementation(libs.junit.jupiter)
testRuntimeOnly(libs.junit.launcher)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,131 +1,32 @@
package net.onelitefeather.lasius;

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.publish.PublishingExtension;
import org.gradle.api.publish.maven.MavenPublication;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.api.tasks.testing.Test;
import org.gradle.api.tasks.testing.logging.TestLogEvent;
import org.gradle.jvm.toolchain.JavaLanguageVersion;
import org.gradle.testing.jacoco.plugins.JacocoPlugin;
import org.gradle.testing.jacoco.tasks.JacocoReport;
import org.jetbrains.annotations.NotNull;

import java.nio.charset.StandardCharsets;

public class LasiusJavaPlugin implements Plugin<@NotNull Project> {
public class LasiusJavaPlugin extends LasiusBasePlugin {

@Override
public void apply(@NotNull Project project) {
applyPlugins(project);
super.apply(project);
configureJava(project);
configureTasks(project);
configurePublishing(project);
}

private void applyPlugins(@NotNull Project project) {
project.getPluginManager().apply("java-library");
project.getPluginManager().apply("maven-publish");
project.getPluginManager().apply(JacocoPlugin.class);
}

private void configureJava(@NotNull Project project) {
project.getPluginManager().withPlugin("java", java -> {
project.getPluginManager().withPlugin("java-library", _ -> {
var javaExt = project.getExtensions().getByType(JavaPluginExtension.class);
javaExt.getToolchain().getLanguageVersion().set(JavaLanguageVersion.of(25));
javaExt.withJavadocJar();
javaExt.withSourcesJar();

project.getLogger().lifecycle("Set Java toolchain to Java {} for project {}", 25, project.getName());
});
}

/**
* Configures tasks for the project.
* @param project the project to configure tasks for
*/
private void configureTasks(@NotNull Project project) {
project.getTasks().withType(JavaCompile.class).configureEach(compileTask -> {
compileTask.getOptions().setEncoding(StandardCharsets.UTF_8.displayName());
compileTask.getOptions().getRelease().set(25);
});

project.getTasks().withType(Test.class).configureEach(testTask -> {
testTask.useJUnitPlatform();
if (hasMinestomDependency(project)) {
testTask.jvmArgs("-Dminestom.inside-test=true");
}

testTask.getTestLogging().setEvents(java.util.Set.of(
TestLogEvent.PASSED,
TestLogEvent.SKIPPED,
TestLogEvent.FAILED
));

testTask.finalizedBy(project.getTasks().named("jacocoTestReport"));
});

project.getTasks().withType(JacocoReport.class).configureEach(jacocoTask -> {
jacocoTask.dependsOn(project.getTasks().withType(Test.class));
jacocoTask.getReports().getXml().getRequired().set(true);
jacocoTask.getReports().getCsv().getRequired().set(true);
});
}


/**
* Configures publishing for the project.
*
* @param project the project to configure publishing for.
*/
private void configurePublishing(@NotNull Project project) {
project.getPluginManager().apply("maven-publish");

project.afterEvaluate(p -> {
var publishing = p.getExtensions().findByType(PublishingExtension.class);
if (publishing == null) return;

publishing.publications(publications ->
publications.create("maven", MavenPublication.class, publication ->
publication.from(p.getComponents().getByName("java"))
)
);

publishing.repositories(repos -> repos.maven(maven -> {
maven.setName("OneLiteFeatherRepository");

String username = System.getenv("ONELITEFEATHER_MAVEN_USERNAME");
String password = System.getenv("ONELITEFEATHER_MAVEN_PASSWORD");
project.getTasks().withType(JavaCompile.class).configureEach(task -> {
task.getOptions().setEncoding("UTF-8");
task.getOptions().getRelease().set(25);
});

maven.credentials(passwordCredentials -> {
passwordCredentials.setUsername(username);
passwordCredentials.setPassword(password);
});

String version = p.getVersion().toString();
String url = version.contains("SNAPSHOT")
? "https://repo.onelitefeather.dev/onelitefeather-snapshots"
: "https://repo.onelitefeather.dev/onelitefeather-releases";

maven.setUrl(p.uri(url));
}));

project.getLogger().lifecycle("Configured publishing for {}", project.getName());
project.getLogger().lifecycle("Set Java toolchain to Java {} for project {}", 25, project.getName());
});
}

/**
* Checks if the project has a dependency on Minestom.
*
* @param project the project to check
* @return true if the project has a dependency on Minestom, false otherwise
*/
private boolean hasMinestomDependency(@NotNull Project project) {
return project.getConfigurations().stream()
.anyMatch(config -> config.getDependencies().stream()
.anyMatch(dep -> dep.getName().toLowerCase().contains("minestom")));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class LasiusJavaPluginTest {
@BeforeEach
void setUp() {
this.project = ProjectBuilder.builder().build();
project.getPluginManager().apply("java-library");
}

@Test
Expand Down
27 changes: 27 additions & 0 deletions lasius-kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
plugins {
`kotlin-dsl`
}

dependencies {
implementation(project(":lasius-base"))
// Needed because your plugin applies and configures the Kotlin plugin
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin")
testImplementation(platform(libs.junit.bom))
testImplementation(libs.junit.jupiter)
testRuntimeOnly(libs.junit.launcher)
}

tasks.test {
useJUnitPlatform()
}

gradlePlugin {
plugins {
register("lasiusKotlinPlugin") {
id = "${group}.lasius-kotlin"
implementationClass = "net.onelitefeather.lasius.LasiusKotlinPlugin"
displayName = "Lasius Kotlin Plugin"
description = "A gradle plugin to apply default settings to kotlin projects"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.onelitefeather.lasius

import org.gradle.api.Project
import org.gradle.kotlin.dsl.findByType
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension

class LasiusKotlinPlugin : LasiusBasePlugin() {
override fun apply(project: Project) {
configureKotlin(project)
}

private fun configureKotlin(project: Project) {
project.extensions.findByType(KotlinJvmProjectExtension::class)?.apply {
jvmToolchain(21)
project.logger.lifecycle("Configured Kotlin JVM toolchain to Java 21 for ${project.name}")
}
}
}
5 changes: 4 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ rootProject.name = "lasius"
dependencyResolutionManagement {
repositories {
mavenCentral()
gradlePluginPortal()
}

versionCatalogs {
Expand All @@ -16,4 +17,6 @@ dependencyResolutionManagement {
}
}

include("lasius-java")
include("lasius-java")
include("lasius-kotlin")
include("lasius-base")