5月31日 23:47
What are the common Maven commands? How to use Maven commands to improve development efficiency?
Maven common commands cover multiple aspects such as project building, dependency management, and plugin operations. Proficiency in these commands is crucial for improving development efficiency.
Project Build Commands:
mvn clean: Clean the project, delete the target directorymvn compile: Compile project source codemvn test: Run unit testsmvn package: Package the project (JAR, WAR, etc.)mvn install: Install the project to the local repositorymvn deploy: Deploy the project to a remote repositorymvn clean install: Clean and install the projectmvn clean package -DskipTests: Package and skip tests
Dependency Management Commands:
mvn dependency:tree: Display dependency tree, view dependency relationshipsmvn dependency:list: List all project dependenciesmvn dependency:analyze: Analyze dependencies, identify unused and declared dependenciesmvn dependency:sources: Download source code of dependenciesmvn dependency:resolve: Resolve and download all dependenciesmvn dependency:purge-local-repository: Clean dependencies in the local repository
Plugin Operation Commands:
mvn plugin:help: Display plugin help informationmvn help:describe -Dplugin=plugin-name: View detailed plugin informationmvn help:effective-pom: View effective POM configurationmvn help:active-profiles: View currently activated Profilesmvn help:all-profiles: View all available Profilesmvn archetype:generate: Generate a new project from an archetype
Multi-module Project Commands:
mvn clean install -pl module-a: Build only module-amvn clean install -pl module-a -am: Build module-a and its dependent modulesmvn clean install -pl '!module-a': Build all modules except module-amvn clean install -rf module-a: Start building from module-a
Other Common Commands:
mvn -vormvn --version: View Maven versionmvn -X: Enable debug mode, view detailed logsmvn -Dmaven.test.skip=true: Skip testsmvn -Pprofile-name: Activate the specified Profilemvn -Dproperty=value: Set system properties
Practical Tips:
- Offline Build:
mvn -o clean installUse local repository for offline build - Parallel Build:
mvn -T 4 clean installUse 4 threads for parallel build - Batch Update Dependencies:
mvn versions:display-dependency-updatesView updatable dependencies - Force Update Snapshots:
mvn clean install -UForce update SNAPSHOT dependencies - Skip Specific Plugin:
mvn clean install -Dmaven.plugin.skip=true
Best Practices:
- Use
mvn clean installin CI/CD processes to ensure build integrity - Use
mvn compileandmvn testduring development to quickly verify code - Regularly run
mvn dependency:analyzeto clean unused dependencies - Use
mvn dependency:treeto analyze dependency conflicts - Reasonably use
-pland-amparameters in multi-module projects to improve build efficiency
Mastering these Maven commands can significantly improve development efficiency and quickly locate and resolve build issues.