1

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?

RBT
  • 24,161
  • 21
  • 159
  • 240
Naveen Kumar
  • 1,266
  • 1
  • 21
  • 50

2 Answers2

1

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>
RBT
  • 24,161
  • 21
  • 159
  • 240
  • Thanks @ibrahim this is adding META-INF/maven folder inside the jar not the original content of META-INF folder, I need exact folder as it is in jar kind of just copy it in jar. – Naveen Kumar Mar 04 '20 at 09:58
  • Hey @ibrahim-ben-ali I have updated the `pom.xml` now it copies content of META-INF folder as it is to target folder but when I `run mvn package` command it's not appearing correctly inside jar. – Naveen Kumar Mar 04 '20 at 10:24
0

In the build section of my pom.xml, I have added one more resource tag and respective configuration to copy custom files in jar while packaging.

<resource>
    <directory>META-INF</directory>
    <includes>
        <include>**services/**</include>
    </includes>
    <targetPath>META-INF</targetPath>
</resource>
Naveen Kumar
  • 1,266
  • 1
  • 21
  • 50