How to use a jar file as a library in a maven project - maven

So I have a library which I want to use, and I have it inside of a folder called "lib",
I'm working in maven so I put this in my pom.xml
<dependency>
<groupId>org.worldborder</groupId>
<artifactId>worldborder</artifactId>
<scope>system</scope>
<version>1.0</version>
<systemPath>${basedir}/src/lib/WorldBorder.jar</systemPath>
</dependency>
Doing this allows me to write code which uses said library, but when I build the jar it gives me a no class def found error when using, when I use mvn clean install, I do get a warning when I build,
Some problems were encountered while building the effective model for me.acidviper:ViperUHC:jar:1.0-SNAPSHOT
'dependencies.dependency.systemPath' for org.worldborder:worldborder:jar should not point at files within the project directory, ${basedir}/src/lib/WorldBorder.jar will be unresolvable by dependent projects # line 85, column 25
It is highly recommended to fix these problems because they threaten the stability of your build.
For this reason, future Maven versions might no longer support building such malformed projects.
I'm a complete noob when it comes to maven, so I don't understand how to fix this error. Any help is appreciated. Thanks!

Hi I faced the same issue. To solve that I installed that jar directly into my local .m2 repository.
mvn install:install-file
-Dfile=/path/to/jar
-DgroupId=org.worldborder
-DartifactId=worldborder
-Dversion=1.0.0
-Dpackaging=jar
then if you are using spring boot you can use plugin like spring-boot-maven-plugin. It creates a complete jar for your application with all of it's dependencies

Related

Errors with Maven install on a project

I'm trying to install Maven on a project (with mvn clean install) but I have some errors and I don't know what they mean.
Here is the screen shot of the cmd :
I also add the right environment variables for maven (M2, M2_HOME and MAVEN_OPTS).
Can someone help me and tell me what it means please ?
It means that your dependency to eu.akka.jbossas:jboss-as-client:7.1.7.Final that you have specified in your POM is not available at Maven central. Do you have the jar file available somewhere?
If that is the case, run this:
mvn install:install-file -Dfile=<your jar file> -DgroupId=eu.akka.jbossas -DartifactId=jboss-as-client -Dversion=7.1.7.Final -Dpackaging=jar
Please bear in mind that this means that only the machine you are running on will be able to build your project. If other developers/machines also need to build this project, consider installing a central repository at your site, such as Nexus or Artifactory, and upload the jar file there. You will then also need to make Maven aware that it should fetch the dependencies from there.
The error tells you that the maven dependency eu.akka.jbossas:jboss-as-client doesn't exist. I've checked the url where it should be and it doesn't exist.
You should check other dependencies. For example the one maven provides:
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-appclient</artifactId>
<version>7.1.1.Final</version>
</dependency>
If you've got the jar local, you can create a maven-dependency by using this guide.

Maven dependency for org.apache.poi.hssf.usermodel.contrib.HSSFRegionUtil

I am converting an existing ant build project to maven. The project uses the POI-2.5-FINAL and JXL jar files for writing data to excel. Although I have added the POI jar in the pom.XML dependencies, running maven build is failing due to the following compilation error :
error: package org.apache.poi.hssf.usermodel.contrib does not exist!
Note that all other classes from org.Apache.poi.hssf.usermodel such as HSSFWorkbook, HSSFSheet are not giving any error.
Also the code runs successfully in ant build.
Please let me know how I can solve this error.
Thanks.
If you want to stick to 2.5, try adding below dependency:
<dependency>
<groupId>poi</groupId>
<artifactId>poi-contrib-2.5-final</artifactId>
<version>20040302</version>
</dependency>

Maven ant task to add system path jar to WAR file

Running into a small problem. I have a spring-maven project. And there are some external jars I need to add into the POM which I did using .
Now to build the WAR file we are using an Ant Maven task i.e. artifact:mvn providing the argument war:war.
Here somehow my external jars are not getting added to the WAR file i.e. WEB-INF/lib
Can some one please let me know if I am missing something. Below is my pom entry
<dependency>
<groupId>{test}</groupId>
<artifactId>Test</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/test.jar</systemPath>
</dependency>
Best solution is to start using a repository manager (a.k.a "Maven proxy server") and install the needed artifacts (test.jar) into the repository manager and use it as a usual dependency instead of using system scope via systemPath.
Calling mvn war:war via Ant does not make sense and shows you should learn how Maven works.
Change the packaging type in your pom file to war and you can simply call maven via:
mvn clean package
and everything should work. But this is only gues cause you didn't show your full pom file.
Install the test.jar locally using mvn install:install-file (docs). Now you can remove the system scope (and the systemPath) and everything will work out of the box.

Maven build order

I have a multi-module maven build and I need one particular module (lets call it project-A) to be build at the end. It depends on a module (lets call it project-B) that holds native code that gets compiled to a dll and installed into the maven repository as a zip file using some maven trickery. As it doesn't depends on it directly because the native code is not a java jar, I use Maven Dependency Plugin to unpack the zip file and place the native dll in my build directory. Everything is working fine except for the building order. It builds first project-A in spite of being declared the other way around in the tag in the parent. I would like to tell maven that project-A depends on project-B. I tried to add project-B as a dependency, but as it builds no jar it throws an ERROR, also this seemed hacky to me. Any help would be appreciated.
Just declare dependency in project A to project B and it will work fine. It does not matter if the project B is a native rather than a java project. Just make sure you declare the dependency correctly taking the packaging into account as type.. (which is probably pom so you would have
<dependency>
<groupId>...</groupId>
<artifactId>B</artifactId>
<version>...</version>
<type>pom</type>
</dependency>
in Project A)
The order in which you specify the modules in the parent Pom is also relevant. Maven actually builds in this order unless it has to build a module out of sequence due to direct dependencies.

Adding Jar files to Maven

I have started to work on maven project recently.
The piece of code I am writing needs JAR files that are not a part of the project.
When I add the JARS TO THE build path and use a mvn clean install, the build is failing with errors saying that the classes that were supposed to be in the jar were not found.
Is there anything that i am missing?
Is there a different way to add the JAR's in maven projects?
If the JARs are already hoisted in some public Maven repositories, add them to the <dependency> section in the pom.xml . You may have to configure the address of these public Maven repositories in the <repositories> section in the pom.xml in order to cause Maven can connect them.
Otherwise , you have to use the install command to include these JARs into the your local repository and then add their <dependency> section in the pom.xml
The command to install the JARs into your local repository:
mvn install:install-file -DgroupId=com.abc -DartifactId=XXXXX
-Dversion=1.0 -Dpackaging=jar -Dfile=/path/to/jars
It sounds like you don't understand how Maven works. You don't add jars to the build path, you declare them in the pom, and let Maven download to the local drive, and it adds it to the build path for you. I would recommend you read this 5 minute intro, and understand how the dependency management works.

Resources