13

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?

Julián Urbano
  • 8,378
  • 1
  • 30
  • 52

4 Answers4

20

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.

Naman
  • 27,789
  • 26
  • 218
  • 353
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • 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. – Julián Urbano Aug 09 '16 at 01:11
  • 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. – davidxxx Aug 09 '16 at 08:12
  • 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. – Cristian Jul 18 '17 at 06:39
  • 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. – davidxxx Jul 18 '17 at 08:32
  • I'm using Spring Boot and everything goes under BOOT-INF which is next to META-INF. Any idea here? Thanks. – Cristian Jul 18 '17 at 08:37
  • @Cristian 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 ? – davidxxx Jul 18 '17 at 08:38
  • 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. – Cristian Jul 18 '17 at 08:52
  • 1
    @Cristian 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. https://docs.spring.io/spring-boot/docs/current/maven-plugin/repackage-mojo.html#layoutFactory Anyway the question is interesting, why not ask it on SO ? – davidxxx Jul 18 '17 at 10:25
2

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>
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

Ronald Coarite
  • 4,460
  • 27
  • 31
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
Edward D. Wilson
  • 351
  • 1
  • 4
  • 11