Maven cannot find JAR - java

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)

Related

Building a Maven project offline

I am trying to install OreKit (an orbital mechanics toolkit) to validate some code i've written. Orekit is a maven project and thus it tries to download all its dependencies from the maven repo.
Unfortunately my company has pretty strict internet security measures and the maven repo is not whitelisted. The only way to access non-whitelisted websites is through a secure browser (tightGate) which is basically a video-feed of the browser running on a server. Files downloaded in this browser can then be transfered to my computer using a separate program.
This of course means that the build fails. I have been trying to download all the dependencies manually and put them in the local repository.
example:
eclipse error: "Missing artifact junit:junit:jar:4.12"
I downloaded the corresponding jar and pom files (junit-4.12.jar and junit-4.12.pom) and put them into my local repository (C:/Users//.m2/repository/junit/junit/4.12/)
I did this for every error eclipse reported but nothing changes and the same errors are still there.
Am i doing something wrong here? Is it even possible to build a project this way or should i just give up already?
In principle, this could work, but it would be easier to use mvn install:install-file for the separate jars.
Be aware that the number of artifacts that Maven usually requires is > 100.
We also have strict regulations, but managed to get an extra server with a Nexus that proxies MavenCentral, so we can reach the artifacts through there.
to put jar file in your local maven repository, you must install it.
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
get more info from https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html

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.

Trying to pass local jar to maven install

I'm trying to do a mvn install with my local jar.
Here is the pom file, Allure cli.
I want to use the allure-report-builder jar from my local machine, rather than from the maven repo. So I tried this:
mvn install:install-file -Dfile=allure-report-builder.jar -DgroupId=ru.yandex.qatools.allure -DartifactId=allure-report-builder -Dversion=2.1 -Dpackaging=jar install
Here allure-report-builder.jar is my local jar, in the current directory. But it doesn't seems to be working properly. The resulting jar is still using the jar from maven repo.
Where I'm making mistake?
You're trying to do two different things at once:
install:install-file -Dfile=allure-report-builder.jar -DgroupId=ru.yandex.qatools.allure -DartifactId=allure-report-builder -Dversion=2.1 -Dpackaging=jar
and
install
The first one is called a standalone goal. This will just install the file.
The second is called a phase, in this case the install phase. This will run every project-task/goal up until the install, which includes testing and compiling.
Depending on what you want, the first will always work. The second suggest that you have code to compile, but that you don't have all the required dependencies yet. If this is all fixed, you just run mvn install to get this jar in your local repository.

Add artifact to local repository from project

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

Embed local jar files in maven project

I have some local jar files from a non-maven project which I wish to include in my maven-based eclipse project.
These jar files are undergoing a lot of change as me and my project buddy attempt to 'fix' them, so I'd prefer not to upload them to a repository to avoid making a maven version of this non-maven project if this is possible.
Of course, the jar files need to be embedded in the resulting deployment jar. We did this before using Ant which let us specify that those jar files should be included.
How do you do the same thing in maven? Take into consideration that we do have maven dependencies too which all work fine and aren't required in the deployment. Some answers I've seen don't allow for this requirement.
Here's one of my attempts - the problem is that the jar does not get embedded:
<dependency>
<groupId>se.krka.kahlua</groupId>
<artifactId>kahlua-core</artifactId>
<version>5.1_2.1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/kahlua-5.1_2.1.0-core.jar</systemPath>
</dependency>
System paths are a very bad idea. When anybody else checks out your projects, he cannot build it anymore. (I always see such crap in many companies). The right solution would be to install the jar into the local repository:
$ mvn install:install-file -Dfile=[JAR NAME] -DgroupId=[GROUPID OF
JAR] -DartifactId=[ARTIFACT OF JAR] -Dversion=[VERSION OF JAR]
-Dpackaging=jar
In your project, you just add the dependency as usual after you installed the jar into the local repository.
<dependency>
<groupId>[GROUPID OF JAR]</groupId>
<artifactId>[ARTIFACT OF JAR]</artifactId>
<version>[VERSION OF JAR]</version>
</dependency>
You can use maven-install-plugin to install kahlua-5.1_2.1.0-core.jar into the local repository then this dependency will behave as any other, see http://maven.apache.org/plugins/maven-install-plugin/usage.html. Or make a remote repository in a location shared with your buddy and let him upload his jar there with maven-deploy-plugin:deploy-file (http://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html) each time he changes it and add this repository to your pom. You can use SNAPSHOT version if this jar changes often

Categories