how to use maven with intellij 13 - maven

I have some trouble with intellij and maven.
I try to add a library and I do not want to type the text. I want to use the maven repository explorer. When I do this from the project settings->libraries->add from maven the library is fetched but its not automatically added to pom.xml which is what I would expect.
If i use the code generator to add a dependency in pom.xml the search artifact only searches through the libraries already existing and doesn't go through the repositories.

If you want to add a dependency you need to edit the pom.xml. IntelliJ will ask you if you want to update your project with the updated pom (or just do it if you have enable the auto-import under Settings-Maven-Importing).
Adding a dependency as a library will simply update your .iml (IntelliJ's format of your module information).

Related

How to trigger IntelliJ to reimport single Maven dependency?

I have a workflow working on an application and one of its libraries that somewhat looks like this:
Make changes to library -> Push library jar to remote Maven repository with no version change -> Pull updated library jar from the remote repo to the downstream app -> Test and make changes to the app and library
But seems like the way IntelliJ indexes and/or caches Maven dependencies is not affected by me running a clean install from the Maven interface. Is there a surefire way to force IntelliJ to discard any cached dependency and reimport, or possibly do it only for a desired library?
Very likely this has nothing to do with IntelliJ. Since the version number is the same, maven won't re-download your dependency. Try to just delete the dependency locally from the maven repository:
rm -rf ~/.m2/repository/<..path to your library package..>
You could also avoid pushing the library to the remote repository, and test completely locally, by using the library as a local dependency. For this approach, see answers here: How to add local jar files to a Maven project?
Or since you are not effectively changes the library version the right approach would be to use the library project sources as a direct dependency for IDE maven project. For this - add this Maven library project as a new module to existing Maven project: File | New... | Module from Existing Sources... and select pom.xml file of this library project.

can't find my maven artifacts

I'm working in eclipse and I have a problem that I can't import my personal java libraries.
I created the libraries and 'installed' them into my local maven repo (using mvn install). This created a subdirectory related to the 'version' name that was in the POM file from when I ran the command. Which seemed fine.
So in this directory there where the usual jar files and other stuff.
When I released this file I manually changed the name of the version in the POM. going from 0.0.1-SNAPSHOT to 0.0.1-RELEASE
This seems to have worked as I would have expected.
However I can't seem to find import the new release jar.
Using the maven repositories browser in eclipse I can see that the new artifact is in the 'local' repository.
I try to add the dependency in the following methods:
Select the main project -> Maven -> add dependency.
This adds the dependency details into the pom but with a type value detail of <type>pom.lastUpdated</type>
Select the project pom.xml file -> Maven -> add dependency.
This time the artifact for the 0.0.1-RELEASE is greyed out I can select it, but I guess nothing is actually happening.
The original 0.0.1-SNAPSHOT it selectable, and if I use this I do not have a <type> detail in the pom.
I don't understand why there is a difference in the RELEASE and SNAPSHOT artifacts, as they have both been generated in the same way, and clearly they are both visible in the browser, the contents of the directory on disk are the same. The file names and contents are identical with the exception of the word RELEASE or SNAPSHOT.
I know that I can simply add in the RELEASE jar to my build path, but this seems to be a ridiculous thing to have to do if I intend to use maven (or do I need to do this).
I don't want to use an external repo for storing my artifacts, and I'm not too keen to go to the trouble of installing nexus (or similar) on my local machine (just because I've had trouble with it in the past).
What am I missing so as I can get my maven project to see my local repository and all its artifacts.
Thanks in advance.
David
ps I've already tried things such as mvn dependency:purge-local-repository which definitely pulled in / updated all the local jar dependencies.
So I've managed to work around my problem.
As such this solution is ridiculous, and breaks all the purpose of maven.
So I found (from running maven from the cli) that there was an error in the parent of the project I was attempting to use.
The parent was missing a direct link to the scm plugin (version error).
Once I solved this problem, I then returned to the sub project, and got a lot of errors from missing stuff from the parent.
Essentially it was not 'seeing' all the log4j dependencies.
The solution (well non-solution really).
Add all the log4j dependencies to the sub project.
Edit in SCM and surefire test plugins (as it also started to fail the test code due to missing junit).
So this is great.
I have to define all my dependencies on log4j and in my sub / child project's pom.xml file.
I also need to define them all in my parent's pom.
As it then still refused to run tests in my new project (that used the above as a dependency), and refused to find log4j also. I then decided to add all of these as dependencies for my current project.
Great.
I thought the whole point of maven was I could define my dependency on log4j in my my logging library that I use (which is the dependency), and then it would 'automagically' pull in all the required from this dependency.
Clearly not.
As stated at the start. This is NOT AN ANSWER it is a crazy work around.
My logging library that I use should be able to define its own requirement on a specific log4j version (such as moving from log4j to log4j2), and then when I include this as a maven dependency any change to the required dependency should be seen automatically.
But No : I have to import the dependency on log4j in my other projects also. So now if I had updated my logging library from log4j to log4j2 I would need to go to all my project that use this library and update their pom's to ensure that I have the correct version of log4j.
Seems the whole point of maven has just been lost!
Can someone please tell me where I am going wrong!
David.

gral-examples Maven dependency

I added gral-core Maven dependency to my project as it is explained here
http://trac.erichseifert.de/gral/wiki/Download
Problem is that I also need to add gral-examples.jar in Maven dependencies, but I cannot find the code for it. I need this to work because I share this project with few other people via EGit plugin for Eclipse, so I can't just add this library to build path, I need it to be Maven dependency.

Intellij maven dependency prefer local code

i'm working on a project in which i have an android application project which has pom dependencies on other projects
something like this:
<dependencies>
<dependency>
<groupId>some.project/groupId>
<artifactId>some.artifact<artifactId>
<version>1.0.0</version>
<type>apklib</type>
</dependency>
</dependencies>
while developing i always want Intellij to use the local source code from the some.project library i have in the project.
the problem is it will pull version 1.0.0 from the server and use that.
i tried using LATEST as version, this works fine as long as my some.project library version is updated to the latest version on the server.
since we have automated builds that will increment the version for some.project when changes are made i will have a scenario in which my source says 1.0.0 but the server has 1.0.1 and again intellij will pull the server apklib
Any suggestions how to achieve this? if i could use a dev profile to somehow define this for development only and a prod profile to use exact version numbers (which it should) that will be amazing.
Thanks!
You can add a module dependency in IntelliJ:
File->Project Settings->Modules click on the module -> click Dependencies tab and then click the green '+' sign and choose '3. Module dependency'.. add the module on which you want to depend locally and click the blue arrows to bring the module above the Maven dependencies.
There is a checkbox "resolve Workspace artifacts" under Maven Run/Debug Configuration, select it then it'll work as expected.
We are dealing with the same problem...
"IDEA should resolve dependencies as a module dependency type (rather than a local jar Maven library) if this Maven project is opened in IDE and it's version matches the version, installed in a local Maven repository."
https://intellij-support.jetbrains.com/hc/en-us/community/posts/206836605-Always-prefer-local-module-source-over-Maven-dependencies
Our solution was to add local Maven profile in pom.xml and specify local dependency version. To make it work, you have to turn on local profile.
<profiles>
<profile>
<id>local</id>
<properties>
<dependency.version>x.x.x</dependency.version>
</properties>
</profile>
</profiles>
When local dependency is updated, you have to take care to update this version too, but currently this is the best solution we found and it's working for us.
Assuming that you have the project organised as a multi-module project, and that you're talking about a dependency on a library that's one of the project's modules, then you need to use a snapshot dependency, and you need to use the full maven release process. As it is, you're depending on a pre-built, static version of the given library, but you need IntelliJ to look at the source and class you compile instead, and snapshot dependencies are used exactly for this purpose.
This works for Maven projects in the following way:
Add the dependencies that you want to work with to the Maven Projects tab.
Navigate to the implementation of the class for which you are looking.
Once you are in the implementation, you can navigate back to the Super Method for the declaration.
That's either Navigate -> Implementation(s) or ALT+COMMAND+B (on the Mac map). Intellij will then present you with the option of decompiling the class from the JAR in the dependency list or the source code in the attached Maven project.
For example, I am in project bank of the Rooskie Bank, and I want to view the code for the Account class in bank-domain. I click on an Account object declaration and press ALT+COMMAND+B. From the popup menu, I choose the definition from the list of sources that does not have "(Maven: com.rooskiebank.bank-domain.jar)" in the choice.
IMO this is an awkward way of doing things. As much as I like it, I find Intellij is difficult to use in enterprise-sized systems with multiple in-house libraries, and I still use Eclipse a lot for them. Intellij works better with Git and is great for stand-alone microservices.
I imagine Gradle works in a similar way.
Just a small hint how to do the same with Gradle. Supposing you have two workspaces with lib and app. The lib is deployed to some maven repo and needs to be described in the following way:
// build.gradle
group 'lib.group'
version 'lib.version'
// settings.gradle
rootProject.name = 'lib.name'
While app dependencies are the following:
// build.gradle
compile "lib.group:lib.name:lib.version"
So, the app module uses lib as a maven dependency. Now, you can import lib sources into app workspace using File | New | Module from existing sources. After refreshing the project from Gradle tool window, you will get two modules lib and app in the app workspace. However, even after reimporting Gradle the lib dependency will still point to the maven repository, what you can check in File | Project Structure.
The trick to be done here is to click with the right mouse button on app module in the Gradle tool window, and select Composite Build Configuration option. Then, on the popup window just select the local lib module to include it in the app module build. Now, after reimporting Gradle deps again for the whole workspace you will get the maven dependencies for lib replaced with the local dependency.

Maven beginner question, get m2eclipse to download jar and add to build path?

From what I have read, after adding the relevant maven repositories, maven should automatically download the necessary jars to satisfy dependencies in the pom.xml file.
However, no jars ever get downloaded for me after I add dependencies in eclipse. Am I missing some glaringly obvious step?
I'd recommend to start from creating your project with m2eclipse. See more details in this article.
Basically, you need to make sure the following:
your Eclipse project has a valid pom.xml and all dependencies are available (you should see errors on Maven console, in the Problems or Markers view or when opening pom.xml in m2eclipse's POM editor)
Maven support is enabled for this project (you can use Maven / Enable Dependency Management from popup menu on that project)
project configuration is in sync with pom.xml (you can use Maven / Update Project Configuration from the project popup menu)
you can also use Maven / Update Dependencies to refresh your dependencies (e.g. when you got them in your Local Maven repo from the command line)
Dependencies jars aren't in your project but in your local maven repository.
These jars will be automatically used when you compile you project with maven (or m2eclipse).
If you don't have the needed jar yet, maven will download it for you.

Resources