Maven: include files in JAR's META-INF
Asked Answered
N

5

14

I'm using Maven to build a Java project, and I've got a couple files, CHANGELOG and LICENSE, that I'd like to copy to the META-INF directory inside the JAR file.

So far what I've got working is the following:

<build>
    <resources>
        <resource>
            <directory>${project.basedir}</directory>
            <includes>
                <include>CHANGELOG</include>
                <include>LICENSE</include>
            </includes>
            <targetPath>META-INF</targetPath>
        </resource>
    </resources>
    <plugins>
        ...

but that also copies those two files to the classes/META-INF directory when compiling.

So I'd like the files to be included in the JAR file, but nowhere else. Is there a way to do this?

Nitrous answered 8/8, 2016 at 15:39 Comment(0)
L
23

You don't need to specify any custom maven configuration to address your need.

In src/main/resources, simply create a META-INF folder and place your files here.
In this way, you could find them in the META-INF folder of the built JAR.

Besides, you should remove the <resource> element you added in the pom.xml since it changes the default resource folder to the root of the project instead of the default src/main/resources.

Loupe answered 8/8, 2016 at 19:11 Comment(8)
But the result is the same, right? If we just compile it will copy these resource files as well. I want them copied just to the JAR file.Godrich
not really, since in the Jar, the resources will not be duplicated in classes/META-INF. They will be only in the META-INF dir as explained.Loupe
Hi @davidxxx. I noticed your comment when the resources should go to META-INF, but is it possible to put resources in the root of the jar file, next to META-INF? Thanks.Cervicitis
Hi @Cristian. Of course you can as all resources (files and folders) that you will store in your project in src/main/resources will be by default at the root of the built JAR.Loupe
I'm using Spring Boot and everything goes under BOOT-INF which is next to META-INF. Any idea here? Thanks.Cervicitis
@Cervicitis It is a reapckaging jar by Spring Boot to allow the application to be autobootable. It is not a classic jar. You should not bother about it. The jar will stay accessible to the application all the same. Which is your problem exactly ?Loupe
I'm deploying to AWS and I want to include in the JAR root some configuration files and a configuration folder with files. A Spring Boot jar contains the folders BOOT-INF, META-INF and org, and I want to include the folder .ebextensions and the file cron.yaml.Cervicitis
@Cervicitis You can do it with a Spring Boot autobootable WAR but not with Spring Boot autobootable JAR that is not designed to have this layout. You will indeed find your resources in BOOT-INF\classes. The problem is that you could not change it easily as this repackaging is performed by Spring Boot itself. You could try to create a Layout and to pass it to the layoutFactory property of the repackage plugin of Spring Boot but no guarantee. docs.spring.io/spring-boot/docs/current/maven-plugin/… Anyway the question is interesting, why not ask it on SO ?Loupe
C
3

If you want to add files and a directory to the META-INF folder use this code in your pom.xml

   <build>
        <finalName>CustomJsfComponents</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**.xml</include>
                    <include>**resources/**</include>
                </includes>
                <targetPath>META-INF</targetPath>
            </resource>
        </resources>
    </build>
Corene answered 27/11, 2019 at 21:12 Comment(0)
W
1

Modify your pom by adding webResources

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                <webResources>
                    <resource>
                        <directory>target/classes/META-INF/</directory>
                        <includes>
                            <include>*.*</include>
                        </includes>
                        <targetPath>META-INF/</targetPath>
                    </resource>
                </webResources>
            </configuration>
        </plugin>

Consider the config.properties file to be found in

src / main / resources / META-INF / config.properties

War example output war output example

Watch answered 5/1, 2021 at 7:38 Comment(0)
S
0

If you want to add your files to META-INF when creating a WAR, you can create your own META-INF folder in webapp directory.

src/main/webapp/META-INF
Straightaway answered 28/10, 2022 at 18:6 Comment(0)
S
0

I tried adding META-INF/resources/org.glassfish.jersey.internal.spi.ForcedAutoDiscoverable with the following content:

org.glassfish.jersey.jaxb.internal.JaxbAutoDiscoverable

But after doing the jar package, it still contained only:

org.glassfish.jersey.server.wadl.internal.WadlAutoDiscoverable
org.glassfish.jersey.server.internal.monitoring.MonitoringAutodiscoverable

Then I manually edited jar so that it contains:

org.glassfish.jersey.server.wadl.internal.WadlAutoDiscoverable
org.glassfish.jersey.server.internal.monitoring.MonitoringAutodiscoverable
org.glassfish.jersey.jaxb.internal.JaxbAutoDiscoverable

Then application works fine. Now I just need to find a way to add this missing line to that file.

Sherd answered 22/2 at 12:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.