Is there a plugin like "mvn search"? - maven

I want to use VIM for Java programing, but I found it is very cumbersome to find and set a Maven dependence at command line.
Is there a plugin to search dependence like "apt-cache search"? and adding a dependence to pom.xml like "apt-get install"?
This should be a basic function, Why there is no such plugin ? Thank you :)

There is no plugin, and I can't answer why.
However in the IDE I use (IntelliJ) and probably others, there are help pop-ups that ask you if you want to add a dependency to a POM automatically (most likely only for things in your local repository). Also, when editing a POM, anything already in your local repository will autocomplete as you type out the dependency (and if there are multiple versions, the list of versions to choose from is displayed). Just letting you know in case switching to an IDE would help you.
I think the best you can do for search is use a browser off to the side with MVNRepository loaded up. You can search for libraries and when you see the one you want, you can copy-paste the example dependency into your POM. Note a small fraction of dependencies listed there are not in the most common public repositories and require you to customize your repository list.

maven dependency plugin could help you a little bit on listing/analyzing dependencies. But it cannot add a dependency for you.
in fact, if you prefer VI to edit files, why not just open pom.xml in your vim, and edit the file directly?

If you prefer working with Vim, you can still do it in Eclipse: try Vrapper on the free side. On the non-free side, you can try Viable or viPlugin.
Eclipse then has Sonatype's m2e plugin, which lets you add dependencies and plugins with a sleek search wizard (not to say a whole bunch of other helping features).

Related

Ant to maven conversion

I need to convert few projects from ant to maven. I know the basics of both, also read a lot of articles on how to. However, is it a good idea to write a pom using eclipse? Or is it better to write it without using eclipse? The M2Eclipse plugin needs maven to be tweaked more, will the changes related to M2Eclipse cause problems when the war is built on jenkins?
Pros of using Eclipse IDE:
The formatting of pom.xml will be taken care, when you add the 'maven-eclipse-codestyle.xml' to the code formatter, as mentioned here. Formatting will be harder when you do it without an IDE.
The auto-completion feature of eclipse will make your coding easier, since it will automatically sense the open tags and close them. You need to close all the open tags manually, if you don`t use an IDE.
You can view the dependency hierarchy of the dependencies added and hence it might be a bit helpful while managing the dependencies.This can be extremely useful ,when you have transitive dependencies (Dependencies within other dependencies). You can find more about transitive dependencies here.
An IDE will warn you of common mistakes that may occur while coding (something like, 'forgetting to close an open tag','placing a tag in an incorrect location'). This will save a whole lot of time. If you don`t use an IDE, you need to correct the mistakes only when you get an exception after executing a maven command.
Eclipse will warn of missing artifacts (when the dependencies are not present in the local repository), which can help you to fix it before executing the maven goal.
Cons of using Eclipse IDE:
m2e plugin will throw 'Plugin execution not covered by lifecycle configuration' errors all over your pom.xml files. These errors won`t affect your build, but may be quite annoying.
I personally have done migration from Ant to Maven2. IMO it is better to write pom.xml on your own so that you wont face any last minute surprises as well as you will get complete overall knowledge on what you are exactly doing with your pom file.
However if you still want to go with m2e, from my experience it did not create any problems at all. Regarding the build - I guess there shouldn't be any issue. You can refer this link if you need more info about m2e and jekins - m2e and jenkins

How to find a library reference in Intellij Idea

I have a complex project with a lot of Maven libraries. From time to time I am facing the problem of a library version conflict. I can open the class which is conflicting and see all libraries a class is packed in. But unfortunately I don't find an easy way to check where these libraries are referenced in the Maven dependencies. Do you have an idea how to find a library in the Maven tree?
You need Maven Helper plugin
https://plugins.jetbrains.com/plugin/7179-maven-helper
It has a dependencies viewer and lets you resolve conflicts right there. Open your pom file after installing the plugin and you will see another tab at the bottom of the editor window.
Right click on any dependency to bring up the context menu and you will see an option to exclude it. It will add exclude to your pom file. That obviously won't work for uberjars.
Going more low-tech, you can let maven tell you the answer with the command
mvn dependency:tree -Dincludes=groupId:artifactId
You can also invoke this command simply from an Intellij terminal window.
mvn dependency:tree will print the entire tree, the includes addition will let you filter on a specific artifact making it easier to search (although I've been known to just copy the entire tree into a scratch file and just do searches on it there).
If you don't know the groupId or the artifactId is already unique enough, you can filter like this:
mvn dependency:tree -Dincludes=*:artifactId

Can I use maven to add sources as part of my project?

I am using a dependency and I have some issues with it.
I could download the sources and include it as part of my project and then start modifying the source to help me debug my issue.
However, is there an easier way to do this, using maven ? I have the source-jars downloaded but I am not sure if I can then use these source jars and modify the code as well ?
I could in theory unbar the sources and add them to my source build path, but is there an easier way to accomplish this ?
I am using maven and IntelliJ.
Are you able to get a successful build of the dependency you're having issues with (in it's own project?).
If so, change the version, e.g. 1.2.3-CUSTOM-1, make the improvements, rebuild, and use it as a proper maven depdendency with <version>1.2.3-CUSTOM-1</version>.
This might seem a lot of work, but it's not really - you end up with a properly versioned jar.... having a "hacked" version of 1.2.3 jar is asking for all sorts of problems later.
On the plus side, you can share and deploy the -CUSTOM-1 jar if you need to, and you can keep versioning -CUSTOM-2, etc.
This is the "proper way" I would say.
As Thorbjørn Ravn Andersen wrote, source jars are read-only. You can setup debugger breakpoints in them, but you can't "write" in them
Either unjar the sourced jar or if you know it came from a git release tag, clone the original repository and checkout the correct tag
Make sure its sources compile
Bump up the version in case you know you are going to hack the source
On the right pane, use Maven projects / Plus icon to add sources and use the "m" Execute goal icon to build the sources in IDEA :

How to run plugin on maven dependency

I'm setting up a (java) maven project that depends on a library (Jettison, among others) that is in the Maven repo. Jettison, in turn, depends on stax. I need to run a tool (Jar Jar Links) on stax (to change the namespace). How do I alter the rules for a transitive dependency in a maven project? My transitive dependencies are being included in my target folder using the copy-dependencies goal (I assume this is how things are usually done). I assume that this is the point where the plugin would be run on the transitively-generated artifact.
Extra question: I don't need this at this point but how would I go about altering the source in the transitive dependency? I can get the jar of the source with mvn dependency:sources but, from there, I'm not sure what the right approach is.
Victory!
Seems at least two people are even more clueless about Maven than me so let me explain what I'm doing before I report the fix at the bottom of this post (spoiler alert: it looks to be a bug in JarJar).
Android uses Java but its missing a lot of the java core (specifically, javax classes). The Android DEX compiler (which converts .jars to Android .dex files) won't even allow you to compile things in the java.* or javax.* namespace because it'll (usually) break stuff. However, in some (many) cases, there are routines that you might want to include -- specifically because they are used by existing libraries. The most legendary is StAX, which is why Google posted an example of how to include it here in the Dalvik repo's wiki. The example uses JarJar... with ant. Transitive dependencies are not really an issue when you aren't using a repo so they are not addressed in the wiki.
I was able to get JarJar to run on my source with Maven but without changing the namespaces in the dependencies (and transitive dependencies), that's worthless. Hence my question.
I thought that the copy-dependencies plugin might be useful for... copying the dependencies and running a transforming plugin in the process. Copying dependencies is mentioned as a step in the official "Maven in 5 minutes" doc so it seemed like a good start but maybe the the people who wrote the official docs don't know how to use it :-) . Either way, it it didn't help -- there is no simple way I could see to transform the jars as it copies.
Using the verbose spew from Maven, I was able to see that Jar Jar was in fact processing my jars properly... and then throwing out the result. It would have packaged the converted classes from the transitive dependencies in my artifact with the rest of my code but, instead, it "Excluded" them. Jar Jar parameters are basically undocumented and most of the tags aren't even listed in the docs but all of the examples I could find use a section with wild-cards that tell it what classes to hold onto. At least I thought (think?) that's what the section is for. Instead, it seems to randomly throw out stuff. Basically, the section is busted. For example, I had:
<keep>
<pattern>com.example.**</pattern>
</keep>
...thinking that this would keep classes that began with com.example. Wrong. It keeps whatever the hell it wants. I tried a million things in that spot until one worked:
<keep>
<pattern>*.**</pattern>
</keep>
This only keeps the classes I wanted -- the classes it updated and the originals of the ones that it didnt touch. Note that ** doesn't even work. This is version 1.8 of the JarJar plugin (the version most poms Ive found use).
Back to work.

how to get over these maven/m2eclipse issues

(first, I admit, I have no love for maven/m2eclipse, but it wouldn't be that bad if I could figure out how to overcome these issues)
I am using maven/m2eclipse. m2eclipse is the only good way I know of to suck in the maven jars. Some of these may not have solutions(but I am hoping to be surprised). Maybe solving #9 solves them all?
ISSUES
When I run "mvn clean package", I am dead in the water as far as running a unit test in eclipse while maven is building as I LOVE to multitask but maven prevents me here. How to get around this?
I move eclipse to point to eclipsegen/classes but then the unit tests are still using the classes in target/classes so it's not using my latest code that I just edited in eclipse and debugging is not lining up and it's stepping on blank lines that don't have code.
If I just slightly touch the pom.xml, bam, it builds when I don't really want it to and turning of automatic builds did not seem to help.
On top of #3, I get random pom builds downloading jars which just freezes eclipse from doing anything why the jars are being downloaded(I am a bit multitasker so this frustrates me to no end)
If I want to modify or do something really custom I need, the answer is usually create a java plugin but this then would require me to create another source control project with another automated build making sure the build tags all versions so we can reproduce issues with certain versions. (in ant, I just modify the xml to do custom stuff).
(I hear there is a bug open for 5 years on this one). global exclusions because people on our project keep breaking stuff when they include new things that depend on log4j and sucking that library in breaks us so we want to globally exclude it so people stop breaking the project when adding new things (IVY has global exclusions, why doesn't maven!!!!)
The xml code for generation from an xsd in maven is about 2-3 times the code of doing it in ant. Why is this? That really shouldn't be the case I think.
Running my unit test says xxxx-12.0.8-SNAPSHOT is missing but in my pom.xml it clearly says 12.0.9-SNAPSHOT not .8. ie. m2eclipse gets into some weird state and I get screwed wasting yet more time because someone selected maven
(I don't like IvyDE for the same reasons I don't like m2eclipse). In maven, is there any way like in Ivy to say on a build MOVE ALL jars into target/lib so that I can uninstall m2eclipse(if maven had this one feature, I think all my problems might go away)....That IS AN Ivy feature that rocks by the way!!!!
NOTE: I just realized that uninstalling m2eclipse and running "mvn eclipse:clean eclipse:eclipse" is not really an option since on this project I had to import 30 projects. I think on single projects, that is a great solution.
Is there no way like ant to log the command that was run for debugging purposes? ( in maven how to log the command that was run? )
I should really look into gradle(I hear it's best of maven and ant) as the theory of maven sounded great but you can tell there was a lot of controversy over it(which usually indicates a bad tool). Good tools that really help typically do have some controversy, but not as much as maven has had so it makes me think twice as I don't want to screw the guy who takes over my project(and I know ant will work). Many people I think don't even consider that. They think "I am fine, so why won't the next guy be fine".
Any ideas on how to fix the above issues?
About #9, if you have 2 alternatives :
Execute
mvn dependency:copy-dependencies
See http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html for customization options. You'll have to set your Eclipse classpath manually to point to the newly copied jars.
Use JBoss Tools JDT Extensions to get the "Materialize Library" feature (see http://docs.jboss.org/tools/whatsnew/core/core-news-3.3.0.M4.html). You'll basically just have to right-click on the Maven Classpath library, select a destination folder, select (and rename) the jars you want, and you'll get a m2e-free project in Eclipse (still a valid Maven project in command line though).
You can install JBoss Tools JDT Extensions from http://download.jboss.org/jbosstools/updates/development/indigo/
Disclaimer: I like Maven and M2Eclipse, and I have not experienced any of the issues that you mention. In general, M2Eclipse does not get in the way much for the way I'm working.
One thing that might help is disabling the Maven Builder for the projects (right-click the project, select "Properties", then "Builders"). This will get rid of many of the issues you're complaining about.
One other thing that might help you (and comes close to #9 on your list: Uninstall M2Eclipse and use mvn eclipse:eclipse, which will generate Eclipse .project and .classpath files, which include all dependencies as Eclipse project dependencies. Whenever you add or change dependencies, you will have to run mvn eclipse:eclipse again. Give this a try...
Although it does sound like a faster PC would solve some of your issues I do agree that the m2eclipse plugin sucks (although it sucks a little less since eclipse indigo). Because of this I switched to using Intellij for a while but I switched back to eclipse after a month (for me, eclipse is still the best in spite of m2eclipse).
I use m2eclipse to be able to work in eclipse but nothing more. All my maven builds (package, install, whatever) I run with maven itself (command line) simply because there have been too many occasions where the result was different (working in one, not in the other and and maven was always correct).
So, sorry, no direct answers to your questions, just some tips:
1) do it outside eclipse
2) stick to maven standards (target/classes); that will make your life a lot easier
6) using dependency management in a parent pom might help a bit
8) if you get the same issue when running from command line then there is a problem in your pom (resolve using mvn dependency:tree), if not, see 1
9) maybe assembly is an option here but I would not recommend your approach

Resources