Adding a 3rd Party JAR to Maven Repo - java

I just needed to use a 3rd party JAR in my project and deployed the JAR in my local maven repo. I followed these steps while doing this:
->Runned the belowed statement:
mvn install:install-file -Dfile=c:\DEVEL\gsa-japi-src-1.3.jar -DgroupId=net.sf.gsaapi
-DartifactId=gsaapi -Dversion=1.3 -Dpackaging=jar
->I saw "BUILD SUCCESSFUL" message, checked local repo files and it seemed the deployment was succesfull.
->After installed and deployed, i just added following statements in pom.xml file.
<dependency>
<groupId>net.sf.gsaapi</groupId>
<artifactId>gsaapi</artifactId>
<version>1.3</version>
</dependency>
But I still can not reach the library's methods. Am I missing something?

gsa-japi-src-1.3.jar contains only source files (.java) and apidocs. You need a jar with .class files

Correct dependency is:
<dependency>
<groupId>net.sf.gsaapi</groupId>
<artifactId>gsa-japi-src</artifactId>
<version>1.3</version>
</dependency>

You have installed JAR use net.sf.gsaapi:gsaapi artifact identifier but in dependency you are using com.google.code:kaptcha. So, you should use the same artifact identifier in dependencies.

Related

How to add local Maven package jar files to a Maven project

Summary
When using maven-install-plugin:install-file to install maven jar package all dependencies are ignored and this plugin write a dumb/broken pom file.
Context
I have a local Maven package jar file foo-java-1.2.3.jar which contains its own pom.xml in META-INF/maven/org.mizux.foo/foo-java/pom.xml with few dependencies
<dependencies>
<dependency>
<groupId>org.mizux.foo</groupId>
<artifactId>foo-linux-x86-64</artifactId>
<version>1.2.3</version>
<type>jar</type>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.12.2</version>
</dependency>
</dependencies>
Then, I tried to install it locally using:
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=foo-java.1.2.3.jar
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=foo-linux-x86-64.1.2.3.jar
ref: https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
note: net.java.dev.jna:jna-platform and com.google.protobuf:protobuf-java can be found on maven central...
Then when using it in a Bar project pom.xml:
...
<dependencies>
<dependency>
<groupId>org.mizux.foo</groupId>
<artifactId>foo-java</artifactId>
<version>1.2.3</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
while mvn compile pass ?
I can't run my Bar's main since all transitive dependencies of foo-java are not passed to Bar so I got some java.lang.NoClassDefFoundError
mvn exec:java -Dexec.mainClass="org.mizux.bar.Test"
...
java.lang.NoClassDefFoundError: com/sun/jna/Platform
Looking at ~/.m2/repository/org/mizux/foo/foo-java/1.2.3/foo-java-1.2.3.pom this one seems to only contains the minimum (groupID, artifactID...) but the whole dependencies part is not present !!
questions:
How I can install local maven jar package so the pom installed is the full one and not a truncated version.
Why install plugin generate this dummy pom while the complete one is present inside the jar artifact ?
note: When using mvn install on a local build of foo-java the pom exported in .m2 is vastly different i.e. it's a copy of the foo-java original pom.xml !
i.e.
%diff ~/.m2/repository/org/mizux/foo/foo-java/1.2.3/foo-java-1.2.3.pom .../foo-java/pom.xml
note2: here a github project to reproduce the issue
https://github.com/Mizux/java-ortools you can take a look at action to see a full log https://github.com/Mizux/java-ortools/actions on linux/macos/windows and a docker ubuntu container
Why install plugin generate this dummy pom while the complete one is present inside the jar artifact ?
According to the doc
Generate a minimal POM for the artifact if none is supplied via the parameter pomFile. Defaults to true if there is no existing POM in the local repository yet.
ref: https://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html#generatePom
Now I don't know if we can force the maven-install-plugin to copy the pom.xml present in the jar file itself (ed which is already parsed by the plugin !!!)
Ugly workaround
I could extract the pom.xml from the .jar file (which contains it!) then pass it along the jar in the install-file command.
unzip -j foo-java-1.2.3.jar META-INF/maven/org.mizux.foo/foo-java/pom.xml
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file \
-Dfile=foo-java-1.2.3.jar -DpomFile=pom.xml
mvn compile
mvn exec:java -Dexec.mainClass="org.mizux.bar.Bar"
Since the feature you are expecting does not work:
Either use the latest version of the plugin (3.0.0-M1) to see if this is fixed
Either run maven in debug (mvnDebug -X) and place a break point in the line below; for all that matters, your JAR file is probably broken.
At the point where JAR entries are matched
Where the JAR entries are processed
Since you will have to debug in your IDE, you may also want to clone the GIT repository and switch to the tag as to match the version of the plugin you use.
Either extract the pom manually and pass it.

Use a locally stored .jar file as a dependency in a Maven project: compile class files

I'm a relative Maven novice and am having difficulties using a locally stored jar as a module within my IntelliJ project - a project I have taken from an online tutorial.
I've brought it into my .m2 folder using:
mvn install:install-file "-Dfile=C:/../resources/myshop-automatedtestscore 3.1.17-SNAPSHOT.jar" "-DpomFile=C:/../resources/myshop-automatedtestscore-3.1.17-SNAPSHOT.pom --Dsources=C:/../myshop-automatedtestscore-3.1.17-SNAPSHOT-sources.jar"
.jar is located at:
C:\Users\daveb\.m2\repository\com\myshop\automatedtests\myshop-automatedtestscore\3.1.7-SNAPSHOT\3.1.17-SNAPSHOT.jar
And I added the dependency in the main pom.xml as follows:
<dependency>
<groupId>com.myshop.automatedtests</groupId>
<artifactId>myshop-automatedtestscore</artifactId>
<version>3.1.17-SNAPSHOT</version>
</dependency>
When I go to Project Structure -> Libraries, I can see the Sources dependency jar file is there in grey so should be fine. However the core project seems to be unable to access the class versions of the file. In Target folder they remain .class but in External libraries they are .java
Apologies if this is a novice or obvious solution. I'm trying to resolve.
You have to install jar using below command -
mvn install:install-file -Dfile= -DgroupId= -DartifactId= -Dversion= -Dpackaging=
You have to give artifacts and other parameters so that you will be able to use in pom file using below code -
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>1.1.GA</version>
</dependency>
After that you have to complie your project so that this dependency will be added in your project.
mvn eclipse:eclipse
mvn clean install
You can press here to force refresh the plugins in Intelij.
Here you can check which repository you have and also which maven you have.
Click on preferences
Another option you have is to perform this :
mvn clean install -U
If your dependency is
<groupId>com.me.example</groupId>
<artifactId>my-example</artifactId>
<version>1.1.0</version>
Then your jar file would be my-example-1.1.0.jar and it will be under \.m2\repository\com\me\example\my-example\1.1.0\my-example-1.1.0.jar.
In your case, your jar file is myshop-automatedtestscore-3.1.17-SNAPSHOT.jar and it should be under
C:\Users\daveb.m2\repository\com\myshop\automatedtests\myshop-automatedtestscore\3.1.17-SNAPSHOT\myshop-automatedtestscore-3.1.17-SNAPSHOT.jar.
After this, do a Maven > Reimport from IntelliJ.

Missing artifact com.oracle:ojdbc7 in eclipse

I have imported a Maven Project in Eclipse (EE Developer) and I have in my pom.xml file the following error, “Missing artifact com.oracle:ojdbc7:jar:12.1.0.2″ in this code:
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.2</version>
</dependency>
I have done so by downloading the ojdbc7.jar and run this command:
mvn install:install-file -Dfile=/Path-to-jar/ojdbc7.jar
-DgroupId=com.oracle
-DartifactId=ojdbc7
-Dversion=12.1.0.2
-Dpackaging=jar
-DgeneratePom=true
After that, I got as an output BUILD SUCCESS, and if I go to the .m2 folder I see in the com->oracle->ojdbc7 two files called “ojdbc7-12.1.0.1.jar.lastUpdated” and “ojdbc7-12.1.0.1.pom.lastUpdated” but still Eclipse brings me the code into the pom.xml file as an error?!?!?!
Can some one help?
This artifact version (12.1.0.2) has been removed to an another dependency.
See: https://mvnrepository.com/artifact/com.oracle/ojdbc7
Try to change the new dependency:
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1</version>
</dependency>
After successfully running the "mvn install" command right click your Project -> Maven -> Update Project (or Alt+F5).
I would get rid of those 2 files manually (lastUpdated) then re-run your install command and finally build your project. Those 2 files have been created by mvn as flags to avoid refetching them for a certain amount of time. They certainly have been created prior to your manual install-file command.
I recommend you to follow the instruction given in this link.
"http://javabycode.com/build-tools/maven/add-oracle-jdbc-driver-maven.html"
As per your dependency in pom.xml there should be 4 files generated inside "m2repo\com\oracle\ojdbc7\12.1.0.2" folder.
ojdbc7-12.1.0.2.jar
ojdbc7-12.1.0.2.jar.lastUpdated
ojdbc7-12.1.0.2.pom
ojdbc7-12.1.0.2.pom.lastUpdated
But as you are saying you checked for "ojdbc7-12.1.0.1.jar.lastUpdated", that should not be the case. I recommend you to delete your dependency folder and then start from scratch.
Some how the jar is not getting downloaded into the local pc. I opened the maven site https://mvnrepository.com/artifact/com.oracle/ojdbc7/12.1.0.2 and downloaded the required jar and placed it in to the .m2 folder, and the error is gone.

have maven download a jar as a dependency

There is a website with a jar I want to use in my project. I could download it, and make a local dependency on it, but that is lame.
Other people will build this project. I don't want to clutter source control by checking it in. I want to comunicate where the resource can be downloaded from and to easily change the versions. At some point I would like to attach the sources and javadocs.
Is there a plugin that will let me use the URL and download it to my .m2 with a spoofed pom?
The way I do it, is to search for the jars in Maven's central repo:
http://search.maven.org/
This gives you enough data to build the dependency in your pom.xml file
Example:
If I wanted to add jSoup to my project as dependency, then I go and search in the central repo
and add the dependency to the pom file with the info that's in there:
http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.jsoup%22%20AND%20a%3A%22jsoup%22
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.7.3</version>
<scope>test</scope>
</dependency>
</dependencies>
I think maven2 repo is included by default when creating the super pom, so you don't have to write it, it will look there by default.
I think the best solution is to set up a Nexus or Artifactory repo for the team available over the network. Download the jar from the third-party location, and then upload it with the proper pom GAV values to your new local repo. Then add the URL of this repo to the repositories section of the pom. Everyone will get it when they sync up with version control.
maven-install-plugin can install files in local repo and generate poms, usage example:
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.3.0 -Dpackaging=jar -Dfile=ojdbc6_g.jar -DgeneratePom=true

Add a dependency in Maven

How do I take a jar file that I have and add it to the dependency system in maven 2? I will be the maintainer of this dependency and my code needs this jar in the class path so that it will compile.
You'll have to do this in two steps:
1. Give your JAR a groupId, artifactId and version and add it to your repository.
If you don't have an internal repository, and you're just trying to add your JAR to your local repository, you can install it as follows, using any arbitrary groupId/artifactIds:
mvn install:install-file -DgroupId=com.stackoverflow... -DartifactId=yourartifactid... -Dversion=1.0 -Dpackaging=jar -Dfile=/path/to/jarfile
You can also deploy it to your internal repository if you have one, and want to make this available to other developers in your organization. I just use my repository's web based interface to add artifacts, but you should be able to accomplish the same thing using mvn deploy:deploy-file ....
2. Update dependent projects to reference this JAR.
Then update the dependency in the pom.xml of the projects that use the JAR by adding the following to the element:
<dependencies>
...
<dependency>
<groupId>com.stackoverflow...</groupId>
<artifactId>artifactId...</artifactId>
<version>1.0</version>
</dependency>
...
</dependencies>
You can also specify a dependency not in a maven repository. Could be usefull when no central maven repository for your team exist or if you have a CI server
<dependency>
<groupId>com.stackoverflow</groupId>
<artifactId>commons-utils</artifactId>
<version>1.3</version>
<scope>system</scope>
<systemPath>${basedir}/lib/commons-utils.jar</systemPath>
</dependency>
Actually, on investigating this, I think all these answers are incorrect. Your question is misleading because of our level of understanding of maven. And I say our because I'm just getting introduced to maven.
In Eclipse, when you want to add a jar file to your project, normally you download the jar manually and then drop it into the lib directory. With maven, you don't do it this way. Here's what you do:
Go to mvnrepository
Search for the library you want to add
Copy the dependency statement into your pom.xml
rebuild via mvn
Now, maven will connect and download the jar along with the list of dependencies, and automatically resolve any additional dependencies that jar may have had. So if the jar also needed commons-logging, that will be downloaded as well.
I'd do this:
add the dependency as you like in your pom:
<dependency>
<groupId>com.stackoverflow...</groupId>
<artifactId>artifactId...</artifactId>
<version>1.0</version>
</dependency>
run mvn install it will try to download the jar and fail. On the process, it
will give you the complete command of installing the jar with the error message. Copy that command and run it! easy huh?!
I'll assume that you're asking how to push a dependency out to a "well-known repository," and not simply asking how to update your POM.
If yes, then this is what you want to read.
And for anyone looking to set up an internal repository server, look here (half of the problem with using Maven 2 is finding the docs)

Categories