Specify common resources in a multi-module maven project

一笑奈何 提交于 2019-12-18 03:01:53

问题


Is there any way to share resources between modules of a parent project in Maven? For example, I would like to specify one log4j.properties file for all the modules in a multi-module Maven project.

Generally, I use Eclipse IDE to create the parent project by choosing a general project and then convert it to a Maven project by specifying a packaging of pom. This creates a "clean" project structure without src and etc. folders. I wonder where such a shared resource should be put in this case.

EDIT1: I would like to put the common resources in the parent project.


回答1:


I'd create one additional "base" module (project), packaging "jar", that contains the common resources in src/main/resources. Then I'd make the other modules depend on that project. Now they see the common resources on their classpaths.




回答2:


Antoher possibility is to use a remote resource bundle. You would be able to configure it in the parent project. In this example I wanted to copy some files just for tests. If you use this you will need to create the bundle in another project.

    <plugin>      
        <groupId>org.apache.maven.plugins</groupId>         
        <artifactId>maven-remote-resources-plugin</artifactId>
        <version>1.5</version>
        <configuration>
            <resourceBundles>
                <resourceBundle>es.sca:myBundle:1.0.0</resourceBundle>
            </resourceBundles>
            <attachToMain>false</attachToMain>
            <attachToTest>true</attachToTest>
            <appendedResourcesDirectory>${basedir}/src/test/resources</appendedResourcesDirectory>
        </configuration>            
        <executions>
            <execution>
                <goals>
                    <goal>process</goal>
                </goals>
            </execution>
        </executions>                   
    </plugin>                 



回答3:


Yes, it seems as a possible solution. But I was interested whether it is possible to specify these resources in the parent project (without introducing additional module) since the parent project specifies all the common dependencies and Maven configurations for the child modules, I think that the parent project is the most suitable place also for the common resources.

In case of packaging type pom , when goal package specified to manage your shared resources, just add next (check folders) into build section of pom file :

        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
       <executions>
           <execution>
               <id>copy-config-files</id>
               <phase>package</phase>
               <goals>
                   <goal>copy-resources</goal>
               </goals>
               <configuration>
                   <outputDirectory>${project.build.directory}/logconfig</outputDirectory>
                   <resources>
                       <resource>
                        <filtering>false</filtering>
                        <directory>${project.basedir}/src/main/resources</directory>
                       </resource>
                   </resources>
               </configuration>
        </execution>
       </executions>
    </plugin>



回答4:


I think you can just add the resources and/or testResources elements to your pom. E.g. to access an additional test resource directory add:

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

see Maven - Override test resource folder




回答5:


I managed it to work like this:

I create a project/assembly/test/resources/META-INF/persistence.xml file, and add this to my pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-test-persistence-xml-resources</id>
            <phase>process-test-sources</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>src/</outputDirectory>
                <resources>
                    <resource>
                        <directory>${project.parent.basedir}/assembly/</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>


来源:https://stackoverflow.com/questions/17492622/specify-common-resources-in-a-multi-module-maven-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!