Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include files in Maven JAR package

Tags:

java

maven

jar

I have a maven project with the following structure:

project

-src
--main
---java
----(All my .java files are here in their packages)
---resource
----(All my resources are here)

-database (HSQLDB files)
--db.script
--db.properties
--db.data

-target
--Maven build directory

pom.xml

I want the maven jar plugin to package in the database folder when it builds a JAR file. I've tried including as outlined here: https://maven.apache.org/plugins/maven-jar-plugin/examples/include-exclude.html, but it doesn't find the directory "../database" (or ../../database OR ../../../database!), and also it now doesn't include any of the classes or resources (It was including them before).

So what should I have in the configuration of the maven jar plugin? Which is the correct path to include and how do I keep the files it was including before?

Thanks

like image 761
Duane Allman Avatar asked Nov 13 '17 16:11

Duane Allman


People also ask

How to compile a Maven project into a jar?

In order to compile the project into an executable jar, please run Maven with mvn clean package command. Hopefully, this article gives you some more insights on the topic and you will find your preferred approach depending on your needs. One quick final note – make sure the licenses of the jars you're bundling don't...

Where are the Maven-jar-plugin resources located?

What you tried to do by configuring the maven-jar-plugin defines only the filetset to exclude or include in the built JAR. But these files still have to be located in a resources folder where Maven looks for resources. move database in the standard directory : src/main/resources.

How to install local dependencies into Maven projects?

There are multiple ways we can install local dependencies into maven projects Adding local dependencies systemPath in pom.xml install-file goal is a maven commands used to install local jar files This is easy and simple way to install jar from local repository or path of jar file.

How to install JAR file using MVN?

This is easy and simple way to install jar from local repository or path of jar file. mvn install:install-file \ -Dfile=jar-file-path> \ -DgroupId=com.company.feature \ -DartifactId=feature \ -Dversion=version \ -Dpackaging=packaging \ -DgeneratePom=true -DcreateChecksum=true


1 Answers

By default, only resources located in src/main/resources are added in the JAR.
What you tried to do by configuring the maven-jar-plugin defines only the filetset to exclude or include in the built JAR.
But these files still have to be located in a resources folder where Maven looks for resources.

So, you have two ways to solve your requirement:

  • move database in the standard directory : src/main/resources.

  • specifies two resource folders for resources :

To do the latter, in the pom.xml add in the build tag :

<resources>
     <resource>
       <directory>src/main/resources</directory>          
     </resource>
     <resource>
       <directory>database-resources</directory>
     </resource>
</resources>

Then add the database folder that you want package inside the database-resources.

like image 172
davidxxx Avatar answered Oct 21 '22 08:10

davidxxx