Custom spring boot starter for specifying distribution management - spring-boot

We have a spring boot application which has its parent defined as spring-boot-starter-parent. But in our project, we have a parent pom with distribution management defined in it, and all the submodules in the project inherit from it. Now, since the spring boot application already inherits from spring-boot-starter-parent, and I do not want to duplicate the distribution management, I thought of having a custom spring boot starter module just to be able to define the distribution management in there and then include this custom spring boot starter as a dependency in the spring boot application. But when I do a mvn clean install deploy of the spring boot application it fails with the error shown below:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.
2:deploy (default-deploy) on project crs-acc-eve-gridgain-load-app: Deployment f
ailed: repository element was not specified in the POM inside distributionManage
ment element or in -DaltDeploymentRepository=id::layout::url parameter -> [Help
1]
Is it the right way to achieve this? or there any other recommended ways of doing it?
Given below are the poms of custom spring boot starter parent module:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>crs-micro-services</artifactId>
<groupId>com.ing.crs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>crs-spring-boot-starter-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot.version>1.3.5.RELEASE</spring-boot.version>
</properties>
<modules>
<module>crs-spring-boot-starter</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<distributionManagement>
<repository>
<id>XXXX</id>
<name>XXXX</name>
<url>XXXX</url>
</repository>
<snapshotRepository>
<id>XXXX</id>
<name>XXXX</name>
<url>XXXX</url>
<uniqueVersion>false</uniqueVersion>
</snapshotRepository>
</distributionManagement>
</project>
And this..for the starter module
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>crs-spring-boot-starter-parent</artifactId>
<groupId>com.ing.crs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<version>1.0-SNAPSHOT</version>
<artifactId>crs-spring-boot-starter</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
And the sprng boot application has this dependency
<dependency>
<groupId>com.ing.crs</groupId>
<artifactId>crs-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>import</scope>
</dependency>
Thanks and regards,
Priya

There is no requirement that your build should be configured with the spring-boot-starter-parent. If you already have a parent with some distribution management, feel free to keep it. You'll need to adapt the parent a bit. Here are couple of options:
If the whole project is spring boot based, maybe your parent could inherit from the Spring Boot starter parent? This may not work if you have a parent that is used within the entire organization
You can keep your parent as it is and use the spring-boot-dependencies BOM instead. See the documentation for more details. You'll have to copy the bits of the parent that you use (such as the plugin definition) but it's not a ton of work.
Your proposal will never work. BOM only imports <dependencyManagement>. What you're looking for is mixin and it's not supported by Maven yet.

Related

Maven - Add a maven module as dependency to other maven module

I have 2 Spring boot maven projects(module A, Module B). And they both are added as modules to a parent project. Both Modules have some common dependencies and java classes(Domain objects), so I created third module Module C, and placed all the common java files and dependencies there.
I added the Module C to parent pom as one of the module. And added Module C as dependency to Module A and Module B. In Module A, B wherever the classes of Module C is referred there it was resolved and is pointing to Module C Classes (on ctrl+click).No error was shown in eclipse and maven dependencies are updated. But when I build the projects either from Parent pom or build Module A alone (after building the module c), I get cannot find symbol error in the places where module C classes were referenced.
Below are my pom.xml's
Parent 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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.services</groupId>
<artifactId>cloud-services</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>ModuleC</module> <!-- Project where common dependencies and Common java classes are placed -->
<module>ModuleA</module>
<module>ModuleB</module>
</modules>
</project>
Module C Pom.xml - Common Project
<?xml version="1.0"?>
<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>
<parent>
<groupId>com.example.services</groupId>
<artifactId>cloud-services</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ModuleC</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ModuleC</name>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencyManagement>
</project>
Module A Pom.xml - Dependent Project on Module C
<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>
<parent>
<groupId>com.example.services</groupId>
<artifactId>cloud-services</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ModuleA</artifactId>
<dependencies>
<!-- Project where common dependencies and Common java classes are placed -->
<dependency>
<groupId>com.example.services</groupId>
<artifactId>ModuleC</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Module B Pom.xml
Same as ModuleA.
Whats wrong with my configuration. Any help is much appreciated. Thanks.
If its a spring project the root folder of the jar would be BOOT-INF.
If you create a normal project jar it wouldn't be having BOOT-INF.
The way it works is if you have a Project A and it has dependency on x.jar
Say Project A needs com.x.y.ClassA.class from x.jar it will search in root folder of x.jar with this path x/y/ClassA.class.
In your case as it was a spring project the resolution didnt work and it was complaining about that.
I guess that the spring-boot-starter-parent is the culprit.
By using it as a parent POM to your parent POM, you are building all your modules with it. AFAIK, jars build with spring-boot-starter-parent cannot be used as dependencies because the classes are moved to a different directory inside the jar.
As I am not Spring expert, I cannot really offer you a good solution for this.

Why both spring-boot-starter-parent and spring-boot-starter-web are needed for Spring Boot application pom?

I am following a video tutorial on Spring Boot (from javabrains.io). The pom file for the sample project contains a parent block with groupId as org.springframework.boot and artifactId as spring-boot-starter-parent.
Additionally it contains a dependency block with groupId as org.springframework.boot and artifactId as spring-boot-starter-web.
Q) Why do we need both elements (i.e. parent and dependency) in our pom.xml?
I thought that since the project pom inherits from spring-boot-starter-parent, all the dependencies will be automatically inherited as well.
The sample project pom.xml file is as follows:
<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>io.javabrains.springbootquickstart</groupId>
<artifactId>course-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Java Brains Course API</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
</project>
If you check the the spring-boot-starter-parent pom file, you'll see that it provides default properties and maven plugin configuration, whereas spring-boot-starter-web provides web-related spring dependencies without any additional configuration. Furthermore, both starters inherit from spring-boot-dependencies, which defines a list of dependencies that spring supports. This allows you to omit the version for any of these dependencies in your build configuration. You can learn more by reading the official documentation.
So to summarize, spring-boot-starter-parent provides
default maven plugin setup
default maven properties
dependency management
Whereas spring-boot-starter-web pulls in web-related dependencies.

IntelliJ can't find maven dependencies for GAE

I was following along with the Creating a Simple Hello World Backend API tutorial (GAE) and all was running fine via local terminal but as soon as I opened the project in IntelliJ, IntelliJ can't build the project...
The pom file has a bunch of errors about being unable to find any dependencies, however I can still build the application just fine outside of IntelliJ.
I'm not overly familiar with Maven but I believe it is configured correctly in the IDE as I can build other generic examples generated by IntelliJ. I've attempted to re-import all dependencies but no luck...
Is there something else I need to do here?
Snippet from the pom file where both dependencies are failing to find versions.
<?xml version="1.0" encoding="UTF-8"?>
<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>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<groupId>com.example.helloworld</groupId>
<artifactId>helloworld</artifactId>
<properties>
<app.id>your-app-id</app.id>
<app.version>1</app.version>
<appengine.version>${appengine.version}</appengine.version>
<gcloud.plugin.version>0.9.58.v20150505</gcloud.plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.showDeprecation>true</maven.compiler.showDeprecation>
</properties>
<prerequisites>
<maven>3.1.0</maven>
</prerequisites>
<dependencies>
<!-- Compile/runtime dependencies -->
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<version>${appengine.version}</version>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-endpoints</artifactId>
<version>${appengine.version}</version>
</dependency>
Your appengine.version property is recursively defined, so IntelliJ can't figure out what it is. Substitute <appengine.version>${appengine.version}</appengine.version> with <appengine.version>1.9.24</appengine.version> and you should be fine.

Overrriding spring version in Spring boot/ spring cloud

I note that with spring cloud Angel.SR3 which uses Spring Boot 1.2.4, the spring version loaded is 4.1.6.RELEASE.
There is a memory leak issue in the yaml processor that I read was fixed in spring 4.1.7.RELEASE.
Specifically addressed in this https://jira.spring.io/jira/secure/ReleaseNote.jspa?projectId=10000&version=14936 ([SPR-13173] - YAML Processor leaves StreamDecoder open)
I tracked through and saw that the starters use ${spring.version} to specify the spring version to be used.
I tried overriding by adding 4.1.7.RELEASE to my properties, but it still loads the 4.1.6 version
Anyway I can do this? I understand that these are curated but this particular memory leak is causing us a lot of issues.
As requested I have attached the front part of the pom. All base dependencies are pulled in through the spring cloud parent. I noticed that they use ${spring.version} to define the correct value of spring. I attempted to override it via a property setting. At no time in the rest of the my pom do I explicitly bring in any of the spring framework libs. The parent pom does that.
<?xml version="1.0" encoding="UTF-8"?>
<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.libertas.vipaas</groupId>
<artifactId>vipaas-starter</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>Vipaas Starter</name>
<description>Vipaas Starter</description>
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Angel.SR3</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.javadoc.skip>true</maven.javadoc.skip>
<spring.version>4.1.7.RELEASE</spring.version>
..... other stuff ...
</properties>
... other stuff as not relevant ....
Using dependencyManagement it should work:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
You can check it with:
mvn help:effective-pom | grep -A1 "artifactId>spring-context"

how to automatically update maven dependencies in a multi-level project?

At my workplace, different groups works on providing different services.
My team consumes these services. Currently, whenever a new version of Service is rolled out, we manually change the pom.xml to the latest version of dependency and then make a build. I am wondering if there is an automatic way of pulling latest release into build.
Here is an example to explain:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<groupId>com.company.product</groupId>
<artifactId>User-Application</artifactId>
<version>1.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<Service1-version>1.0.2</Service-1>
<Service2-version>1.1.2</Service-2>
<Service3-version>2.0.2</Service-3>
</properties>
<dependencies>
<dependency>
<groupId>com.company.product</groupId>
<artifactId>Service1</artifactId>
<version>${Service1-version}</version>
</dependency>
<dependency>
<groupId>com.company.product</groupId>
<artifactId>Service2</artifactId>
<version>${Service2-version}</version>
</dependency>
<dependency>
<groupId>com.company.product</groupId>
<artifactId>Service3</artifactId>
<version>${Service3-version}</version>
</dependency>
.....
....
</project>
When new release of each service is made, we manually change the pom.xml to get the latest dependency. How can this be managed automatically?
The versions-maven-plugin should be able to do this with the task versions:update-properties. See an example here.

Resources