3838
3939import  javax .inject .Inject ;
4040import  java .io .File ;
41- import  java .nio .file .Paths ;
4241import  java .util .ArrayList ;
4342import  java .util .List ;
4443
@@ -74,7 +73,7 @@ public void module(String directory) {
7473    public  void  module (String  directory , Action <Module > action ) {
7574        Module  module  = getObjects ().newInstance (Module .class , new  File (settings .getRootDir (), directory ));
7675        action .execute (module );
77-         includeModule (module , new  File (settings .getRootDir (), directory ));
76+         includeModule (module , new  File (settings .getRootDir (), directory ),  false );
7877    }
7978
8079    /** 
@@ -92,7 +91,7 @@ public void module(ProjectDescriptor project, Action<Module> action) {
9291        module .getArtifact ().set (project .getName ());
9392        module .getArtifact ().finalizeValue (); // finalize, as the project name can no longer be changed 
9493        action .execute (module );
95-         configureModule (module , project );
94+         configureModule (module , project ,  false );
9695    }
9796
9897    /** 
@@ -111,7 +110,7 @@ public void directory(String directory, Action<Directory> action) {
111110        action .execute (moduleDirectory );
112111
113112        for  (Module  module  : moduleDirectory .customizedModules .values ()) {
114-             includeModule (module , module .directory );
113+             includeModule (module , module .directory ,  false );
115114        }
116115        Provider <List <String >> listProvider  = getProviders ().of (ValueModuleDirectoryListing .class , spec  -> {
117116            spec .getParameters ().getExclusions ().set (moduleDirectory .getExclusions ());
@@ -124,7 +123,7 @@ public void directory(String directory, Action<Directory> action) {
124123            Module  module  = moduleDirectory .addModule (projectDir );
125124            if  (!module .getModuleInfoPaths ().get ().isEmpty ()) {
126125                // only auto-include if there is at least one module-info.java 
127-                 includeModule (module , new  File (modulesDirectory , projectDir ));
126+                 includeModule (module , new  File (modulesDirectory , projectDir ),  false );
128127            }
129128        }
130129    }
@@ -133,47 +132,58 @@ public void directory(String directory, Action<Directory> action) {
133132     * Configure a subproject as Platform for defining Module versions. 
134133     */ 
135134    public  void  versions (String  directory ) {
136-         String  projectName  = Paths .get (directory ).getFileName ().toString ();
137-         settings .include (projectName );
138-         settings .project (":"  + projectName ).setProjectDir (new  File (settings .getRootDir (), directory ));
139-         settings .getGradle ().getLifecycle ().beforeProject (new  ApplyJavaModuleVersionsPluginAction (":"  + projectName ));
135+         versions (directory , m  -> {});
140136    }
141137
142-     private  void  includeModule (Module  module , File  projectDir ) {
138+     /** 
139+      * Configure a subproject as Platform for defining Module versions. 
140+      */ 
141+     public  void  versions (String  directory , Action <Module > action ) {
142+         File  versionProjectDir  = new  File (settings .getRootDir (), directory );
143+         Module  module  = getObjects ().newInstance (Module .class , versionProjectDir );
144+         action .execute (module );
145+         includeModule (module , module .directory , true );
146+     }
147+ 
148+     private  void  includeModule (Module  module , File  projectDir , boolean  definesVersions ) {
143149        String  artifact  = module .getArtifact ().get ();
144150        settings .include (artifact );
145151        ProjectDescriptor  project  = settings .project (":"  + artifact );
146152        project .setProjectDir (projectDir );
147153
148-         configureModule (module , project );
154+         configureModule (module , project ,  definesVersions );
149155    }
150156
151-     private  void  configureModule (Module  module , ProjectDescriptor  project ) {
157+     private  void  configureModule (Module  module , ProjectDescriptor  project ,  boolean   definesVersions ) {
152158        String  mainModuleName  = null ;
153-         for  (String  moduleInfoPath  : module .getModuleInfoPaths ().get ()) {
154-             ModuleInfo  moduleInfo  = moduleInfoCache .put (project .getProjectDir (), moduleInfoPath ,
155-                     project .getPath (), module .getArtifact ().get (), module .getGroup (), settings .getProviders ());
156-             if  (moduleInfoPath .contains ("/main/" )) {
157-                 mainModuleName  = moduleInfo .getModuleName ();
159+         if  (!definesVersions ) {
160+             for  (String  moduleInfoPath  : module .getModuleInfoPaths ().get ()) {
161+                 ModuleInfo  moduleInfo  = moduleInfoCache .put (project .getProjectDir (), moduleInfoPath ,
162+                         project .getPath (), module .getArtifact ().get (), module .getGroup (), settings .getProviders ());
163+                 if  (moduleInfoPath .contains ("/main/" )) {
164+                     mainModuleName  = moduleInfo .getModuleName ();
165+                 }
158166            }
159167        }
160168
161169        String  group  = module .getGroup ().getOrNull ();
162170        List <String > plugins  = module .getPlugins ().get ();
163-         moduleProjects .add (new  ModuleProject (project .getPath (), group , plugins , mainModuleName ));
171+         moduleProjects .add (new  ModuleProject (project .getPath (), group , plugins , mainModuleName ,  definesVersions ));
164172    }
165173
166174    private  static  class  ModuleProject  {
167175        private  final  String  path ;
168176        private  final  @ Nullable  String  group ;
169177        private  final  List <String > plugins ;
170178        private  final  @ Nullable  String  mainModuleName ;
179+         private  final  boolean  definesVersions ;
171180
172-         public  ModuleProject (String  path , @ Nullable  String  group , List <String > plugins , @ Nullable  String  mainModuleName ) {
181+         public  ModuleProject (String  path , @ Nullable  String  group , List <String > plugins , @ Nullable  String  mainModuleName ,  boolean   definesVersions ) {
173182            this .path  = path ;
174183            this .group  = group ;
175184            this .plugins  = plugins ;
176185            this .mainModuleName  = mainModuleName ;
186+             this .definesVersions  = definesVersions ;
177187        }
178188    }
179189
@@ -192,8 +202,14 @@ public void execute(Project project) {
192202            for  (ModuleProject  m  : moduleProjects ) {
193203                if  (project .getPath ().equals (m .path )) {
194204                    if  (m .group  != null ) project .setGroup (m .group );
195-                     project .getPlugins ().apply (JavaModuleDependenciesPlugin .class );
196-                     project .getExtensions ().getByType (JavaModuleDependenciesExtension .class ).getModuleInfoCache ().set (moduleInfoCache );
205+                     if  (m .definesVersions ) {
206+                         project .getPlugins ().apply (JavaPlatformPlugin .class );
207+                         project .getPlugins ().apply (JavaModuleVersionsPlugin .class );
208+                         project .getExtensions ().getByType (JavaPlatformExtension .class ).allowDependencies ();
209+                     } else  {
210+                         project .getPlugins ().apply (JavaModuleDependenciesPlugin .class );
211+                         project .getExtensions ().getByType (JavaModuleDependenciesExtension .class ).getModuleInfoCache ().set (moduleInfoCache );
212+                     }
197213                    m .plugins .forEach (id  -> project .getPlugins ().apply (id ));
198214                    if  (m .mainModuleName  != null ) {
199215                        project .getPlugins ().withType (ApplicationPlugin .class , p  ->
@@ -203,22 +219,4 @@ public void execute(Project project) {
203219            }
204220        }
205221    }
206- 
207-     private  static  class  ApplyJavaModuleVersionsPluginAction  implements  IsolatedAction <Project > {
208- 
209-         private  final  String  projectPath ;
210- 
211-         public  ApplyJavaModuleVersionsPluginAction (String  projectPath ) {
212-             this .projectPath  = projectPath ;
213-         }
214- 
215-         @ Override 
216-         public  void  execute (Project  project ) {
217-             if  (projectPath .equals (project .getPath ())) {
218-                 project .getPlugins ().apply (JavaPlatformPlugin .class );
219-                 project .getPlugins ().apply (JavaModuleVersionsPlugin .class );
220-                 project .getExtensions ().getByType (JavaPlatformExtension .class ).allowDependencies ();
221-             }
222-         }
223-     }
224222}
0 commit comments