2525package org .spongepowered .gradle .plugin ;
2626
2727import org .checkerframework .checker .nullness .qual .MonotonicNonNull ;
28+ import org .checkerframework .checker .nullness .qual .NonNull ;
2829import org .gradle .api .Action ;
30+ import org .gradle .api .GradleException ;
2931import org .gradle .api .NamedDomainObjectProvider ;
3032import org .gradle .api .Plugin ;
3133import org .gradle .api .Project ;
3436import org .gradle .api .artifacts .Dependency ;
3537import org .gradle .api .artifacts .component .ComponentIdentifier ;
3638import org .gradle .api .artifacts .component .ModuleComponentIdentifier ;
39+ import org .gradle .api .artifacts .dsl .RepositoryHandler ;
3740import org .gradle .api .artifacts .result .ResolvedArtifactResult ;
3841import org .gradle .api .file .Directory ;
3942import org .gradle .api .file .FileCollection ;
43+ import org .gradle .api .initialization .Settings ;
44+ import org .gradle .api .invocation .Gradle ;
4045import org .gradle .api .plugins .JavaLibraryPlugin ;
4146import org .gradle .api .plugins .JavaPlugin ;
4247import org .gradle .api .provider .Property ;
5762import java .nio .file .Path ;
5863import java .util .Collections ;
5964
60- public final class SpongePluginGradle implements Plugin <Project > {
65+ public final class SpongePluginGradle implements Plugin <Object > {
6166
6267 private @ MonotonicNonNull Project project ;
6368
6469 @ Override
65- public void apply (final Project project ) {
70+ public void apply (final @ NonNull Object target ) {
71+ if (target instanceof Project ) {
72+ this .applyToProject ((Project ) target );
73+ } else if (target instanceof Settings ) {
74+ this .applyToSettings ((Settings ) target );
75+ } else if (target instanceof Gradle ) {
76+ // no-op
77+ } else {
78+ throw new GradleException (
79+ "Sponge gradle plugin target '" + target
80+ + "' is of unexpected type " + target .getClass ()
81+ + ", expecting a Project or Settings instance"
82+ );
83+ }
84+ }
85+
86+ public void applyToProject (final Project project ) {
6687 this .project = project ;
6788
6889 project .getLogger ().lifecycle ("SpongePowered Plugin 'GRADLE' Toolset Version '{}'" , Constants .VERSION );
@@ -74,6 +95,12 @@ public void apply(final Project project) {
7495 // final MinecraftExtension minecraft = project.getExtensions().getByType(MinecraftExtension.class);
7596 final SpongePluginExtension sponge = project .getExtensions ().create ("sponge" , SpongePluginExtension .class );
7697
98+ // Only inject repositories if the project does not have sponge repos from other sources
99+ sponge .injectRepositories ().convention (
100+ !project .getGradle ().getPlugins ().hasPlugin (SpongePluginGradle .class )
101+ && !project .getGradle ().getPlugins ().hasPlugin ("org.spongepowered.gradle.repository" )
102+ );
103+
77104 this .configurePluginMetaGeneration (sponge );
78105
79106 this .addApiDependency (sponge );
@@ -100,13 +127,25 @@ public void apply(final Project project) {
100127 runServer .configure (t -> t .setEnabled (false ));
101128 }
102129 if (sponge .injectRepositories ().get ()) {
103- project .getRepositories ().maven (r -> {
104- r .setUrl (Constants .Repositories .SPONGE );
105- r .setName ("sponge" );
106- });
130+ SpongePluginGradle .addRepository (project .getRepositories ());
131+ } else {
132+ project .getLogger ().info ("SpongeGradle: Injecting repositories was disabled, not adding the Sponge repository at the project level" );
107133 }
108134 });
109135 }
136+
137+ private void applyToSettings (final Settings settings ) {
138+ SpongePluginGradle .addRepository (settings .getDependencyResolutionManagement ().getRepositories ());
139+ settings .getGradle ().getPlugins ().apply (SpongePluginGradle .class );
140+ }
141+
142+ private static void addRepository (final RepositoryHandler handler ) {
143+ handler .maven (r -> {
144+ r .setUrl (Constants .Repositories .SPONGE );
145+ r .setName ("spongeAll" );
146+ });
147+ }
148+
110149 private static String generateApiReleasedVersion (final String apiVersion ) {
111150 final String [] apiSplit = apiVersion .replace ("-SNAPSHOT" , "" ).split ("\\ ." );
112151 final boolean isSnapshot = apiVersion .contains ("-SNAPSHOT" );
0 commit comments