Project Structure and Build Files

Project Structure and Build Files in Spring Boot: On a manufacturing line that assembles appliances, nothing is bought ad hoc.

The Analogy: A Factory's Bill of Materials and Bonded Parts Kits

On a manufacturing line that assembles appliances, nothing is bought ad hoc. Every component is governed by a Bill of Materials: a master document that fixes, for each part, the exact approved part number and revision that has been tested to work with everything around it. When a line worker needs a capacitor, they do not choose one from a catalogue and hope; they read the part number off the Bill of Materials, and that is the part they fit. The document exists precisely so that thousands of assemblies come out identical and none of them fail because two parts were subtly incompatible.

Spring Boot's parent POM is that Bill of Materials. It carries dependency management -- a curated list that fixes the exact version of every library known to work together. This is why the dependencies you declare carry no version tag: like the line worker, you name the part and the master document supplies the approved revision. Add the web starter and you do not specify which version of Jackson or Tomcat you want; the Bill of Materials already pins a set that was tested as a whole, and you inherit it. The entire class of "it worked on my machine but the versions clash on the server" problems is what this document was created to end.

A starter is a bonded parts kit: a single bag that arrives pre-assembled with a coherent set of components. Asking for the web kit gets you the MVC stove, the JSON prep bench, and the embedded server together, their revisions already matched by the Bill of Materials. You could source each library individually, but then you are back to checking compatibility by hand. And the standard project layout -- main code under one package tree, resources beside it, tests mirroring the structure -- is the standard workstation arrangement on the line: not enforced by the machine, but so universally recognised that any worker can walk up to any station and know where everything is.

Two failures come straight from the factory floor. The first is substituting an off-spec part: you override the managed version of a library with one of your own, and now you own the compatibility risk the Bill of Materials was protecting you from -- the very version clash it exists to prevent walks back in through the part you pinned. The second is shipping the carton unassembled: if you forget the packaging step, you hand over a box of loose parts -- a plain jar with no embedded server and no manifest -- that cannot stand up and run on its own. The parts were all correct; the assembly was skipped.

Syntax or Core Structure

A Boot project is generated from Spring Initializr, which produces the correct parent POM and dependency set. The parent supplies dependency management, so the dependencies you list carry no explicit version -- each one's version is resolved from the Bill of Materials. A dependency you pin with an explicit version is the exception, and it is now yours to keep compatible.

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>3.2.0</version>
</parent>

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>   <!-- no version: from BOM -->
  </dependency>
  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.14.0</version>                          <!-- pinned: yours to own -->
  </dependency>
</dependencies>

Resolving the versions for a small set of dependencies shows the split between what the BOM manages and what you have taken responsibility for:

Output:

declared dependency              version resolution
---------------------------------------------------
spring-boot-starter-web          from BOM
spring-boot-starter-validation   from BOM
postgresql                       from BOM
jackson-databind (pinned 2.14)   explicit
---------------------------------------------------
resolved from BOM : 3
explicitly pinned : 1

Three dependencies carry no version and are resolved from the parent's dependency management; one names its own version and is therefore pinned. The three managed ones will move together the day you bump the parent version, staying mutually compatible. The pinned one will not -- it stays at 2.14 until you change it by hand, and it is exactly the kind of override that can drift out of step with the set the BOM guarantees.


Technical Depth: Layout, Dependency Management, and Packaging

The standard layout

The project layout is convention rather than enforcement, but it is universal, so following it makes any Boot project instantly navigable. Main code lives under a single package tree, resources sit beside it, and tests mirror the same structure.

src/
  main/
    java/com/example/store/
      StoreApplication.java     the @SpringBootApplication class, at the root
      web/                      controllers
      service/                  business logic
      repository/               data access
      domain/                   entities
    resources/
      application.yml           configuration
      static/                   served assets
  test/
    java/com/example/store/     tests mirror the main tree

The one placement that is not merely convention is StoreApplication.java at the root of the package tree, because the component scan starts there and reaches only downward.

Dependency management resolves version conflicts

Without a Bill of Materials, Maven resolves a version clash by "nearest definition wins": when the same library is pulled in through two dependency paths at different depths, the one closer to your project takes precedence. A managed version in the BOM overrides that entirely -- it pins the library regardless of how deep the transitive path is.

SituationRule appliedWinner
Same lib, two depths, no BOMNearest definition winsThe shallower one
Only a deep transitive pathOnly candidateThat version
Lib is in the BOMManaged version overridesThe BOM version

Output:

scenario                winning version
----------------------------------------
no BOM, two depths      1.0 (depth 1)
no BOM, only far dep    2.0 (depth 2)
BOM manages the lib     3.0 (BOM)
----------------------------------------
BOM overrides depth     : yes

The first row is Maven's default: two paths, the nearer version wins. The last row is what the BOM changes -- once a library is managed, its version is fixed no matter how deep the transitive path that requested it, which is how the parent keeps a large dependency tree internally consistent.

Starters bring coherent, overlapping sets

Each starter pulls in a set of libraries, and those sets overlap -- many starters depend on the same core modules. The libraries you end up with are the union of the sets, with shared ones appearing once, so adding a second starter rarely doubles your dependency count.

Output:

starter       libraries
------------------------
web           3
validation    2
data-jpa      4
------------------------
sum if independent: 9
unique (deduped)  : 7
shared libraries  : 1

The three starters list nine libraries between them, but spring-core is shared by all three, so the actual set is seven distinct libraries. This overlap is deliberate: the starters are layered on a common foundation, and the build resolves the shared pieces once. It is also why declaring an extra starter is cheap and why removing one does not necessarily drop a library another starter still needs.

Packaging: the assembled appliance

The Boot Maven plugin repackages your build into an executable "fat jar" that contains your code, every dependency, and the embedded server. Omit the plugin and you get a plain jar with none of that wiring -- a carton of parts that will not start.


Real World Example: Assembling the Build for a New Service

A team starts a new service that needs JSON endpoints, a Postgres database, and request validation. Rather than hand-assemble a build file, they generate it from Spring Initializr and pick the matching starters, letting the parent BOM supply every version. One developer, out of habit from an older project, pins jackson-databind to a version they remember using -- reintroducing the exact risk the BOM removes. This program models the assembled build and what it resolves to.

<!-- requirements mapped to starters; versions come from the parent BOM -->
<dependencies>
  <dependency><artifactId>spring-boot-starter-web</artifactId></dependency>
  <dependency><artifactId>spring-boot-starter-data-jpa</artifactId></dependency>
  <dependency><artifactId>postgresql</artifactId></dependency>
  <dependency><artifactId>spring-boot-starter-validation</artifactId></dependency>
  <!-- the habit that causes trouble: pinning a lib the web starter manages -->
  <dependency>
    <artifactId>jackson-databind</artifactId><version>2.14.0</version>
  </dependency>
</dependencies>
<!-- the packaging plugin is present, so the build produces an executable jar -->

Output:

requirement       starter / dependency
------------------------------------------------------
JSON endpoints    spring-boot-starter-web
Postgres          starter-data-jpa + postgresql
validation        spring-boot-starter-validation
------------------------------------------------------
starters chosen   : 3
packaging plugin  : present
jar type          : executable
pinned conflicts  : 1
conflict          : jackson-databind pinned vs BOM

Three starters cover the three requirements: web brings the JSON stack, data-jpa plus the Postgres driver covers the database, and validation adds Bean Validation. Because the packaging plugin is present, the build produces an executable jar with the server inside, ready to run with a single command. So far the build is exactly what Initializr would hand you, and it works.

The one problem is the pinned jackson-databind. The web starter already brings Jackson at the version the BOM manages, and by pinning a different version the developer has overridden that managed value with one of their own. Nothing fails at build time -- Maven happily uses the pinned version -- so the conflict is invisible until Jackson behaves differently at runtime from the version the rest of the stack was tested against. It is a single line, added from muscle memory, that quietly steps outside the guarantee the BOM was providing.

The model is deliberately coarse, and its coarseness marks its limits. It reports a "conflict" as a simple boolean, whereas a real clash may be benign (the pinned version happens to be compatible) or catastrophic (a binary-incompatible change), and only running the application reveals which. It also assumes the plugin's mere presence yields a working jar, ignoring misconfiguration like a wrong main class or a missing manifest entry. The deeper design flaw it points at, though, is structural: a build file lets you pin any version with no warning that you are overriding a managed one, so the safety the BOM provides is opt-out by a single line and nothing in the normal build output tells you the line is there. A team that reviews dependencies only when something breaks will not notice the pin until it costs them a debugging session.


Common Mistakes

  1. Adding an explicit version to a dependency the BOM already manages. Pinning a library the parent manages overrides the tested version with your own and reintroduces the version-conflict risk the BOM exists to remove. Leave the version off unless you have a specific, documented reason to override.
  1. Forgetting the Spring Boot Maven plugin. Without the packaging plugin you get a plain jar with no embedded server and no launcher manifest, and it will not start. The plugin is what repackages the build into the executable fat jar you actually run.
  1. Assembling the build file by hand instead of using Initializr. Hand-writing the parent, the plugin, and the dependency set invites subtle mistakes -- a wrong parent version, a missing plugin. Generate the skeleton and edit from there.
  1. Putting the main class in a sub-package. The layout convention exists partly so the @SpringBootApplication class sits at the root of the package tree; move it deeper and the component scan no longer reaches its siblings, exactly as covered in the previous lesson.
  1. Shipping devtools to production. spring-boot-devtools enables automatic restart and other development conveniences that must not run in production. Scope it to development so it is excluded from the production jar.
  1. Assuming adding a starter roughly doubles the dependency count. Starters share a common foundation, so their library sets overlap heavily. A second starter usually adds only its unique libraries, and removing one does not necessarily drop a library another starter still requires.
  1. Bumping a single transitive library instead of the parent version. When you need newer libraries, raising the parent version moves the whole managed set together and keeps it consistent. Bumping one transitive dependency in isolation is how a tested set drifts apart.
  1. Reading a clean build as proof there is no version conflict. Maven builds successfully even when you have overridden a managed version, because the clash surfaces at runtime, not at build time. A green build says nothing about whether your pinned version is compatible.
  1. Mixing build tools or duplicating dependency declarations. Declaring the same dependency in two places, or splitting configuration between an inherited parent and an ad hoc import, produces confusing resolution. Keep one source of managed versions -- the parent -- and declare each dependency once.

Best Practices

  • Generate new projects from Spring Initializr so the parent POM, plugin, and dependency set are correct from the first commit, then edit rather than assemble from scratch.
  • Let the parent BOM manage versions and declare dependencies without a version tag, so the whole set stays mutually compatible and moves together when you upgrade.
  • When you must override a managed version, do it deliberately, document why, and treat that library as yours to keep compatible on every parent upgrade.
  • Keep the @SpringBootApplication class at the root of the package tree and mirror the standard layout, so both the component scan and any new team member find things where they expect.
  • Include the Spring Boot Maven plugin so every build produces a self-contained executable jar, and run that same artifact in every environment.
  • Scope development-only dependencies such as devtools to the development profile so they never reach a production build.
  • Upgrade by raising the parent version rather than nudging individual transitive libraries, keeping the managed set consistent.
  • Review the dependency list -- including any explicit versions -- as part of code review, so an override added from habit is caught before it drifts out of step with the BOM.

Practice Exercises

  1. You need JSON endpoints, a Postgres database, and request validation. List the starters required and state, for each, where its library versions come from.
  1. A colleague added <version>2.14.0</version> to a dependency that the web starter already brings. Explain what this changes, why the build still succeeds, and when the problem would first appear.
  1. Given three starters that list eight libraries between them with two libraries shared across two starters each, work out how many distinct libraries the project actually pulls in.
  1. A freshly built jar refuses to start with no obvious error, while mvn spring-boot:run works fine. Name the most likely cause in the build file and the single change that fixes it.