Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find a Maven Dependency or Repo from an Import Statement in Java Code

Tags:

java

maven

Is there a nice easy way to find out the Maven information for a library specified in an import statement in Java code?

For example, if I see some random piece of Java code on the internet that looks useful (happens often!), and I want to copy it into a Maven project, how do I find out what that dependency information is (i.e. the groupId, artifactId and version) to place into the POM.xml file?

e.g. If I see this:

import wow.magiclibrary.net.*;

public class Magic
{
  public static void main ( String[] args ) throws IOException 
  {
    try 
    {    
      MagicLibrary ml = new MagicLibrary( args[0] );

      ml.doSomethingAmazing();

...

... where can I find the information to put into the POM.xml file to make Maven download the wow.magiclibrary.net library as a dependency from whatever repository it comes from?

<dependency>
    <groupId>wow.magiclibrary.net</groupId>
    <artifactId>magiclibrary</artifactId>
    <version>2.0.0</version>
</dependency>

Do I just have to use Google to search for a repo on the web that has the library, download jars manually and install them into my local maven repo?

Just curious. I'm lazy. I know I can do something like this after downloading a jar file:

mvn install:install-file -DgroupId=wow.magiclibrary -DartifactId=magiclibrary \
     -Dversion=2.0.0 -Dpackaging=jar -Dfile=magiclibrary2.0.0.jar -DgeneratePom=true

... but if there was a way to automate the search and install process through Maven then that would rock.

like image 765
Fuzzy Analysis Avatar asked Apr 19 '14 13:04

Fuzzy Analysis


People also ask

Where can I find Maven dependencies?

In your project's POM, press Ctrl and hover the mouse over the dependency. Click the dependency to open the dependency's POM. In the dependency POM, view the active dependency, its transitive dependencies and their versions. You can check the origin from which the dependency was pulled in.

How do I get Maven dependencies in IntelliJ?

You can enable such feature going to File > Settings > Maven > Importing, there is a checkbox that says "Import Maven projects automatically". If that doesn't help, then I would suggest to make a full clean-up and start again: Close your project window (and IntelliJ) and remove all *.

How do I find the dependencies of a jar file?

jar file. Use the -verbose:class option to find class-level dependencies or use the -v or -verbose option to include dependencies from the same JAR file. Use the -R or -recursive option to analyze the transitive dependencies of the com.

What is Maven dependency in Java?

What is Maven Dependency? In Maven, a dependency is just another archive—JAR, ZIP, and so on—which our current project needs in order to compile, build, test, and/or run. These project dependencies are collectively specified in the pom. xml file, inside of a <dependencies> tag.


2 Answers

When I'm looking for the maven dependency for a library I know usually go to

http://mvnrepository.com/

If I'm looking for a library I don't know, I would first do a simple google search or grepcode.com and then go to mvnrepository.com

mvnrepository allows you to easily copy paste the maven dependency, here is an example for commons.lang version 2.6

http://mvnrepository.com/artifact/commons-lang/commons-lang/2.6

like image 135
Frederic Close Avatar answered Nov 09 '22 00:11

Frederic Close


You can use online tools like grepcode.com, which will identify you the jar names and maven artifacts. Here is an example for apache commons-lang StringUtils.

like image 45
Angular University Avatar answered Nov 08 '22 23:11

Angular University