Essential Development Tools for Java Projects
Maven
A Maven project includes a pom.xml file in its root directory, which defines the project's build lifecycle. This file specifies project coordinates, dependencies, project metadata, and plugin configurations.
Maven serves three primary functions:
- Project Build: Offers a standardized, cross-platform automated build process.
- Dependency Management: Simplifies handling project dependencies (JAR files) and prevents version conflicts.
- Standardized Structure: Provides a consistent project layout.
Maven Lifecycles and Build Commands
Maven defines three independent lifecycles:
- Default Lifecycle: Manages the project build process through various phases.
- Clean Lifecycle: Cleans project artifacts, typically removing files from the target directory.
- Site Lifecycle: Generates project documentation sites with details like project status, team information, and Javadocs.
Commands start with mvn and can be combined with spaces:
mvn compile # Compile source code
mvn clean # Clean project
mvn test # Run tests
mvn package # Package artifact
mvn install # Install to local repository
Core Concepts
Artifacts (dependencies and plugins) are uniquely identified by Maven coordinates:
- groupId (required): Organization or company identifier, often with domain segments (e.g.,
org.apache). - artifactId (required): Project name, matching the root directory.
- version (required): Project version.
- packaging (optional): Artifact type (default:
jar). - classifier (optional): Distinguishes artifacts built from the same POM.
Repositories store artifacts, categorized as local or remote.
POM Structure
<project>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>sample</artifactId>
<version>1.0</version>
<type>jar</type>
<scope>compile</scope>
<optional>false</optional>
<exclusions>
<exclusion>
<groupId>org.conflict</groupId>
<artifactId>conflict-artifact</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
- dependencies: Manages all project dependencies.
- dependency: Defines a single dependency.
- type: Dependency packaging type.
- optional: Marks dependency as optional.
- exclusions: Excludes transitive dependencies.
- scope: Dependency scope.
Dependency Scopes
- compile: Default; available for compilation, testing, and runtime.
- test: Only for test compilation and execution (e.g., JUnit).
- provided: For compilation and testing, but not runtime (e.g., servlet API).
- runtime: For testing and runtime, not compilation (e.g., JDBC driver).
- system: Requires explicit file path via
systemPath.
Dependency Resolution and Conflict Management
Resolution Principles:
- Shortest Path Priority: Prefers the dependency with the shortest transitive path.
- Declaration Order Priority: In case of equal paths, uses the first declared dependency in the POM.
- Override Priority: Child POM dependencies override parent POM ones.
Conflict Resolution:
- Use
mvn dependency:treeto inspect the dependency tree. - Adjust dependency declarations or exclude conflicting artifacts:
<exclusions>
<exclusion>
<groupId>org.conflict</groupId>
<artifactId>conflict-artifact</artifactId>
</exclusion>
</exclusions>
Optional dependencies can be hidden with <optional>true</optional>.
Aggregation
Aggregation manages multiple modules in a multi-module project. Create a parent module with packaging set to pom:
<packaging>pom</packaging>
<modules>
<module>module-a</module>
<module>module-b</module>
</modules>
Build order depends on module dependencies, not configuration order.
Inheritance
Child modules inherit dependencies from a parent POM:
<parent>
<groupId>com.parent</groupId>
<artifactId>parent-project</artifactId>
<version>1.0</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
relativePath locates the parent POM, defaulting to ../pom.xml.
Properties
Properties enable dynamic configuration:
- Custom Properties: Define reusable values.
<properties>
<spring.version>5.1.9.RELEASE</spring.version>
</properties>
Access with ${spring.version}.
2. Built-in Properties: Use Maven's internal properties (e.g., ${version}).
3. Setting Properties: Reference settings.xml values (e.g., ${settings.localRepository}).
4. Java System Properties: Access system properties (e.g., ${user.home}).
5. Environment Variables: Read environment variables (e.g., ${env.JAVA_HOME}).
Resource Filtering:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
Compiler Plugin Configuration
Configure the Maven compiler plugin to specify JDK version:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
Multi-Environment Configuration
Define profiles for different environments:
<profiles>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>dev</env>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
Activate with mvn clean install -P production.
Skipping Tests
Skip tests during build:
- Command:
mvn install -DskipTests - Configure in POM with
skipTestsproperty.
Nexus Private Repository
Set up a Nexus private repository for internal artifact management. Configure mirror in settings.xml:
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/repository/maven-public/</url>
</mirror>
Ensure local Maven settings align with the repository configuration.
Deploy Artifacts:
Add distribution management to POM and run mvn deploy.
Git
Remote Repository Connection
Initialize a Git repository and connect to a remote:
git init
git remote add origin https://github.com/user/repo.git
git remote -v
Clone existing repositories with git clone <url>.
Basic Workflow
- Create and Switch Branches:
git checkout -b feature-branch
git branch -v
git checkout main
- Stage Changes:
git status
git add .
- Commit Changes:
git commit -m "Commit message"
- Push Changes:
git push -u origin feature-branch
git push
Branch Merging
Merge branches from the target branch:
git checkout main
git merge feature-branch
Resolve conflicts manually when changes overlap.
Docker
Docker containers package applications with dependencies, offering lightweight isolation via Linux kernel features like cgroups and namespaces.
Core Components
- Image: Read-only filesystem template (e.g.,
ubuntu:20.04). - Container: Runnable instance of an image.
- Repository: Storage for images.
Image Operations
docker search nginx # Search images
docker pull nginx # Pull image
docker rmi nginx # Remove image
docker build -t myapp:1.0 . # Build image
docker tag <id> myapp:2.0 # Tag image
docker save myapp > app.tar # Export image
docker load < app.tar # Import image
Container Operations
docker ps -a # List containers
docker run -itd --name mycontainer myapp:1.0 # Start container
docker stop mycontainer # Stop container
docker start mycontainer # Start stopped container
docker rm -f mycontainer # Force remove container
docker logs -f --tail 10 mycontainer # View logs
docker exec -it mycontainer /bin/bash # Attach to container
Installation and Setup
Install Docker on CentOS:
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io
Configure Mirror Acceleration:
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://mirror.example.com"]
}
EOF
sudo systemctl restart docker
Uninstall Docker:
sudo yum remove docker docker-client docker-common docker-engine