Create Spring Boot project from CLI with custom package name instead of "com.example.XXXX"

753 Views Asked by At

I am trying to create a Spring Boot project using its CLI with the command spring init --dependencies=web TestProject. With this TestProject project created. And the main class DemoApplication.java created with the package name com.example.

I want to create a spring-boot project with custom packageName and main className.

1

There are 1 best solutions below

2
Debargha Roy On

Taking a look at the init command helps it. When you type spring help init, below is what you get

$ spring help init
spring init - Initialize a new project using Spring Initializr (start.spring.io)

usage: spring init [options] [location]

Option                       Description
------                       -----------
-a, --artifactId <String>    Project coordinates; infer archive name (for
                               example 'test')
-b, --boot-version <String>  Spring Boot version (for example '1.2.0.RELEASE')
--build <String>             Build system to use (for example 'maven' or
                               'gradle') (default: maven)
-d, --dependencies <String>  Comma-separated list of dependency identifiers to
                               include in the generated project
--description <String>       Project description
-f, --force                  Force overwrite of existing files
--format <String>            Format of the generated content (for example
                               'build' for a build file, 'project' for a
                               project archive) (default: project)
-g, --groupId <String>       Project coordinates (for example 'org.test')
-j, --java-version <String>  Language level (for example '1.8')
-l, --language <String>      Programming language  (for example 'java')
--list                       List the capabilities of the service. Use it to
                               discover the dependencies and the types that are
                               available
-n, --name <String>          Project name; infer application name
-p, --packaging <String>     Project packaging (for example 'jar')
--package-name <String>      Package name
-t, --type <String>          Project type. Not normally needed if you use --
                               build and/or --format. Check the capabilities of
                               the service (--list) for more details
--target <String>            URL of the service to use (default: https://start.
                               spring.io)
-v, --version <String>       Project version (for example '0.0.1-SNAPSHOT')
-x, --extract                Extract the project archive. Inferred if a
                               location is specified without an extension

examples:

    To list all the capabilities of the service:
        $ spring init --list

    To creates a default project:
        $ spring init

    To create a web my-app.zip:
        $ spring init -d=web my-app.zip

    To create a web/data-jpa gradle project unpacked:
        $ spring init -d=web,jpa --build=gradle my-dir

Thus to achieve what you want, you just need to specify the Group ID using the below command

$ spring init TestProject --groupId=com.mydomain

The above command create a project in which the DemoApplication.java is found with package name package com.mydomain.TestProject; and the pom has the group ID as <groupId>com.mydomain</groupId>

If your requirement is not to change the artifact's Group ID but just change the package name, then you can use the command with the --package-name flag

$ spring init TestApp --package-name=com.mydomain

this will create the class DemoApplication under the package com.mydomain and retain the Group ID to com.example in the POM. Combine multiple flags if you need.