Home > Software design >  Include files from a JAR in a dependent project
Include files from a JAR in a dependent project

Time:04-05

I'd like to synchronize log4j and logback config files across multiple projects. I have one project (Project A) that contains the log4j and logback dependencies, along with the config files.

Project A

  • src/test/resources
    • log4j2.xml
    • logback-test.xml

Project B has a dependency on Project A. I would like to include the log config files in Project A's JAR and have them automatically put in a specific target folder in Project B when resolving Maven dependencies for Project B.

I have tried maven-jar-plugin in Project A but it doesn't seem to work for my purpose.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.2.2</version>
    <configuration>
        <includes>
            <include>src/test/resources/log4j2.xml</include>
            <include>src/test/resources/logback-test.xml</include>
        </includes>
    </configuration>
</plugin>

Any help would be appreciated.

Thanks!

CodePudding user response:

Please don't put logging configuration files into JARs, if someone depends on your JAR, your configuration may overwrite theirs. That depends on which JAR is loaded first

If you want, like this. And package them into JAR

  • src/main/resources
    • log4j2.xml
    • logback.xml

CodePudding user response:

You can use Apache Maven Remote Resources Plugin

This plugin is used to retrieve JARs of resources from remote repositories, process those resources, and incorporate them into JARs you build with Maven.

A very common use-case is the need to package certain resources in a consistent way across your organization. For example at Apache, it is required that every JAR produced contains a copy of the Apache license and a notice file that references all used software in a given project

  1. Define in Project A which resources you want to share or better create separate project for shared resources
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-remote-resources-plugin</artifactId>
                <version>1.7.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>bundle</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <resourcesDirectory>src/test/resources/</resourcesDirectory>
                    <includes>
                        <include>log4j2.xml</include>
                        <include>logback-test.xml</include>
                      </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>

Maven will create resources bundle file for sharing resources ${basedir}/target/classes/META-INF/maven/remote-resources.xml

<remoteResourcesBundle xsi:schemaLocation="http://maven.apache.org/remote-resources/1.1.0 https://maven.apache.org/xsd/remote-resources-1.1.0.xsd"
    xmlns="http://maven.apache.org/remote-resources/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <remoteResources>
    <remoteResource>log4j2.xml</remoteResource>
    <remoteResource>logback-test.xml</remoteResource>
  </remoteResources>
  <sourceEncoding>UTF-8</sourceEncoding>
</remoteResourcesBundle>

See documentation

  1. Configure other modules to use the shared resources
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-remote-resources-plugin</artifactId>
                <version>1.7.0</version>
                <configuration>
                    <outputDirectory>[your output directory]</outputDirectory>
                    <resourceBundles>
                        <!--The resource bundles that will be retrieved and processed. For example: org.test:shared-resources:${project.version}-->
                        <resourceBundle>groupId:artifactId:version[:type[:classifier]]</resourceBundle> 
                    </resourceBundles>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>process</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

See documentation

Alternative solution:
Apache Maven Dependency Plugin It can copy and/or unpack artifacts from local or remote repositories to a specified location.
It was described there Maven: Extract dependency resources before test and there Use a dependency's resources?

  • Related