Include JAR Lib Maven
This is very important to the project if you are using maven to maintain dependencies in your project. Including dependencies to the maven project is quite straight forward where you only use dependencies tag with its group id and artifact id and jar dependencies. But most tricky part is when you are taking final build of your project you will have include all it dependencies in one common location where you can set dependencies.
- To understand let’s create simple maven project as below:
- 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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>IncludeJarsLib</groupId> <artifactId>IncludeJarsLib</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>IncludeJarsLib</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> </project>
- As you see above this project has two dependencies Junit and Log4j and this two should be available in classpath if you are using functaionly fro m these two. Now if you go to command prompt or eclipse and run mvn install command it will create only Jar file and put inside target folder and it does not include those two jars. To include these two jars in some folder and later which running from command prompt you could set its jar class path.
- To include all static folder and its jar dependencies you will have to modify your pom.xml file as below:
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>IncludeJarsLib</groupId> <artifactId>IncludeJarsLib</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>IncludeJarsLib</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/config</directory> <excludes> <exclude>*.xml</exclude> <exclude>*.properties</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> <excludes> <exclude>*.xml</exclude> <exclude>*.properties</exclude> </excludes> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.8</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
- Now if you take mvn install it will create lib folder inside target folder and copy all project dependencies jars and as you see I am excluding all static file resources but if you want to include just change it’s name from exclude to include
- For more information please visit maven documentation here