Upload artifacts to multiple nexus instances without running build twice using maven - maven

We have some legacy reasons to upload our software artifacts to two instances of nexus (one internal and the other with the cloud solution we are hosted with)
Currently, we accomplish the same running the build twice with different settings files
mvn clean deploy -s=internal_nexus_settings.xml
mvn clean deploy -DskipTests=true -s=external_nexus_settings.xml
Is there a possibility of uploading artifacts to both without running the build twice
Currently, settings file contain
external_nexus_settings.xml
<id>external_cloudprovider</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<nexus.release.url>https://cloudvendor/nexus/content/repositories/releases/</nexus.release.url>
<nexus.snapshot.url>https://cloudvendor/nexus/content/repositories/snapshots/</nexus.snapshot.url>
<nexus.site.url>dav:https://cloudvendor/nexus/content/repositories/sites/${project.groupId}/${project.artifactId}/${project.version}/</nexus.site.url>
<nexus.username>admin</nexus.username>
<nexus.password>mycreds</nexus.password>
</properties>
internal_nexus_settings.xml
<profile>
<id>internal_nexus</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<nexus.release.url>https://internal/nexus/content/repositories/releases/</nexus.release.url>
<nexus.snapshot.url>https://internal/nexus/content/repositories/snapshots/</nexus.snapshot.url>
<nexus.site.url>dav:https://internal/nexus/content/repositories/sites/${project.groupId}/${project.artifactId}/${project.version}/</nexus.site.url>
<nexus.username>admin</nexus.username>
<nexus.password>mycreds</nexus.password>
</properties>
Tried adding multiple profiles (activating the profile via default and command line params) but maven picks the settings of the latest profile only
mvn clean deploy -DskipTests=true -s=external_nexus_settings.xml -P internal,external

Related

Having trouble getting springboot app to run/build specific profile

I am using Maven as my build tool.
For profile management in SpringBoot I am using yml files.
For my SpringBoot app, I have the following application-*.yml files set up:
application.yml
application-local.yml
application-foobar.yml
My corresponding pom.xml profile configuration:
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<activatedProperties>local</activatedProperties>
</properties>
</profile>
<profile>
<id>foobar</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<activatedProperties>foobar</activatedProperties>
</properties>
</profile>
</profiles>
Whenever I attempt to either package or run my app via Maven:
> mvn package -P <any configured profile>
> mvn spring-boot:run -P <any configured profile>
The app runs, however it only runs falling back to the default profile (application.yml).
I get the following log entry every time I attempt to run the application under any of my configured profiles:
: No active profile set, falling back to default profiles: default
I can't seem to find any clear information on the internet regarding this issue.
Any help would be greatly appreciated. Thanks!
Maven build profiles and Spring bean profiles are two completely separate concepts that happen to use the same name but do not interact with each other.
The XML you showed with <profiles></profiles> configures Maven build profiles, but will not have any effect on Spring bean profiles.
Per the Spring Framework documentation on activating a profile and Spring Boot documentation on passing arguments when running an application, you can select the active Spring bean profile when running the app using Maven with a command like this:
> mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=local"

How to activate a profile in a maven submodule using a property?

I am using Maven 3.6.0 and OpenJDK8 on Ubuntu 18.04 (also tested with Alpine Linux).
I have a pom.xml in the root of my project that includes my submodules :
...
<modules>
<module>mysubmodule</module>
</modules>
...
In the mysubmodule folder, the pom.xml has a profile that I want to activate based on a property passed to the mvn executable:
...
<profile>
<id>my-profile</id>
<activation>
<property>
<name>activateMyProfile</name>
</property>
</activation>
...
</profile>
...
I then execute mvn to start the build, but the profile is never activated:
If I run mvn -DactivateMyProfile release:prepare from the root of my project, the profile is never activated and never runs
If I run mvn release:prepare from the root of my project, the profile is never run.
I also tried the inverse:
...
<profile>
<id>my-profile</id>
<activation>
<property>
<name>!doNotActivateMyProfile</name>
</property>
</activation>
...
</profile>
...
If I run mvn -DdoNotActivateMyProfile release:prepare from the root of my project, the profile is still executed
If I run mvn release:prepare from the root of my project, the profile is also executed
It looks like mvn is not able to see the properties being passed through the command line. What is the correct way to activate a profile in a submodule using a property?
As I am using the maven release plugin, parameters must be passed using the -Darguments argument.
For example, instead of using mvn -DactivateMyProfile release:prepare, the correct invocation is: mvn -Darguments=-DactivateMyProfile release:prepare
If there are multiple arguments, use mvn -Darguments="-DactivateMyProfile -DsomeOtherArg -DanotherArg=something" release:prepare

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

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?

How can I exclude a project from a mvn clean install? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I exclude certain modules from a maven build using the commandline
I am running a maven clean install in a pom file which includes several modules (and sub-modules). I was wondering if it is possible to run a maven build but specifying on command line to skip a module from the build ( at the moment I exclude them manually from the build, but Id prefer to do it via command line).
I know that with -pl you can selectively choose projects, but what I would like is to selectively exclude (in a blacklist fashion) some.
You could have a separate <modules> section in a profile, and activate the profile you need in the command line.
Example:
<profiles>
<profile>
<id>profile-1</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>...</modules> <!-- module set 1 -->
</profile>
<profile>
<id>profile-2</id>
<modules>...</modules> <!-- module set 2 -->
</profile>
</profiles>
Now, dependent on your current need, execute
mvn install
mvn install -P profile-2
Note that you'd have to think it over carefully, there must be no cross-profile dependencies on the excluded module.

Resources