Adding external JAR to Maven project in NetBeans - maven

When I right click on my Maven project and choose the Add dependency option and I type my external jar's name in query, it is not found. How to add external jar to a Maven project?

From the NetBeans forum:
Open the Projects tab.
Right-click on Dependencies.
Select Add dependency.
Set groupId to: group.id (can be anything)
Set artifactId to: artifact.id (can be anything)
Set version to: 1.0 (can be anything)
Click Add to continue.
Dependency is added to pom.xml and appears under the Libraries node of Maven project. Continue:
Expand Dependencies.
Right-click on library (e.g., group.id).
Select Manually install artifact.
Set Artifact to install with the Java Archive (.jar) file path.
Click Install locally.
Library is installed locally with dependency attributes (coordinates) entered in steps 4 - 6.
I found those instructions helpful when going through the NetBeans GUI. Basically when right clicking to add a dependency, the group id, version, and name must be populated with anything. Then that "dependency" will be listed in the dependency drop down. Right click on that newly created dependency and right click to install locally and navigate to the appropriate jar file.

You can follow this tutorial:
http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
Example:
Install the jar to your local maven repository:
mvn install:install-file -Dfile=cxf-2.7.3.jar -DgroupId=org.apache.cxf -DartifactId=cxf-bundle -Dversion=2.7.3 -Dpackaging=jar
Edit the pom.xml file in your project to include the newly added dependency:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-bundle</artifactId>
<version>2.7.3</version>
</dependency>
This should work regardless of the IDE you are using.

In Netbeans, the approach to adding dependencies that are not in repository is reversed. First come up with maven coordinates in the Add Dependency dialog. Then right click on the new dependency node and trigger "Manually install Artifact" action.

This answer is for jars that are in the maven repo
Let's say I want to add log4j-1.2.17.jar to my project, all I have to do is find it in maven repository
Step 2 is to copy that and place it inside the dependencies tag of your pom.xml` file:
<dependencies>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.4</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
....
....
<dependencies>
Step 3 Build and clean your project. The jar file will be in your dependencies folder afterwards

one trick is in the netbeans main menu select: profile->options->java->maven put in the global execution options the parameters example: -Dfile=C:\Users\anonym\Desktop\commons-pool-1.6.jar -DgroupId=commons-pool -DartifactId=commons-pool -Dversion=1.6 -Dpackaging=jar
where de parameter -Dfile is the location of the jar file
-Dfile=routeToJar
after that select your project. then rigth clic on the select project. and then select Run Maven->Goal. when the wizard appear type in as Goals install:install-file .. and then clic OK buttom

Related

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.

Maven local dependency isn't working: ClassNotFoundException

I have a Maven Web project made in netbeans and I have a local dependency:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>3.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/sqljdbc4.jar</systemPath>
<optional>true</optional>
</dependency>
That dependency works perfectly if I run the project over Apache Tomcat 6.0 but I have some class that I run on demand (main method) and when I try to run the class I get this exception:
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
Try this (it works for me)
Get rid of the maven dependency, and the jar from the lib (we'll have it automatically added)
From your project, right click on the "Dependencies" -> "Add Dependency"
In the dialog, type the coordinates for the groupId, artifactId, version, click ok
This will leave an unresolved jar in the dependency tree.
Right click on the jar from the "Dependencies", select "Manually Install Artifact". Locate the artifact and add it, and "Install Locally". This will install the jar to the local repo. Also since its a webapp, the jar will get sent to the lib like it normally would
Now you should be able to run it.

maven v3.0.5 cannot find sqoop artifact

i am trying to build my project with maven v3.0.5 and jdk 1.7. snippets of my pom xml looks like the following.
<properties>
<sqoop.version>1.4.4</sqoop.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.sqoop</groupId>
<artifactId>sqoop</artifactId>
<version>${sqoop.version}</version>
</dependency>
</dependencies>
when i run "mvn clean package" i see the following error:
Could not find artifact org.apache.sqoop:sqoop:jar:1.4.4 in central (http://repo.maven.apache.org/maven2)
usually and typically, i go to mvnrepository.com, and search for artifacts, and whatever artifacts i can find there, i have always been able to simply reference them in my pom.xml. according to this link, http://mvnrepository.com/artifact/org.apache.sqoop/sqoop/1.4.4, this artifact should exist?
i can't really browse http://repo.maven.apache.org/maven2.
Browsing for this directory has been disabled.
View this directory's contents on http://search.maven.org instead.
so i decided to use the search site for sqoop.
http://search.maven.org/#search%7Cga%7C1%7Csqoop
it seems to me that this artifact exists, but somehow, i can't access it. any ideas on what i'm doing wrong?
EDIT -
The Sqoop JAR varies based on what version of Hadoop you're using.
If you want to build against some Hadoop 0.23.x variant, what you need is the following:
<dependency>
<groupId>org.apache.sqoop</groupId>
<artifactId>sqoop</artifactId>
<version>1.4.4</version>
<classifier>hadoop23</classifier>
</dependency>
You will be able to see what classifiers are available using Eclipse. Right-click pom.xml, Maven --> Add Dependency. Type 'sqoop' in the search box, then expand the collapsed menu for org.apache.sqoop and note the options, hadoop23, hadoop100, etc.

Add Javadoc/source to Joda-Time when imported as Maven dependency

I'm new to Maven and the Joda-Time library.
Is there a simple way to add Javadoc/source code to the Joda-Time library when I import it with Maven?
This is what I added to the pom.xml:
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.0</version>
</dependency>
Thanks.
If you are using Eclipse with the Maven Plugin you'll find this setting under: Preferences / Maven / Download Artifact Sources and Download Artifact JavaDoc
For Netbeans8 IDE In Your maven project:
Choose services folder.
Click 'Maven Repositories'.
Click 'Central Repository'.
Find joda-time.
Double click joda-time artifact group.
Double click the joda-time artifact.
Choose your version number.
Right click and add dependency.
It will then be added to your pom.xml and be
downloaded and compiled into your project.

question about maven dependency

i loaded the maven project in eclipse then found sth wrong with pom.xml file, when i clicked the 'overview' tab(m2eclipse), it said:
Failed to read artifact descriptor for commons-logging:commons-logging:jar:1.1.2-SNAPSHOT
when i clicked the dependency hierarchy tab, it showed 'Project read error', however i have no problem to run 'mvn dependency:tree' from command line and can see there is a dependency on commons-logging:
commons-logging:commons-logging:jar:1.1.1:compile
just don't understand where the commons-logging 1.1.2-SNAPSHOT comes from. any idea ? Thanks.
In order to determine where the dependency commons-logging comes from (even without the .pom editor and its dependency editor) open the console and execute the following command:
mvn dependency:tree -Dverbose -Dincludes=commons-logging
This will show all dependencies of commons-logging.
I looked through the dependency tree, but did not find any reference to 1.1.2-SNAPSHOT.
Finally, adding the following dependency to my pom.xml solved the problem:
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.2-SNAPSHOT</version>
</dependency>
eclipse by default does not use the same maven installation as your command line. Depending on your m2eclipse version it might use an embedded maven snaphsot (with strange behaviour). Check
eclipse -> Window -> Preferences -> Maven -> Installations
and add your external maven installation (prefer current version 3.0.3) to the list and use this as default.
It's most likely coming into your project transitively. You can check the "Dependency Hierarchy" section of your pom editor in eclipse and see where it's coming from (search for commons-logging in the right top box). Also, I cannot see a 1.1.2-SNAPSHOT version of commons-logging on central so most likely someone has made a mistake in a dependency pom.

Resources