Is there a way to force intellij to mvn reimport before I build everytime - maven

I am trying to refer to the versions using ranges in my pom
e.g.
[13.1.0-betatest101,13.1.0-betatest110]
When I upload a new version of jar to the nexus hosted within our company maven seems to pick it up and download it to local repository. But intellij is still referring to old version of the jar. The only way to fix is to mvn reimport but is there a way to force this behavior before every build so I am always working with latest version?

Related

how to force mvn redownload snapshot

I got a maven project (myApp) depending on another maven project in snapshot version.
like:
<dependency>
<groupId>org.group.dep</groupId>
<artifactId>arty</artifactId>
<version>12.1.4-SNAPSHOT</version>
</dependency>
But I got a problem with this after the "arty" got an update without changing the version (I know that would be the cleanest solution).
I build the myApp local and got still the old version of the "arty" dependency.
I verified tow option working for me (and a college):
1) Manual cleaning of the local repository: navigating to my .m2/repo/org/group/dep/arty and deleted all folders inside. After rebuilding the myApp local it was working fine - arty was downloaded form the artifactory.company.com again with the updated content.
2) Local building of the arty package so it got updated in the local repository. After rebuilding the myApp local it was working fine.
But I got similar problem on the Jenkins:
I got a Jenkins job just building org.group.myApp without building before org.group.dep.arty. It failed for the missing changes form "arty".
What can I do now to solve my problem there?
I can not rely on first building org.group.dep.arty as I can not be sure for Jenkins to run both jobs on the same host using same local repository (I don't want to change that).
Somehow the myApp-Jobs was failing after I manually cleared on that Jenkins node the org.group.dep.arty in the repository and running than the myApp-job (was somehow not downloading the package).
I finally found the mvn -u but as I tried this I was as well disappointed.
I tried different maven versions on that jenkins and got the same result.
Is there no way to force the update of the snapshot versions?
Is this "another project" is a part of the same multi-module project?
If so you can build your project with --also-make options so that maven will effectively rebuild your module and all of its dependencies
If its an entirely different project, use mvn -U to forcefully download all the snapshot dependencies of your project.
If there is a particular issue with one concrete dependency consider using mvn dependency:get. This get goal of maven-dependency-plugin downloads one specific artifact from the remote repository
Here is a link to the plugin documentation
The simplest solution to redownload -SNAPSHOT is by using the command line option: -U or as long option --update-snapshots
Furthermore your project sounds like the need for a multi module build which prevents such issues. Or you might need to define those Jobs depending on each other (There is an option to build if a SNAPSHOT has been updated in Jenkins).

IntelliJ does not compile my project when I update jar in my local maven repository

I created a maven project with lots of external dependencies. I was able to build it fine. Then I installed new SNAPSHOT of dependency into my local maven repository.
First maven itself was not picking up latest SNAPSHOT. I deleted the old SNAPSHOT. This caused maven to pick up the new SNAPSHOT and I was able to compile against new jar from mvn command line but now IntelliJ is not recognizing any of my imports and I cannot build from IntelliJ (although mvn package works).
How to fix this? If I create a brand new project from same sources in a new folder then IntelliJ works.
In the upper left corner, right-click on your project and re-import Maven. That sometimes works for me. In some cases even that fails, and honestly I've just gone into the M2 repository (~/.m2), and deleted all copies of that particular library. That forces a rebuild as well and seems to work.
I'll be curious to see if someone has a better answer ...

Jenkins - get latest artifact version from remote repository

I'm trying to create a deploy job in Jenkins. Up until now I was building my artifact via the maven install goal and then deploying it on the application server with a shell script. However, I'd now like to skip the install part and just get the artifact from my nexus repository.
I know there is the maven dependency:get which I can use to retrieve the artifact from the repository but is there any way I can make sure I'll get the latest version without passing it as a build parameter?
You have different options:
1) Use the Repository Connector Plugin. With this plugin, you get an additional "Artifact Resolver" build step, where you can download an artifact from a centrally configured (Manage Jenkins) repository to the workspace of your deploy job (with different options like renaming etc).
If you use the version LATEST, you always get the latest version. Likewise, you can use RELEASE for the latest release version or ranges like [1.0,1.1).
There are two caveats however:
In the newest version of the plugin, LATEST is broken (see https://issues.jenkins-ci.org/browse/JENKINS-20263), so you need to use version 0.8.2 for now).
You should manually fingerprint the downloaded artifact, since this is not automatically done right now.
2) Use dependency:get as suggested, but use LATEST or RELEASE as above. However, I do not think this is a really elegant solution. (if you simply use SNAPSHOTs with the same base version, follow khmarbaise's advice and simply add -U to the commandline)
3) Use the Maven Deployment Linker Plugin plugin, which is a rather elegant alternative, since you can copy artifacts from other jobs like Copy-Artifact, but they are still retrieved from your Artifact repository (thus you do not waste diskspace and time). The largest problem with that plugin is that it currently does not support authentification.

maven + elicpse related questions

Forgive me asking following questions. I am totally lost in regards to maven+eclipse. I checked out someone's java project (maven built) from SVN to my local eclipse (kepler). When I click Windows > Preferences, I see Maven.
question 1)
Is this a maven plugin? When developers say maven in eclipse, are they referring to maven plugin? maven and maven plugin are two separate components?
question 2)
when I click on user settings, C:\Users\myName.m2\settings.xml is missing. Exact error message is "User settings file doesn't exist". Does it get created when you install maven plugin at first time?
question 3)
I found three folders may have to do with maven C:\workspace\maven_local_repo_artifactory directory, C:\maven_local_repo and C:\Users\myName.m2\respository but not sure how they get created and what is the relationship among them.
question 4)
Is it ok to remove current maven plugin from eclipse and re-install it then check out the java project from SVN? I think my maven or maven plugin settings are not correct in my local box.
1) Is this a maven plugin? When developers say maven in eclipse, are
they referring to maven plugin? maven and maven plugin are two
separate components?
Yes. This is the maven-plugin. maven-plugin uses the configurations of maven (%M2_HOME%/conf).
If you wanna work with maven, you need to install it on your machine. Then you can run maven commmands. In addition, if you want to invoke maven commands within eclipse (conveniently) - you can install the eclipse-plugin. "maven-plugin" is a plugin for eclipse, that lets you use maven within Eclipse conveniently.
2) when I click on user settings, C:\Users\myName.m2\settings.xml is
missing. Exact error message is "User settings file doesn't exist".
Does it get created when you install maven plugin at first time?
By default, the maven-plugin assumes that your settings.xml (which is the configuration file of maven) is in the path you have mentioned. However, there are cases (like in my case) where the config file is not there, but under %M2_HOME%/conf. you can update it in Eclipse, and the error will disappear.
3) I found three folders may have to do with maven
C:\workspace\maven_local_repo_artifactory directory,
C:\maven_local_repo and C:\Users\myName.m2\respository but not sure
how they get created and what is the relationship among them.
C:\Users\myName.m2\respository is the "local repository". If you learned a bit about how maven works, it holds a local repo on the local machine, and it keeps there all artifacts. It downloads them from the "repository" - if you have one in your company (Nexus, Artifactory, etc) or from Maven Central. However, this path is configurable by Maven's settings. So there might be that someone played with it and changed the path, and these other directories were created. You did not mention what resides inside these paths...
4) Is it ok to remove current maven plugin from eclipse and re-install
it then check out the java project from SVN? I think my maven or maven
plugin settings are not correct in my local box.
Sure it is OK. You may remove the plugin, and the source plus maven itself will not be deleted from your machine.
HTH.

Cannot get the latest version of maven plugin

I am new to maven, I have write my own maven plugin with the pom file
<groupId>com.xxx.api</groupId>
<artifactId>xxx-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
then, in my IDE (intellij) I used clean install to install my maven plugin
and in the main project, my maven plugin works fine.
However, when I modify my maven plugin by adding parameter to my mojo and "mvn clean install" ,
(the groupId,artifactId,version keep the same) it supposed to be the latest version.
However, in the main project, when I try to use the latest maven plugin, I always get the old version
i.e I cannot configure the parameter that I just add to my maven plugin (because it is not exist!)
I have try to delete the maven plugin in my .m2 repo, before I install the latest version of my maven plugin, it still not work.
Any solution that I can get the latest version of my maven plugin by keeping the same
(groupId, artifactId,version)?
thanks,
Zach
Check how the using project is configured. I guess it has its own lib/classes folder. I would suspect that your project has a copy of the older version of the plugin in its lib folder. If so, that is where it is getting the old version. Clean it out from there, i.e. you need to clean the using project.

Resources