3737
3838import javax .annotation .Nullable ;
3939import javax .inject .Inject ;
40+ import java .io .File ;
4041import java .util .Collection ;
4142import java .util .Collections ;
4243import java .util .HashMap ;
4344import java .util .HashSet ;
4445import java .util .Map ;
4546import java .util .Objects ;
4647import java .util .Set ;
48+ import java .util .StringJoiner ;
49+ import java .util .function .Predicate ;
4750import java .util .stream .Collectors ;
4851import java .util .stream .Stream ;
4952
@@ -60,33 +63,100 @@ public SpongeImplementationExtension(final Project project, final Logger logger)
6063 this .logger = logger ;
6164 }
6265
63- public void copyModulesExcludingProvided (final Configuration source , final Configuration provided , final Configuration target ) {
64- final DependencyHandler deps = this .project .getDependencies ();
66+ private record Module (ModuleIdentifier id , String classifier ) {}
67+
68+ public String buildRuntimeFileNames (final Configuration config ) {
69+ final Configuration runtimeConfig = this .project .getConfigurations ().getByName ("runtimeClasspath" );
70+
71+ final Map <Module , String > runtimeFileNames = new HashMap <>();
72+ for (final ResolvedArtifact artifact : runtimeConfig .getResolvedConfiguration ().getResolvedArtifacts ()) {
73+ final ComponentIdentifier id = artifact .getId ().getComponentIdentifier ();
74+ if (id instanceof ModuleComponentIdentifier moduleId ) {
75+ runtimeFileNames .put (new Module (moduleId .getModuleIdentifier (), artifact .getClassifier ()), artifact .getFile ().getName ());
76+ }
77+ }
78+
79+ final StringJoiner joiner = new StringJoiner (File .pathSeparator );
80+
81+ for (final ResolvedArtifact artifact : config .getResolvedConfiguration ().getResolvedArtifacts ()) {
82+ final ComponentIdentifier id = artifact .getId ().getComponentIdentifier ();
83+
84+ String fileName = null ;
85+ if (id instanceof ModuleComponentIdentifier moduleId ) {
86+ fileName = runtimeFileNames .get (new Module (moduleId .getModuleIdentifier (), artifact .getClassifier ()));
87+ }
88+
89+ if (fileName == null ) {
90+ fileName = artifact .getFile ().getName ();
91+ }
92+
93+ joiner .add (fileName );
94+ }
95+
96+ return joiner .toString ();
97+ }
6598
99+ private String buildDependencyNotation (final ModuleComponentIdentifier moduleId , final String classifier ) {
100+ final ModuleIdentifier module = moduleId .getModuleIdentifier ();
101+ String notation = module .getGroup () + ":" + module .getName () + ":" + moduleId .getVersion ();
102+ if (classifier != null ) {
103+ notation += ":" + classifier ;
104+ }
105+ return notation ;
106+ }
107+
108+ public void copyModulesExcludingProvided (final Configuration source , final Configuration provided , final Configuration target ) {
66109 final Map <ModuleIdentifier , String > providedModuleVersions = new HashMap <>();
67- for (ResolvedArtifact artifact : provided .getResolvedConfiguration ().getResolvedArtifacts ()) {
110+ for (final ResolvedArtifact artifact : provided .getResolvedConfiguration ().getResolvedArtifacts ()) {
68111 final ComponentIdentifier id = artifact .getId ().getComponentIdentifier ();
69- if (id instanceof ModuleComponentIdentifier ) {
70- final ModuleComponentIdentifier moduleId = (ModuleComponentIdentifier ) id ;
112+ if (id instanceof ModuleComponentIdentifier moduleId ) {
71113 providedModuleVersions .put (moduleId .getModuleIdentifier (), moduleId .getVersion ());
72114 }
73115 }
74116
117+ this .copyModulesExcludingPredicate (source , (moduleId ) -> {
118+ final ModuleIdentifier module = moduleId .getModuleIdentifier ();
119+ final String version = moduleId .getVersion ();
120+
121+ final String providedVersion = providedModuleVersions .get (module );
122+ if (providedVersion == null ) {
123+ return false ;
124+ }
125+
126+ if (!providedVersion .equals (version )) {
127+ this .logger .warn ("Version mismatch for module {}. {} expects {} but {} has {}." , module , source .getName (), version , provided .getName (), providedVersion );
128+ }
129+ return true ;
130+ }, target );
131+ }
132+
133+ public void copyModulesExcludingPrefix (final Configuration source , final String group , final String namePrefix , final Configuration target ) {
134+ this .copyModulesExcludingPredicate (source , (moduleId ) -> {
135+ final ModuleIdentifier module = moduleId .getModuleIdentifier ();
136+ return module .getGroup ().equals (group ) && module .getName ().startsWith (namePrefix );
137+ }, target );
138+ }
139+
140+ public void copyModulesExcludingExact (final Configuration source , final String group , final String name , final Configuration target ) {
141+ this .copyModulesExcludingPredicate (source , (moduleId ) -> {
142+ final ModuleIdentifier module = moduleId .getModuleIdentifier ();
143+ return module .getGroup ().equals (group ) && module .getName ().equals (name );
144+ }, target );
145+ }
146+
147+ private void copyModulesExcludingPredicate (final Configuration source , final Predicate <ModuleComponentIdentifier > predicate , final Configuration target ) {
148+ final DependencyHandler deps = this .project .getDependencies ();
149+
75150 for (ResolvedArtifact artifact : source .getResolvedConfiguration ().getResolvedArtifacts ()) {
76151 final ComponentIdentifier id = artifact .getId ().getComponentIdentifier ();
77- if (id instanceof ModuleComponentIdentifier ) {
78- final ModuleComponentIdentifier moduleId = (ModuleComponentIdentifier ) id ;
79- final ModuleIdentifier module = moduleId .getModuleIdentifier ();
80- final String version = moduleId .getVersion ();
81-
82- final String providedVersion = providedModuleVersions .get (module );
83- if (providedVersion == null ) {
84- ModuleDependency dep = (ModuleDependency ) deps .create (module .getGroup () + ":" + module .getName () + ":" + version );
85- dep .setTransitive (false );
86- target .getDependencies ().add (dep );
87- } else if (!providedVersion .equals (version )) {
88- this .logger .warn ("Version mismatch for module {}. {} expects {} but {} has {}." , module , source .getName (), version , provided .getName (), providedVersion );
152+ if (id instanceof ModuleComponentIdentifier moduleId ) {
153+ if (predicate .test (moduleId )) {
154+ continue ;
89155 }
156+
157+ final ModuleDependency dep = (ModuleDependency ) deps .create (this .buildDependencyNotation (moduleId , artifact .getClassifier ()));
158+ dep .setTransitive (false );
159+ target .getDependencies ().add (dep );
90160 }
91161
92162 // projects are not copied because we cannot always recreate properly a project dependency from the resolved artefact
0 commit comments