diff --git a/src/main/java/com/almostreliable/almostgradle/AlmostGradleExtension.java b/src/main/java/com/almostreliable/almostgradle/AlmostGradleExtension.java index 22a3b6e..6bf1c9a 100644 --- a/src/main/java/com/almostreliable/almostgradle/AlmostGradleExtension.java +++ b/src/main/java/com/almostreliable/almostgradle/AlmostGradleExtension.java @@ -10,7 +10,10 @@ import org.gradle.api.plugins.BasePluginExtension; import org.gradle.api.plugins.JavaPluginExtension; import org.gradle.api.provider.Property; +import org.gradle.api.publish.PublishingExtension; +import org.gradle.api.publish.maven.MavenPublication; import org.gradle.api.tasks.SourceSet; +import org.gradle.jvm.tasks.Jar; import org.gradle.language.jvm.tasks.ProcessResources; import javax.inject.Inject; @@ -19,6 +22,7 @@ public abstract class AlmostGradleExtension { public static final String NAME = "almostgradle"; + public static final String MAVEN = "mavenJava"; private final Project project; private final RecipeViewers recipeViewers; @@ -32,6 +36,8 @@ public AlmostGradleExtension(Project project) { var providers = project.getProviders(); getTestMod().convention(false); + getApiSourceSet().convention(false); + getMavenPublish().convention(false); getDataGen().set(providers.gradleProperty("datagen").map(s -> { if (s.equals("true")) return true; if (s.equals("false")) return false; @@ -53,6 +59,10 @@ public AlmostGradleExtension(Project project) { public abstract Property getDataGen(); + public abstract Property getApiSourceSet(); + + public abstract Property getMavenPublish(); + public RecipeViewers getRecipeViewers() { return recipeViewers; } @@ -103,6 +113,7 @@ public void setup(Action onSetup) { createProcessResourcesTask(); applyBuildConfig(); + applyApiSourceSet(); applyBasicMod(); applyTestMod(); getRecipeViewers().createRuns(); @@ -127,6 +138,51 @@ private void onPostRunConfigs() { }); } + private void applyApiSourceSet() { + if (!getApiSourceSet().get()) { + return; + } + + var javaPlugin = project.getExtensions().getByType(JavaPluginExtension.class); + var main = javaPlugin.getSourceSets().getByName("main"); + var api = javaPlugin.getSourceSets().create("api"); + var neoForge = project.getExtensions().getByType(NeoForgeExtension.class); + neoForge.addModdingDependenciesTo(api); + project.getDependencies().add(main.getImplementationConfigurationName(), api.getOutput()); + + var apiJar = project.getTasks().register("apiJar", Jar.class, jar -> { + jar.getArchiveClassifier().set("api"); + jar.from(api.getOutput()); + }); + + var apiSources = project.getTasks().register("apiSources", Jar.class, jar -> { + jar.getArchiveClassifier().set("api-sources"); + jar.from(api.getAllJava()); + }); + + project.getTasks().named("jar", Jar.class, jar -> { + jar.dependsOn(apiJar); + jar.from(api.getOutput()); + }); + + project.artifacts(a -> { + a.add("archives", apiJar); + a.add("archives", apiSources); + }); + + var maven = project.getExtensions().getByType(PublishingExtension.class); + maven.getPublications().withType(MavenPublication.class).configureEach(pub -> { + pub.artifact(apiJar); + pub.artifact(apiSources); + }); + + if (getWithSourcesJar().get()) { + project.getTasks().named("sourcesJar", Jar.class, jar -> { + jar.from(api.getAllJava()); + }); + } + } + private void applyBasicMod() { var neoForge = project.getExtensions().getByType(NeoForgeExtension.class); var javaPlugin = project.getExtensions().getByType(JavaPluginExtension.class); @@ -135,6 +191,10 @@ private void applyBasicMod() { var mainSourceSet = javaPlugin.getSourceSets().getByName("main"); mainMod.sourceSet(mainSourceSet); + if (getApiSourceSet().get()) { + mainMod.sourceSet(javaPlugin.getSourceSets().getByName("api")); + } + neoForge.getRuns().create("client", (run) -> { run.client(); run.getLoadedMods().set(Set.of(mainMod)); @@ -167,12 +227,15 @@ private void applyDataGen(NeoForgeExtension neoForge, ModModel mainMod, SourceSe neoForge.getRuns().create("datagen", (run) -> { run.data(); run.getLoadedMods().set(Set.of(mainMod)); - run.getProgramArguments().addAll( - "--mod", getModId(), - "--all", - "--output", project.file(generatedPath).getAbsolutePath(), - "--existing", project.file("src/main/resources").getAbsolutePath() - ); + run + .getProgramArguments() + .addAll("--mod", + getModId(), + "--all", + "--output", + project.file(generatedPath).getAbsolutePath(), + "--existing", + project.file("src/main/resources").getAbsolutePath()); }); log("đź“•Applied datagen output under: " + generatedPath.replace('/', '.')); @@ -195,6 +258,13 @@ private void applyBasics() { var javaPlugin = project.getExtensions().getByType(JavaPluginExtension.class); javaPlugin.withSourcesJar(); } + + if (getMavenPublish().get()) { + project.getPlugins().apply("maven-publish"); + var maven = project.getExtensions().getByType(PublishingExtension.class); + var pub = maven.getPublications().create(MAVEN, MavenPublication.class); + pub.from(project.getComponents().getByName("java")); + } } private void applyBuildConfig() {