Add artifact to local repository from project - java

Is there a way add artifact to local maven repository from my eclipse project?
currently i have a project that contain many jars, and i have started using maven. what i need is to add all these jars to the local repository in an automated way without redownload them or adding them one by one and specifying their coordinates.

Make a new Maven project in Eclipse, and add all your code to the src/main directory. Now you will have lots of compile errors, because of missing dependencies.
Now start auto adding the dependencies. In Intellj you can add something using alt-enter, which also has the option to "add maven dependency". This adds that dependency from the maven repository to the pom. I do not know eclipse well enough, but it probably also has this feature.
Now, in a normal project, you will find most of your required dependencies somewhere in Maven Central. If you miss any, you can add them using manual installation to your local repository, as suggested by Manas Mukherjee
mvn install:install-file -Dfile={jar_file_name_path}.jar -DgroupId={groupId}
-DartifactId={artifactId} -Dversion={version} -Dpackaging=jar

you can write a script using mvn install command.
mvn install:install-file -Dfile={jar_file_name_path}.jar -DgroupId={groupId}
-DartifactId={artifactId} -Dversion={version} -Dpackaging=jar
You can add all dependencies in pom file as well
Thanks

Related

How can I recover maven dependency issues after changing my Intellij license user account to a new account?

I changed the Intellij license user account I have been using and all my maven dependencies are no longer visible to my spring boot project. I deleted the repository folder in C:\Users{My Username}.m2, executed mvn dependency:tree and run mvn clean install. This has recovered my maven dependencies downloaded online. However, the dependencies included as POM from other projects residing on my system are still not visible.
You need to install other projects so that they are in the local repository and visible to the current project.
Even without IntelliJ, you can simply go to each of your other local project folders and do a:
mvn dependency:resolve
Using that resolve mojo, that would be enough to download the dependencies of those projects back to your new %USERPROFILE%\.m2 cache folder.

Maven cannot find JAR

Recently I tried to develop an interface for my group. My service runs and works well on my local machine, but when trying to use Maven Compile in IDEA, the compilation fails and tells me it cannot find my JAR package. The JAR package I use is JAVE (Java Audio Video Encoder), which was manually added into my CLASSPATH. I know Maven cannot find JAR package without adding dependency to pom, but I can't find the Maven dependency for JAVE, it seems that they only provide a JAR package for users. JAVE HomePage
So in this case, what should I do if I want to successfully compile my code using Maven? I need to deploy my service in the future, so manually adding JAR package to my CLASSPATH instead of adding dependency to pom is definitely not acceptable.
You can install a maven jar locally using:
mvn install:install-file
-Dfile=<path-to-file>
-DgroupId=<group-id>
-DartifactId=<artifact-id>
-Dversion=<version>
-Dpackaging=<packaging>
-DgeneratePom=true
You can install the artefact to either your local or a remote maven repo or you could use it as a system-scoped dependency (see https://stackoverflow.com/a/2177417/9705485 or http://roufid.com/3-ways-to-add-local-jar-to-maven-project/ for examples)

Adding many third party jars to Maven Eclipse runtime [duplicate]

This question already has answers here:
How do I add third-party jars into local Maven repository? [duplicate]
(2 answers)
Closed 7 years ago.
I have a group of aboout 20+ third party jars that I need to add to the RUNTIME of an Eclipse Maven project written in Java. These jars are not available from mvnrepository.com. How can I accomplish this?
I tried Build Path.. Configure Build Path and added the jars manually, but this does not make the jar APIs available in Spring MVC controllers because the jars were added to the Build Path and not to the RUNTIME.
I then tried Project Properties -> Deployment Assembly -> Add -> Archives from file system as per this other posting. This imported the jars into /WEB-INF/lib, but did not make their APIs available.
I then read these instructions for importing 3rd party jars into a maven project, but the instructions say to use this line of code mvn install:install-file -Dfile=<path-to-file>. As you can see, this code only specifies one jar, when I have over 20 third party jars to add. The maven link also does not specify where or when to type the code.
Do I navigate the terminal to the root directory of the Maven repository and then type that line of code with 20 variations, once for each jar?
And do I repeat this every time I do a Maven update from within Eclipse? Clearly there has to be an easier way.
What is the easiest and most effective way to get Eclipse Maven to add all 20+ jars to the runtime of my project, so that my Spring mvc controllers can call the APIs when I try Run As... Run on server, and so that the jars are also bundled up with any war files that get created by Eclipse Maven?**
Edit
#DaveNewton suggested writing a shell script. How would such a script look?
I tried mv /path/to/all/the/jars/* /path/to/workspace/MyApp/src/main/webapp/WEB-INF/lib/ and then typed F5 to make the jars visible in the /WEB-INF/lib folder, but this did not resolve the compilation errors from the code elsewhere in the project that calls the API, even after Project... Clean.
Also, I would like for the Eclipse Maven plugin to be able to manage this as much as possible.
Edit 2
As per #alainlompo's suggestion, I imagine a script that includes the following commands, and then the dependency addition below it. Here is what might be in the script:
mvn install:install-file -Dfile=/thefirst.jar -DgroupId=my.group.id -DartifactId=myartifactid -Dversion={version} -Dpackaging=jar
mvn install:install-file -Dfile=/thesecond.jar -DgroupId=my.group.id -DartifactId=myartifactid -Dversion={version} -Dpackaging=jar
mvn install:install-file -Dfile=/thethird.jar -DgroupId=my.group.id -DartifactId=myartifactid -Dversion={version} -Dpackaging=jar
mvn install:install-file -Dfile=/thefourth.jar -DgroupId=my.group.id -DartifactId=myartifactid -Dversion={version} -Dpackaging=jar
mvn install:install-file -Dfile=/thefifth.jar -DgroupId=my.group.id -DartifactId=myartifactid -Dversion={version} -Dpackaging=jar
mvn install:install-file -Dfile=/thesixth.jar -DgroupId=my.group.id -DartifactId=myartifactid -Dversion={version} -Dpackaging=jar
mvn install:install-file -Dfile=/theseventh.jar -DgroupId=my.group.id -DartifactId=myartifactid -Dversion={version} -Dpackaging=jar
And so on for all 20+ jars.
Here is what the dependency tag in the pom.xml might be:
<dependency>
<groupId>my.group.id</groupId>
<artifactId>myartifactid</artifactId>
<version>what goes here?</version>
</dependency>
I would guess I would add some symbols to the groupid to make sure that Maven cannot find anything with the same name at mvnrepository.com. This way the Eclipse Maven update could be used. But what do I use for the version number? Many of the jars have different version numbers.
Also note I am using linux. What would the actual shell script and dependency tag look like?
I think the simplest way to solve this issue is to add your jars to your maven local repository. There is answer for this on SO here and also here. The jars will then be available in Eclipse but also outside eclipse for any of your project that is using maven as a build tool and where the jar is correctly referenced (with the groupId, artifactId and version dependency informations properly provided) as in the following example:
<dependencies>
....
<dependency>
<groupId>org.matlabcontrol</groupId>
<artifactId>matlabcontrol</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
And then you may easily write a .bat or a .sh (depending on the OS) that will do this operation once and for all.
[EDIT]
#CodeMed regarding the jars that are behind the link that you've shared in your comments, some of them are related to each others and therefore will share the same version as the same groupId and most certainly different artifactIds
Example:
org.eclipse.emf.common_2.5.0.v200906151043.jar
org.eclipse.emf.ecore.xmi_2.5.0.v200906151043.jar
Obviously the suffixed 4 parts number is the version number of the jar. So You already have the version number and since it is common to many jar it is to your advantage to define a variable for it in your .bat file and a property for it in your pom.xml file.
Also the groupId will generaly be the same for all related jars. Therefore you could suspect that the groupId would be org.eclipse.emf, and if you google this: maven dependency for org.eclipse.emf + 2.5.0.v200906151043you would find among other links a link to a pom.xml file on a related github project with the following depency informations
I would now be logical to suspect that the artifactId is what's left from the name of the jar commonin one case and ecore.xmiin the other case. This will be checked against a little googling or on Mvnrepository (for example here where we see that this artifact id is also prefixed by the group id)
Therefore we would write the following kind of instructions in a .bat file to install these jars in our local repository
First define a variable to hold the version number
set eclipseEmfVersion=2.5.0.v200906151043
call mvn install:install-file -Dfile=org.eclipse.emf.ecore.xmi_2.5.0.v200906151043.jar -DgroupId=org.eclipse.emf -DartifactId=org.eclipse.emf.ecore.xmi -Dversion=%eclipseEmfVersion% -Dpackaging=jar
call mvn install:install-file -Dfile=org.eclipse.emf.common_2.5.0.v200906151043.jar -DgroupId=org.eclipse.emf -DartifactId=common -Dversion=%eclipseEmfVersion% -Dpackaging=jar
In the case of the second jar however I find on MvnRepository that the artifactId does not set the groupId as its prefix (see here)
Note also that in a .bat file you need to use call mvn (instead of simply mvn as you would do had you only one instruction to run) to be able to execute many maven instruction sequentially.
On the same pattern you can distinguish another group of related jars with the groupId: org.openhealthtools.mdht.uml.cdaand version number 1.2.0.201405161834. You can apply the same approach to install them in your local repository
Here is the successfull test I run (from a bat file that I created in the same directory where I unzip your file) using the first four jars in your list of jars
call mvn install:install-file -Dfile=net.sourceforge.lpg.lpgjavaruntime_1.1.0.v200803061910.jar -DgroupId=net.sourceforge.lpg -DartifactId=net.sourceforge.lpg.lpgjavaruntime -Dversion=1.1.0.v200803061910 -Dpackaging=jar
call mvn install:install-file - Dfile=org.apache.commons.lang_2.3.0.v201005080501.jar - DgroupId=org.apache.commons -DartifactId=lang -Dversion=2.3.0.v201005080501 - Dpackaging=jar
call mvn install:install-file - Dfile=org.eclipse.core.runtime_3.8.0.v20120521-2346.jar - DgroupId=org.eclipse.birt.runtime -DartifactId=org.eclipse.core.runtime - Dversion=3.8.0.v20120521-2346 -Dpackaging=jar
set cdaversion=1.2.0.201405161834
call mvn install:install-file - Dfile=org.openhealthtools.mdht.uml.cda.ihe_1.2.0.201405161834.jar - DgroupId=org.openhealthtools.mdht.uml.cda -DartifactId=ihe - Dversion=%cdaversion% -Dpackaging=jar
You can easily complete the .bat and after running you may manually check the successfull installation of all the jars in your maven local repository.

Download dependencies to local repository but not to "target" one

I have a project, that has pom.xml and depends on lots of dependencies from outside (located far-far in internet..).
So, I want to download all those dependencies which I depend on to my "local repository".
This is my try (I do not need do compilation, so I use "validate" here. So I'm do not expect to have "target" folder in the end):
mvn validate -Dmaven.repo.local=C:\my\.m2\repository dependency:copy-dependencies
In the end - yes I have many dependencies been downloaded to "C:\my\.m2\repository", but some of them went to: C:\projects\myProject\java\trunk\target\dependency, like these ones:
junit-4.8.1.jar
log4j-1.2.8.jar
mockito-all-1.8.2.jar
Question is: how to make those to be downloaded to "C:\my\.m2\repository" but not to "target" of my project?
For now, because of that, another projects that depend on that are failing while building, because they are expecting to find "junit-4.8.1.jar" in local repo.
Another try:
mvn validate -Dmaven.repo.local=C:\my\.m2\repository dependency:resolve
Then those dependencies are not resolvable at all.
Could not resolve dependencies for project bla-bla-SNAPSHOT: The
following artifacts could not be resolved:
commons-lang:commons-lang:jar:2.4, log4j:log4j:jar:1.2.8,
junit:junit:jar:4.8.1: Could not find artifact
commons-lang:commons-lang:jar:2.4 -> [Help 1]
Maven did that because you invoked the goal dependency:copy-dependencies. It will copy the dependencies of the current module to
${project.build.directory}/dependency
See http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html
I really can't imagine these dependencies did not get into your local repo. Fiddling around with a script may not be the solution to your problem. Try
mvn process-resources -U -Dmaven.repo.local=C:\my\.m2\repository
The -U option forces a download of all dependencies. I suggest using process-resources, although as of my understanding validate should be fine, too.

Maven: How to include jars in Eclipse, which are not available in repository?

I have copied the JARs into src\main\webapp\WEB-INF\lib.
I use eclipse. If I add the jars one-by-one to Project-> Java Build Path-> Add jars, then I do Project-> Maven-> Update Project Configuration, they are removed by Maven. And Eclipse shows errors, which contain " xxx cannot be resolved".
Env:
Eclipse Java EE IDE for Web Developers.
Version: Indigo Service Release 1
Build id: 20110916-0149
m2e - Maven Integration for Eclipse 1.0.100.20110804-1717
Note: I don't want to create my own Maven repository. It will be just used once.
How should I proceed ?
Not all libraries can be found in a public Maven repository, for example your own libraries or proprietary libraries. Anyway first you can search the Maven Repository, maybe some of them are there.
In case they are really not there, and you don't want to install Nexus or Artifactory, you can choose one of these two approaches:
install the jars in your local repository cache, no need to create a special repository. See the instructions on mkyong.
another approach is using system dependencies, you just put a path to reach the Jar in the filesystem. It's a less recommended approach, but if you really want it will work. Here is the official documentation.
Add this an a Systems Dependency.
In case you want to add this (this uses jquantlib as example) to the maven local repo use:
mvn install:install-file -Dfile=./jquantlib-0.2.4.jar -DgroupId=org.jquantlib -DartifactId=jquantlib -Dversion=0.2.4 -Dpackaging=jar
In order to do this I use a batch file
#echo off
cd lib
CMD /C "mvn install:install-file -Dfile=./jquantlib-0.2.4.jar -DgroupId=org.jquantlib -DartifactId=jquantlib -Dversion=0.2.4 -Dpackaging=jar"
CMD /C "mvn install:install-file -Dfile=./jquantlib-helpers-0.2.4.jar -DgroupId=org.jquantlib -DartifactId=jquantlib-helpers -Dversion=0.2.4 -Dpackaging=jar"
CMD /C "mvn install:install-file -Dfile=./jquantlib-samples-0.2.4-ubber.jar -DgroupId=org.jquantlib -DartifactId=jquantlib-ubber -Dversion=0.2.4 -Dpackaging=jar"
CMD /C "mvn install:install-file -Dfile=./jquantlib-samples-0.2.4.jar -DgroupId=org.jquantlib -DartifactId=jquantlib-samples -Dversion=0.2.4 -Dpackaging=jar"
CMD /C "mvn install:install-file -Dfile=./ta-lib-0.4.0.jar -DgroupId=com.tictactec -DartifactId=ta-lib -Dversion=0.4.0 -Dpackaging=jar"
You can use a similar script file on other systems.
I believe the system dependency approach shouldn't be used unless you don't have any other choice, and that's because you're loosing the whole 'build portability thing' here.
Of course you can store your jars in your source control system together with your project's source files, but I don't think its a good approach neither...
Using only install:install-file is not good enough - this would indeed deploy the jars in the proper format into your local repository, but what happens when you'll move to another computer and start to build your project there?
You will need to make this once more.
So, If you don't want to install nexus/artifactory (which is the best solution, I believe), you probably should create an another repository (just in a file system on some of your servers), and deploy the jars there (you can use mvn install:install-file as was suggested here, and then just copy the whole tree). Now you can configure apache web server and access the directory with all your jars via http. I don't believe its better then nexus/artifactory approach, but it can be a little be easier to do if you're familiar with apache web server.
In order to get your maven aware about this new repository you'll need to edit the %MAVEN_HOME%\conf\settings.xml file
There are atleast three approaches in which 3rd party JARs can be added to Maven projects.
Install manually using mvn install command
Adding the location of jar file in pom dependency with the the following tag system
Creating a 'dummy' maven repository pointing to jar location.
While approach 1 and 2 has been suggested above, I will focus on third approach which I find more cleaner and does not require any mvn command and works out of box from any IDE.
Step 1: Add the location of local 'dummy' repository in pom.xml
<repositories>
<repository>
<id>repo</id>
<name>repo</name>
<url>file:${project.basedir}/src/main/resources/lib</url>
</repository>
</repositories>
Here the 'dummy' repository location is the 'lib' folder of my project directory
Step 2 : Add the jar dependency into your pom.xml
<dependency>
<groupId>com.cloudera.impala</groupId>
<artifactId>impala-frontend</artifactId>
<version>0.1-SNAPSHOT</version>
</dependency>
choose any groupId but make sure that artifactId and version is of the format <artifactId>-<version>.jar ( Name of 3rd party jar)
Step 3 : Create the folder structure as per the groupId,artifactId and version mentioned in the Step 2 in your local 'dummy' repository. So in this case the folder struction would be /src/main/resources/lib/com/cloudera/impala/impala-frontend/0.1-SNAPSHOT/
Place your jar in the version folder and build your project.
You will get the following output which treats your 'dummy' repository to be the provider of your 3rd party jar.
[INFO] Downloading from repo: file:C:\Users\skumar\eclipse-workspace\chdQueryBuilder/src/main/resources/lib/com/cloudera/impala/impala-frontend/0.1-SNAPSHOT/maven-metadata.xml
[INFO] Downloading from repo: file:C:\Users\skumar\eclipse-workspace\chdQueryBuilder/src/main/resources/lib/com/cloudera/impala/impala-frontend/0.1-SNAPSHOT/impala-frontend-0.1-SNAPSHOT.pom
[WARNING] The POM for com.cloudera.impala:impala-frontend:jar:0.1-SNAPSHOT is missing, no dependency information available
[INFO] Downloading from repo: file:C:\Users\skumar\eclipse-workspace\chdQueryBuilder/src/main/resources/lib/com/cloudera/impala/impala-frontend/0.1-SNAPSHOT/impala-frontend-0.1-SNAPSHOT.jar
[WARNING] Could not validate integrity of download from file:C:\Users\skumar\eclipse-workspace\chdQueryBuilder/src/main/resources/lib/com/cloudera/impala/impala-frontend/0.1-SNAPSHOT/impala-frontend-0.1-SNAPSHOT.jar: Checksum validation failed, no checksums available
[WARNING] Checksum validation failed, no checksums available from repo for file:C:\Users\skumar\eclipse-workspace\chdQueryBuilder/src/main/resources/lib/com/cloudera/impala/impala-frontend/0.1-SNAPSHOT/impala-frontend-0.1-SNAPSHOT.jar
[INFO] Downloaded from repo: file:C:\Users\skumar\eclipse-workspace\chdQueryBuilder/src/main/resources/lib/com/cloudera/impala/impala-frontend/0.1-SNAPSHOT/impala-frontend-0.1-SNAPSHOT.jar (7.0 MB at 79 MB/s)
[INFO]
To add external JAR files not in the local repository simply right click on your main source folder and from the build path menu select: "configure build path", then navigate to the libraries tab and click "add external JAR files". next, locate the JAR (or zip) file you would like to add as a library and click ok.
Congratulations, you have now successfully added an external JAR (or zip) to your build path and you can now import any classes from that JAR file in your project without throwing an errorPicture How-To :) http://hostthenpost.org/uploads/541be8420657320c74489ff8d456ad08.png

Categories