Search code examples
javamavenjarmaven-3maven-plugin

How to include custom folder in Maven Package?


Below is my project structure:

META-INF/abc.jpg
META-INF/xyz.jpg
src/com/hcc/files.java
pom.xml

Below are the contents of pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>xyz</groupId>
    <artifactId>zyx</artifactId>
    <packaging>jar</packaging>
    <version>1.0.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <build>
        <resources>
            <resource>
                <directory>src</directory>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <copy file="${basedir}/META-INF/services/abc.px" tofile="${project.build.directory}/META-INF/services/abc.px" />
                                <copy file="${basedir}/META-INF/services/xyz.px" tofile="${project.build.directory}/META-INF/services/xyz.px" />
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>abc</groupId>
            <artifactId>abc</artifactId>
            <version>936</version>
            <scope>system</scope>
            <systemPath>path</systemPath>
        </dependency>
    </dependencies>
</project>

When I'm trying to compile using mvn compile it creates a target folder.

ex - target/classes/com/hcc

How can I include META-INF folder as it is while creating package i.e mvn package, so that my jar file includes target folder files and META-INF folder as it is?


Solution

  • In the build section of your pom.xml, under the maven war plugin declaration you can add the necessary configuration in which you specify your web resource as a directory and it's output path too:

           <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <groupId>org.apache.maven.plugins</groupId>
                <version>${maven-war-plugin.version}</version>
                <configuration>
                    <webResources>
                        <resource>
                            <directory>META-INF</directory>
                            <filtering>true</filtering>
                            <targetPath>META-INF</targetPath>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
    

    Or by adding it as a resource directly under build :

    <build>
       ....
       <resources>
            <resource>
                <directory>META-INF</directory>
            </resource>
        </resources>
    </build>