How do I convert a exist Java project to maven project? - java

I've installed m2eclipse, and using the embedded Maven3.
Now I want to add dependencies to my project(there are existing jars), but the search result is always 0, how do I deal with this ?
I don't want to download all jars for now, just want to create the dependencies now.

"spring" and "spring" are not valid specifiers for the Spring libraries.
Here is a link to a helpful article about artifact names for Spring:
http://spring.io/blog/2009/12/02/obtaining-spring-3-artifacts-with-maven/
You should be using names like:
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>

if you want to convert a project to eclipse maven project, you can open terminal and run
mvn eclipse:eclipse
or convert to idea maven project:
mvn idea:idea

Try searching for spring-context instead of spring. If you are not sure about the name of the artifact you are looking for you can use wildcards, eg *spring*.

The dialog in your question is a bit confusing. It's actually two dialogs and should be using tabs. In the upper part, you can manually add a dependency if you happen to know all the details.
In the lower part, you can search for dependencies. The search will only work if you leave the fields at the top empty.

Let me see, you want to use along with the maven dependencies existing in the project, right?
Assuming your project structure already in structured maven, you can follow this tip below, but beware, i think maybe you can have some sort of conflict between dependencies..
https://stackoverflow.com/a/11320920/1133742
http://charlie.cu.cc/2012/06/how-add-external-libraries-maven/
Use the website below to browse dependencies:
http://mvnrepository.com/

Related

How to copy maven dependency list from Eclipse as text

In Eclipse, when I open my pom.xml file and go to the "Dependency Hierarchy" tab, it shows two windows: Dependency Hierarchy and Resolved Dependencies.
Is there a way to copy/paste or export the list of dependencies shown in the Resolved Dependencies window into a text file, or a way to have maven print out an equivalent list on the command line?
Background: our team has been told we need to provide a list of all our dependencies and their versions, and it'd be a lot faster if there's a way to easily get a textual list of them vs having to manually type out each dependency and its version one by one into a spreadsheet. We have a lot of services and they all have a ton of dependencies when you need to list out the entire dependency tree.
You can try mvn dependency:tree -Doutput=<path_to_file> to get dependencies in a file.
It's more than just a list, but if you generate a Site it's all nicely documented. Just run
mvn site:site
inside the directory of your POM. You'll find the Site under target/site.
Little suggestion:
If your company wants to track the dependencies of applications, you shouldn't rely on lists or the like. Have a look into things like the dependency-check-maven plugin to scan for vulnerabilities during development and SBOM to track dependencies later.

Elasticsearch without Maven

Recently I have begun working on a Java Spring project that does not use Maven and I need to build elasticsearch functionality into the program but I cannot find any information about how to import elasticsearch without the use of Maven.
Has anyone ever had to do something similar?
Is it maybe possible to just use Maven on this one part of the project?
Thank you for any help.
Maven just provide you a way to manage your dependencies. So, if you manage your dependencies in directly downloading JARs, you just have to download all the dependencies you needs and include them in your classpath.
As #Camille Gerin-Roze said , all you need is to download the ElasticSearch dependencies and add them to the classpath.
A place to start is :
https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch/2.3.5
And using the "Download Jar" link
If you scroll down and read the part that says "Compile Dependencies (32)" , it will tell you what other dependencies you need to download manually and add to the class path.
Please note that each of these dependencies may or may not have co dependencies that you may need to download and add to the classpath as well.

How to properly use any Java API

I have been trying to use the vget library/api to make my own youtube video downloader. The vget library can be found here: https://github.com/axet/vget
I have downloaded the zip on github and I imported the project into eclipse. However, I am confused to how I am supposed to properly use this API. Should I make a completely new project, and import the classes that I need or do I put my own source files in the project of the api?
I have read other threads concerning this problem. However, they all mention how a api is typically packaged in a JAR file, but in my case it is just files and classes. So I am confused to how I should properly use this api.
The vget project is a maven project. You can see that because it has a pom.xml file in the root folder of the project.
To use it, you don't even need to download the source, because the compiled jar files are already stored in the central maven repository. You can find more information about this here:
http://mvnrepository.com/artifact/com.github.axet/vget/1.1.23
(in general, you can use the http://mvnrepository.com/ site to search whether your library is available on the maven central repository. If it's even a mildly popular library, then chances are that it is)
What you need to do is to make your own project a maven project.
Here's a "5 minutes" starter guide that describes how to do that.
When you've done that, you just add the dependency on vget to your pom.xml file in the <dependencies> section:
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>vget</artifactId>
<version>1.1.23</version>
</dependency>
Since you are making use of a 3rd party software, and not extending it with your own logic, the way to go is to create a new project, which references the 3rd party software.
You then construct your application and make it do whatever you need it to do. When it comes to using logic which is available within the 3rd party logic, you would then simply delegate that call to the 3rd party library.
I have seen on the link you have provided, that this is a maven project. You have to execute a maven package command, or maven install, so that the jar file will be generated.
With this jar follow the Bill's instructions, and add it as external library to your claspath.
When you do this, you will be able to invoke methods of that api.
Let us know if you need some help doing this in eclipse.
If your project is a maven project, you can solve dependencies problems just adding the dependency written on Readme file to your pom file.
The easiest and most automatic way is to use something like maven, ant, or gradle, that will automatically download and put the jars in to your classpath if they are in the central repositories. For example, in the maven configuration file(pom.xml) you can add this to the dependency list:
VGet Maven Repository
These build tools also allow you to add external jars if needed.
If
I would suggest you get familiar with Maven. At the bottom there is a Maven dependency you just have to include into your pom.xml, and then you can use the extension immediately.
Maven is a build platform which organizes your project in a technical way (convention over configuration, e.g. code is in /src/main/java, tests are in /src/test/java). The proper way is it to create a Maven project in Eclipse (you have to install the plugin and download Maven as well) and put the dependency
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>vget</artifactId>
<version>1.1.23</version>
</dependency>
into your <dependencies> inside your pom.xml. After adding it, you project recognizes the additional package automatically.
Nobody tinkers by adding libraries manually. It's actually not professional to work without a build platform like Maven or Gradle.

OpenIMAJ Jar Files

I am new to openIMAJ and I want to process some pictures using it. There are a lot of tutorial available but they all tell using Maven. Does anyone know from where I can download the jar files of openIMAJ to directly use in my Java project?
Thanks!
For all the features of OpenIMAJ there are more than 50 Jar file that you need to Download individually. Also if you somehow manage to find all those files manually on internet you may end up mixing some or other version which will make some classes incompatible. Although I would strongly suggest you to try understand what maven is and its capabilities, you can follow the below steps if you dont want to add dependencies using maven or even don't even want to know what it is.
1.) Go to Help Menu -> Open MarketPlace.
2.) Search for maven and download the plugin.
3.) In file menu create new-> new maven project
4.) Select your workspace and click next
5.) In the select an Archetype window look for add archetype and enter the following details
GroupID: org.openimaj
ArtifactID: openimaj-quickstart-archetype
version: 1.1
URL: http://maven.openimaj.org/
6.) Click next and give details of your own project and click finish
All the jars will be downloaded which you can see in your project structure. You can now stop worrying about maven and start concentrating on openimaj.
OpenIMAJ is rather complex and contains a lot of modules that you probably don't need to use in your project (i.e. if your making something to do image processing, you probably don't care to much about audio analysis, or content analysis of web-pages). In addition each of the OpenIMAJ modules has dependencies on numerous other projects (which themselves have dependencies, and so on). For these reasons, it isn't really all that practical to provide direct downloads of all the modules and their dependencies as it would take an incredible effort for the user to try and figure out which bits are needed and which bits are not.
The ideal way to specify which bits of OpenIMAJ you need is to use an automatic dependency management system; this does not need to be Maven however - any Maven compatible dependency manager will work (i.e. Ivy, SBT, Grape, etc...). There are examples of the snippets you need to add to your build system configuration for these on the OpenIMAJ front page in the box on the right (you might need to scroll down a bit).
If you really do want to manually include the relevant jar files in your existing project, I'd recommend the following approach, which uses Maven to build a customised set of jars based on the exact OpenIMAJ modules you need:
Run mvn -DarchetypeRepository=http://maven.openimaj.org -DarchetypeArtifactId=openimaj-quickstart-archetype -DarchetypeGroupId=org.openimaj -DarchetypeVersion=1.2.1 -DartifactId=oi-deps -DgroupId=oi-deps -Dversion=1.0-SNAPSHOT -DinteractiveMode=false archetype:generate to create a basic OpenIMAJ project (called oi-deps in this case).
Go to the oi-deps directory: cd oi-deps
Edit the pom.xml file to include only the bits of OpenIMAJ you need by removing any unnecessary <dependency> sections.
Run mvn dependency:copy-dependencies. This will create a target/dependencies folder that contains all the jars you need to add to your project.
I also didn't find any 'download all' site. And I think the reason is that there are a lot of dependences in some jar files.
I extracted some jars recently to use in a project without maven but that was quite wiry.
And I was only able to do this using another project with maven.
I think maven is good stuff and easy to use within ie. netbeans. So give it a try.

How check if dependency is used by any of my classes and list them in Maven?

I want to do some clean up in POM.XML. How can I check which dependencies aren't used at all by my code and if one is used then how tell which one is it?
Of course I don't want to simply delete dependency and then search for errors in IDE, because that could take ages when pom has got about 80+ dependencies.
I'm using SpringSource Tools Suite version of Eclipse.
Greetz
Have a look at the Maven Dependency Plugin. When running, it should list the dependencies that you declared but do not use, but also which intransitive dependencies you use without explicitly declaring them. Note that it you use reflection, the report may not be accurate.

Categories

Resources