I have a Java project, which is still in a very early stage. There are no real releases yet, so I set version as 1.0.0-SNAPSHOT in my pom.xml.
Before each submit, I perform a Sonar analysis directly from Maven using:
mvn sonar:sonar
Sonar has this nice ability to record changes of KPIs over time. However, it only does that for a version. Now, as I don't update my version number, it overrides the last analysis result and replaces it with the current one.
I wonder how to get around that. I don't want to increase version numbers manually each day as this doesn't seem very natural to me.
What other options do I have? Should I use one of the Maven plugins to add a build number to the version string so that I have unique versions? Do you have any other strategy?
If you want increase version number every release, the maven release plugin is recommendation.
But if you want increase the version number everyday even every build, you can use unique version number like timestamp which can get from Maven building phase, after Maven 2.1, it has the built-in ${maven.build.timestamp} you can use, and you can use format
<properties>
<maven.build.timestamp.format>yyyyMMdd-HHmm</maven.build.timestamp.format>
</properties>
for more information you can visit http://maven.apache.org/guides/introduction/introduction-to-the-pom.html
Sonar does preserve the metrics for each snapshot build (based on your configuration). You can then rename the snapshots suitably and use them to get historical information.
You say that you don't to releases yet. As soon as you do, the Maven Release Plugin is a recommendation. It handles the SVN related stuff that comes with a release.
I'm not sure if this helps you with day-to-day work on a SNAPSHOT, though.
Related
This Question is answered successfully by checking Preferences/Settings > Build, Execution, Deployment > Build tools > Maven > Always update snapshots (checkbox) in IntelliJ IDEA.
➠What exactly does that setting do?
The documentation is unhelpful.
Always update snapshots
Select this checkbox, if you want IntelliJ IDEA to update snapshots on sync.
What snapshots?
In sync with what?
Why would I not want to stay in sync?
Why is this not checked by default?
My guess is that since SNAPSHOT dependencies may be continually worked on, that they may have changes while in development. As for your questions:
What snapshots?
Any snapshot dependencies you have in your pom.xml (or equivalent). This would typically (but not necessarily) be anything that you depend on with a version ending in '-SNAPSHOT'.
In sync with what?
With the repository you initially pulled it from. Maven has its own local cache, ".m2" (and actually, IntelliJ has its own Maven cache as well), so it's asking if it should pull it down from the repo even if you have a local copy.
Why would I not want to stay in sync?
If you found a "version" of the snapshot version that works, you may just want to use it until the release build is out. It also increases build times to pull down new versions, and uses up bandwidth (although that should probably not be an issue).
Why is this not checked by default?
Because if an update is suddenly pushed that breaks things, it could be frustrating/unreliable/etc.
However, if you're waiting for a certain feature to be implemented and you need it ASAP, or if you're working on the dependency project and constantly updating it while debugging, then keeping SNAPSHOT dependencies up-to-date may make sense.
I have builds like: 1.0.0-9, 1.0.0-10, 1.0.0-11, 1.0.0-12, etc.
I've configured my dependency like:
[1.0.0,)
Which means that it should use the latest version from the existing ones (ofc which starts with 1.0.0), but instead of using the 1.0.0-12 it used the 1.0.0-9. I think that it's because the 9 is grater then the 1.
Firstly I would like to force the maven to use truly my latest builds.
I can also have 1.0.0-LOCAL build (which is a local build on the developer's PC). I would like to force the maven to use the 1.0.0-LOCAL instead any other builds if it's available.
I don't know exactly how I could do these, maybe somehow with the settings.xml configuration file which is located on the build machine and on the developer's PC as well.
If I would have a any other way to do this please let me know.
Have a good day,
Arnold Robert Turdean
Update:
It turned out that the Maven Version Range - downloads all the available versions not just the latest one was the original problem.
Which maven version do you use? From Maven point of view all of those given versions are releases? The question is if it wouldn't be better to use a SNAPSHOT version instead?
Furthermore to check if the ordering of your artifacts is correct or work like you expect this can be checked by using this:
java -jar apache-maven-3.3.9\lib\maven-artifact-3.3.9.jar 1.0.0-12 1.0.0-9
Display parameters as parsed by Maven (in canonical form) and comparison result:
1. 1.0.0-12 == 1-12
1.0.0-12 > 1.0.0-9
2. 1.0.0-9 == 1-9
which shows correctly that 1.0.0-12 is greater than 1.0.0-9. So the question is also if you are using a repository manager etc. ? Do you do only a mvn install ?
What I don't understand is your statement about `1.0.0-LOCAL' ?
Apart from that I would suggest to prevent using of version ranges cause they make you build non reproducible.
In maven, dependency hierarchy is so important. Make sure that you don't have other dependencies which added before, into other pom's.
(In hierarchy, maven cares latest dependency.). Dependency 1.0.0-LOCAL must be the latest one.
I suggest to you, while the local developing time use generic -SNAPSHOT, use numbers when you start packaging .
I`d like to fetch the number based version number of a maven package programmatically - to be more specific: I need the number of the RELEASE version.
An example:
JUnit is a wellknown maven package. According to http://mvnrepository.com/artifact/junit/junit, the current RELEASE is 4.12.
So given the artifact identifier I'd like to receive the corresponding RELEASE version number (4.12 in this specific case). Since maven is some kind of a repository, I hope that there is an easy way for doing so.
I currently do have two different approaches that might work, however both are not satisfying.
I could create a pseudo java project that requires the dependency by maven. After mvn install it could be possible to get the version number from the jar-Files.
I could do a GET Request to mvnrepository.com/artifact/junit/junit followed by some fancy regex ... definitely not a best practice
Edit:
And here is the magic number three:
http://search.maven.org/solrsearch/select?q=g:%22groupid%22+AND+a:%22artifactid%22&core=gav&rows=1&wt=json
returns a json with the number I've searched.
It is not 100% clear what you want to do but, if you want to be able to use the build version when executing the artifact built by the pom, the following should help.
With the resource plugin you can do token substitution in a resource.This means you can replace any string in a property file or even a java source file with the value of a maven property like ${project.version}.
It is not clear what version you want to have but:
by using a property to specify the version of a dependency like ${junit.version} you can use the dependency version.
by using the builtin project version property you can use the version of the current project.
I work on a big legacy project and I've noticed that in the project root pom we explicitly forced certain maven plugin versions.
I've read about the 'maven way' and it seems to me that this is a violation of this way - forcing versions instead of inheriting them from the superpom. Here's an example of what we have in the project pom:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>...</configuration>
</plugin>
My question is - what are the valid reasons (if any) to force plugin versions like that. I wonder because often times I find code that was written without any clear purpose and I do wonder if this is such a case, and if I should just drop the version from the project root pom.
Afterthought: On this site they say:
When declaring a "normal" version such as 3.8.2 for Junit, internally
this is represented as "allow anything, but prefer 3.8.2." This means
that when a conflict is detected, Maven is allowed to use the conflict
algorithms to choose the best version. If you specify [3.8.2], it
means that only 3.8.2 will be used and nothing else.
So this means, if you force the version to ensure stability, then you should also use [] otherwise maven is free to ignore your forced version.
The best is to define plugins versions only in a corporate pom and of course mainain this corporate pom over the time which means update the plugins versions from time to time.
This means in consequence that in no other project it is needed or better should be prevented to use a different versions of plugins (except there are very good reasons for this bugs in plugins).
Furthermore the excerpt you have given is an example of bad practice cause plugins and/or their configuration should be defined by using pluginManagement instead.
So if a project needs an older version of a maven plugin there should be at least a comment in the pom which describes why it's using not the inherited version. May be with a link to an appropriate JIRA issue...
what are the valid reasons (if any) to force plugin versions like that.
A valid reason is to keep a build repeatable, especially if there are known problems with a later version of the plugin. This ensures that the specific version is used, rather than a later version from an organisational parent pom (or, worse, from the default with no version specified anywhere).
I wonder because often times I find code that was written without any clear purpose and I do wonder if this is such a case
It's very possible, in a large code base, that this is exactly the case. The plugin configuration could have been copied from somewhere else and the version included without a good reason.
and if I should just drop the version from the project root pom.
If there is no reason given, either in a comment or a commit message, and if the same plugin has a version specified in the parent pom and if the build still works perfectly without it, then you should drop that version.
If it doesn't work, you should either fix the build or add a comment explaining exactly why this version is necessary.
As has already been stated, this can be because of corporate reasons - using new versions of plugins can break tests, functionality or even a whole product itself; new versions have to be tested thoroughly, advanced teams even need to discuss them because it can change the product in many ways.
Not forcing versions will create some kind of unstable situation during the next big build - which is always unwanted, developers dont like randomness :).
If your POM only describes a small, private project or maybe even a small community project you may very well let maven do all the version-management but thats pretty much a no-go for professional products which are worth .... say hundreds of thousands or even millions of currency units.
Currently, my Java applications have the same version on every build. I am using Maven, but I am not sure how to set up the workflow to add a version to the application on each build.
I imagine this works with my version control system? I am using git, does this mean I need git tags?
Thanks for any insights, I know it's a big question, but I am not sure where to get started.
You start by setting your version to, for example, 1-SNAPSHOT.
Then you use the maven-release-plugin to release early and often.
In the interim, each snapshot gets a unique timestamp.
If you want to apply a version that isn't the official maven version, see the buildnumber-maven-plugin.
I use this Maven plugin:
https://github.com/ktoso/maven-git-commit-id-plugin
and get it to generate a git.properties file for me. This includes the commit id, comments, etc.
From there, you can do whatever you like. I have a properties page in my webapp that simply iterates over everything in git.properties. Works for me.
You can use maven POM file to define versions. Here is an explanation.
Also you can update versions using this.