Problems with Maven 2 - maven-2

I recently started to use Maven2 in one of my Java web application projects. Now I had many issues with it, some times project fails to build for no apparent reason and then it suddenly starts to work when nothing was done at all to project. Or some times our project members must delete project from their harddrive and download project again from SVN. There seems to be many very odd bugs in Maven in eclipse, but there some issues I would like know if it is possible to solve this issues.
1) I have understood that Maven2 should be able to get dependencies for added jars, but when I add a new dependency in Eclipse, it fails when I build it, it says dependecies are missing. How can I make maven to download those missing dependecies automatically?
2) I have Tuckey UrlRewrite Filter in use, but public repositories have only old version of this dependecy, so when I use this old version (3.1 when I need 3.2). How can I include this to project? We have many programmers in this project, so setting up local repository would mean that all our programmers would have to install that local repository.

Now I had many issues with it, some times project fails to build for no apparent reason and then it suddenly starts to work when nothing was done at all to project. (...)
Ok and what is the point of this free rant? I use Maven and my builds are 100% reproducible, there are well known practices to follow to achieve this. Maybe you're just not following them. Anyway if you're not happy with it, what can I say, don't use it.
I have understood that Maven2 should be able to get dependencies for added jars, but when I add a new dependency in Eclipse, it fails when I build it, it says dependencies are missing. How can I make maven to download those missing dependencies automatically?
I think you misunderstood, Eclipse won't guess what Maven coordinates to add if you don't provide the required informations for them. Dependencies must be declared in the POM, either by editing the POM manually or by using m2eclipse wizards.
And if this is what you did (and if I misunderstood the question) then please provide the <dependency> declaration and the exact error trace.
I have Tuckey UrlRewrite Filter in use, but public repositories have only old version of this dependecy, so when I use this old version (3.1 when I need 3.2). How can I include this to project? We have many programmers in this project, so setting up local repository would mean that all our programmers would have to install that local repository.
This question has already been asked several times, see for example Maven, how to add additional libs not available in repo where I suggest two possible solutions (use a corporate repository like Nexus or a "file-based" repository, the former suggestion being the preferred one for a long term solution).

Related

Intellij-IDEA selects the wrong maven dependency

I have used IDEA for some time and recently I updated to version 2018.1.1 from 2016.3. So it seems as if IDEA have gotten smarter, but not to my pleasure I am afraid.
So basically I have started to run into a lot of troubles lately. I have for example noted that when there are possible issues with the unit tests (eg. TestNG tests) there are currently two things that happens
1) Maven cannot find the jar (which btw builds perfectly from command line using mvn clean install)
2) Maven reverts to the latest working jar.
If I then try to reimport, I will go directly to (2). I also get weird error where commercial libraries such as guava generates errors.
I have never seen this until I upgraded IDEA Intellij. Anyone that have an suggestion on that might cause this and how to solve the problem.
The behavior I want is that the version marked out in the pom is imported with all the possible flaws, like it use to do. Then I can find the errors and fix them. I should also point out that, mvn clean install -Dmaven.test.skip works perfectly from command line, so not dependency issues with production code.
maven 3.3.9
jdk 1.8_172-b11
EDIT
I must mention that I use the "import maven projects automatically" feature here, which might explain (2), but I can still not see how this issue appears in the first place. Apart from changing IDEA version previously I have not had any problems in finding SNAPSHOTs.
EDIT 2
So the problem was indeed a pom error. I am not sure how eclipse managed to find the "correct" file (which was btw, not the correct file). After updating the pom it all worked out. I will not mention this here, since the behavior was a bit obscure (and I am not sure I understand it really). It have something to do with versions of the libraries, dependencyManagment, submodules and dependencies from submodules.
BR
Patrik

How to prevent maven to resolve dependencies in local repository

Is there a way to tell maven (when doing mvn package, mvn site or ...) not to resolve the dependencies from the local repository?
Background of this question: Sometimes I get into problems, when previously cached dependencies (e.g. SomeProject-0.7-ALPHA) are no longer available in the remote repository. In my local build everything still works fine as the dependency has been cached before. As soon as I share my pom with others, they may get into trouble, as they dont have a cached version of that dependency and the dependency can no longer be resolved from the remote repository.
Any help will be appreciated. Thanks in advance!
Is there a way to tell maven (when doing mvn package, mvn site or ...) not to resolve the dependencies from the local repository?
No, that's how the whole dependency resolution works (via the local repository).
Sometimes I get into problems, when previously cached dependencies (e.g. SomeProject-0.7-ALPHA) are no longer available in the remote repository.
Sorry for stating the obvious but removing dependencies from a remote repository is an horrible practice and leads to... well the kind of troubles you're facing. If possible, avoid doing that.
As soon as I share my pom with others, they may get into trouble, as they dont have a cached version of that dependency and the dependency can no longer be resolved from the remote repository.
One way to check that things would work for others would be to purge the dependencies of the project you're going to share from your local repository and to re-resolve them. Of course, doing this manually would be really painful but the good news is that the Maven Dependency Plugin has a purge-local-repository goal for that.
mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:purge-local-repository \
-DreResolve=true \
-DactTransitively=true \
-Dverbose=true
If this fails, then you know that something is missing and can't be resolved anymore which is basically what you want to know. But you've also lost it so make a backup of your repository if this matters to you.
The basic test of a maven build is to delete the local repository .m2/repository and try to build (mvn package) and see if it is working. If not you have other problems here. As mentioned the problem seemed to be the process you are working with. If a dependency is not available anymore from a remote repository there is an other problem. Are you using a Repository Manager for example Nexus, Archiva, Artifactory ?

hunting maven dependencies

I want to start using maven in code I distribute but I can't find an efficient way to work with dependencies. Every new dependency takes me far too long to add.
As a simple example, I need to add Tomcat for compilation. Do I really have to manually trawl the repo in my browser to find the group-id, artifact-id and version number?
In every case it seems easier to find the non-maven downloads. I hope I've missed something obvious.
Do I really have to manually trawl the repo in my browser to find the group-id, artifact-id and version number?
No. Use a repository search engine. Some are mentioned here, more here. Some IDE also offer facilities to search and add dependencies (e.g. Eclipse with m2eclipse).
First of all, using some IDE really helps. Eclipse has m2eclipse plugin and if you define locations of remote maven repositories in your pom definition, you can add dependencies in its pom editor and it will automatically search the proper artifacts for you.
What is really helpful is using repository manager like Nexus / Artifactory, where you gather the most important maven repositories and then you don't need to declare particular repository locations in your pom definition for various artifacts, but everything will be proxied via Nexus.

Maven: Variables are not replaced in installed and deployed POM

We have been trying to migrate our multi-module projects to maven. I have been struggling with the maven install plugin bug "http://jira.codehaus.org/browse/MNG-2971". I have written a Java program which can find and replace the expressions using my settings.xml and POM in my local repository view.
As a result all of my parent POM's are having a additional phase in the build process. What i'm doing is that i have attached a goal which will run my Java program during "validate" phase.
I think this is a not a good idea instead i would have asked individuals to run the program on their local repository on their own.
What i'm asking here is that the best way to work around the "install" plugin issue (MNG-2971).
I searched through the net but i could not locate such work around.
Well, I didn't try it because I'm not using properties from the settings.xml but the issue has a work around in the comments.

How to stop Maven renaming installed jars

I am installing to Maven repository our internal files using mvn install:install command. All Jars installed in such way have version name added automatically as a suffix. Since we have many batch scripts with the Jars names it is very inconvenient for us. How this auto-renaming can be switched off?
You can't. Dependency resolution in Maven works because Maven has conventions and the naming of artifacts is one of them. So you can't turn off the way maven install artifacts (and you actually don't want to).
The common way to handle scripts (bat/sh) is to put them in your source tree (e.g. in src/main/bin) and to create a distribution of your project with the assembly plugin. When building your assembly, you can rename artifacts, filter distribution files, etc. That would be the right place to do such kind of things.
it's tempting to want to remove the version number, but I'd recommend keeping it on if at all possible.
Instead of removing the version numbers, maybe you could use Maven's resource filtering capability to manage your batch scripts? Maven can manage your batch scripts by replacing placeholders such as ${project.version} inside the bat script with the current version number from the pom.xml. That way you can ensure that the batch script is running the correct (expected) version of the code.
You could probably force maven to deploy a jar without a version, but if you do so, you lose some of the main benefits of having Maven manage your build in the first place, imo. For example, just yesterday I was asked to maintain a java project not managed by maven with a bunch of non-versioned jar files inside a lib directory. There's no way to know where any of the jars came from.
It might be a little bit of a headache up front, but if you're using maven, you might as well jump in 100% (again, from my experience).