Fuzzy Analysis
Fuzzy Analysis

Reputation: 3188

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

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.

Upvotes: 11

Views: 6839

Answers (2)

Frederic Close
Frederic Close

Reputation: 9639

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

Upvotes: 7

Angular University
Angular University

Reputation: 43117

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.

Upvotes: 3

Related Questions