Specify common resources in a multi-module maven project
Asked Answered
M

6

43

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.

Mcfarland answered 5/7, 2013 at 15:44 Comment(0)
O
36

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.

Obsessive answered 5/7, 2013 at 15:49 Comment(5)
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.Mcfarland
A parent project with "pom" packaging does not have any resources, it is just a build file, that can be installed/deployed as an artifact.Obsessive
I see. So it is kind of impossible to do that with a Maven parent project? +1 but I am leaving the question yet open for a while if any other suggestions.Mcfarland
Yes, it won't work that way or, at least, it would break the conventions. Add dependency management, properties, repositories and all of that stuff to the parent pom but files, that need to be on the classpath while compiling, testing or later at runtime really should go into jars.Obsessive
@AndreasDolk As stated in yout answer, how to make other modules depend on the base-module, I am trying that but it says cannot add module to a project which is not packaged as pomInsensibility
A
6

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>                 
Antimagnetic answered 1/7, 2015 at 15:29 Comment(1)
Can you share the code snippet of "how you are accessing the shared resources"?Archegonium
S
2

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>
Spume answered 18/11, 2016 at 10:15 Comment(0)
R
2

Another way, put in your project root pom:

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <!-- don't propagate to child poms -->
            <!-- this will only execute in root pom -->
            <inherited>false</inherited>
            <configuration>
                <descriptors>
                    <descriptor>assembly.xml</descriptor>
                </descriptors>
                <!-- don't add classifier -->
                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    <plugins>

And example of assembly.xml

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">

    <id>resources</id>

    <formats>
        <format>jar</format>
    </formats>

    <includeBaseDirectory>false</includeBaseDirectory>

    <fileSets>
        <fileSet>
            <directory>${project.basedir}/resources/</directory>
            <outputDirectory/>
            <useDefaultExcludes>true</useDefaultExcludes>
            <includes>
                <include>**</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

Assembly plugin will generate artifact and attach it to current reactor, so it will be installed and deployed.

No you can use it as standard dependency event in the same pom.

Important is to trigger assembly (proper phase) before another plugin which will use generated artifact.

Eg. You can have in your root pom, bellow configuration will be propagated to all your module:

              <plugin>
                <artifactId>some-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>verify</phase>
                        <goals>
                            <goal>goal</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>your.project.groupid</groupId>
                        <artifactI>your.project.artifactId</artifactId>
                        <version>${project.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

You can see this method in project: https://github.com/s4u/pgp-keys-map resources directory is shared by all module.

Retirement answered 7/3, 2020 at 23:6 Comment(0)
L
1

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

Lightheaded answered 21/8, 2018 at 19:12 Comment(0)
T
0

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>
Tired answered 2/7, 2015 at 5:36 Comment(1)
This is not gonna work and is extremely bad practice.Infest

© 2022 - 2024 — McMap. All rights reserved.