Tuesday, July 21, 2020

Spring Boot Starters



Spring Boot provides a number of starters that allow us to add jars in the classpath. Spring Boot built-in starters make development easier and rapid. Spring Boot Starters are the dependency descriptors.
In the Spring Boot Framework, all the starters follow a similar naming pattern: spring-boot-starter-*, where * denotes a particular type of application. For example, if we want to use Spring and JPA for database access, we need to include the spring-boot-starter-data-jpa dependency in our pom.xml file of the project.

Third-Party Starters

We can also include third party starters in our project. But we do not use spring-boot-starter for including third party dependency. The spring-boot-starter is reserved for official Spring Boot artifacts. The third-party starter starts with the name of the project. For example, the third-party project name is abc, then the dependency name will be abc-spring-boot-starter.

The Spring Boot Framework provides the following application starters under the org.springframework.boot group.
NameDescription
spring-boot-starter-thymeleafIt is used to build MVC web applications using Thymeleaf views.
spring-boot-starter-data-couchbaseIt is used for the Couchbase document-oriented database and Spring Data Couchbase.
spring-boot-starter-artemisIt is used for JMS messaging using Apache Artemis.
spring-boot-starter-web-servicesIt is used for Spring Web Services.
spring-boot-starter-mailIt is used to support Java Mail and Spring Framework's email sending.
spring-boot-starter-data-redisIt is used for Redis key-value data store with Spring Data Redis and the Jedis client.
spring-boot-starter-webIt is used for building the web application, including RESTful applications using Spring MVC. It uses Tomcat as the default embedded container.
spring-boot-starter-data-gemfireIt is used to GemFire distributed data store and Spring Data GemFire.
spring-boot-starter-activemqIt is used in JMS messaging using Apache ActiveMQ.
spring-boot-starter-data-elasticsearchIt is used in Elasticsearch search and analytics engine and Spring Data Elasticsearch.
spring-boot-starter-integrationIt is used for Spring Integration.
spring-boot-starter-testIt is used to test Spring Boot applications with libraries, including JUnit, Hamcrest, and Mockito.
spring-boot-starter-jdbcIt is used for JDBC with the Tomcat JDBC connection pool.
spring-boot-starter-mobileIt is used for building web applications using Spring Mobile.
spring-boot-starter-validationIt is used for Java Bean Validation with Hibernate Validator.
spring-boot-starter-hateoasIt is used to build a hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS.
spring-boot-starter-jerseyIt is used to build RESTful web applications using JAX-RS and Jersey. An alternative to spring-boot-starter-web.
spring-boot-starter-data-neo4jIt is used for the Neo4j graph database and Spring Data Neo4j.
spring-boot-starter-data-ldapIt is used for Spring Data LDAP.
spring-boot-starter-websocketIt is used for building the WebSocket applications. It uses Spring Framework's WebSocket support.
spring-boot-starter-aopIt is used for aspect-oriented programming with Spring AOP and AspectJ.
spring-boot-starter-amqpIt is used for Spring AMQP and Rabbit MQ.
spring-boot-starter-data-cassandraIt is used for Cassandra distributed database and Spring Data Cassandra.
spring-boot-starter-social-facebookIt is used for Spring Social Facebook.
spring-boot-starter-jta-atomikosIt is used for JTA transactions using Atomikos.
spring-boot-starter-securityIt is used for Spring Security.
spring-boot-starter-mustacheIt is used for building MVC web applications using Mustache views.
spring-boot-starter-data-jpaIt is used for Spring Data JPA with Hibernate.
spring-boot-starterIt is used for core starter, including auto-configuration support, logging, and YAML.
spring-boot-starter-groovy-templatesIt is used for building MVC web applications using Groovy Template views.
spring-boot-starter-freemarkerIt is used for building MVC web applications using FreeMarker views.
spring-boot-starter-batchIt is used for Spring Batch.
spring-boot-starter-social-linkedinIt is used for Spring Social LinkedIn.
spring-boot-starter-cacheIt is used for Spring Framework's caching support.
spring-boot-starter-data-solrIt is used for the Apache Solr search platform with Spring Data Solr.
spring-boot-starter-data-mongodbIt is used for MongoDB document-oriented database and Spring Data MongoDB.
spring-boot-starter-jooqIt is used for jOOQ to access SQL databases. An alternative to spring-boot-starter-data-jpa or spring-boot-starter-jdbc.
spring-boot-starter-jta-narayanaIt is used for Spring Boot Narayana JTA Starter.
spring-boot-starter-cloud-connectorsIt is used for Spring Cloud Connectors that simplifies connecting to services in cloud platforms like Cloud Foundry and Heroku.
spring-boot-starter-jta-bitronixIt is used for JTA transactions using Bitronix.
spring-boot-starter-social-twitterIt is used for Spring Social Twitter.
spring-boot-starter-data-restIt is used for exposing Spring Data repositories over REST using Spring Data REST.

Spring Boot Production Starters

NameDescription
spring-boot-starter-actuatorIt is used for Spring Boot's Actuator that provides production-ready features to help you monitor and manage your application.
spring-boot-starter-remote-shellIt is used for the CRaSH remote shell to monitor and manage your application over SSH. Deprecated since 1.5.

Spring Boot Technical Starters

NameDescription
spring-boot-starter-undertowIt is used for Undertow as the embedded servlet container. An alternative to spring-boot-starter-tomcat.
spring-boot-starter-jettyIt is used for Jetty as the embedded servlet container. An alternative to spring-boot-starter-tomcat.
spring-boot-starter-loggingIt is used for logging using Logback. Default logging starter.
spring-boot-starter-tomcatIt is used for Tomcat as the embedded servlet container. Default servlet container starter used by spring-boot-starter-web.
spring-boot-starter-log4j2It is used for Log4j2 for logging. An alternative to spring-boot-starter-logging.


Reference :


Thursday, February 20, 2020

Java: How to Remove Unused Dependencies from a Maven pom.xml

The Maven Dependency Plugin will help, especially the dependency:analyze goal:
dependency:analyze analyzes the dependencies of this project and determines which are: used and declared; used and undeclared; unused and declared.
Another thing that might help to do some cleanup is the Dependency Convergence report from the Maven Project Info Reports Plugin.
Let's use maven-depedency-plugin to find obvious (and easy) stuff to fix:

1. Duplicated Dependencies

mvn dependency:analyze-duplicate
Just remove what's duplicated and that's it.

2. Used and declared; used and undeclared; unused and declared

mvn dependency:analyze
This will take a while and may warn you about a lot of depedency errors.
You will be able to remove dependencies that are not used anymore and add dependencies that are directly used.
Beware: This might show some false-positives.

Bring the big guns

1. Multiple versions of the same depedency

This is the harder, as maven doesn't have a plugin to find them easily.
So, I used a little bit of bash trickery to do the job for me:
mvn dependency:list -Dsort=true |
  grep "^\[INFO\]    " |
  awk '{print $2}' |
  cut -f1-4 -d: |
  sort |
  uniq |
  cut -f1-3 -d: |
  uniq -c |
  grep -v '^ *1 '
Line-by-line, explained version:
mvn dependency:list -Dsort=true | # list all deps
  grep "^\[INFO\]    " |          # grep for the deps list only
  awk '{print $2}' |              # remove the INFO prefix
  cut -f1-4 -d: |                 # removes the dep scope
  sort |                          # sort (duh)
  uniq |                          # remove duplicates
  cut -f1-3 -d: |                 # removes the version
  uniq -c |                       # count line groups
  grep -v '^ *1 '                 # grep groups that repeat
You'll end up with a list like this:
2 com.foo:foo-modules-commons:jar
    3 com.fasterxml.jackson.core:jackson-annotations:jar
    3 com.fasterxml.jackson.core:jackson-core:jar
    2 com.fasterxml.jackson.core:jackson-databind:jar
    2 com.squareup.okio:okio:jar
    2 commons-collections:commons-collections:jar
    2 commons-fileupload:commons-fileupload:jar
    2 joda-time:joda-time:jar
    2 org.mapstruct:mapstruct:jar
Now, for each of these deps, you'll have to run:
mvn dependency:tree -Dincludes=DEP
as in
mvn dependency:tree -Dincludes=org.mapstruct:mapstruct
This will show you all the places this dependency is being used, so all you need to do is fix it.

2. Duplicated Classes

This is something that I have seen a lot in Apache Commons libraries.
As far as I know, the easiest way to find these problems is by using Jboss' Tattletale Maven Plugin.
While the plugin seems to be abandoned, it still works. Just add it to your parent pom and run it to get the reports.
These kind of problems may require manually removing .class files from jars, excluding dependencies or, sometimes, just ignoring some of them.

Preventive actions

Act when the everything went to shit already usually is harder than avoiding small mistakes.
So, my tips are:
  • Give more attention to it in code reviews;
  • Run maven-dependency-plugin and tattletale-maven in the build for every pull request to block problematic changes;
  • You can tweak the script to find dependencies with multiple versions to exit 1 in thoses cases, and just add it to the build too.
That's it. Hope it helps!

Reference:

Maven Depedency Scope

Maven dependency scope attribute is used to specify the visibility of a dependency, relative to the different lifecycle phases (build, test, runtime etc). Maven provides six scopes i.e. compile, provided, runtime, test, system, and import.
Table of Contents

1. Compile Scope
2. Provided Scope
3. Runtime Scope
4. Test Scope
5. System Scope
6. Import Scope
7. Transitivity Resolution

Maven dependency scope – compile

This is maven default scope. Dependencies with compile scope are needed to build, test, and run the project.
Scope compile is to be required in most of the cases to resolve the import statements into your java classes sourcecode.
<dependencies>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
        <!-- You can ommit this because it is default -->
        <scope>compile</scope>
    </dependency>
</dependencies>

Maven dependency scope – provided

Maven dependency scope provided is used during build and test the project. They are also required to run, but should not exported, because the dependency will be provided by the runtime, for instance, by servlet container or application server.
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>

Maven dependency scope – runtime

Dependencies with maven dependency scope runtime are not needed to build, but are part of the classpath to test and run the project.
<dependency>
    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
    <version>1.4.4</version>
    <scope>runtime</scope>
 </dependency>

Maven dependency scope – test

Dependencies with maven dependency scope test are not needed to build and run the project. They are needed to compile and run the unit tests.
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

Maven dependency scope – system

Dependencies with system are similar to ones with scope provided. The only difference is system dependencies are not retrieved from remote repository. They are present under project’s subdirectory and are referred from there. See external dependency for more detail.
<dependency>
  <groupId>extDependency</groupId>
  <artifactId>extDependency</artifactId>
  <scope>system</scope>
  <version>1.0</version>
  <systemPath>${basedir}\war\WEB-INF\lib\extDependency.jar</systemPath>
</dependency>

Maven dependency scope – import

import scope is only supported on a dependency of type pom in the dependencyManagement section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM’s dependencyManagement section.
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>other.pom.group.id</groupId>
            <artifactId>other-pom-artifact-id</artifactId>
            <version>SNAPSHOT</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>  
    </dependencies>
</dependencyManagement>

Maven dependency transitivity resolution

When you include a maven dependency and it has it’s own other dependencies (i.e. transitive dependencies) then you may want to be clear about the scope of these transitive dependencies as well.
Let’s understand about maven transitive dependencies with a simple table. In this table, if a dependency is set to the scope in the left column, transitive dependencies at top row will result in a dependency with the scope listed at their intersection.
Dependencycompileprovidedruntimetest
compilecompile–runtime–
providedprovided–provided–
runtimeruntime–runtime–
testtest–test–
Drop me your questions in comments section.
Happy Learning!!

Reference: