Search code examples
javagradlemicronaut

Micronaut property injection does not work with Gradle multi-project build


I am trying to setup Micronaut (1.1.0.BUILD-SNAPSHOT) project with a shared package using Gradle multi-project build so I can put the common logic in one place.

I encountered the problem during the creation of the property file for the library. I put some library specific properties in application.yml but looks like they are not pushed forward to the application layer:

lib/src/main/java/lib/CommonSingeton.java

@Singleton
public class CommonSingleton {

  public CommonSingleton(
    @Value("${lib.testProperty}") String value
  ) {
    System.out.println(value);
  }
}

lib/src/main/resources/application.yml

lib:
  testProperty: test

app/src/main/java/app/AppController.java

@Controller("/app")
public class AppController {

  @Inject
  private CommonSingleton commonSingleton;

  @Get(produces = MediaType.TEXT_PLAIN)
  public String index() {
    return "Hello World";
  }
}

The error is:

Unexpected error occurred: Failed to inject value for parameter [value] of class: lib.CommonSingleton

Message: Error resolving property value [${lib.testProperty}]. Property doesn't exist
Path Taken: AppController.commonSingleton --> new CommonSingleton([String value])
io.micronaut.context.exceptions.DependencyInjectionException: Failed to inject value for parameter [value] of class: lib.CommonSingleton

The complete example is available on Github.


Solution

  • Currently it is designed to just read a single application.yml. If you were to remove the file from app, it would be read from lib. You can file an issue to make us aware this is something you want. https://github.com/micronaut-projects/micronaut-core/issues