Maven local dependency isn't working: ClassNotFoundException - maven

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.

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.

Add a module dependency in Maven

I have got two Maven projects. One of them has to be dependent on the other. I use IntelliJ and I have tried to right click on project1 > Open Module Settings, and in the dependencies tab I clicked on the + symbol to add a directory or jar dependency. So far so good, when I try to import packages from the dependency it autocompletes it for me, however the compilation throws errors, saying that there are no such packages. What am I doing wrong ?
There is no notion of project in Maven.
You have a Maven project B. You chose its groupId (com.mycompany, for example), its artifactId (B, for example), and its version (1.0-SNAPSHOT, for example). You run mvn install on this project. This generates a B-1.0-SNAPSHOT.jar file and stores it in your local Maven repository, with its pom.
Now you want to use B-1.0-SNAPSHOT.jar in another Maven project A. For A, B is a library, just like any other library you use (log4J, Spring, Hibernate, Guava, whatever). So you add a dependency to it in the pom of A, just like you do for any other library:
<dependencies>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>B</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- other dependencies: log4J, Spring, Hibernate, Guava, whatever -->
</dependencies>
Read the awful documentation for more details.

Adding external JAR to Maven project in NetBeans

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

How to attach JavaFX2 javadoc jar automcatically with maven?

I have in my pom.xml the dependency to jfxrt.jar, as a system scope one:
<dependency>
<groupId>com.oracle</groupId>
<artifactId>javafx</artifactId>
<version>${java.fx.version}</version>
<scope>system</scope>
<systemPath>${javafx.abs.dir}</systemPath>
</dependency>
The problem is I would also like to attach the javadoc for this jar. So I went and downloaded the javafx2 javadoc,packed it into a jar, installed it in maven using install file and using javadoc as a classifier. The resulted jar has name javafx-2.2.4-javadoc.jar. As you know the main jar has name jfxrt.jar.
<dependency>
<groupId>com.oracle</groupId>
<artifactId>javafx</artifactId>
<version>${java.fx.version}</version>
<classifier>javadoc</classifier>
</dependency>
When I call mvn dependency:resolve -Dclassifier=javadoc I get:
[INFO] The following files have NOT been resolved:
[INFO] com.oracle:javafx:java-source:javadoc:2.2.4
What am I missing?
Regards,
Aurelian
Well, you don't want the javadoc as dependency, I guess, you probably just want the IDE to show the JavaDoc. I just tried the following in NetBeans and it worked:
select a referenced JavaFX class and hit CTRL+SPACE to show the JavaDoc
The pop-up provides links to configure the JavaDoc
don't use the system dependency for binary, just install:install-file it into local repo as you did with the javadoc.. then it should start working

Maven - Unable to resolve dependencies

Im trying to compile a Maven project. The compile fails however due to a "Failure to find xx.xxx.jar" in the repository i have specified in my settings.xml. I have access to this repository and when i navigate to the Url of the repository maven is trying to use i can see a pom file with the name of the jar but no jar. When i open the pom it contains the correct groupid and artificatid and jar name however the jar is not in the same directory.
Maven gives another error saying that "resolution will not be reattempted until the update interval of my repo-server has elasped or updated are forced".
What is happening here?
When maven goes to the repo i specify in settings.xml and finds a pom for the jar does it then try and go out to some external site to resolve the dependency or should the jar exist in the same folder as the pom?
What module are you attempting to download?
I discovered something similar with the following Maven central module:
http://search.maven.org/#artifactdetails|net.sf.json-lib|json-lib|2.4|jar
The Maven POM packaging declaration was jar, but no jar in Maven called "json-lib-2.4.jar"
When I looked at the files actually stored, I discovered that the author is providing two versions of the jar, each compiled for different versions of the JVM:
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk13</classifier>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>

Resources