How to convert jars to a maven respository - maven

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/

Related

How can I add maven artifact into an existing maven project

How can I add maven artifact into an existing maven project.I understand that I can build a jar locally and use file: protocol but this is possible using maven also.
For example I have a basic maven project
https://maven.apache.org/guides/getting-started/index.html#How_do_I_make_my_first_Maven_project and the artifact:
<repositories>
<repository>
<id>myrepo.org</id>
<name>MyRepository</name>
<url>http://mywork.com/repository</url>
</repository>
</repositories>
<dependency>
<groupId>org.ethereum</groupId>
<artifactId>ethereumj-core</artifactId>
<version>1.1.0-RELEASE</version>
I tried adding the code above to the project pom.xml fails because dependency is not in central maven repo.
mvn clean install
I then tried editing my settings.xml by adding the tag, that also failed because dependency was not found.
Im overlooking something pretty basic here.
Maven works on the concept of local and remote repositories.
The local repository refers to a copy on your own installation that is a cache of the remote downloads, and also contains the temporary build artifacts that you have not yet released.
Remote repository is repository you access the artifacts via file or http / ftp protocols , it can be an internal repo or a remote public hosted.
When you add dependency maven search that artifact in local if not found then remote repo will be searched. Still not found then error is reported.
https://maven.apache.org/guides/introduction/introduction-to-repositories.html
In your case, 'ethereumj-core' can not be located neither of location, you need find this jar and do a manual install to local repo.
mvn install:install-file -Dfile=< folder >\ethereumj-core.1.1.0-RELEASE.jar -DgroupId=org.ethereum
-DartifactId=ethereumj-core -Dversion=1.1.0-RELEASE -Dpackaging=jar
once properly installed maven should be able find this artifact when you add this as dependency to any of the projects (in pom.xml).
<dependency>
<groupId>org.ethereum</groupId>
<artifactId>ethereumj-core</artifactId>
<version>1.1.0-RELEASE</version>
</dependency>

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

Create local repository for jar

I have main parent POM file which I use to declare all dependencies. I have 3 submaven projects. This is the file structure:
C:\NetBeansProjects\project\pom.xml
C:\NetBeansProjects\project\Core\System_Commons\pom.xml
C:\NetBeansProjects\project\Core\Tracking_Service\pom.xml
I need to use into my project weblogic client - wlfullclient.jar. I create local maven repository:
C:\NetBeansProjects\project\lib\com\weblogic\wlfullclient\10.3.6\wlfullclient-10.3.6.jar
C:\NetBeansProjects\project\lib\com\weblogic\wlfullclient\10.3.6\wlfullclient-10.3.6.pom
I added local repository:
<repository>
<id>lib</id>
<name>In Project Repo</name>
<url>file://${basedir}/lib</url>
</repository>
Jar dependency:
<dependency>
<groupId>com.weblogic</groupId>
<artifactId>wlfullclient</artifactId>
<version>10.3.6</version>
</dependency>
mvn install:install-file -Dfile=C:\wlfullclient-10.3.6.jar -DgroupId=com.weblogic -DartifactId=wlfullclient -Dversion=10.3.6 -Dpackaging=jar -DgeneratePom=true
But I get this error:
[ERROR] Failed to execute goal on project System_Commons: Could not resolve dependencies for project org.project:System_Commons:bundle:1.0: Could not find artifact weblogic:wlfullclient:jar:10 at specified path C:\NetBeansProjects\project\Core\System_Commons\lib\wlfullclien
t-10.3.6.jar -> [Help 1]
The correct path should be here C:\NetBeansProjects\project\lib\com\weblogic\wlfullclient\10.3.6\wlfullclient-10.3.6.jar
Can you give me some idea how I can fix this?
there is not need to create a new repository (do you have any specific requirement?), you already have one, your local repository. The easiest thing to do is, to install this specific jar to your local repository. Usually that resides in the ~/.m2 folder (Mac/Unix) or C:\Documents and Settings{your-username}.m2 for windows. See here.
So what you need to do is apply the following command :
mvn install:install-file -Dfile=wlfullclient-10.3.6.jar
-DpomFile=wlfullclient-10.3.6.pom
-DgroupId=com.weblogic
-DartifactId=wlfullclient
-Dversion=10.3.6
-Dpackaging=jar
In case though you want to have a separate 'local' lib repository you can use the following param on the example above :
-DlocalRepositoryPath=path-to-specific-local-repo
See references here.
You can now reference your jar from your pom and and check of course your local report that the jar is indeed installed and the naming is correct.
Hope that helps.

Maven Amdatu Bundle Remote Repository

I'm new to OSGi and Amdatu and I'm using OSGi with Maven.
Usually I import bundles like this
sudo mvn pax:import-bundle -DgroupId=org.eclipse.jetty.osgi -DartifactId=jetty-osgi-boot -Dversion=9.1.3.v20140225
In upper case importing works great because the bundle can be found in maven central repository.
But there is no amdatu bundle in maven central repository, so I tried to add amdatu repositories in pom.xml.
<repositories>
<repository>
<id>dependencies</id>
<name>Amdatu Dependencies</name>
<url>http://repository.amdatu.org/dependencies/</url>
</repository>
<repository>
<id>snapshot</id>
<name>AmdatuSnapshots</name>
<url>http://repository.amdatu.org/snapshot/</url>
</repository>
<repository>
<id>release</id>
<name>AmdatuRelease</name>
<url>http://repository.amdatu.org/release/</
</repository>
</repositories>
I can see added maven repositories in Netbeans, however when exploring any repository I get the message <No result, processing index...>
If I open the link in browser I can see the repository with index.xml file.
So I tried to install a jaxrs bundle and no luck.
sudo mvn pax:import-bundle -DgroupId=org.amdatu.web.rest.jaxrs -DartifactId=org.amdatu.web.rest.jaxrs -Dversion=1.0.4
What am I doing wrong? How can I use maven amdatu repository and import bundles?
Thank you!
What you're doing wrong is assuming that the Amdatu repositories are Maven repositories. They are not. They are in fact OSGi Bundle Repositories, and we provide an index in both the "old" (repository.xml) and "new" (index.xml) format.
Within the Amdatu project, we don't use Maven, we develop with Bndtools, so you would have to provide the proper metadata (pom.xml) yourself and then import the bundles into your own Maven repository. If someone would contribute a tool to automatically generate the proper metadata and/or upload artifacts in Maven central, we would be happy to accept that though.
Marcel, thank you for your help.
I have downloaded all bundles and install them in local maven repository with next command.
mvn org.apache.maven.plugins:maven-install-plugin:2.5.1:install-file -Dfile=path-to-your-artifact-jar \
-DgroupId=your.groupId \
-DartifactId=your-artifactId \
-Dversion=version \
-Dpackaging=jar \
-DlocalRepositoryPath=path-to-specific-local-repo
I saw that you already found a way to automatically generate pom.xml with BND Tools.
https://amdatu.atlassian.net/browse/AMDATU-712

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