Eclipse how to import a jar file as a maven project - java

I have downloaded a source jar file of OrientDB from maven repo, orientdb-source.jar. The structure is:
orientdb-source.jar/com/orienttechnologies/orient/core/...
/META-INF/...
I have installed m2e plugin. But I dont know to import this jar file as a maven project. I dont want to import this jar file as an external jar. Can anyone help me?

It sounds like you want to use/access this artifact within a maven environment.
If all you want to do is use the jar in your class path you should add the jar as a dependency in your project's pom file. You may also need to add the repository reference to the pom if the jar is coming from a private repository. Once your pom file is modified with the reference to the jar then you can select your project and click "Maven -> Update project dependencies". Maven will download the jar and modify your class path.
If you want to actually look at the source then look for a reference on the 3rd party's web site to the repository containing the source code. Use the appropriate Eclipse plugin (git, svn, etc.) to point to and checkout the version of the source.

If you want to import the jar on your project, put this on your <dependecies> tag in pom.xml on root of your project:
<dependency>
<groupId>com.orientechnologies</groupId>
<artifactId>orientdb</artifactId>
<version>1.3.0</version>
</dependency>
From Maven Repository
This will import the jar on your project lib folder.
Anyway, if you want to modify code from this lib, you need to create a new Project with content of this jar. Package as jar on maven and put as dependencie.

Related

Is there any way to modify classpath through POM.xml?

I have a situation like below:
1. I have a JAR which is available in cloud URL
2. I want to download the JAR and use it as a JAR in my project.
3. For downloading I am using Maven ant run plugin and putting it in specific folder of the project.
Now I want to add the jar to classpath file via pom.xml
I have downloaded the JAR from the cloud and placed it in specific folder.
Is there anyway in which as soon as I download the JAR from cloud, I can modify my classpath by adding a classpathEntry through POM.xml?
There are various ways but the easiest option is to download the file to your local repository with the file path matching to your dependency's groupId, artifactId and version.
Assuming below is the dependency that you are trying to add to you pom.xml
<dependency>
<groupId>your.company</groupId>
<artifactId>downloaded-file</artifactId>
<version>1.0.0</version>
</dependency>
then download the file to /your/company/downloaded-file/downloaded-file-1.0.0.jar

Import external JAR Eclipse

I have a Maven project where I've imported an external JAR (via Build Path) and it's under Referenced libraries folder. How can I import it within my .java class file? I mean literally the code "import ??". The class is in com.example.demo package. Do I need to add a dependency somewhere (pom.xml)?
If that's a maven project the best way would be to add that jar as a dependency in your pom.xml dependencies section. Then when you compile with maven it will use that dependency. In order to build properly in eclipse you need to call mvn eclipse:eclipse and it will sync the dependencies in the pom.xml with the build paths of the eclipse project.
If the jar is not a common one and you cannot add find it in the public repo (for example some jar that you have created) then you need to first install it in your maven repository with the required group and artefact name. Otherwise if it is some library which is publicly available in the central maven repository maven will download the required jar and install it in your local repository for you

Maven dependency for self placed jars in project level

I created the java class and converted into jar files. So, I want to use those jar files which I have placed in project level in some folder like "External Jar".
So I need to write a dependency in maven that when someone imports my project they should be able to run the program.
Basically you created your own jar and you want to publish this jar, so that when somebody else clone/use your project, this jar comes with (assuming that you have a maven project and dependency of your jar is included in pom.xml).
To achieve this, you need to publish your jar to maven , you can follow many of the online docs like http://kirang89.github.io/blog/2013/01/20/uploading-your-jar-to-maven-central/ on how to publish jar to maven central.
Edit:- As suggested by khmarbaise, please use official reference http://central.sonatype.org/ for central repository.

IntelliJ: How to import a folder of .java files as a library?

It's probably a stupid question, but I can't figure out how to properly import a folder of .java files as a library into my IntelliJ project.
I am trying to use the JavaMoney API, which I downloaded from here.
I know that the normal way would be File > Project Structure > Modules > Dependencies > + JARs or Directories, yet that always adds an "Empty Library" to the list, which is nowhere to be seen in the Project View.
Could someone set me on the right track here?
You should not use the sources directly. You should start to use maven (or gradle). In that case you can simply add a dependency:
<dependency>
<groupId>javax.money</groupId>
<artifactId>money-api</artifactId>
<version>1.0.1</version>
</dependency>
If you don't want to use maven, you should import the existing maven project from the repository and export it as a *.jar file.
Or you simply download the prebuilt version from a mvn-repository.

local dependency in my case

I am using maven to build my java project.
I have a library jar named my.jar used as my project's dependency. However, it is not available in the remote central repository. So, I would like to put the jar file under my project. So, I created a folder named my_repo/ under MyProject.
The directory structure of MyProject looks like this:
MyProject/
my_repo/
my.jar
pom.xml
But I have no idea how could I define my pom to find this particular dependency under MyProject/my_repo/my.jar ?
Could someone please help me for my scenario? By the way, I have also some other dependencies defined in my pom.xml, they are available in the remote central repo.
Using the system scope. ${basedir} is the directory of your pom.
<dependency>
<artifactId>..</artifactId>
<groupId>..</groupId>
<scope>system</scope>
<systemPath>${basedir}/my_repo/my.jar</systemPath>
</dependency>
The recommended way is usually to use a Maven Repository Manager (e.g. Nexus) and then deploy your library to this Maven Repository Manager.
See here for how to set up the settings.xml file:
http://books.sonatype.com/nexus-book/reference/maven-sect-single-group.html
How to create maven dependency for your local jars..
1)Create a maven project of the program of which you have to create the jar.
2)Add required groupid and artifact id and version no.
3)Then after writing the java files export it as a jar.
4)Now open other project in which you want to add local jar dependency.Go into its pom.xml and add dependency with the group id,artifact id and version which u had entered in the jar project.
5)Maven clean and install.Now that jar file will be available in your local repository. :)

Categories

Resources