Maven deploy snapshot or release based off of profile - maven

Is there a way to append the -SNAPSHOT suffix to the version of a project based on profile? I'd like the prod profile to be the only one able to deploy RELEASES to nexus.

We can use user-defined properties to accomplish this:
<profile>
<id>local</id>
<properties>
<env>local</env>
<snapshot>-SNAPSHOT</snapshot>
</properties>
<profile>
<id>prod</id>
<properties>
<env>prod</env>
<snapshot></snapshot>
</properties>
</profile>
Then update the version to refer to our new property:
<version>0.1.0${snapshot}</version>

by configuring different profile do SNAPSHOT build or a RELEASE build is not a good idea, here you are going to make both of them to have same source, (i.e. the source in development would be in release and vice versa)
you should keep only one version at a time, RELEASE it once and increment to the next SNAPSHOT by using mvn release plugin

They better way to acomplish this is using the maven-release-plugin this will versioning your project and modules and prepare it to the release version saving you from the tedious work.

Related

Execute analysis in SonarQube from Maven but not storing the results in database

I am developing a new project in Eclipse and I have to analyze it with SonarQube. I am doing a lot of changes in the code and analysing it, but I wouldn't like that SonarQube store all the results in it's database (they are temporary versions). Is there any option for it?
I have configured my pom file like sonarQube official config:
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>
http://myserver:9000
</sonar.host.url>
</properties>
</profile>
It connects with sonnar and execute the analysis ok, but I am storing lots of useless executions in SonarQube.
Regards
You might get some value from the SonarLint Eclipse plugin: https://www.sonarlint.org/eclipse .
With this, you might be able to iterate on local changes before submitting a full sonarqube scan.

maven - remove -SNAPSHOT from properties tag in pom.xml

The parent pom.xml of my application contains the following snippet:
...
<version>2.0-SNAPSHOT</version>
...
<properties>
<property.version>1.0-SNAPSHOT</property.version>
</properties>
Essentially, I would like to update both the -SNAPSHOT versions to release versions. i.e., I want my resultant pom.xml to be like:
...
<version>2.0</version>
...
<properties>
<property.version>1.0</property.version>
</properties>
[Note: v1.0 of <property.version> is not yet deployed to the artifactory and also might not be the latest version available
There might exist more properties with SNAPSHOT versions that I do not want to update to release versions]
I figured out a way to update the <version> using maven versions:set plugin. However, I could not find any solution for updating the version (by removing -SNAPSHOT) inside the <properties> tag.
I've looked at documentations for versions:update-properties, versions:update-property.
I am trying to achieve this by using the mvn versions plugin, otherwise I would have to write a script (eg. shell) which would parse and do the needful.

Questions about pom.xml in Jenkins to run sonarQube through maven project

I'm trying to run sonarQube through Jenkins but I have some difficulties right now. When I build a new job, I use Maven Project and inside the configuration I have to give à pom.xml path but what does it correspond to ?
Thank you in advance
You should find in any jenkins job a post action for sonarqube analyse.
The pom.xml you mention is the pom.xml for your maven project, because sometimes you can put your parent pom.xml in a subdirectory and this is the way for helping jenkins to find it.
Instead of adding Sonar Task to each project why not just configure Sonar at Global Level configuring the settings.xml for your maven configuration, just go to $HOME/someUser/.m2/settings.xml (if you don't have it created yet) with this content:
<settings>
<pluginGroups>
<pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- Optional URL to server. Default value is http://localhost:9000 -->
<sonar.host.url>
http://myserver:9000
</sonar.host.url>
</properties>
</profile>
</profiles>
</settings>
After you you have done that you will be able to run sonar in all the projects this way:
mvn clean verify sonar:sonar
 
# In some situation you may want to run sonar:sonar goal as a dedicated step. Be sure to use install as first step for multi-module projects
mvn clean install
mvn sonar:sonar
 
# Specify the version of sonar-maven-plugin instead of using the latest. See also 'How to Fix Version of Maven Plugin' below.
mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar
You may find more information in sonar official documentation:
https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Maven

How to compile all modules but install/deploy only selected modules?

I've a typical multi-module maven project.
There's a Jenkins job which builds and deploys all snapshots to the internal repository.
There's another Jenkins build which checks out code, updates all pom versions, and builds & deploys versioned artifacts.
I would like to optimize the latter by deploying only the needed artifacts: that's 2 or 3 out of 100+ modules.
The build should still compile and test all modules but install/deploy only selected module artifacts to internal repo.
Question: Is there a way to do it?
In this case you could define in your aggregator/parent project (from which the main build should start) to skip the install and deploy executions via a property in order to disable them through all the modules by default. Then, in the few modules where this action should still be performed, you could override the specific property to enable them back again.
Since the whole action is targeting a CI job, I would also suggest to wrap this behavior in a maven profile as following:
In your aggregator/parent project you could define:
<profiles>
<profile>
<id>ci-job</id>
<properties>
<disable.install.deploy>true</disable.install.deploy>
<maven.install.skip>${disable.install.deploy}</maven.install.skip>
<maven.deploy.skip>${disable.install.deploy}</maven.deploy.skip>
</properties>
</profile>
</profiles>
The snippet above is defining withinb the ci-job profile a new property, disable.install.deploy, set to true by default. Its value is then passed to the maven.install.skip propert of the maven-install-plugin:
Set this to true to bypass artifact installation. Use this for artifacts that does not need to be installed in the local repository.
And to the maven.deploy.skip property of the maven-deploy-plugin:
Set this to 'true' to bypass artifact deploy
As such, running the following:
mvn clean install -Pci-job
Would effectively skip install and deploy goals executions across the build (across all modules).
That's half of the job however. In the few modules where you still want this action you could then define the following:
<profiles>
<profile>
<id>ci-job</id>
<properties>
<disable.install.deploy>false</disable.install.deploy>
</properties>
</profile>
</profiles>
That is. Keeping the same profile name it will also be activated via the same global build invocation, setting however the key property to false and as such enabling again install and deploy for the modules where this profile would be added.

always download sources (and javadocs) from maven ant task

I am trying to get this ant-based project's init target to download all the sources and javadocs.
I added the following to my ~/.m2/settings.xml (as per Maven – Always download sources and javadocs) but it doesn't force source downloads when used from ant:
<profiles>
<profile>
<id>downloadSources</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<downloadSources>true</downloadSources>
</properties>
</profile>
</profiles>
The only way I could get the sources to download was by hacking build.xml so that all <artifact:dependencies> elements include sourcesFilesetId="sources.dependency.fileset", but this is a pretty distasteful commit that is unlikely to be accepted by the maintainers. A better solution would exist with a property file definition, preferably in the user's settings (not something that mutates the project definition)
Is there a simpler way to ensure that all the sources (and potentially javadocs) are globally downloaded in maven ant tasks?

Resources