How to add user defined library to maven dependencies in eclipse - maven

How to add user defined library as a maven dependency in my eclipse. i just have bunch of jars needed to add to create a webapp. so i just created a custom library

You can either add the dependency directly in the pom
<dependency>
<groupId>sample</groupId>
<artifactId>com.sample</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/yourJar.jar</systemPath>
</dependency>
or you can install the jar to your maven repository
mvn install:install-file
-Dfile=<path-to-file>
-DgroupId=<group-id>
-DartifactId=<artifact-id>
-Dversion=<version>
-Dpackaging=<packaging>
-DgeneratePom=true
Where: <path-to-file> the path to the file to load
<group-id> the group that the file should be registered under
<artifact-id> the artifact name for the file
<version> the version of the file
<packaging> the packaging of the file e.g. jar
and then add the dependency to your jar.

Related

How to convert jars to a maven respository

I am developing an application with JavaPos inclusive. The problem I have is that I am using maven but the hardware manufacturer's libs are all in jars and xml file. How do I bundle them as a maven dependency and use them in my maven project.
Any good idea is welcome so long as it helps me get this done quickly. Help really needed.
Add external jars to local .m2 (for local development)
This approach is not distributable. It assumes just putting jars to local .m2 and nothing more.
For adding jars to local maven repository:
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version>
<path-to-file> - is path to jar
Create a new maven repo and distribute it within a project
This approach assumes creating a new maven repository, which will include only external jars. Then this repository placed to the project root, added to git and referented by the project pom.
So anyone, who will download the project will have all maven dependencies in place without extra actions.
Adding jar to the new maven repo:
mvn deploy:deploy-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=jar -Durl=file:./.m2/repository/ -DrepositoryId=project-internal -DupdateReleaseInfo=true
Then reference the repo in your pom:
<repositories>
...
<repository>
<id>project-internal</id>
<url>file:///${project.basedir}/.m2/repository</url>
</repository>
</repositories>
Reference
https://www.google.com/amp/s/roufid.com/3-ways-to-add-local-jar-to-maven-project/amp/

Maven - Can't find transitive dependency of 3rd party jar in war

I've installed a 3rd party jar in my local maven repository using
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
so I can use that jar in my war project. This jar has the following dependency on its pom.xml:
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-core</artifactId>
<version>5.1.6.Final</version>
</dependency>
I am able to successfully compile and generate the war. But when I deploy it in Jboss 7, I get the following runtime error:
java.lang.NoClassDefFoundError: org/infinispan/manager/DefaultCacheManager
Which is a class that I should find in transitive dependency infinispan-core. Why is this transitive dependency not being included in my war?
This JBoss issue mentions:
Can we see your MANIFEST.MF and jboss-deployment-structure.xml ?
Infinispan jar files are not automatically exported to WildFly deployments and you need to explicitly add them as dependencies using the techniques described at "Class Loading in WildFly"

packaging maven project with external jar

I've been trying to make a runnable jar from my project (in Intellij IDEA) which has a dependency to an oracle (driver -> ojdbc6) jar. When I package the project with all of the dependencies, the only one what will be excluded is the jar. Which means my db queries are going to fail when I run it.
I've found several similar questions*, but I've failed the execution of them, because I don't know the groupid and artifact id of the oracle's jar.
*like this one: build maven project with propriatery libraries included
p.s.: the jar wad added through the IDEA's feature (project structure -> modules), and with this solution the project could run without failure. The problem starts with the packaging.
Short Solution: Try using the below:
<dependency>
<groupId>LIB_NAME</groupId>
<artifactId>LIB_NAME</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${basedir}/WebContent/WEB-INF/lib/YOUR_LIB.jar</systemPath> // give the path where your jar is present
</dependency>
Make sure that the groupId, artifactID and the version number are unique.
Long Solution:
Download the jar file to your machine.
Navigate using the prompt to the folder where you downloaded the jar.
Run the following command to install the jar to your local repository.
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.3 -Dpackaging=jar -Dfile=ojdbc6.jar -DgeneratePom=true
Finally, add the dependency to the pom.xml.
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
Also, don't forget to use -U option while running the project.

Dependency on a local maven project?

Is it possible to have a dependency on a project that is only on my local machine and not in any repository?
If so, how do I specify it in my POM, would I use the following format below?
<dependency>
<groupId></groupId>
<artifactId></artifactId>
<version></version>
</dependency>
Install that dependency to your local maven repository using mvn install. Then your local projects can use it as a dependency. Of course that will only work on that one machine.
If you use Eclipse/NetBeans/IntelliJ and have the dependency as well as the project using that dependency opened, you don't need to install it as those IDEs resolve this without involving the local maven repo.
If your dependency is not a maven project, you simply have to reference the jar file. Or you assign artifactId and groupId and install the jar file to your repo.
Both ways are shown here.
install the dependency using mvn install like take a example of oracle ojdbc6 or ojdbc14 jar we cannot find this jar in central or remode repository so to use this we need to install this jar in maven local repository
Syntax:-
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
example:-
mvn install:install-file -Dfile=C:/oraclexe/app/oracle/product/11.2.0/server/jdbc/lib/ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0 -Dpackaging=jar
note:- Cmd should be opened in location of jar only i.e- mvn install:install-file command must run at jar location only
Configuring POM.XML(in program)
<!-- ORACLE database driver -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
note:-
even one project developed in maven can be added as jar in another

Jenkins and maven multi module Projects missing artifacts

This is a simplified example of an ear project, the parent pom aggregates the EAR, the EJBs, and the jars.
I have this structure in a Maven project, stored in SVN:
parent/
|- pom.xml
|- modulA/
| |- pom.xml
|- modulB/
| |- pom.xml
modulB has a Dependency of modulA
The pom.xml have the modules section
<modules>
<module>modulA</module>
<module>modulB</module>
</modules>
And a Dependency Management section
<dependencyManagement>
<dependencies>
<dependency>
<groupId>group</groupId>
<artifactId>modulA</artifactId>
<version>0.0.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>group</groupId>
<artifactId>modulB</artifactId>
<version>0.0.2-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
The sub-modules reference the parent
<parent>
<groupId>group</groupId>
<artifactId>parent</artifactId>
<version>0.0.2-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
in my PC when I compile for the first time with maven 2.2.1 (windows)
mvn clean compile
I don't have any problems
but.... when Jenkins try to compile for first time (Maven 2.2.1 Linux RedHat)
Missing:
----------
1) modulA:jar:0.0.2-SNAPSHOT
Try downloading the file manually from the project website.
Then, install it using the command:
mvn install:install-file -DgroupId=group -DartifactId=modulA -Dversion=0.0.2- SNAPSHOT -Dpackaging=jar -Dfile=/path/to/file
Alternatively, if you host your own repository you can deploy the file there:
mvn deploy:deploy-file -DgroupId=group -DartifactId=modulA -Dversion=0.0.2-SNAPSHOT -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]
Path to dependency:
1) modulB:ejb:0.0.2-SNAPSHOT
2) modulA:jar:0.0.2-SNAPSHOT
----------
1 required artifacts are missing.
Why????????
After that if I deploy the project from my pc to Artifactory, Jenkins doesn't have problems, because Jenkins downloads the artifact from the repository... but why does Jenkins depend on the artifacts in the repository?
:(
Thanks in Advance
EDIT:
I thought the dependencyManagement section only "defines" the dependencies, but if a submodule doesn't use the dependency, the dependency isn't added to the submodule.
I drop the dependencyManagement section and the problem in Jenkins still occurs.
It works on my PC without problems.
I hope above dependency management section is inside the parent pom. According to your requirement modulB has a Dependency of modulA. So I suggest you to include dependency in moduleB instead of having it in the parent pom. I think when it runs in first time maven is looking for both dependencies since you have mentioned in in the parent pom.Look at your project build order. First it builds module A and then B. In your case I hope you have include all other dependencies in moduleA's pom file and once it built it will deploy a jar file in to m2 repository. And then moduleB start to build and since your dependency is already in the m2 repository it wont shout and project will build successfully.
The first time you build parent project, your Jenkins user's maven repository won't have modulA installed. clean compile is then run successfully in modulA, but nothing is installed. When it is run in modulB, the dependency on modulA can't be resolved.
If your Jenkins job's goal was clean install instead of clean compile, then modulA's artifacts would be installed to the Jenkins user's repository before the modulB build begins, and all would work.
Presumably this worked on your own machine because either you had run mvn install at least once in modulA, or because your IDE's classpath resolved the problem for you.

Resources