Home > Software engineering >  How can I read a File from another maven module?
How can I read a File from another maven module?

Time:11-03

My Project looks like:

project:
├── pom.xml
│
├── project-base
│   ├── pom.xml
│   └── src
│       └── main
│           └── java
│               └── com
│                   └── company
│                       └── project
│                           └── util
│                               └── io
│                                   └── YamlReader
│ 
├── project-algo
│   ├── pom.xml
│   └── src
│       └── test
│           └── java
│               └── com
│                   └── company
│                       └── project
│                           └── AlgorithmTest
│
└── project-config
    ├── pom.xml
    └── configs
        └── application_config.yaml

on the AlgorithmTest I have to read some values from application_config.yaml which is located in another maven module.

To read yaml file I created a utility interface called YamlReader that uses jackson-dataformat-yaml.

At the moment I didn't find a way to read file from another module, so I'm copying the application_config.yaml into the src/test/resources folder of the project-algo module, but it's not ok, every time I have to change somthing on the application_config.yaml, I need to update both file.

I'm using the following method:

public static <C extends ConfigParent> C readConfiuration(Class<C> configClass, String yamlFileName) throws IOException {
    InputStream inputStream = new FileInputStream(new File("./src/test/resources/"   yamlFileName));

    YAMLMapper mapper = new YAMLMapper();
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);

    return mapper.readValue(inputStream, configClass);
}

This works fine for each Yaml file located into the src/test/resources folder of the project-algo module.

To read from project-config module I tried the following code:

public static <C extends ConfigParent> C readConfiuration(Class<C> configClass, String yamlFileName) throws IOException {
    InputStream inputStream = new FileInputStream(new File("./project-config/configs/"   yamlFileName));
    
    YAMLMapper mapper = new YAMLMapper();
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
    
    return mapper.readValue(inputStream, configClass);
}

and also

public static <C extends ConfigParent> C readConfiuration(Class<C> configClass, String yamlFileName) throws IOException {
    
    Module specificModule = ModuleLayer.boot()
        .findModule("project-config")
        .orElseThrow(IOException::new);
    InputStream inputStream = specificModule.getResourceAsStream(yamlFileName);
    
    YAMLMapper mapper = new YAMLMapper();
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
    
    return mapper.readValue(inputStream, configClass);

}

but it doesn't work.

Maybe can be helpful, poms are configured in the following way:

project pom.xml contains all the modules:

<modules>
    <module>project-config</module>
    <module>project-base</module>
    <module>project-algo</module>
</modules>

project-algo pom.xml contains the dependency of project-base :

<dependency>
  <groupId>com.company.project</groupId>
  <artifactId>project-base</artifactId>
  <version>${project.version}</version>
</dependency>

The pom.xml of project-base and project-config doesn't contain any dependency to other modules of the project.

CodePudding user response:

Seems project-config is a sibling module. Add its dependency in your project-algo pom and run "mvn clean package"

CodePudding user response:

Instead of

InputStream inputStream = new FileInputStream(new File("./src/test/resources/"   yamlFileName));

I fix the problem using

InputStream inputStream = YamlReader.class.getResourceAsStream("/"   yamlFileName);

Seems to be able to read from everywhere. I test it with a YAML file in the "src/test/resources" folder and another yaml file in the "configs" folder on the "project-config" module.

  • Related