Adding Jar files to Maven - 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.

Related

Adding 3rd Party JAR to Spring Boot War Build intended for a remote Server

I am converting a local Spring boot Jar app to a War build , using an external TomCat server.
Spring Boot app is running using 2 3rd party jars placed in libs folder.
I am facing an issue trying to include these 2 jars to the final war.
Added these jars to the ClassPath intelliJ Artifact settings but no luck.
Tried putting them manually in WEB-INF/lib folder but no luck.
Tried the above method.
<dependency>
<groupId>myJarGroup</groupId>
<artifactId>myJarArtifact</artifactId>
<version>>1.7.0.1</version>
<scope>compile</scope>
<systemPath>${project.basedir}/src/main/resources/xxx/myjar.jar</systemPath>
</dependency>
Again no luck.
WEB-INF/lib is still missing my 2 jars.
Only a type can be imported. ec.ec.ada.ec.common.ec.code.testValidate resolves to a package
testValidate cannot be resolved
Error importing : 'ec.ec.ada.ec.common.ec.code.testValidate'
You will have to incluse these two jars as dependency in pom. I know this is 3rd party depencdency but you can add it like below:
mvn deploy:deploy-file -Durl=file:///path/to/yourproject/repo/ -Dfile=mylib-1.0.jar -DgroupId=com.example -DartifactId=mylib -Dpackaging=jar -Dversion=1.0
This way they will be saved in maven repository and maven war plugin will include same in war that will be generated for deployment.
Here you go!
Although rarely, but sometimes you will have 3rd party JARs that you
need to put in your local repository for use in your builds, since
they don't exist in any public repository like Maven Central. The JARs
must be placed in the local repository in the correct place in order
for it to be correctly picked up by Apache Maven. To make this easier,
and less error prone, we have provide a goal in the
maven-install-plugin which should make this relatively painless. To
install a JAR in the local repository use the following command:
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
With version 2.5 of the maven-install-plugin it gets even better. If
the JAR was built by Apache Maven, it'll contain a pom.xml in a
subfolder of the META-INF directory, which will be read by default. In
that case, all you need to do is:
mvn install:install-file -Dfile=<path-to-file>
From a Maven Guide to installing 3rd party JARs.

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.

Use Jar as intelliJ/Maven Dependency

How do i use Jar files as a dependency using IntelliJ with Maven? I have some jar's containing API codes but when I put them in my external library it gives an error saying it's missing.
The easiest way to do it is by installing the jar file in your local Maven repository, as JB Nizet already mentioned:
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
See also How to add local jar files in maven project?
This works fine as long as you are only using your local Maven repository. However when others should be able to build your project, they will run into the same problem. So here the easiest way would be to install the jar file in proxy Maven repository if you have one (many companies do).
You could also reference the jar file as a system dependency:
<dependency>
<artifactId>..</artifactId>
<groupId>..</groupId>
<scope>system</scope>
<systemPath>${basedir}/lib/dependency.jar</systemPath>
</dependency>
There is also a very nice alternative: Maven: add a dependency to a jar by relative path

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.

Where can I get the NaturalDocs Maven plugin?

The maven configuration mentioned in the Usage section of the Natural Docs Maven Plugin site:
<groupId>org.codehaus.mojo.naturaldocs</groupId>
<artifactId>naturaldocs-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
However; I can't seem to find any version in any Maven repository including Maven Central or the Snapshot repo.
I was able to find what seems like the initial version attached to this Mojo plugin submission but it only includes source, and while it's nice to look at I don't wish to compile it for use myself.
Grab the source archive that you referenced and unzip it in a convenient spot.
From that folder, run:
mvn clean install
Assuming that runs without issue, run:
mvn install:install-file -Dpackaging=jar -Dfile=target\naturaldocs-maven-plugin-1.0-SNAPSHOT.jar -DgroupId=org.codehaus.mojo.naturaldocs -DartifactId=naturaldocs-maven-plugin -Dversion=1.0-SNAPSHOT
Assuming that runs fine, check the local deploy works as expected by including the pom reference in your project.
<dependency>
<groupId>org.codehaus.mojo.naturaldocs</groupId>
<artifactId>naturaldocs-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
If your project build without issue, then deploy it to your local Maven repo by running:
mvn deploy:deploy-file -Dpackaging=jar-Drepository=extNonFree -Durl=scpexe://maven.your-company.com/your-serverpath/maven/proximity/ext-non-free/storage -Dfile=target\naturaldocs-maven-plugin-1.0-SNAPSHOT.jar -DgroupId=org.codehaus.mojo.naturaldocs -DartifactId=naturaldocs-maven-plugin -Dversion=1.0-SNAPSHOT
Or have one of your local Maven admins deploy the pom and jar manually.
I hope that helps.

Resources