How to access resources in jar where it can be present in multiple jar

前端 未结 2 1465
情书的邮戳
情书的邮戳 2021-01-13 21:09

I have a project where I generate lots of code against many XSD. To keep things separate each set of XSD are bundled together within a project. I have multiple project tha

相关标签:
2条回答
  • 2021-01-13 21:44

    I usually make it a rule to add a resource directory into each JAR with resources that are unique to that JAR held under it. For example (in the Maven structure)

    module1/src/main/resources/module1/example.xsd
    module2/src/main/resources/module2/example.xsd
    

    The XSDs are then referenced using

    InputStream module1XSD= SomeClass.class.getResourceAsStream("/module1/example.xsd");
    InputStream module2XSD= SomeClass.class.getResourceAsStream("/module2/example.xsd");
    

    so long as the JARs for module1 and module2 have been placed on the classpath of the application containing SomeClass.

    Spring contexts would reference these as

    classpath:module1/example.xsd,
    classpath:module2/example.xsd
    

    This does mean that you'll have to be able to move the location of XSDs in the JARs that you generate. Maybe even regenerating them through a build script.

    0 讨论(0)
  • 2021-01-13 21:55

    If the XSDs all have the same name, you will need another criteria with knowledge of what the "right one" is. Can you embed some information in it (or with it, like a properties file or something) indicating its purpose?

    And, a more convenient iterator to get them might be:

     getClass().getClassLoader().getResources("/file.xsd");
    

    or something. (Relies on the classpath, whether it is on the file system or in a jar)

    0 讨论(0)
提交回复
热议问题