Intellij maven options - What are differences between them - maven

Im a bit confused with maven options in intellij.
What are the differences between and when would you use them?
Reimport
Generate Sources and Update Folders
Download Sources
Download Documentation

Reimport forces a reload of the POM. This should only be necessary if you have any major changes.
Generate Sources generates code. Think WSDL to Java, etc.
Downoad ... downloads the source or documentation packages for dependencies. Generally not required, but it can help if you try to debug your code and you want to jump into (nicely formatted and documented) dependencies or with writing proper JavaDocs.
PS: In rare cases when your project is really messed up and Reimport does not help, use File -> Invalidate Caches / Restart...

Related

Why refresh of Maven repository is not enough for IntelliJ?

I had a NoClassDefFoundError problem with some test, launched from IntelliJ. In order to repair the situation, I had to make several changes in many poms of the project - adding new packages and excluding some old ones for to escape the overlapping of them. Also, I reapired the situation with different versions. But the situation did not improve. Again, some package, declared in pom, was not found where it should be.
I refreshed the maven repository by
mvn -e clean install -U
, as is advised in https://stackoverflow.com/a/9697970/715269 - so old and upvoted answer, that it surely looks as Santa.
The problem remained unchanged.
I output the maven map. It was correct and it contained all needed.
I looked at the list of the External Libraries of the project. It was the old uncorrected list of overlapping jars with same names and different versions, and without good packages I added just now, and well seen in maven tree output!
Already hapless,
I reimported packages in IntelliJ
by:
Ctrl+Shift+A, Reimport All Maven Projects.
Ho! The list of libraries got repaired. And the problem, mentioned in subj, disappeared.
The question is: How it could happen, that the same project has that very pom for everything, but gets packages differently being launched in maven and in IntelliJ?
I know about that feature "delegate IDE build to Maven". And I keep it turned off. But I am NOT talking about the different SW for building. Whether they are different or not, they should be up to the actual pom's. And whereas maven, if turned off from the automatic building won't know about changes in poms, IntelliJ KNOWS about them. It could have jars up to pom, or up to maven - it has sense, but it simply has some old rubbish. Was there some deep thought under that construction?
Every time you manually change the pom.xml file, including the dependencies you need to load these changes into IDE. IDE does it on Reload from Maven action. See also Import Maven dependencies.
Intellij doesn't use maven to bulid and run a project except you are delegating build and run action to maven:
Since, IDEA doen't really use maven to run and build, it uses the pom.xml to import the project structure and "tries" to build the project the same way was maven does.
Actually, there are quite a few differences between these to build processes.
Generating sources or filtering resources (don't know if this is still an issue) aren't done during building the project with Intellij IDEA.
In case you are using code generation you have to build the project via maven first and then - when all the resouces are filtered and additional sources are generated - you are able to run, debug aso. the project with Inellij IDEA.
That's an important thing to be aware of and that's the reason why maven and IntelliJ IDEA project structures might get out of sync.
You can enable the "Reload project after changes in build scripts" feature and select the Any changes checkbox to keep your project structure updated:
Why should you disable this feature anyway
If you are working on a build file (gradle or maven is not important) reloading the structure on any change can be very anoying. It's cpu intense, dependcies are fetched aso.
Therefore, I prefer to reload project structure only in case of an external change. This happens when pulling an updated version of the build file for example.

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

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 :

using pom.xml instead of iml files in idea

We are using IntelliJ idea in order to develop our product.
till now, we saved the iml/ipr files in a version control in order to share the same configuration among developers (and also between different versions of our product).
however, since idea changes those iml files a lot, it is an exhausted task to resolve its conflicts, and actually feels a bit wrong.
Moreover, idea 15 is no longer checks-out iml/ipr automatically.
I tried the maven-idea-plugin which suppose to generate the iml/ipr based on the pom.xml and it looks promising (except for some minor issues) but it's obsolete and there many stackoverflow posts advise against it.
I also undestand that the "correct way" to load our project is by loading the e pom.xml directly in idea.
The problem with that loading the pom.xml is that each user has to reconfigure idea (such as exclude directories, set the run configuration, plugins, maven version, JDK location etc.)
every time he download the code from the version control, which happens quite often.
So, my question is what is the best way to use intellij idea with our project.
my goal is that once a developer download the code he will be able run idea without manually configure a thing.
Thanks,
Meir
I am using Intellij 15.0.3 Community edition.
In our current project we do not add *.iml to Git.
For shure we do add pom.xml to Git.
I did not add any special plugins or configure idea in the way you described
(such as exclude directories, set the run configuration, plugins, maven version, JDK location etc.)
So I am using Idea out of the box, only the plugin 'Maven integration' is activated. I initially cloned the Git repository in a new folder.
In Idea I choosed File| New | 'Project from existing sources '
and selected the folder with the pom.xml.
Thats all, works perfectly. I can build in Idea or on the command line.
I can use Git in Idea or on the command line.
Everything works perfect.
So I can asure you that its absolutely possible without having *.iml under version controle.

Can't navigate to sources for Maven dependencies easily with IntelliJ IDEA 12

I added a library to maven by searching from within IntelliJ, and I checked the boxes to download sources and javadoc, which Idea tells me it did.
However, in some of the library files I click into, it just says "/* compiled code */", even though the exact source code for those files is available in another jar which i can see right there in the project.
Does anyone know how to fix this?
If you check the libraries tab in the project settings you'll see that each library can have an entry for its sources jar. If the one you're looking for doesn't have one, add it manually.
This is all a lot easier if you use maven.

Resources