rfscholte commented on a change in pull request #27: MCOMPILER-372 - fix test 
compile issue: added dependency test path for modules
URL: 
https://github.com/apache/maven-compiler-plugin/pull/27#discussion_r362945805
 
 

 ##########
 File path: src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java
 ##########
 @@ -478,58 +472,173 @@ protected void patchModulesForTestDependencies(
             throw new RuntimeException( e );
         }
         
+        // Prepare a path list containing test paths for modularized paths
+        // This path list will augment modules to be able to compile
+        List<String> listModuleTestPaths = new ArrayList<>( testPath.size() );
+        
+        // Browse modules
+        for ( Entry<File, ModuleNameSource> pathElt : 
result.getModulepathElements().entrySet() )
+        {
+            
+            File modulePathElt = pathElt.getKey();
+
+            // Get module name
+            JavaModuleDescriptor moduleDesc = result.getPathElements().get( 
modulePathElt );
+            String moduleName = ( moduleDesc != null ) ? moduleDesc.name() : 
null;
+            
+            if ( 
+                  // Is it a modularized compile elements?
+                  ( modulePathElt != null ) && ( moduleName != null )
+                  &&
+                  // Is it different from main module name
+                  !mainModuleName.equals( moduleName )
+            ) 
+            {
+                
+                // Get test path element
+                File moduleTestPathElt = getModuleTestPathElt( modulePathElt );
+                
+                if ( moduleTestPathElt != null ) 
+                {
+                    listModuleTestPaths.add( 
moduleTestPathElt.getAbsolutePath() );
+                }
+                
+            }
+            
+        }
+        
+        // Remove main target path
+        listModuleTestPaths.remove( mainModuleTarget );
+        listModuleTestPaths.remove( outputDirectory.getAbsolutePath() );
+        
+        // Freeze list
+        listModuleTestPaths = Collections.unmodifiableList( 
listModuleTestPaths );
+        
+        if ( getLog().isDebugEnabled() ) 
+        {
+            getLog().debug( "patchModule test paths:" );
+            for ( String moduleTestPath : listModuleTestPaths ) 
+            {
+                getLog().debug( "  " + moduleTestPath );
+            }
+            
+        }
+        
         // Get modularized dependencies resolved before
         for ( Entry<File, ModuleNameSource> pathElt : 
result.getModulepathElements().entrySet() ) 
         {
             
             File path = pathElt.getKey();
             ModuleNameSource moduleNameSource = pathElt.getValue();
             
-            // Retain only modules with an explicit module-info descriptor
-            if ( ModuleNameSource.MODULEDESCRIPTOR.equals( moduleNameSource ) )
+            // Get module name
+            JavaModuleDescriptor moduleDesc = result.getPathElements().get( 
path );
+            String moduleName = ( moduleDesc != null ) ? moduleDesc.name() : 
null;
+            
+            if ( 
+                  // Is it a modularized compile elements?
+                  ( path != null ) && ( moduleName != null )
+                  &&
+                  // Not an auto-module
+                  !moduleDesc.isAutomatic()
+                  &&
+                  // Is it different from main module name
+                  !mainModuleName.equals( moduleName )
+            ) 
             {
-                // Get module name
-                JavaModuleDescriptor moduleDesc = 
result.getPathElements().get( path );
-                String moduleName = ( moduleDesc != null ) ? moduleDesc.name() 
: null;
+            
+                // Add --add-reads <moduleName>=ALL-UNNAMED
+                compilerArgs.add( "--add-reads" );
+                StringBuilder sbAddReads = new StringBuilder();
+                sbAddReads.append( moduleName ).append( "=ALL-UNNAMED" );
+                compilerArgs.add( sbAddReads.toString() );
                 
-                if ( 
-                      // Is it a modularized compile elements?
-                      ( path != null ) && ( moduleName != null )
-                      &&
-                      // Is it different from main module name
-                      !mainModuleName.equals( moduleName )
-                ) 
+                // Add compile classpath if needed
+                if ( !listModuleTestPaths.isEmpty() )
                 {
-                
-                    // Add compile classpath
                     // Yes, add it as patch module
                     List<String> listPath = patchModules.get( moduleName );
                     // Make sure it is initialized
                     if ( listPath == null ) 
                     {
                         listPath = new ArrayList<>();
                         patchModules.put( moduleName, listPath );
-                    }
+                    } 
                     
                     // Add test compile path but not main module target
-                    listPath.addAll( testPath );
-                    // Remove main target path
-                    listPath.remove( mainModuleTarget );
-                    
+                    listPath.addAll( listModuleTestPaths );
                 }
+                
             }
             
-            if ( pathElements == null ) 
+        }
+
+        if ( pathElements == null ) 
+        {
+            pathElements = new LinkedHashMap<>( 
result.getPathElements().size() );
+            for ( Entry<File, JavaModuleDescriptor> entry : 
result.getPathElements().entrySet() ) 
             {
-                pathElements = new LinkedHashMap<>( 
result.getPathElements().size() );
-                for ( Entry<File, JavaModuleDescriptor> entry : 
result.getPathElements().entrySet() ) 
-                {
-                    pathElements.put( entry.getKey().getAbsolutePath(), 
entry.getValue() );
-                }
+                pathElements.put( entry.getKey().getAbsolutePath(), 
entry.getValue() );
             }
         }
     }
 
+    /**
+     * Get module test path element from module path element
+     * @param modulePathElt
+     * @return
+     */
+    private File getModuleTestPathElt( File modulePathElt )
 
 Review comment:
   This methods feels a bit like best guessing. It is better to loop over all 
the reactorProjects to find the Paths that belong to the same Maven Project 
(and because of that to the same module)

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to