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:

  1. mvn clean: Clean the project, delete the target directory
  2. mvn compile: Compile project source code
  3. mvn test: Run unit tests
  4. mvn package: Package the project (JAR, WAR, etc.)
  5. mvn install: Install the project to the local repository
  6. mvn deploy: Deploy the project to a remote repository
  7. mvn clean install: Clean and install the project
  8. mvn clean package -DskipTests: Package and skip tests

Dependency Management Commands:

  1. mvn dependency:tree: Display dependency tree, view dependency relationships
  2. mvn dependency:list: List all project dependencies
  3. mvn dependency:analyze: Analyze dependencies, identify unused and declared dependencies
  4. mvn dependency:sources: Download source code of dependencies
  5. mvn dependency:resolve: Resolve and download all dependencies
  6. mvn dependency:purge-local-repository: Clean dependencies in the local repository

Plugin Operation Commands:

  1. mvn plugin:help: Display plugin help information
  2. mvn help:describe -Dplugin=plugin-name: View detailed plugin information
  3. mvn help:effective-pom: View effective POM configuration
  4. mvn help:active-profiles: View currently activated Profiles
  5. mvn help:all-profiles: View all available Profiles
  6. mvn archetype:generate: Generate a new project from an archetype

Multi-module Project Commands:

  1. mvn clean install -pl module-a: Build only module-a
  2. mvn clean install -pl module-a -am: Build module-a and its dependent modules
  3. mvn clean install -pl '!module-a': Build all modules except module-a
  4. mvn clean install -rf module-a: Start building from module-a

Other Common Commands:

  1. mvn -v or mvn --version: View Maven version
  2. mvn -X: Enable debug mode, view detailed logs
  3. mvn -Dmaven.test.skip=true: Skip tests
  4. mvn -Pprofile-name: Activate the specified Profile
  5. mvn -Dproperty=value: Set system properties

Practical Tips:

  1. Offline Build: mvn -o clean install Use local repository for offline build
  2. Parallel Build: mvn -T 4 clean install Use 4 threads for parallel build
  3. Batch Update Dependencies: mvn versions:display-dependency-updates View updatable dependencies
  4. Force Update Snapshots: mvn clean install -U Force update SNAPSHOT dependencies
  5. Skip Specific Plugin: mvn clean install -Dmaven.plugin.skip=true

Best Practices:

  • Use mvn clean install in CI/CD processes to ensure build integrity
  • Use mvn compile and mvn test during development to quickly verify code
  • Regularly run mvn dependency:analyze to clean unused dependencies
  • Use mvn dependency:tree to analyze dependency conflicts
  • Reasonably use -pl and -am parameters in multi-module projects to improve build efficiency

Mastering these Maven commands can significantly improve development efficiency and quickly locate and resolve build issues.

标签:Maven