Maven Plugin for Version Management - maven

I am looking for a possibility to manage my maven project versions.
I have some maven modules in my maven project and some of these modules are depending on others of these modules.
I want to define the version to work with globally for every module or dependency.
Is this somehow possible?
Something like
globalVersion=2.0
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>${globalVersion}</version>
But as i said, not in each single pom. I mean globally for all my poms in my maven modules.

(I assume you have a parent pom common for all your modules.)
define a property in the parent pom:
<properties>
<globaleVersion>1.0.0</globalVersion>
</properties>
And define a <dependencyManagement> section in the parent pom too:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>${globalVersion}</version>
</dependency>
<dependency>
<groupId>A</groupId>
<artifactId>A</artifactId>
<version>${globalVersion}</version>
</dependency>
</dependencies>
</dependencyManagement>
And in you modules define dependencies without specifying the version (maven will find it from the dependencyManagement section of the parent)
<dependencies>
<dependency>
<groupId>test</groupId>
<artifactId>test</artifactId>
</dependency>
<dependency>
<groupId>A</groupId>
<artifactId>A</artifactId>
</dependency>
</dependencies>

Related

How to force maven dependency version via dependencyManagement in parent pom?

Let's say B:1.0.1 has transitive dependency A:1.0.1, but the child project is supposed to depend on A:1.0.2 (with intentional overriding transitive dependencies).
It is easy to discover that the order of dependencies in <dependencyManagement> affect versions overriding, so adding A:1.0.2 in the child pom just before B:1.0.1 would force using A:1.0.2 even as a dependency for B:1.0.1.
In this case I'm looking for a way to declare A:1.0.2 in the parent pom, and remove boilerplate from all its children. Unfortunately, the following setup leads to using both versions in the final artifact: A:1.0.1 (comes as a dependency of B:1.0.1) and A:1.0.2 (comes from the explicit declaration in the parent pom).
How to force using version A:1.0.2 in all child projects, keeping the declaration in the parent?
Parent pom:
<groupId>my-group</groupId>
<artifactId>my-parent</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>g</groupId>
<artifactId>A</artifactId>
<version>1.0.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Child pom:
<parent>
<groupId>my-group</groupId>
<artifactId>my-parent</artifactId>
<version>0.0.1</version>
</parent>
<artifactId>my-child</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>g</groupId>
<artifactId>A</artifactId>
<!-- version 1.0.2 comes from the parent pom -->
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>g</groupId>
<artifactId>B</artifactId>
<version>1.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
You are using the dependencyManagement incorrectly.
If A and B are jar artifacts, you should not have the tags
<type>pom</type>
<scope>import</scope>
These are for BOMs only.

custom parent pom with org.springframework.cloud and spring-boot-starter-parent

I have couple of spring cloud projects and wish to put all common dependencies into my own parent pom too. Many samples shows how to do it with <dependencyManagement>. But in my case with spring-boot-starter-parent and org.springframework.cloud, it seems to be not working using dependency management as the parent has already become 'spring-boot-starter-parent' and dependency management is also there having org.springframework.cloud. Following is one of my spring cloud projects' pom files.
<groupId>com.demo</groupId>
<artifactId>demo-customer-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo-customer-service</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
as above, there are two parents: org.springframework.boot starter parent and cloud. So how can I have my own parent ?
Any suggestion please how the parent and the child pom files should be ?
The pom you provided can perfeclty become a parent pom. Parent poms can have parent's themself also. It is commonly used and if you check the projects you are using on github they have chain of parents.
For example spring-cloud-build-dependencies has spring-boot-dependencies as parent. spring-boot-dependencies has spring-boot-build as parent.
Inside your parent pom, dependencies defined inside <dependencies></dependencies> will be added to all the childs of your parent. If all your childs are using these dependencies you can add them in this tag. (I generally add unit test dependencies here, but you can add anything)
Parent poms usualy located one folder above of childs pom.xml. (But <relativePath> can be used if it is elsewhere)
|-parent pom.xml
|-child project folder
|--child pom.xml

Download/import all dependencies from Spring IO

I work for a large organization where all development tools must go through a rigorous certification process and security assessment. I want to have Spring IO Brussels SR2 certified, along with Apache Maven.
I'd like to provide them with a ZIP file containing all JARs (dependencies and transitive dependencies) of the Spring IO platform so they can do the security assessment on it.
Being a new Maven user, I've created a new Maven project in Eclipse, and simple added this in 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.company</groupId>
<artifactId>MySpringApp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>MySpringApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Brussels-SR2</version>
<type>pom</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>MySpringApp</finalName>
</build>
</project>
The project compiles and packages correctly, but it's not importing/adding any JARs to the packaged WAR file. I've also tried to change the scope to compile.
Is it possible? Is it because it's a bill of material?
Thank you
According to the Website you have to enable the dependency-tree using
<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>my.company</groupId>
<artifactId>MySpringApp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>MySpringApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Brussels-SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<build>
<finalName>MySpringApp</finalName>
</build>
</project>
Eclipse and Maven are designed to be able to work alone, a bit of experiences is required to let them work together, often a cleanup using Update Project ... (project-rightclick/maven) is required
I ended up downloading the dependency versions list into a CSV file, then loop through it in a simple Java class, outputting the results in a tag manually. Then, I was able to copy/paste all those tags into my pom.xml file and then get all those dependencies resolved by Maven.
Finally, I have taken the content of /target/WEB-INF/lib.

What is the difference between tags parent, dependency and plugin (Maven)

I am confused by the fact that POM.xml contains parent, dependency and plugin. Somethings something specified with parent must be again included in dependency, for example
<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>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
</dependencies>
</project>
Why do I have to specify a dependency twice ? How can I use these three tags ?
The parent is a POM which gets "inherited", it basically gets combined with your current POM by means of overwriting. (You can use "mvn help:effective-pom" to print the combined result). So all the dependencies and plugins in the parent will become yours (and all managed dependency versions, which is in case of spring-boot a majority).
The dependendency specifies artifacts you need on your classpath for compiling or testing or running your project (depending on its scope).
Whereas plugin is a artifact which gets executed at build time (like a compiler, report generator and stuff).
I would suggest to read a good tutorial or book on maven, my simple explanation is not enough to make good use of it. https://maven.apache.org/articles.html

How a maven dependecy can be defined without version tag?

I have a pom which works without defining dependency version in pom works fine and another one without dependency version which does not work.
The one which works:
<project>
<parent>
<artifactId>artifact-parent</artifactId>
<groupId>group-parent</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-a</artifactId>
</dependency>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-b</artifactId>
</dependency>
</dependencies>
</project>
This one which does not work:
<project>
<dependencies>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-a</artifactId>
</dependency>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-b</artifactId>
</dependency>
</dependencies>
</project>
The only thing differ in these two seems to be:
<parent>
<artifactId>artifact-parent</artifactId>
<groupId>group-parent</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
Second one does not work it seems fine to me but my question is why the first one works ?
Quoted from maven pom reference :
This trinity represents the coordinate of a specific project in time,
demarcating it as a dependency of this project.
So my question is how the first one is working?
The main thing to notice here is:
<parent>
<artifactId>artifact-parent</artifactId>
<groupId>group-parent</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
Version of the dependency looks like to be defined in the parent pom. This can be something like this:
<project>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-a</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-b</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Again quoting from the doc :
This is because the minimal set of information for matching a
dependency reference against a dependencyManagement section is
actually {groupId, artifactId, type, classifier}.
Here we did not need to define the {type, classifier} as it is same as default value which is as follows :
<type>jar</type>
<classifier><!-- no value --></classifier>
If this value differ form the default, you need to define it explicitly in both parent pom and child pom.

Resources