@@ -72,10 +72,27 @@ public void module(String directory) {
7272 * Register and configure Module located in the given folder, relative to the build root directory.
7373 */
7474 public void module (String directory , Action <Module > action ) {
75- Module module = getObjects ().newInstance (Module .class , settings .getRootDir ());
76- module .getDirectory ().set (directory );
75+ Module module = getObjects ().newInstance (Module .class , new File (settings .getRootDir (), directory ));
7776 action .execute (module );
78- includeModule (module , new File (settings .getRootDir (), module .getDirectory ().get ()));
77+ includeModule (module , new File (settings .getRootDir (), directory ));
78+ }
79+
80+ /**
81+ * {@link JavaModulesExtension#module(ProjectDescriptor, Action)}
82+ */
83+ public void module (ProjectDescriptor project ) {
84+ module (project , m -> {});
85+ }
86+
87+ /**
88+ * Register and configure Module already registered as project by an 'include' statement.
89+ */
90+ public void module (ProjectDescriptor project , Action <Module > action ) {
91+ Module module = getObjects ().newInstance (Module .class , project .getProjectDir ());
92+ module .getArtifact ().set (project .getName ());
93+ module .getArtifact ().finalizeValue (); // finalize, as the project name can no longer be changed
94+ action .execute (module );
95+ configureModule (module , project );
7996 }
8097
8198 /**
@@ -94,7 +111,7 @@ public void directory(String directory, Action<Directory> action) {
94111 action .execute (moduleDirectory );
95112
96113 for (Module module : moduleDirectory .customizedModules .values ()) {
97- includeModule (module , new File ( modulesDirectory , module .getDirectory (). get ()) );
114+ includeModule (module , module .directory );
98115 }
99116 Provider <List <String >> listProvider = getProviders ().of (ValueModuleDirectoryListing .class , spec -> {
100117 spec .getParameters ().getExclusions ().set (moduleDirectory .getExclusions ());
@@ -119,7 +136,7 @@ public void versions(String directory) {
119136 String projectName = Paths .get (directory ).getFileName ().toString ();
120137 settings .include (projectName );
121138 settings .project (":" + projectName ).setProjectDir (new File (settings .getRootDir (), directory ));
122- settings .getGradle ().getLifecycle ().beforeProject (new ApplyJavaModuleVersionsPluginAction (projectName ));
139+ settings .getGradle ().getLifecycle ().beforeProject (new ApplyJavaModuleVersionsPluginAction (":" + projectName ));
123140 }
124141
125142 private void includeModule (Module module , File projectDir ) {
@@ -128,28 +145,32 @@ private void includeModule(Module module, File projectDir) {
128145 ProjectDescriptor project = settings .project (":" + artifact );
129146 project .setProjectDir (projectDir );
130147
148+ configureModule (module , project );
149+ }
150+
151+ private void configureModule (Module module , ProjectDescriptor project ) {
131152 String mainModuleName = null ;
132- for (String path : module .getModuleInfoPaths ().get ()) {
133- ModuleInfo moduleInfo = moduleInfoCache .put (projectDir , path ,
134- module .getArtifact ().get (), module .getGroup (), settings .getProviders ());
135- if (path .contains ("/main/" )) {
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/" )) {
136157 mainModuleName = moduleInfo .getModuleName ();
137158 }
138159 }
139160
140161 String group = module .getGroup ().getOrNull ();
141162 List <String > plugins = module .getPlugins ().get ();
142- moduleProjects .add (new ModuleProject (artifact , group , plugins , mainModuleName ));
163+ moduleProjects .add (new ModuleProject (project . getPath () , group , plugins , mainModuleName ));
143164 }
144165
145166 private static class ModuleProject {
146- private final String artifact ;
167+ private final String path ;
147168 private final String group ;
148169 private final List <String > plugins ;
149170 private final String mainModuleName ;
150171
151- public ModuleProject (String artifact , String group , List <String > plugins , String mainModuleName ) {
152- this .artifact = artifact ;
172+ public ModuleProject (String path , String group , List <String > plugins , String mainModuleName ) {
173+ this .path = path ;
153174 this .group = group ;
154175 this .plugins = plugins ;
155176 this .mainModuleName = mainModuleName ;
@@ -170,7 +191,7 @@ public ApplyPluginsAction(List<ModuleProject> moduleProjects, ModuleInfoCache mo
170191 @ Override
171192 public void execute (Project project ) {
172193 for (ModuleProject m : moduleProjects ) {
173- if (project .getName ().equals (m .artifact )) {
194+ if (project .getPath ().equals (m .path )) {
174195 if (m .group != null ) project .setGroup (m .group );
175196 project .getPlugins ().apply (JavaModuleDependenciesPlugin .class );
176197 project .getExtensions ().getByType (JavaModuleDependenciesExtension .class ).getModuleInfoCache ().set (moduleInfoCache );
@@ -187,15 +208,15 @@ public void execute(Project project) {
187208 @ NonNullApi
188209 private static class ApplyJavaModuleVersionsPluginAction implements IsolatedAction <Project > {
189210
190- private final String projectName ;
211+ private final String projectPath ;
191212
192- public ApplyJavaModuleVersionsPluginAction (String projectName ) {
193- this .projectName = projectName ;
213+ public ApplyJavaModuleVersionsPluginAction (String projectPath ) {
214+ this .projectPath = projectPath ;
194215 }
195216
196217 @ Override
197218 public void execute (Project project ) {
198- if (projectName .equals (project .getName ())) {
219+ if (projectPath .equals (project .getPath ())) {
199220 project .getPlugins ().apply (JavaPlatformPlugin .class );
200221 project .getPlugins ().apply (JavaModuleVersionsPlugin .class );
201222 project .getExtensions ().getByType (JavaPlatformExtension .class ).allowDependencies ();
0 commit comments