sormuras / junit-platform-maven-plugin

Maven Plugin launching the JUnit Platform
Apache License 2.0
61 stars 15 forks source link

Include `runtime` scoped dependencies in main classloader #76

Closed sormuras closed 3 years ago

sormuras commented 3 years ago

Discussed in https://github.com/sormuras/junit-platform-maven-plugin/discussions/75

Originally posted by **zregvart** July 27, 2021 Seems like dependencies scoped as `runtime` are not included in the main classloader. For example if I add`ch.qos.logback:logback-classic` dependency in `runtime` scope, `org.slf4j:slf4j-api` in `compile` scope will not detect it and will issue a warning about missing `org.slf4j.impl.StaticLoggerBinder` class. The only workaround I've found is to run in isolation of `MERGED` or `NONE`. I've created [an example](https://github.com/zregvart/junit-maven-runtime) to help illustrate this. When running with 1.1.2 I get: ``` $ mvn test ... [INFO] --- junit-platform-maven-plugin:1.1.2:launch (injected-launch) @ junit-maven-runtime --- [INFO] Launching JUnit Platform 1.7.2... SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. ... ``` And if I change the isolation to `MERGED` or `NONE` I get: ``` $ mvn test ... [INFO] --- junit-platform-maven-plugin:1.1.2:launch (injected-launch) @ junit-maven-runtime --- [INFO] Launching JUnit Platform 1.7.2... 12:42:31.608 [pool-3-thread-1] INFO io.github.zregvart.Log - Hello from main 12:42:31.616 [pool-3-thread-1] INFO i.g.zregvart.RuntimeClasspathTest - Hello from test ... ``` The real issue I'm having with this is that I would like to declare some dependencies in `runtime` scope so the main code doesn't depend on them at compile time, and have the implementation discovered via service loader mechanism at runtime. This might be a conscious decision and I'm wondering about the reason behind this?
sormuras commented 3 years ago

Before applying the suggested change:

Isolator Path Layering
main
  classes                                     
  slf4j-api-1.7.25.jar                        
test
  test-classes                                
  logback-classic-1.2.3.jar                   
  logback-core-1.2.3.jar                      
  junit-jupiter-api-5.7.1.jar                 
  apiguardian-api-1.1.0.jar                   
  opentest4j-1.2.0.jar                        
  junit-platform-commons-1.7.1.jar            
launcher
  junit-platform-launcher-1.7.1.jar           
  junit-platform-engine-1.7.1.jar             
  junit-platform-reporting-1.7.1.jar          
  junit-jupiter-engine-5.7.1.jar              
isolator
  junit-platform-isolator-worker-1.0.0-M10.jar
  junit-platform-isolator-1.0.0-M10.jar       

After applying the suggested change:

Isolator Path Layering
main
  classes                                     
  slf4j-api-1.7.25.jar                        
  logback-classic-1.2.3.jar                   
  logback-core-1.2.3.jar                      
test
  test-classes                                
  junit-jupiter-api-5.7.1.jar                 
  apiguardian-api-1.1.0.jar                   
  opentest4j-1.2.0.jar                        
  junit-platform-commons-1.7.1.jar            
launcher
  junit-platform-launcher-1.7.1.jar           
  junit-platform-engine-1.7.1.jar             
  junit-platform-reporting-1.7.1.jar          
  junit-jupiter-engine-5.7.1.jar              
isolator
  junit-platform-isolator-worker-1.0.0-M10.jar
  junit-platform-isolator-1.0.0-M10.jar       

Seems to work and does not break existing integration tests.