Building with Maven¶
This guide covers building, testing, and packaging the Authentication Test API using Apache Maven.
Project Location
All Maven commands should be run from the API_server/ directory where the pom.xml file is located.
Maven Lifecycle¶
Maven uses a standard build lifecycle with these main phases:
Basic Build Commands¶
Clean Build¶
Remove all generated files and start fresh:
This deletes the target/ directory within API_server/.
Compile¶
Compile the source code without running tests:
Output: Compiled classes in API_server/target/classes/
Run Tests¶
Execute unit tests:
Package¶
Create the WAR file:
Output: API_server/target/authentication-test-api.war
Full Build¶
Clean, compile, test, and package in one command:
This is the most common build command.
Development Mode¶
Liberty Dev Mode¶
Run the application with hot reload:
Features:
- Hot Reload: Automatically recompiles and redeploys on code changes
- Test on Save: Runs tests when files change
- Debug Ready: Debug port available on 7777
- Interactive: Press Enter to run tests, r to restart
Exit with Ctrl+C.
Skip Tests¶
Build without running tests (faster):
Warning
Only skip tests during development. Always run tests before committing code.
Advanced Build Options¶
Update Dependencies¶
Force update of all dependencies:
The -U flag forces Maven to check for updated snapshots.
Offline Build¶
Build without downloading dependencies:
Requires all dependencies to be in local repository.
Verbose Output¶
Show detailed build information:
Useful for debugging build issues.
Parallel Builds¶
Build faster using multiple threads:
Uses 4 threads. Adjust based on your CPU cores.
Build Profiles¶
Development Profile¶
Production Profile¶
Note
Profiles are defined in API_server/pom.xml and can customize build behavior.
Liberty Maven Plugin¶
Start Server¶
Start OpenLiberty server:
Server runs in background.
Stop Server¶
Stop the running server:
Run Server¶
Start server in foreground:
Exit with Ctrl+C.
Create Server Package¶
Create a runnable server package:
Output: API_server/target/authentication-test-api.zip
Deploy Application¶
Deploy WAR to running server:
Build Output¶
Directory Structure¶
After a successful build:
API_server/target/
├── authentication-test-api.war # Deployable WAR file
├── authentication-test-api/ # Exploded WAR
├── classes/ # Compiled classes
├── generated-sources/ # Generated code
├── maven-archiver/ # Maven metadata
├── maven-status/ # Build status
└── test-classes/ # Compiled test classes
WAR File Contents¶
The generated WAR file contains:
authentication-test-api.war
├── WEB-INF/
│ ├── classes/ # Application classes
│ │ └── com/example/api/
│ ├── lib/ # Dependencies (if any)
│ └── web.xml # Web descriptor (optional)
└── META-INF/
└── MANIFEST.MF # Manifest file
Dependency Management¶
List Dependencies¶
Show all project dependencies:
Analyze Dependencies¶
Check for unused or undeclared dependencies:
Download Sources¶
Download source JARs for dependencies:
Useful for IDE debugging.
Download Javadocs¶
Download Javadoc JARs:
Testing¶
Run Specific Test¶
Run a single test class:
Run Test Method¶
Run a specific test method:
Skip Tests¶
Skip test compilation and execution:
Test Coverage¶
Generate test coverage report (requires jacoco plugin):
Report: API_server/target/site/jacoco/index.html
Code Quality¶
Compile with Warnings¶
Show all compiler warnings:
Format Code¶
Format code according to style rules (requires formatter plugin):
Troubleshooting¶
Clear Local Repository¶
If dependencies are corrupted:
# Remove project dependencies from local repo
rm -rf ~/.m2/repository/com/example/authentication-test-api
# Rebuild
cd API_server
mvn clean install -U
Increase Memory¶
If build runs out of memory:
Debug Build¶
Run Maven in debug mode:
Review build.log for detailed information.
Dependency Conflicts¶
Resolve dependency conflicts:
Look for conflicts marked with (omitted for conflict with X.Y.Z).
Build Best Practices¶
1. Always Clean Before Release¶
Ensures no stale artifacts.
2. Run Tests¶
Runs all tests including integration tests.
3. Check for Updates¶
4. Verify Build Reproducibility¶
cd API_server
mvn clean package
mvn clean package
diff target/authentication-test-api.war target/authentication-test-api.war
Builds should be identical.
CI/CD Integration¶
GitHub Actions¶
GitLab CI¶
Jenkins¶
Next Steps¶
- Containerization - Build container images
- Running the Server - Deploy and run the application
- Configuration - Configure the application