Maven - How to find correct groupId/artifactId to include dependency in POM - maven-2

In general, what is the best strategy for finding the groupId and artifactId for a well-known dependency for use in a Maven POM? For example, how would I find the correct entry for the MySQL JDBC driver? It's not mentioned on the mysql.com site, so I would usually spend time looking for another POM that already uses the Maven repository version of the jar. This can't be the best way.

I would suggest to use http://search.maven.org

M2Eclipse, the maven integration of eclipse, supports local searchable indexes of maven repositories. So you can right-click a project or a pom.xml and do
Maven -> Add Dependency
A dialog appears where you can search for artifacts from your local indexes. Wouldn't want to miss it.
If that's not an option, use mvnrepository.com

Most of the time my IDE does this for me. But when I'm not sure I search manually; Google really helps.
You can also search on maven search engines like this mvnrepository.com.

I use http://repository.sonatype.org.

Maven site suggests to browse biblio to get maven co-ordinates; http://www.ibiblio.org/ .
Say for log4j
First, we need to know what the groupId, artifactId, and version are for log4j. We can browse ibiblio and look for it, or use Google to help by searching for "site:www.ibiblio.org maven2 log4j".
Maven metadata for log4j: http://mirrors.ibiblio.org/maven2/log4j/log4j/maven-metadata.xml

In my experience the best option is mvnrepository.com. for example in your case, just type mysql in the search text field. you will find all available artifacts that contain mysql string. in your case the one fit your needs is: mysql-connector-java
for each artifact you have a general description that helps you decide what is best for you.
once you click it you get all the available versions and once you click on a version you have all sort of information about it like the code you need to add to your pom.xml, the artifact dependencies and so on...

1. Go to http://search.maven.org
2. Search in the required jar with groupId/artifactId or Just with basic text.
Like for your case

Related

IntelliJ - is there any good Maven plugin?

I am trying IntelliJ 12 and one thing is really disappointing.... this is the maven integration.
I am missing following functionality:
effective pom view
search for particular jar trough dependency hierarchy
classpath should be derived directly from maven pom, and not in some crazy random unknown way. I have the case now, where some dependencies are just omitted, and I can see them when executing mvn dependency:analyze
changes is pom.xml are sometimes not reflected in project (classpath does not change, or has still old dependency and new one), also when I click "reimport"
Does anyone know some other maven plugin? Something like m2e would be really nice ;)
As #tieTYT noticed, there is a 'Dependency popup' (Ctrl+Alt+U, Ctrl+F works there).
Idea can automatically detect changes made to your pom.xml and apply them to project. To enable this, press Ctrl+Shift+A, type 'maven auto', choose "Importing", checkbox "Import Maven project automatically";
See https://stackoverflow.com/a/19582120/417846 for my answer. It at least provides a relatively easy way to get the effective pom.
I just moved from eclipse and my idea was, that there are many different plugins for single technology, well not in this case - case closed.

Maven Artifact Search is always empty

When using Alt-Insert to insert a dependency into the POM, the Artifact Search is always completely blank regardless of what I search for.
I tried to add repo1 to Settings->Maven->Repository Services, it says "no repository found." I find that hard to believe.
I've also tried to "update" my local repository but that results in an error.
FYI I'm using Community Edition Snapshot.
Thanks!
If you have a brand new IntelliJ install and have never updated your Maven Repository, notice that there is a little "Update" button to the right that becomes clickable when you click on each repository.
Preferences > Build, Execution, Deployment > Build Tools > Maven >
Repositories
The important one is the https://repo.maven.apache.org/maven2/ remote repository, make sure you update it.
I used Artifactory to generate a settings.xml for me, and by default it pointed me to the "libs-releases" repository.
Instead want my "releases" repository to point to the much larger virtual repository "remote-repos."
All it took was a simple change to the options of the "Generate Settings" function.
Weird. The Artifact Search is working fine for me with IntelliJ Community Edition, at least for artifacts from the central repo that has been indexed:
And as you can see above, all repositories declared in POMs known by IntelliJ are listed.
There might be something wrong with your Maven settings. This is a wild guess but is your Maven home directory properly defined? Same for the user settings file? Is Maven actually working fine under IntelliJ?
Solved - by reinstalling Intellij Idea
Search for class tab in Maven Artifact Search popup was always empty
Solution in linux:
remove idea folder (for me it was ~/idea-YOUR_VERSION_HERE)
remove ~/.IntelliJIdeaYOUR_VERSION_HERE folder (settings)
download new version form jetbrains.com, unzip, run installer from bin
It appears to be a blocked port, as I am using my own artifactory repository. Of course, the port it is using looks to be completely undocumented, but WireShark shows it to be 58754. Sounds random, I hope it isn't!

Get full name of an artifact in Maven

I need to know the complete filename of a specific artifact in Maven. I looked at versions-maven-plugin but it seems to change the pom itself. Is there another way to reach my goal? I want to store the complete artifact name in a property so I can pick it up in Java code.
Normally you have your dependencies and let maven do all the work of retrieving the files from the repository for you.
But you want to use it in your Java code - what Java code?
Are you writing your own mojo that will be doing something during the install phase of your lifecycle?
Then you should be able to access the artifacts that are currently being processed, from there it's not a problem to get the filename.
But maybe you've got just the groupId, artifactId, version and type of an already existing artifact.
With these informations it should be no problem to navigate to your local maven repository, open the directory: groupId/artifactId/version and look what files you've got there. Usually there should be your .pom a .jar, maybe a md5-hashsum and some maven-metadata. If you're not sure which file's the interesting one: take that one with the biggest filesize.

Setting up a standard directory layout using Maven

I'm new to Maven and have skimmed over the documentation as I am following the Hibernate tutorial at http://docs.jboss.org/hibernate/stable/core/reference/en/html_single/#tutorial-firstapp-mvn.
I have installed Maven and successfully setup a web-app but this does not contain all of the standard directories mentioned in the tutorial. Am I going mad?
When building my Maven project I am using the maven-archetype-webapp. This gives me the arh-webapp\src\main\resources and arh-webapp\src\main\webapp directories but I'm missing quite a few directories mentioned on the link http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html.
Surely I don't have to manually add these? If not then the Hibernate documentation does not mention what archetype to use in order to achieve the directory structure used in their tutorial. Please can someone enlighten me.
What archetype do I need to use in order to have the above directory plus the src/main/java directory? If there is no such archetype then can easily append these using Maven? and how?
Surely you'll have to manually add these.
Just create those directories that according to the Maven convention are missing. Remember, a Maven Archetype is just a starting point to save you time configuring your workspace. After encountering many problems in some Archetypes myself I've been accustomed to just use a basic-web-app-archetype and then customize it myself, as a beginner with Maven you'll be better off doing that, and will learn a lot.
Regards.
Not all the directories mentioned are required for your standard web application. In fact, the reason behind the presence of the src/main/java, src/main/resources and the src/main/webapp directories is due to the archetype that you've used.
IMHO, the book titled "Better Builds with Maven" will serve you better; the Sonatype books on Maven might also help. The complete Maven documentation is also available as a PDF file, for future reference.
But just in case, you need some clarity on the terms used, here's some:
Archetype: A pattern for projects. Simple web applications (with no dependencies on other frameworks/libraries) have their own archetypes, so do applications using Spring, Hibernate, Seam, etc. Some archetypes will result in creation of different directories, as they are coded that way. You might be able to change the directory structures in most cases, although I cannot vouch for every archetype. For instance, it is quite possible to place your sources in 'src' instead of 'src/main/java', although this requires additional configuration in the POM.
Lifecycles, Phases and Goals: A Maven build lifecycle is a series of phases, with each phase executing a set of goals. Maven can be commanded to execute a build phase, which results in execution of all phases until and including the specified phase.
Maven plugins: Maven plugins contain one or more goals. Goals need not be bound to phases, but usually you would bind them to particular phases. Plugins are the basis for everything operational in Maven; you're using plugins even though you are just compiling the application (the Maven compiler plugin is a core plugin that is present in the Maven distribution).
I hope the above helps, but I would suggest that the reference books be followed.

Maven repository configurations

I've asked a similar question in which part of this was addressed, but I'd like to expand in more detail.
When configuring maven to look at internal repositories, is it best to put that information in the project pom or in a user's settings.xml? An explanation on why would be really helpful here.
thanks,
Jeff
You should always try to make the maven project so that it compiles from a clean checkout from source control in your local environment; without a settings.xml. In my opinion this means that you place any overrides to sensible default values in the user's settings.xml file. But the pom should contain sensible values that will work for everyone.
I encourage you to put the repository definition in the POM, this way any developer just grab a copy of the code and run Maven to get it compiled, without having to change things in his settings file.
I find the setting.xml file useful just for hacking Maven's behaviour in special situations, for example when one repository is not accessible due to a firewall and you need to use a mirror. But that's my personal opinion. Maven documentation gives you more freedom:
The settings element in the
settings.xml file contains elements
used to define values which configure
Maven execution in various ways, like
the pom.xml, but should not be bundled
to any specific project, or
distributed to an audience. These
include values such as the local
repository location, alternate remote
repository servers, and authentication
information.
If you have a local repository which is used in every single project you may add that at the settings.xml, just be sure that configuration is well documented, in my current project it's not and new developers struggle at the beginning when they try to compile something.
We use the user's settings.xml and include info in the README about what possible other repos may be needed.
In theory a given group-artifact-version is the same no matter which repo it comes from. It works pretty well for us. If you find yourself with two different assets that have the same group-artifact-version identifier, then that indicates you're doing something really bad.