11

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.

2 Answers 2

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

Another great source to search for artifacts is search.maven.org. It provides much more detailed (and better presented) information on them. Check out this page for some example.
3

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.

2 Comments

Yesssss... that's what I wanted. Cheers
Glad I could help. If you found the answer was what you needed, consider marking it as accepted as described in this link meta.stackexchange.com/questions/5234/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.