How to reference to packages from Maven local repository? - java

In my project I need to reference to the package installed by Maven to local repository. I've added the dependency to pom.xml, but the project doesn't want to build as it wouldn't even see the package. Here's the pom.xml fragment with dependencies:
<dependencies>
...
<dependency>
<groupId>libraryweb</groupId>
<artifactId>classes</artifactId>
<version>1.0SNAPSHOT</version>
</dependency>
</dependencies>
When I try to import the package "libraryweb" I get
Cannot resolve symbol 'libraryweb'
error. What can I do to use the package?
Edit: Intellij prompts me classes from the package when I try to use them, but I still get "cannot resolve symbol" error.

Note that groupIds and package names are not necessarily the same.
So while your JAR is installed under a certain groupId, artifactId and version, the actual package that you need to import in your Java code may have a different name.
In doubt, unpack the JAR and look at the directory structure.

I solved the problem by linking the .jar file with IntelliJ projects settings instead of using Maven to do it.

Related

Eclipse unable to resolve maven test dependency

I'm running into a bit of a strange issue with eclipse 4.18 that I'm hoping someone can help me with. My project is a jetty webapp that does some communication with an external server over FTP. I'm using mockftpserver (https://mockftpserver.org/) to assist with unit testing the code that needs to communicate with the FTP server.
I have the library dependency defined in the pom as
<dependency>
<groupId>org.mockftpserver</groupId>
<artifactId>MockFtpServer</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
I then have an import defined in my test class like
package my.project.test;
import org.mockftpserver.fake.FakeFtpServer;
public class TestGuiceModule extends GuiceModule {
}
Nothing too exotic going on, and maven seems to be able to run the compile. Eclipse however does not seem to be able to see the library. I have a bunch of errors along the lines of
The import org.mockftpserver cannot be resolved.
I double checked the build path and I can see the library listed under 'Maven Dependencies' and the package structure of the jar matches my import statement. Even stranger, I have other 'test' scoped dependencies in the pom.xml and eclipse is detecting those just fine. I'm at a bit of a loss as to why it is having an issue with this one particular library. Any suggestions on what I could do to track this down?
I was eventually able to solve this by deleting my settings folder and .project file and reimporting the project (see: Eclipse throws NullPointerException during Maven update).
The project was originally created in an older version of eclipse, but I thought I would be ok since I was checking out a fresh copy from the git repo. Apparently something must have gotten checked in that the newer version of eclipse didn't like.

Importing local jar through maven gives fully qualified name of the class

I am using my local jar project in another project through a maven dependency.
<dependency>
<groupId>some.groupid</groupId>
<artifactId>some.artifactid</artifactId>
<version>1.0.0-RELEASE</version>
</dependency>
The jar is getting liked to the project but when I try to access any class from it, say com.companyname.app.Abc; it is imported as com.companyname.app.Abc and not just Abc with import statement separate.
I even tried putting import statement separately, but that did not work.
The IDE I am using is IntelliJ.
It was pom related issue where few of the plugins were causing the issue.
I tried creating project from scratch and got the difference.

Importing a 3rd party jar on Maven and IntelliJ IDEA

I know there are tons of questions and answers about this problem, but none of them helped.
I have a jar, which I would like to import to my local java project.
I have created the project on IntelliJ IDEA with Spring Initializer.
I have used this command to install the jar into ~/.m2 repo
mvn install:install-file -Dfile=C:/Users/xxxx/Desktop/xxxx/lib/mylib-1.2.11.jar /
-DgroupId=com.test.custom -DartifactId=mylib -Dversion=1.2.11 -Dpackaging=jar
Then I have added these lines into my pom.xml file
<dependency>
<groupId>com.test.custom</groupId>
<artifactId>mylib</artifactId>
<version>1.2.11</version>
</dependency>
I have no errors, everything seems in order.
However, when I want to import a class from that jar.
import com.ca.bla.bla.MyClass
I receive an error called "Cannot resolve symbol "MyClass".
How can I solve it?
I have tried to create a lib folder and copy the jar in it and add it as a module
I have tried to create a localrepo for maven
They all gave me the same result...
Thanks in advance

Build Maven project with local dependencies

I downloaded old project, which based on Eclipse, Ant or something else, I don't know this build system (I use Maven\Gradle). Project sources was converted to Maven manually, but project had more jar libraries. They were imported in project with help of IDEA (Project Structure -> Modules -> Dependencies), but libraries defined only in .iml file.
When I tried to build project in .jar with help of Maven — Maven show errors, that it cannot find classes from libraries (but in sources all good). I found in Internet example, like in this code sample:
<dependency>
<groupId>example</groupId>
<artifactId>example</artifactId>
<version>examle</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/example.jar</systemPath>
</dependency>
But this don't help. Libraries are old too, and they created by old build tools, which don't write artifact id, group id and etc., so I don't know this information.
How I can write dependencies on local .jar libraries in pom.xml if I don't know information (group, artifact ids) about they?
UPD_0:
When I try to set only system path to library, I take this:
All you need is set correct path in the
<systemPath>${project.basedir}/libs/example.jar</systemPath>
group, artifact ids and version are user defined information. So you can define it as you wish.
Try to install this jar in ur local repository from command line / terminal like this. Then add the dependency with the package and version given by you in the command without scope as system
mvn install:install-file -Dfile=<path>/example.jar
-DgroupId=com.something
-DartifactId=example
-Dversion=<give some version>
-Dpackaging=jar
-DgeneratePom=true

How to import org.apache Java dependencies w/ or w/o Maven

So the quick background is I am creating a java program, that uses many different imports
import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.util.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
I know that Netbeans isn't finding these files because I do not have them on my computer. But is there a way to have Netbeans automatically connect with org.apache and retrieve these files? Or do I just have to go and download them. Someone recommended using Maven, but I am not sure if this is the right solution or how to go about that?
Thanks
Unless you use a Maven structure (see here getting started with Maven) you will have to download all jars manually.
If using only Hadoop (as in your example) this might not seem that much of a deal, but when working with big projects it is easier to declare your dependencies in a pom.xml file. It is much more easier than downloading X different jars, and you can easily move to a newer version of a library, rather than having to delete and and download another.
I saw that someone asked in a comment why people like Maven so much. Well, to be honest, I personally find it easy to use and very useful. Furthermore, a Maven project can be easily imported in IntelliJ, Eclipse or Netbeans, whereas creating for example an IntelliJ project can cause difficulties in importing it in Eclipse or NetBeans.
To get started using Maven with Netbeans, you can go to: New Project, Categories:Maven Projects:{Best Option}. Then in the project files, open pom.xml. Here is where dependencies for your project are added. If you are not sure what to insert try searching for your jar name + "maven" on the internet. The plugin for Netbeans is able to connect to the maven repository and autocomplete most fields.
Sample from: http://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.6
<project...>
....
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
...
</project>
Download the .jar file here: http://mvnrepository.com/artifact/org.apache.hadoop/hadoop-core/0.20.2
In Eclipse, right-click on your project, click Properties, search in the text box for Library, click on Build Paths, click Add External JAR, and select the file you downloaded from the link above.
You will have to download the jar-files yourself. Unless you start using Maven or a similar dependency management tool.
You must download them. The name org.apache.hadoop is a package name, and we only use the name of the site as a convention. See this tutorial on packages for more information. Essentially a package is a folder on your computer, often in the Java\jre\lib\ext\ directory.
Refer tutorial
http://www.tutorialspoint.com/hadoop/hadoop_mapreduce.htm
It mentions :-
Download Hadoop-core-1.2.1.jar, which is used to compile and execute the MapReduce program. Visit the following link http://mvnrepository.com/artifact/org.apache.hadoop/hadoop-core/1.2.1 to download the jar.
Not a netbeans user , but I'm sure that even in netbeans, you have a maven plugin.
"Mavenize" your project, and when you will perform mvn clean install, you will get these jars to local maven repository.
With Eclipse I use the m2Eclipse plugin and it works really well for me.
This of course depends that these jars can be found in maven repositories over the net, such as maven central repository.
I have final figured out my preferred way to create a new Hadoop project and import the dependencies using Maven.
Using NetBeans I create a new Maven project.
Then under project files, I open the pom.xml.
I finally add inside of
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>0.20.2</version>
</dependency>
After building with dependencies I am now ready to code.

Categories