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
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void apply(Project project) {

// 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(LegacyRepositoriesPlugin.class)) {
if (!project.getPlugins().hasPlugin(LegacyRepositoriesPlugin.DeclaredInSettingsPlugin.class)) {
project.getPlugins().apply(LegacyRepositoriesPlugin.class);
} else {
LOG.info("Not enabling legacy repositories since they were applied at the settings level");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,24 @@ public void apply(PluginAware target) {
applyRepositories(project.getRepositories());
} else if (target instanceof Settings settings) {
applyRepositories(settings.getDependencyResolutionManagement().getRepositories());
settings.getGradle().getPlugins().apply(getClass()); // Add a marker to Gradle

// Apply our marker plugin to all projects.
settings.getGradle().beforeProject(project -> project.getPluginManager().apply(DeclaredInSettingsPlugin.class));
} else if (target instanceof Gradle gradle) {
// Do nothing
} else {
throw new GradleException("This plugin does not support being applied to " + target);
}
}

/**
* A marker plugin applied to each project, indicating this plugin was applied on the settings level.
*/
public static final class DeclaredInSettingsPlugin implements Plugin<Project> {
@Override
public void apply(Project target) {}
}

private void applyRepositories(RepositoryHandler repositories) {
repositories.maven(repo -> {
repo.setName("MinecraftForge");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void apply(Project project) {

// 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(RepositoriesPlugin.class)) {
if (!project.getPlugins().hasPlugin(RepositoriesPlugin.DeclaredInSettingsPlugin.class)) {
project.getPlugins().apply(RepositoriesPlugin.class);
} else {
LOG.info("Not enabling NeoForged repositories since they were applied at the settings level");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,24 @@ public void apply(PluginAware target) {
applyRepositories(project.getRepositories());
} else if (target instanceof Settings settings) {
applyRepositories(settings.getDependencyResolutionManagement().getRepositories());
settings.getGradle().getPlugins().apply(getClass()); // Add a marker to Gradle

// Apply our marker plugin to all projects.
settings.getGradle().beforeProject(project -> project.getPluginManager().apply(DeclaredInSettingsPlugin.class));
} else if (target instanceof Gradle gradle) {
// Do nothing
} else {
throw new GradleException("This plugin does not support being applied to " + target);
}
}

/**
* A marker plugin applied to each project, indicating this plugin was applied on the settings level.
*/
public static final class DeclaredInSettingsPlugin implements Plugin<Project> {
@Override
public void apply(Project target) {}
}

private void applyRepositories(RepositoryHandler repositories) {
var mojangMaven = repositories.maven(repo -> {
repo.setName("Mojang Minecraft Libraries");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,34 @@ public void testApplyInEmptyProjectAndEnable() throws IOException {
assertThat(result.getOutput()).contains("createMinecraftArtifacts");
assertEquals(TaskOutcome.SUCCESS, result.task(":tasks").getOutcome());
}

@Test
public void testApplyInSettings() throws IOException {
writeFile(settingsFile, """
plugins {
id "net.neoforged.moddev.repositories"
}

rootProject.name = 'hello-world'

dependencyResolutionManagement {
repositoriesMode = RepositoriesMode.FAIL_ON_PROJECT_REPOS
}
""");
String buildFileContent = """
plugins {
id "net.neoforged.moddev"
}
""";
writeFile(buildFile, buildFileContent);

BuildResult result = GradleRunner.create()
.withPluginClasspath()
.withProjectDir(testProjectDir)
.withArguments("tasks", "--all", "-i")
.build();

assertThat(result.getOutput()).contains("Not enabling NeoForged repositories since they were applied at the settings level");
assertEquals(TaskOutcome.SUCCESS, result.task(":tasks").getOutcome());
}
}
Loading