Use a dependency's resources?

前端 未结 5 846
死守一世寂寞
死守一世寂寞 2020-12-01 09:09

In my Maven project there is one module (core) that has a few resources for its classes. When running classes inside the module its able to get its own resources. Everything

相关标签:
5条回答
  • 2020-12-01 09:33

    After lots of searching I finally stumbled upon a solution.

    My solution takes the core module and unpacks it with the Maven Dependency Plugin, making sure to exclude the META-INF folder and the org folder which is where the compiled classes are. The reason I'm excluding what I don't want instead of explicitly stating what I do want is so new resources can be added easily without me having to update my POM all the time.

    The reason I'm not using the assembly plugin or the remote resources plugin is because they use dedicated resource modules. I don't think I should have to make a core module and a core-resources module, with the core-resources module only containing logback and hibernate configuration files + some other stuff.

    Here is a copy of what I'm using to do this

        <build>
            <plugins>
                <!--Extract core's resources-->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>2.2</version>
                    <executions>
                        <execution>
                            <id>unpack</id>
                            <phase>generate-resources</phase>
                            <goals>
                                <goal>unpack-dependencies</goal>
                            </goals>
                            <configuration>
                                <includeGroupIds>${project.groupId}</includeGroupIds>
                                <includeArtifactIds>core</includeArtifactIds>
                                <excludeTransitive>true</excludeTransitive>
                                <overWrite>true</overWrite>
                                <outputDirectory>${project.build.directory}/core-resources</outputDirectory>
                                <excludes>org/**,META-INF/**,rebel.xml</excludes>
                                <overWriteReleases>true</overWriteReleases>
                                <overWriteSnapshots>true</overWriteSnapshots>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
            <!--New resource locations-->
            <resources>
                <resource>
                    <filtering>false</filtering>
                    <directory>${project.build.directory}/core-resources</directory>
                </resource>
                <resource>
                    <filtering>false</filtering>
                    <directory>${basedir}/src/main/resources</directory>
                </resource>
            </resources>
        </build> 
    
    0 讨论(0)
  • 2020-12-01 09:41

    I had a similar situation just now. I have a "test code builder" that takes data from the src/test/resources directory and pumps data into a H2 database. Works fine from module A, but when called from module B it didn't. It constructs a path with multiple semi-colons (due to resources being in the jar file); moduleA then fails to find files "local to itself".

    In the end, I added

        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
            </testResource>
            <testResource>
                <directory>../moduleA/src/test/resources</directory>
            </testResource>
        </testResources>
    

    to moduleB's build section, and now the build runs through fine and as expected.

    0 讨论(0)
  • 2020-12-01 09:45

    If the resources are located in /src/main/resources and classes in the JAR are accessing them using getResourceAsStream(), you should be able to access these files from classes in other JARs as well (also using getResourceAsStream()). This is because all the files located in /src/main/resources eventually land in the same logical directory (CLASSPATH). In other words: all the files located in /src/main/resources and all the classes compiled from /src/main/java are so to say merged into a single namespace.

    If you still can't access files even though all the classes are using uniform getResourceAsStream() API, you might have some class-loading visibility issues.

    0 讨论(0)
  • 2020-12-01 09:50

    I do not think it is a good idea. Suppose you have module A. It depends from 'B'. 'B' depends from C. Basically, you should care in A only about interface of B module. Perhaps, tomorrow it change dependence from C to D. If it would happen you will need change A. It is not good situation, is it? So declare dependence A from C in implicit way.

    In short: How can I access the resources of a dependency?

    In short: declare this dependence directly.

    0 讨论(0)
  • 2020-12-01 09:53

    If your dependent JAR has a class named OneDependent, then this should work:

    OneDependent.class.getResourceAsStream(resourcePath)
    
    0 讨论(0)
提交回复
热议问题