Java Project

Author

Ludovic Deneuville

Tree structure

Why Organize Your Project?

  • Maintainability: Easier to find and modify code
  • Readability: Improves understanding for developers
  • Collaboration: Essential for team projects
  • Build Process: Tools (Maven/Gradle) rely on it

The Standard Structure

  • src: The root directory for source code.
  • main: Contains the application’s core code.
  • test: Contains code for testing the application.
  • resources: Non-Code Files (config files, images, text files…)
  • target: Output directory for compiled code

library-project/
├── pom.xml
└── src/
|   ├── main/
|   │   ├── java/
|   │   │   └── fr/
|   │   │       └── ensai/
|   │   │           └── library/
|   │   │               ├── Book.java
|   │   │               └── Author.java
|   │   └── resources/
|   │       └── books.csv
|   └── test/
|       └── java/
|           └── fr/
|               └── ensai/
|                   └── library/
|                       └── BookTest.java
└── target/

bottom level library divided :

  • service
  • dao
  • business_object

test : Mirrors the main structure

Packages

  • A way to organize Java classes and interfaces into namespaces
  • Used to group related classes together
  • Help to prevent naming conflicts
  • Similar to folders or directories in a file system

Package Naming Conventions

  • Named in lowercase
  • Reverse domain name notation
  • Examples:
    • fr.ensai.library
    • com.myorg.myapp.service

service, dao…

Example

package fr.ensai.library;

public class Book{
    ...
}

Importing Packages

  • To use classes from other packages
  • import fr.ensai.library.dao;
  • No need to import classes from the same package
  • Avoid import fr.ensai.library.*

Maven

What is Maven?

  • A powerful build automation tool for Java projects
  • Helps manage dependencies
  • Provides a standardized build lifecycle
  • dependencies: external libraries your project needs
  • lifecycle: compile, test, package

essential just to manage dependencies

Alternative: Gradle

Why Use Maven?

  • Simplifies dependency management: No more manual downloads
  • Ensures consistent builds: Everyone uses the same process
  • Promotes best practices: Encourages a well-defined project structure
  • withoud maven : java -cp <all_dependencies> Main

POM

  • Project Object Model
  • a configuration file: pom.xml
  • describes your project and its dependencies

pom.xml

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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>fr.ensai</groupId>
    <artifactId>library</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- JUnit for testing -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.9.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.9.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>21</source>
                    <target>21</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Artifacts

  • Packaged component with unique ID: groupId, artifactId, version.
  • Versioning: Essential to identify artifacts
    • Major.Minor.Bugfix
    • Development: -SNAPSHOT versions
  • maven.compiler.source: Java source version compatibility for the compiler
  • maven.compiler.target: Determines the Java Runtime Environment (JRE) versions on which the compiled code can run

Dependencies

  • External libraries your project relies on
  • Maven downloads and manages these dependencies automatically
  • Example: JUnit, database connectors, logging frameworks

Plugins

  • Extend Maven’s functionality
  • Examples:
    • maven-compiler-plugin,
    • maven-surefire-plugin (for testing)
  • Help with tasks like compiling code, running tests, creating JAR files

Lifecycles

  • mvn clean: Deletes the target directory
  • mvn compile: Compiles your Java code
  • mvn test: Runs your unit tests
  • mvn package: Creates a JAR file of your project
  • mvn exec:java: Run your program
  • mvn site: Creates a website for your project

Deployment

Packaging for deployment

  • Creation of a JAR file (for API)
  • Or a WAR file (for web applications)
    • Use a Tomcat server
JAR file
  • A package file format used to aggregate many Java class files and associated resources into a single file
  • Based on the ZIP file format
  • WAR: Designed for deployment on a Java web server (e.g., Tomcat, Jetty, WildFly).
  • JAR: simply run it java -jar my-api.jar on a server or using Docker
  • Use Docker
  • Backend (Business logic)

Java Application history

  • monolith struts
  • back Java / front JS
  • Archi 3-tiers

Deployment history

  • Monolith
  • Virtualisation
  • Containerization
  • Orchestration
  • Monolith
    • one physical server: one app
    • Underutilized server resources
  • Virtualisation
    • Application isolation
    • Issue: compatibility
  • Containerization
    • Portability
  • Orchestration
    • Automated container management

Before: Filezilla, integrator, it work on my machine
Now: CICD, git, devops