Import external JAR Eclipse - java

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

Related

How to build a jar in Spring Boot then add it to other projects

I have a package in one Spring Boot project. The package contains domain objects.
The folder looks like this:
IDomainModel
ModelOne
ModelTwo
How can I make a jar of only these files then bring it into my other projects. I cannot use artifactory.
I will assume you have used maven.
If you have installed de jar using mvn install, then it should be available for all the projects you try to build.
Since mvn install puts your compiled jar in your local repository with the groupId, artifactId and version you provided in your pom.xml file. Then you could use that same jar as a dependency in any project with access to your local repository.

Add multiple dependencies of Maven project into a non-Maven project in Java

I am trying to add the Maven dependencies of a Maven project into a non-Maven project. However, there are too many and adding them one by one would be too tedious. Could someone suggest a shortcut to add them all at once?
What do you mean by adding dependencies to a non-maven project?
If you want to make it a maven project, right-click the project and in configure click convert to maven project. Otherwise, you can copy-past the jars into your build path. If you have the other maven project in your computer, you can locate the jars in .m2 folder in your user directory and add them to the project or download them one by one.
Run the following maven command in the main directory of your maven project:
mvn dependency:copy-dependencies
Then you will be able to find all of the downloaded jar files of your dependencies here:
{PROJECT-MAIN-DIRECTORY}/target/dependency/
You can add the -DoutputDirectory parameter to have the jar files placed somewhere else.
Afterwards you may simply add the jars to the build path of your non-maven project.

maven-jar-plugin repository layout combined with maven-dependency-plugin

So I am using the maven jar plugin to copy my projects dependencies to a lib folder in the target folder.
I set the classpathLayoutType to repository (repository) since some of the dependencies have the same name.
The manifest now states something like this: lib/some/group/id/artifact/version/artifact.jar
However the maven dependency plugin, which I use to copy the dependencies to the lib folder does not follow this layout. It just copies the dependencies to the base of the lib folder. How can I change this behavior to match that of the jar plugin?
Assuming you are using the maven dependency plugin, and goal copy-dependencies setting useRepositoryLayout to true should do the trick

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. :)

Eclipse how to import a jar file as a maven project

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.

Categories

Resources