Maven Repository on github duplicates whole folder structure with additional folder - maven

I am using maven 3 with IDEA IntelliJ and the publishing process does work as mostly provided here Hosting a Maven repository on github but the folder structure on github does not match the local folder structure, which means if i pull changes i end up with a whole duplicated structure because locally it does add another folder /locallogback/ but only the first structure is pushed to github on deploy
.../at/midneid/....
.../locallogback/at/midneid/....
Instead it should be just like on github
.../at/midneid/....
And the pom.xml looks like
If I do remove either of the ${project.artifactId} entries it does not add the additional local folder locallogback BUT instead the publishing to github does not work anymore as i need to interrupt it, because instead of 12 files (correctly) it starts to generate 525 BLOBS where i have no idea why.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>at.midneid.logging</groupId>
<artifactId>locallogback</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Local Logback</name>
<url>http://sr.midneid.at</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<github.global.server>github</github.global.server>
</properties>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
</dependency>
</dependencies>
<distributionManagement>
<repository>
<id>mvn.public.release</id>
<url>file://E:/Eigene Dokumente/Repositories/mvn/mvn.public/${project.artifactId}</url>
</repository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>com.github.github</groupId>
<artifactId>site-maven-plugin</artifactId>
<version>0.9</version>
<configuration>
<message>Maven artifacts for ${project.version}</message> <!-- git commit message -->
<noJekyll>true</noJekyll> <!-- disable webpage processing -->
<outputDirectory>E:/Eigene Dokumente/Repositories/mvn/mvn.public/${project.artifactId}</outputDirectory> <!-- matches distribution management repository url above -->
<branch>refs/heads/master</branch> <!-- remote branch name -->
<includes><include>**/*</include></includes>
<repositoryName>mvn</repositoryName> <!-- github repo name -->
<repositoryOwner>NoxMortem</repositoryOwner> <!-- github username -->
<merge>true</merge>
</configuration>
<executions>
<!-- run site-maven-plugin's 'site' target as part of the build's normal 'deploy' phase -->
<execution>
<goals>
<goal>site</goal>
</goals>
<phase>deploy</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>

I am very sorry I posted the question but I just found a solution, altough it does look very odd, so any improvements are very much appreciated, which is why i won't mark this answer directly.
The reason the original tutorial uses the target folder seems to be because it is temporary and therefore it makes sense to deploy the artifacts there such that it will be deleted on maven clean.
The essential settings are therefore:
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.1</version>
<configuration>
<altDeploymentRepository>internal.repo::default::file://${project.build.directory}/mvn-repo</altDeploymentRepository>
</configuration>
</plugin>
<plugin>
<groupId>com.github.github</groupId>
<artifactId>site-maven-plugin</artifactId>
<version>0.9</version>
<configuration>
<message>Maven artifacts for ${project.version}</message><!-- git commit message -->
<noJekyll>true</noJekyll><!-- disable webpage processing -->
<outputDirectory>${project.build.directory}/mvn-repo</outputDirectory> <!-- matches distribution management repository url above -->
<branch>refs/heads/master</branch> <!-- remote branch name --> <includes><include>**/*</include></includes>
<repositoryName>mvn</repositoryName> <!-- github repo name -->
<repositoryOwner>NoxMortem</repositoryOwner> <!-- github username -->
<merge>true</merge>
</configuration>
<executions>
<!-- run site-maven-plugin's 'site' target as part of the build's normal 'deploy' phase -->
<execution>
<goals>
<goal>site</goal>
</goals>
<phase>deploy</phase>
</execution>
</executions>
</plugin>
It does solve the problem which is why i post the answer, although it is not perfect as I do not really understand why I do need the distributionManagement entry. It does not seem to change anything in that folder now, but if i remove it, the build does not work anymore.
<distributionManagement>
<repository>
<id>mvn.public.release</id>
<url>file://E:/Eigene Dokumente/Repositories/mvn/mvn.public/${project.artifactId}</url>
</repository>
</distributionManagement>

Related

Parent POM is not flattened when deployed to Nexus

I have a multi-module Maven project where the project version is set via the revision variable.
<groupId>pricing</groupId>
<artifactId>pricing-backend-pom</artifactId>
<version>${revision}</version>
<packaging>pom</packaging>
<properties>
<revision>3.0.7</revision>
</properties>
<modules>
<module>pricing-backend-war</module>
<module>pricing-backend-model</module>
<module>pricing-backend-client</module>
</modules>
<build>
<plugins>
<!-- flatten before deploy. removes $revision -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>1.2.7</version>
<configuration>
</configuration>
<executions>
<!-- enable flattening -->
<execution>
<id>flatten</id>
<phase>process-resources</phase>
<goals>
<goal>flatten</goal>
</goals>
</execution>
<!-- ensure proper cleanup -->
<execution>
<id>flatten.clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
During the Gitlab build, the project is deployed to a Nexus repository. Each module and the parent appear in Nexus but only the modules appear to be flattened. The module POMs each contain <version>3.0.7</version> but the parent POM still contains <version>${revision}</version>.
I find it difficult to understand why the parent is deployed differently to the modules. I have checked the build logs but cannot see any indication that the parent is handled in a different way.
The parent POM taken from Nexus:
<groupId>pricing</groupId>
<artifactId>pricing-backend-pom</artifactId>
<version>${revision}</version>
<packaging>pom</packaging>
<properties>
<revision>3.0.7</revision>
...
A module POM:
<groupId>pricing</groupId>
<artifactId>pricing-backend-client</artifactId>
<version>3.0.7</version>
<dependencies>
...
The build applies the required version:
$ echo New version= ${MAVEN_VERSION}
New version= -Drevision=3.0.7-SNAPSHOT
$ mvn $MAVEN_CLI_OPTS ${MAVEN_VERSION} deploy -DskipTests
The pom file to be installed can be explicitly set:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
<configuration>
<pomFile>.flattened-pom.xml</pomFile>
</configuration>
</plugin>
Above, flatten-maven-plugin has been previously invoked to produce .flattened-pom.xml
If you do a test by adding -Drevision=<someVersion> to the command line, does that produce correct results in Nexus?
I suspect it will.
Properties are interpolated very early in the process. When the command first runs, ${revision} is undefined, so Maven leaves it as-is. The flatten then calculates ${revision}, but that only applies from the time the plugin runs and later.
You can try researching "late binding" properties (they start with '#' instead of '$') but I'm not sure if those work in top level fields like the GAV coords.

Maven: How to execute a dependency in a forked JVM?

Using maven-exec-plugin and a java goal I execute a jar program that validates some files in my project. When the validation fails, it calls System.exit to return a non zero return code.
The problem is that it executes in the same JVM as Maven, so when it calls exit, the processing stops since it does not fork.
I configured it to execute with maven-exec-plugin and a java goal (like in here ). The execute jar is in my Nexus repository, so I want to download it as a dependency in my pom.xml.
A very nice feature of configuring the maven-exec-plugin dependency is that it downloads the jar and all its dependencies, so it isn't necessary to use maven assembly plugin to include all jars in the executable.
How do I configure my pom.xml to execute a jar dependency and correctly stop when it fails?
I solved my problem. Basically, instead of using the java goal, I must use the exec goal, and run the java executable. The code below sets the classpath and the class with the main method.
This solution using the pom.xml and a Nexus repository has a lot of advantages over just handling a jar file for my users:
No need to install anything in the machine that will run it, be it a developer machine or a continuous integration one.
The validation tool developer can release new versions and it will be automatically updated.
The developer can turn it off with a simple parameter.
Also solves the original problem: the validation tool will execute in a separate process, so the maven process won't abort when it calls System.exit.
Here is a commented pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>yourId</artifactId>
<version>1.0</version>
<properties>
<!--
Skip the validation executing maven setting the parameter below
mvn integration-test -Dvalidation.skip
-->
<validation.skip>false</validation.skip>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>MyValidator</id>
<phase>integration-test</phase> <!-- you can associate to any maven phase -->
<goals>
<goal>exec</goal> <!-- forces execution in another process -->
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable> <!-- java must be in your PATH -->
<includeProjectDependencies>false</includeProjectDependencies>
<includePluginDependencies>false</includePluginDependencies>
<skip>${validation.skip}</skip>
<arguments>
<argument>-classpath</argument>
<classpath/> <!-- will include your class path -->
<mainClass>com.company.yourpackage.AppMain</mainClass> <!-- the class that has your main file -->
<argument>argument.xml</argument> <!-- any argument for your executable -->
</arguments>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<!-- Specify your executable jar here -->
<groupId>com.company.validator</groupId>
<artifactId>validatorId</artifactId>
<version>RELEASE</version> <!-- you can specify a fixed version here -->
<type>jar</type>
</dependency>
</dependencies>
</project>
You can run more than one executable passing its id: mvn exec:exec#MyValidator
I have stumbled upon the same issue - System.exit halts the maven with exec:java.
I have experimented to use the exec:exec goal, and made it work with the following configuration:
(using exec-maven-plugin 3.1.0)
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-observability-docs</id>
<phase>prepare-package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<arguments>
<argument>-jar</argument>
<argument>${settings.localRepository}/io/micrometer/micrometer-docs-generator/${micrometer-docs-generator.version}/micrometer-docs-generator-${micrometer-docs-generator.version}.jar</argument>
<argument>${micrometer-docs-generator.inputPath}</argument>
<argument>${micrometer-docs-generator.inclusionPattern}</argument>
<argument>${micrometer-docs-generator.outputPath}</argument>
</arguments>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-docs-generator</artifactId>
<version>${micrometer-docs-generator.version}</version>
<type>jar</type>
</dependency>
</dependencies>
</plugin>

OSGi Code coverage - Jacoco + Easymock

I'm currently working on a Java OSGi project(based on Apache felix runtime) with a project setup like the one below:
pkg | parent maven project
persistence | real plugin
persistence.tests | test plugin (indeed a Fragment project with fragment host the persistence plugin above )
... others like the ones above
Basically I use maven + tycho to manage the lifecycle of the project. the whole stuff flows through a continuos integration pipeline which involves jenkins for building, testing, deploying and forwarding code analysis to a Sonarqube server. Just like mentioned above, tests are implemented through Fragment projects pointing OSGi bundles to be tested. In these tests I make use of EasyMock library to generate mocked OSGi bundles.
In order to make Sonarqube aware of tests coverage I had to add Jacoco (maven plugin) into my sets of tools. After a few adjustments to the configuration of my parent pom.xml file I ended up with something that is working partially: jacoco code coverage is only working for classes included in test plugins (the fragment projects). As you may guess - though better than nothing - this result is far from being useful. I need to evalute test coverage on real OSGi bundles. After some googling I understood that the problem could be linked to the usage of EasyMock library, since this alterate original classes during execution causing a mismatch between test classes and real classes. According to my understanding, to solve I need to disable jacoco on-the-fly instrumentation and use offline instrumentation instead.
Nevertheless I'm not able to understand :
what does this really means
how to do it
Can someone kindly revert on this ?
This is the maven command i'm running to generate jacoco report
mvn -f com.mycompany.osgi.myproject.pkg/pom.xml clean test
Below my current parent pom.xml
<?xml version="1.0" encoding="UTF-8"?><project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.osgi</groupId>
<artifactId>com.mycompany.osgi.myproject.pkg</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>
<properties>
<tycho.version>1.0.0</tycho.version>
<surefire.version>2.16</surefire.version>
<main.basedir>${project.basedir}</main.basedir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jacoco.version>0.7.9</jacoco.version>
<!-- Sonar-JaCoCo properties -->
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.junit.reportPaths>${project.basedir}/target/surefire-reports</sonar.junit.reportPaths>
<sonar.jacoco.reportPaths>${project.basedir}/target/jacoco.exec</sonar.jacoco.reportPaths>
</properties>
<modules>
<module>../com.mycompany.osgi.myproject.core.persistence</module>
<module>../com.mycompany.osgi.myproject.core.persistence.tests</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<version>${tycho.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tycho.version}</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${surefire.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>test</id>
<phase>test</phase>
<configuration>
<testClassesDirectory>${project.build.outputDirectory}</testClassesDirectory>
</configuration>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<repositories>
...
</repositories>
<distributionManagement>
...
</distributionManagement>
As suggested by #Godin, my problems were solved using the following jacoco plugin configurations
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<dataFile>../com.mycompany.myproject.pkg/target/jacoco.exec</dataFile>
<destFile>../com.mycompany.myproject.pkg/target/jacoco.exec</destFile>
<outputDirectory>../com.mycompany.myproject.pkg/target/site/jacoco</outputDirectory>
</configuration>
</plugin>
and this project configuration to instruct sonarqube to read expected resources
<properties>
...
<!-- Sonar-JaCoCo properties -->
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.junit.reportPaths>com.mycompany.myproject.pkg/target/surefire-reports</sonar.junit.reportPaths>
<sonar.jacoco.reportPaths>com.mycompany.myproject.pkg/target/jacoco.exec</sonar.jacoco.reportPaths>
</properties>

Aggregate findbugs report in Maven 3.0.5

I am using multi-module Maven Project ( more than 10 modules ). I am trying to create a findbugs report of all module in single html page. Is there any way?
For creating individual report for each module, i am using the below
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<!--
Enables analysis which takes more memory but finds more bugs.
If you run out of memory, changes the value of the effort element
to 'Low'.
-->
<effort>Max</effort>
<!-- Build doesn't fail if problems are found -->
<failOnError>false</failOnError>
<!-- Reports all bugs (other values are medium and max) -->
<threshold>Low</threshold>
<!-- Produces XML report -->
<xmlOutput>false</xmlOutput>
<skip>${skipFindbugs}</skip>
<!-- Configures the directory in which the XML report is created -->
<findbugsXmlOutputDirectory>${project.build.directory}/findbugs</findbugsXmlOutputDirectory>
</configuration>
<executions>
<!--
Ensures that FindBugs inspects source code when project is compiled.
-->
<execution>
<phase>compile</phase>
<goals>
<goal>findbugs</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<transformationSets>
<transformationSet>
<!-- Configures the source directory of XML files. -->
<dir>${project.build.directory}/findbugs</dir>
<!-- Configures the directory in which the FindBugs report is written.-->
<outputDir>${project.build.directory}/findbugs</outputDir>
<!-- Selects the used stylesheet. -->
<!-- <stylesheet>fancy-hist.xsl</stylesheet> -->
<stylesheet>${project.parent.basedir}/default.xsl</stylesheet>
<!--<stylesheet>plain.xsl</stylesheet>-->
<!--<stylesheet>fancy.xsl</stylesheet>-->
<!--<stylesheet>summary.xsl</stylesheet>-->
<fileMappers>
<!-- Configures the file extension of the output files. -->
<fileMapper implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper">
<targetExtension>.html</targetExtension>
</fileMapper>
</fileMappers>
</transformationSet>
</transformationSets>
</configuration>
<executions>
<!-- Ensures that the XSLT transformation is run when the project is compiled. -->
<execution>
<phase>compile</phase>
<goals>
<goal>transform</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>findbugs</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
</plugin>
According to official documentation of the plugin (question n. 1), it is not possible.
However, here is the approach I used to achieve it:
Add an additional module to your existing multimodule project. This additional module will only be used for reporting
Configure the Buildhelper Maven Plugin to dynamically add the source code of the other modules to the reporting module. Note: you can do the same for resources, if required.
Configure the Findbugs plugin only on the reporting module
Add the other modules as dependencies of the reporting module, in order to have the Maven reactor build to build it only at the end.
If required: you don't want the reporting module to be part of your default build, create a profile in the aggregator/parent module which redefines the modules element and add the reporting module to it. As such, only when the profile will be activated (i.e. via command line, on demand) the reporting module will be added and the aggregated report will be created.
As an example, in the aggregator/parent module you can define as following:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>findbugs-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>findbugs-module1</module>
<module>findbugs-module2</module>
</modules>
<profiles>
<profile>
<id>findbugs-reporting</id>
<modules>
<module>findbugs-module1</module>
<module>findbugs-module2</module>
<module>findbugs-reporting</module>
</modules>
</profile>
</profiles>
</project>
Note: the findbugs-reporting module is only added in the findbugs-reporting profile. By default, the build will ignore it.
In the findbugs-reporting module, configure the POM using the configuration you posted (findbugs and XML maven plugin) and also add as following:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>..\findbugs-module1\src\main\java</source>
<source>..\findbugs-module2\src\main\java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Note the added sources from other modules (change it according to your project).
Furthermore, we also need to add dependencies to the reporting module. It has to depend on other modules in order to be built at the end (and as such make sure to take the latest changes/sources from other modules). As an example:
<dependencies>
<dependency>
<groupId>com.sample</groupId>
<artifactId>findbugs-module1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sample</groupId>
<artifactId>findbugs-module2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
Finally, you can invoke the reporting build as following from the aggregator/parent dir:
mvn clean install -Pfindbugs-reporting
As such, you will build the whole project and additionally activate the reporting module, which will dynamically include sources from other modules (as configured) and generate an aggregated report.
Depending on your needs, you can avoid the profile step (if you want it as part of your default build) or activate the profile by default (so that you can skip the reporting build deactivate it via -P!findbugs-reporting) or use the skipFindbugs property you already configured (and without the profile, in such a case).

How could I deploy multiple maven artifacts to multiple nexus repositories including snapshot repositories without having to specify a profile?

We have a gateway-client project that is part of a multi-module maven project. The gateway-client pom.xml is configured to create two main artifacts: gateway-client.jar and gateway-services-client.jar and deploy them to two separate Nexus repositories: the Releases repo and the 3rd Party repo respectively. This is done through a profile that is active by default:
<profile>
<!-- ====================================================================== -->
<!-- default Profile -->
<!-- This is the default profile which will run by default. This profile -->
<!-- produces two client artifacts: gateway-client and gateway-services-client -->
<!-- for the releases and thirdparty repositories respectively. -->
<!-- ====================================================================== -->
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<!-- ====================================================================== -->
<!-- default Profile Build plugins -->
<!-- ====================================================================== -->
<build>
<plugins>
<!-- ====================================================================== -->
<!-- default Profile Maven deploy plugin -->
<!-- ====================================================================== -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>deploy-thirdparty-jar</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<url>${nexus.url}/content/repositories/thirdparty</url>
<repositoryId>thirdparty</repositoryId>
<file>${project.build.directory}/${project.build.finalName}.${project.packaging}</file>
<groupId>${project.groupId}</groupId>
<artifactId>gateway-services-client</artifactId>
<version>${project.version}</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
</execution>
<execution>
<id>deploy-release-jar</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<url>${nexus.url}/content/repositories/releases</url>
<repositoryId>releases</repositoryId>
<file>${project.build.directory}/${project.build.finalName}.${project.packaging}</file>
<groupId>${project.groupId}</groupId>
<artifactId>gateway-client</artifactId>
<version>${project.version}</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
The problem is that because this profile is active by default, if we try to run a mvn deploy and the version of the GAV coordinates is a -SNAPSHOT, the build unintentionally still tries to deploy to Nexus 3rd Party and Releases repos and fails because of course it won't accept -SNAPSHOT artifact versions. To get around this, I setup a profile specifically for -SNAPSHOT versions which will only deploy to the Snapshot repository:
<profile>
<!-- ====================================================================== -->
<!-- snapshot Profile -->
<!-- Activating this profile will automatically deactivate the default profile. -->
<!-- The purpose of this profile is to produce a a gateway-services-client and gateway-client -->
<!-- snapshot artifacts and deploy them to the snapshots Nexus repository where they can -->
<!-- act as the latest development dependencies for other projects -->
<!-- ====================================================================== -->
<id>snapshot</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<!-- ====================================================================== -->
<!-- snapshot profile Build plugins -->
<!-- ====================================================================== -->
<build>
<plugins>
<!-- ====================================================================== -->
<!-- snapshot profile Maven deploy plugin -->
<!-- ====================================================================== -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>deploy-thirdparty-snapshot-jar</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<url>${nexus.url}/content/repositories/snapshots</url>
<repositoryId>snapshots</repositoryId>
<file>${project.build.directory}/${project.build.finalName}.${project.packaging}</file>
<groupId>${project.groupId}</groupId>
<artifactId>gateway-services-client</artifactId>
<version>${project.version}</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
The problem with this is that you must specify the profile when executing the Maven command: mvn deploy -P 'snapshot'. My question is what can I do so that all I have to do is run mvn deploy without specifying the snapshot profile and have the build automatically deploy to the snapshot repository or to the 3rd Party and Releases repositories all based on the presense of -SNAPSHOT in the version of the GAV coordinates?
The only solution that comes to my mind is using properties and adding three executions during deployment. The ugly thing is that in case of SNAPSHOT your artifact would be deployed twice to the same repository.
Here is what you could do:
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>eval-repo</id>
<phase>initialize</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
if (project.version.endsWith("-SNAPSHOT")){
project.properties.repoId = "snapshots";
project.properties.repoUrl = "snapshots url";
project.properties.thirdPartyRepoId = "snapshots";
project.properties.thirdPartyRepoUrl = "snapshots url";
}
else {
project.properties.repoId = "releases";
project.properties.repoUrl = "releases url";
project.properties.thirdPartyRepoId = "thirdparty";
project.properties.thirdPartyRepoUrl = "thirdparty url";
}
</source>
</configuration>
</execution>
</executions>
</plugin>
Then add three executions with the following configurations:
<configuration>
<artifactId>gateway-client</artifactId>
<url>${repoUrl}</url>
<repositoryId>${repoId}</repositoryId>
...
<configuration>
<artifactId>gateway-services-client</artifactId>
<url>${repoUrl}</url>
<repositoryId>${repoId}</repositoryId>
...
<configuration>
<artifactId>gateway-services-client</artifactId>
<url>${thirdPartyRepoId}</url>
<repositoryId>${thirdPartyRepoUrl}</repositoryId>
...
You can't do it with profiles. From the maven doc:
A profile can be triggered/activated in several ways:
Explicitly
Through Maven settings
Based on environment variables
OS settings
Present or missing files
So you can't do it the way you want it. However, we do this all the time. Our setup is we use the following in our super-pom
<distributionManagement>
<repository>
<id>deploymentRepo</id><!-- key in settings.xml -->
<name>Releases</name>
<uniqueVersion>false</uniqueVersion>
<url>${repos.release}</url>
<layout>default</layout>
</repository>
<snapshotRepository>
<id>deploymentRepo</id>
<name>Snapshots</name>
<uniqueVersion>true</uniqueVersion>
<url>${repos.snapshot}</url>
<layout>default</layout>
</snapshotRepository>
</distributionManagement>
Note the id is the same because both repos use the same credential.
We are also using nexus where each repo is configured as snapshot or release and just with this, maven is capable of knowing that *-SNAPSHOT goes to the snapshot repo.
In other words, just give both options at the same time, don't put them in mutually exclusive profiles, and maven will know which way to send them. If it doesn't, try a repo manager

Resources