'How to use maven to generate different artifactID with different scala version?

I'm writing a library for scala 2.10 and scala 2.11.

When I install this library to maven local repository or publish to maven central. I want the artifactID of the library to have different suffixes, e.g. like:

org.apache.spark:spark-core_2.10:1.6.3 &
org.apache.spark:spark-core_2.11:1.6.3

This is easy in sbt, but my project is built in maven.

Is there a plugin that allows me to add the suffix to my artifactID easily according to different maven profile? So I can install and publish 2 different versions from a single project codebase.

Thanks a lot for your opinion!



Solution 1:[1]

I do this using Maven properties, e.g.

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <name>my-package</name>
  <version>1.2.3</version>
  <artifactId>${project.name}_${scala.compat.version}</artifactId>
  ...
</project>

You can set the scala.compat.version property either directly in the POM (if you only want one version), or in profiles (if you want to choose between multiple):

<properties>
  <scala.compat.version>2.12</scala.compat.version>
</properties>

You can also specify it manually via the CLI, like mvn -Dscala.compat.version=2.12 package

Using the same property in your dependencies will ensure they match the chosen Scala version, e.g.

<dependency>
  <groupId>org.typelevel</groupId>
  <artifactId>cats-core_${scala.compat.version}</artifactId>
  <version>2.1.1</version>
</dependency>

Note that this isn't ideal, since it generates the following Maven warning:

[WARNING] Some problems were encountered while building the effective model for org.example:my-package_2.12:jar:1.2.3
[WARNING] 'artifactId' contains an expression but should be a constant. @ org.example:${project.name}_${scala.compat.version}:1.2.3, /private/tmp/nix-build-my-package_2.12-1.2.3.jar.drv-0/my-package-src/pom.xml, line 6, column 15
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Warbo