Skip to content

Commit

Permalink
Add a repository management plugin (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte authored Jun 15, 2024
1 parent 14fc5a6 commit 774cd8b
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 27 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ neoForge {
// You can change the name used for this run in your IDE
ideName = "Run Game Tests"
// Changes the source set whose runtime classpath is used for this run. This defaults to "main"
// Eclipse does not support having multiple runtime classpaths per project (except for unit tests).
sourceSet = sourceSets.main
Expand Down Expand Up @@ -283,6 +283,29 @@ public class TestClass {
}
```

### Centralizing Repositories Declaration

This plugin supports Gradle's [centralized repositories declaration](https://docs.gradle.org/current/userguide/declaring_repositories.html#sub:centralized-repository-declaration) in settings.gradle
by offering a separate plugin to apply the repositories to develop mods.
It can be used in the following way in `settings.gradle`:

```groovy
plugins {
id 'net.neoforged.moddev.repositories' version '<version>'
}
dependencyResolutionManagement {
repositories {
mavenCentral()
}
}
```

Please note that defining any repository in build.gradle will completely disable
the centrally managed repositories for that project.
You can also use the repositories plugin in a project to add the repositories there,
even if dependency management has been overridden.

## Advanced Tips & Tricks

### Overriding Platform Libraries
Expand Down
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ gradlePlugin {
description = "This plugin helps you create Minecraft mods using the NeoForge platform"
tags = ["minecraft", "neoforge", "java", "mod"]
}
repositories {
id = 'net.neoforged.moddev.repositories'
implementationClass = 'net.neoforged.moddevgradle.boot.RepositoriesPlugin'
displayName = "NeoForge Mod Development Repositories Plugin"
description = "This plugin adds the repositories needed for developing Minecraft mods. It is applied automatically by the moddev plugin, but can be applied manually in settings.gradle to make use of Gradle dependency management."
tags = ["minecraft", "neoforge", "java", "mod"]
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.neoforged.moddevgradle.boot;

import org.gradle.api.Project;

public class RepositoryPlugin extends TrampolinePlugin<Project> {
public RepositoryPlugin() {
super("net.neoforged.moddevgradle.internal.RepositoryPlugin");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,18 @@ public class ModDevPlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
project.getPlugins().apply(JavaLibraryPlugin.class);
// Do not apply the repositories automatically if they have been applied at the settings-level.
// It's still possible to apply them manually, though.
if (!project.getGradle().getPlugins().hasPlugin(RepositoryPlugin.class)) {
project.getPlugins().apply(RepositoryPlugin.class);
} else {
project.getLogger().info("Not enabling NeoForged repositories since they were applied at the settings level");
}
var javaExtension = ExtensionUtils.getExtension(project, "java", JavaPluginExtension.class);

var configurations = project.getConfigurations();
var layout = project.getLayout();
var tasks = project.getTasks();
var repositories = project.getRepositories();

// We use this directory to store intermediate files used during moddev
var modDevBuildDir = layout.getBuildDirectory().dir("moddev");
Expand All @@ -130,31 +136,6 @@ public void apply(Project project) {
});
}));

var mojangMaven = repositories.maven(repo -> {
repo.setName("Mojang Minecraft Libraries");
repo.setUrl(URI.create("https://libraries.minecraft.net/"));
repo.metadataSources(sources -> sources.mavenPom());
repo.content(MojangRepositoryFilter::filter);
});
// Make sure that Mojang's repository is first
if (repositories.getAsMap().size() > 1) {
repositories.remove(mojangMaven);
repositories.add(0, mojangMaven);
}

repositories.maven(repo -> {
repo.setName("Mojang Meta");
repo.setUrl("https://maven.neoforged.net/mojang-meta/");
repo.metadataSources(sources -> sources.gradleMetadata());
repo.content(content -> {
content.includeModule("net.neoforged", "minecraft-dependencies");
});
});
repositories.maven(repo -> {
repo.setName("NeoForged Releases");
repo.setUrl(URI.create("https://maven.neoforged.net/releases/"));
});

project.getDependencies().attributesSchema(attributesSchema -> {
attributesSchema.attribute(ATTRIBUTE_DISTRIBUTION).getDisambiguationRules().add(DistributionDisambiguation.class);
attributesSchema.attribute(ATTRIBUTE_OPERATING_SYSTEM).getDisambiguationRules().add(OperatingSystemDisambiguation.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package net.neoforged.moddevgradle.internal;

import net.neoforged.moddevgradle.internal.generated.MojangRepositoryFilter;
import org.gradle.api.GradleException;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.dsl.RepositoryHandler;
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
import org.gradle.api.initialization.Settings;
import org.gradle.api.invocation.Gradle;
import org.gradle.api.plugins.PluginAware;

import java.net.URI;

/**
* This plugin acts in different roles depending on where it is applied:
* <ul>
* <li>At the project-level, it will add the required repositories for moddev.</li>
* <li>At the settings-level, it will add the required repositories to the dependency management block, and add a marker plugin to the Gradle instance to prevent the
* repositories from being added again at the project-level.</li>
* </ul>
*/
public class RepositoryPlugin implements Plugin<PluginAware> {
@Override
public void apply(PluginAware target) {
if (target instanceof Project project) {
applyRepositories(project.getRepositories());
} else if (target instanceof Settings settings) {
applyRepositories(settings.getDependencyResolutionManagement().getRepositories());
settings.getGradle().getPlugins().apply(getClass()); // Add a marker to Gradle
} else if (target instanceof Gradle gradle) {
// Do nothing
} else {
throw new GradleException("This plugin does not support being applied to " + target);
}
}

private void applyRepositories(RepositoryHandler repositories) {
var mojangMaven = repositories.maven(repo -> {
repo.setName("Mojang Minecraft Libraries");
repo.setUrl(URI.create("https://libraries.minecraft.net/"));
repo.metadataSources(sources -> sources.mavenPom());
repo.content(MojangRepositoryFilter::filter);
});
sortFirst(repositories, mojangMaven);

var mojangMetaMaven = repositories.maven(repo -> {
repo.setName("Mojang Meta");
repo.setUrl("https://maven.neoforged.net/mojang-meta/");
repo.metadataSources(sources -> sources.gradleMetadata());
repo.content(content -> {
content.includeModule("net.neoforged", "minecraft-dependencies");
});
});
sortFirst(repositories, mojangMetaMaven);

repositories.maven(repo -> {
repo.setName("NeoForged Releases");
repo.setUrl(URI.create("https://maven.neoforged.net/releases/"));
});
}

private static void sortFirst(RepositoryHandler repositories, MavenArtifactRepository repo) {
repositories.remove(repo);
repositories.add(0, repo);
}
}

0 comments on commit 774cd8b

Please sign in to comment.