Easily add libraries to Eclipse Java Oxygen - java

I know there are post about JAVA libraries in Eclipse and I used a way to add some .jar files (like apache.poi and jsoup) to my project. My question is:
Where do we have to put those libraries and what settings do we have to change, so to make the libraries available for every future project we start ?
Thank you!

The most common tools for bringing dependencies into a Java project are Maven, Gradle, and Ant. I'll focus on Maven, as I estimate it to be the most popular of the three (though I have come to prefer Gradle).
I'm going to assume you have the following prerequisites installed on your machine and each of the developers on your team:
JDK 1.7 or above (in other words, Java)
A Java-focused IDE like IntelliJ or Eclipse
Here's what you need to do:
You and your team members will install Maven
You will create a pom.xml for each new project.
You will specify the dependencies for the project (like jsoup) in your pom.xml
Here's what Maven will do for you:
It will download all the dependencies you specify from an online repository like https://mvnrepository.com/
It will cache the dependencies in a directory in your user home folder called .m2 so that it doesn't need to download dependencies more than once per project
It will resolve the dependencies within each of your dependencies and avoid putting the same dependency on the classpath twice
The minimum pom.xml file you need is as follows:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yourcompany</groupId>
<artifactId>your-project</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>
</dependencies>
</project>
Copy that into a file called pom.xml in your root project directory. You can add any additional dependencies in the <dependencies> section of the XML file. By convention, all of your classes and code should go into the directory structure src/main/java/.
Any Java IDE you use will know how to open a project from a pom.xml file. In IntelliJ, you select File->New->Project from existing sources, navigate to your project's root directory, select your pom.xml, and click Open. IntelliJ will then open your project as a Maven project, read your pom.xml, and make all of your dependencies indexed and available for code completion and compilation.
Hope this helps

Related

Attempting to install a java library for the Botometer API, but the Github page doesn't have a lib folder or a centralized Jar file [duplicate]

I don't know how to use Maven at all. I've been developing for a couple years with Eclipse and haven't yet needed to know about it. However, now I'm looking at some docs that suggest I do the following:
"To include it within your project, just add this maven dependency to your build."
<repository>
<id>jboss</id>
<url>http://repository.jboss.org/maven2</url>
</repository>
...
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>1.1.GA</version>
</dependency>
How do I do this with my Eclipse project?
Please assume I know nothing about Maven. I just figured out it might be installed on my computer by typing mvn on the command line, but that's seriously the extent of my knowledge. I would be happy to continue knowing nothing about Maven if there is an equivalent, non-Maven way of following these instructions with Eclipse.
On the top menu bar, open Window -> Show View -> Other
In the Show View window, open Maven -> Maven Repositories
In the window that appears, right-click on Global Repositories and select Go Into
Right-click on "central (http://repo.maven.apache.org/maven2)" and select "Rebuild Index"
Note that it will take very long to complete the download!!!
Once indexing is complete, Right-click on the project -> Maven -> Add Dependency and start typing the name of the project you want to import (such as "hibernate").
The search results will auto-fill in the "Search Results" box below.
In fact when you open the pom.xml, you should see 5 tabs in the bottom. Click the pom.xml, and you can type whatever dependencies you want.
You need to be using a Maven plugin for Eclipse in order to do this properly. The m2e plugin is built into the latest version of Eclipse, and does a decent if not perfect job of integrating Maven into the IDE. You will want to create your project as a 'Maven Project'. Alternatively you can import an existing Maven POM into your workspace to automatically create projects. Once you have your Maven project in the IDE, simply open up the POM and add your dependency to it.
Now, if you do not have a Maven plugin for Eclipse, you will need to get the jar(s) for the dependency in question and manually add them as classpath references to your project. This could get unpleasant as you will need not just the top level JAR, but all its dependencies as well.
Basically, I recommend you get a decent Maven plugin for Eclipse and let it handle the dependency management for you.
Open the pom.xml file.
under the project tag add <dependencies> as another tag, and google for the Maven dependencies. I used this to search.
So after getting the dependency create another tag dependency inside <dependencies> tag.
So ultimately it will look something like this.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>doc-examples</groupId>
<artifactId>lambda-java-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>lambda-java-example</name>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-lambda-java-core -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
Hope it helps.
I have faced the similar issue and fixed by copying the missing Jar files in to .M2 Path,
For example: if you see the error message as Missing artifact tws:axis-client:jar:8.7 then you have to download "axis-client-8.7.jar" file and paste the same in to below location will resolve the issue.
C:\Users\UsernameXXX.m2\repository\tws\axis-client\8.7(Paste axis-client-8.7.jar).
finally, right click on project->Maven->Update Project...Thats it.
happy coding.
I have faced same problem with maven dependencies, eg: unfortunetly your maven dependencies deleted from your buildpath,then you people get lot of exceptions,if you follow below process you can easily resolve this issue.

Why maven is not finding a package located in another module/project?

I'm trying to compile a multi-module maven project. I have problems to compile a new module I added. This problem arises because the new module is trying to import a pair of packages which exist in another module.
This is the pom.xml of the new module:
Here is the view in the eclipse's project explorer, where i highlighted the 2 packages I'm importing from the class ServicioJMS of the new module:
And here's the error showed in the command line after doing a mvn clean package
As you can see below I added the module in the parent pom.xml:
So, I don't know what I'm doing wrong here ¿Any ideas?
NOTE: Eclipse has no problem resolving the dependencies related to the discussed imports.
I had a similar problem. Go to the library on which you are depending (under your ~/.m2/repository directory or in the ./target directory of the library) and find the compiled library jar. Use jar -tf to list it and check that the missing classes are listed directly in the jar.
If you are using the spring boot maven plugin or similar in the library on which you are depending then it constructs a jar with a radically different structure and the classes, while being in there, will be hidden within the structure rather than being directly in the jar by name. Eclipse understands this, but the spring boot loader won't unpack the spring boot maven plugin structure of a library within a dependency, it will only do that for the runnable jar being executed.
If this is true for you then you can remove the offending build plugin from the pom of your library making it just a library, not a runnable spring boot jar.
A few things:
1) Don't put a version or group if you have a parent, let them be picked up from the parent.
2) When importing a dependency from a module under the same parent use ${project.version}
here's an example:
<parent>
<artifactId>parent</artifactId>
<groupId>com.essexboy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>web</artifactId>
<dependencies>
<dependency>
<groupId>com.essexboy</groupId>
<artifactId>library</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
Use install instead of package because IntegradorTransformador artifact (jar) was created but not exists in m2 repository and IntegradorTransformadorReproceso project can't found the artifact
mvn clean install

How Maven looks for dependencies

I have read many documentation and tutorial about getting started with Maven. But few things are still not clear to me :
1) When pom.xml contains <dependency>, Maven will put that artifact/JAr in Maven local Repository. Right ?
2) Suppose, I have sub modules in my MAven project. Then what is the correct way to build the project ? one should start from parent module and then go to further sub modules. right ? and that way maven will create dependency jars and put them in local Maven Repo folder ?
3) For example, if my sub module pom.xml contains following dependency. How should I provide it (vd-ps.jar) to Maven so that Maven finds it when I do Maven clean install on Submodule/pom.xml? I mean to say what does Maven require or from where does it generate such user defined vd-ps.jar ? from compiled class files ?
<dependencies>
<dependency>
<groupId>com.b.t</groupId>
<artifactId>vd-ps</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
Ok, let me ask directly what I want to achieve: I have following pom.xml. and from it I would like to create a separate maven project (Say PTest) that uses dependencies (jars) of another big multi module maven project. Here :- I have a module named v-parent in my multi module project which has pom.xml in its trunk folder.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.b.t</groupId>
<artifactId>v-parent</artifactId>
<version>3.9.0</version>
</parent>
<groupId>com.b.t.test</groupId>
<artifactId>p_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ptesting</name>
<dependencies>
<dependency>
<groupId>com.b.t</groupId>
<artifactId>v-ps</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
</project>
1) Now where should I put this sub module v-parent in my newly created Maven project (PTest) in eclipse ?
I mean how do I exactly make my PTest/pom.xml find this parent jar v-parent.jar ?
I have separate jar file named v-ps.jar. where should I put it exactly (in which structure) in Maven local repo so that it can find it ?
Each time you run
mvn clean install
Your project will be built and the resulting artifact will be 'installed' in your local repository (usually ~/.m2/repository). So if you build the project which makes the jar com.b.t:vd-ps:1.1.0 this will then be available in your local repository for other projects to use as a dependency.
If you dependency is in another module under the same parent it can in included like so :
<dependency>
<groupId>com.b.t</groupId>
<artifactId>vd-ps</artifactId>
<version>${project.version)</version>
</dependency>
If you run a
mvn clean deploy
Your artifact will be built and deployed to the remote repository (Nexus or Artifactory or similar), provided you have the correct config in your pom.xml
See https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
1) Now where should I put this sub module v-parent in my newly created Maven project (PTest) in eclipse ?
I mean how do I exactly make my PTest/pom.xml find this parent jar v-parent.jar ?
So in this case "v-parent" will be another project in Eclipse of packaging type "pom". And if you want to build the child project "p_test" and its dependency "v-ps", you could list them as modules in the "v-parent" project's pom file like below:
<project>
<groupId>com.b.t</groupId>
<artifactId>v-parent</artifactId>
<version>3.9.0</version>
<packaging>pom</packaging>
<modules>
<module>p_test</module> <!-- Make sure you provide the right path here -->
<module>v-ps</module> <!-- Make sure you provide the right path here -->
</modules>
</project>
Now when you run "mvn clean install" on "v-parent" it should build "p_test" and "v-ps" projects and places a copy of their jar files in your local .m2/repository, assuming those projects don't error our during the build. So the parent project acts like an aggregate for the sub-projects and helps building them all at one shot.
I have separate jar file named v-ps.jar. where should I put it exactly (in which structure) in Maven local repo so that it can find it ?
If you have the source for this project, then you should include it as part of the "v-parent" project's pom as a module like I mentioned above, and when you build the parent, on success, the jar gets written to your local .m2 repo. Or if you have the source for "v-ps" project, you could directly build that using "mvn clean install" and the jar will be written to the repo.

How to use multiple GitHub projects in one Eclipse project?

So I have a GitHub repo and I want to supplement it with other projects in other repos. I am using Eclipse and Java as my dev tools. Is there a video I can watch or a tutorial? I've looked on YouTube and Googled the problem -- I'm probably not building a proper query so find what I need.
I don't want to merge two repos into one repo. I want to incorporate the code in another repo into an Eclipse project on my dev machine that uses one of my repos. I think.
If I'm understanding you correctly, you wish to use other projects/libraries in your project. You should look into Java build tools (ev. Maven, Gradle and others). Among other things, they let you specify a list of dependencies for your project.
Maven example
First of all, you should set up your project to use Maven.
For instance, if you wish to use joda-time (a popular date and time library for Java), you could go to http://mvnrepository.com/ and look for joda-time. From there select your desired version and copy-paste the Maven dependency to your pom.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myproject</groupId>
<artifactId>myproject</artifactId>
<version>0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.1</version>
</dependency>
</dependencies>
</project>
Now when you call mvn clean install from your commandline or use the Maven Eclipse plugin, your project is built and the dependencies you specified are downloaded and added to your class path.
Keep in mind, the source code for the dependencies is never added to your project, only the jar files are added to your class path.

How do I build my Maven pom in Eclipse?

I have a project that uses UI4J, and instead of using external jar I decided to go for maven, I am going to distribute it via git, so I guessed that this is a much better approach.
This is my pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Kalamaria</groupId>
<artifactId>KalamariaHarverst</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MavenFirst</name>
<dependencies>
<dependency>
<groupId>com.ui4j</groupId>
<artifactId>ui4j</artifactId>
<version>2.0.0</version>
<type>pom</type>
</dependency>
</dependencies>
</project>
The problem is that I can't find a way to get this to work. What ever I try I am still getting errors on the import of the library, meaning that the jar of the ui4j is not imported.
I have (among others) tried to do a "Maven bumvn eclipse:eclipseild" with "clean install" as goal
Downloading source and Updating Project from the Maven menu
even tried to do a mvn eclipse:eclipse from the console, but i got this error
The program 'mvn' can be found in the following packages: * maven *
maven2 Try: sudo apt-get install
How does this work, what should I do to import the declared jars?
Removing <type></type> was the correct first step that you need to do.
Now, there's probably still a pom.lastUpdated file in your repository that is wrong, you need to forcibly override it. The easiest thing to do is just delete the entire directory in your .m2 directory, which is located in your OS dependent home directory. On windows, this would be:
C:\Users\<username>\.m2\repository\com\ui4j
On Linux, this is usually in:
/home/<username>/.m2/repository/com/ui4j
Delete that directory, and then do Maven -> Update project, this should fix your problem.
By the way, mvn eclipse:eclipse is almost never the right thing to do, it's much better to use m2eclipse for your eclipse integrations as it works much more seamlessly.
I am not able to reproduce the behavior of the pom type being added automatically when you add the ui4j dependency. However, most of the time the correct dependency <type> is jar, as that is the default. pom dependencies are most often used when a project is simply a pom and nothing else, which is common as the parent pom of an entire application.
In this case (as in most cases), the type you want is jar, so don't specify a type parameter.
To add a bit more background to #durron597 's correct answer:
tag defines what Maven looks for in the repository when it downloads your artifact. Various types exist. Most common and default is "jar" - Maven will look for something packaged as "jar" - a regular *.jar file. Other types include "test-jar", "war" and "pom". Type "pom" means that Maven will look for something packaged as "pom" - basically your dependency's pom.xml file. Most of the artifacts you refer to are packaged as "jar" and do not supply "pom" packaging. See http://maven.apache.org/pom.html#Dependencies, search for "Type".

Categories

Resources