Maven/Gradle常用命令

发布于:2025-08-09 ⋅ 阅读:(15) ⋅ 点赞:(0)

1、常用Maven命令

1.1、创建一个Java 工程

mvn archetype:generate -DgroupId=com.mycompany.example -DartifactId=Example -DinteractiveMode=false -DarchetypeCatalog=local

1.2、创建一个java的web工程

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=com.mycompany.app -DartifactId=my-webapp

1.3、打包

mvn package

1.4、编译

mvn compile

1.5、编译测试程序

mvn test-compile

1.6、清空

mvn clean

1.7、运行测试

mvn test

1.8、生成站点目录

mvn site

1.9、生成站点目录并发布:

mvn site-deploy

1.10、安装当前工程的输出文件到本地仓库

mvn install

# 安装指定文件到本地仓库
mvn install:install-file -DgroupId=<groupId> -DartifactId=<artifactId> -Dversion=1.0.0 -Dpackaging=jar -Dfile=<myfile.jar>

mvn install:install-file -DgroupId=jdk.tools -DartifactId=jdk.tools -Dpackaging=jar -Dversion=1.6 -Dfile=tools.jar -DgeneratePom=true

1.11、查看实际pom信息

mvn help:effective-pom

1.12、分析项目的依赖信息

mvn dependency:analyze 或 mvn dependency:tree

1.13、跳过测试运行maven任务

mvn -Dmaven.test.skip=true XXX

1.14、生成eclipse项目文件

mvn eclipse:eclipse  -Dwtpversion=2.0

1.15、查看帮助信息

mvn help:help 或 mvn help:help -Ddetail=true

1.16、查看插件的帮助信息

mvn <plug-in>:help
# 或 
mvn dependency:help 
mvn ant:help

1.17、输出依赖包到lib文件夹

mvn dependency:copy-dependencies -DoutputDirectory=lib

1.18、下载源码

mvn dependency:sources

1.19、下载 API文档

mvn dependency:resolve -Dclassifier=javadoc

1.20、生成 doc文档

mvn javadoc:javadoc

1.21、运行指定的类

简单的用mvn exec:java

mvn exec:java -Dexec.mainClass=com.test.MainMsg

pom需要配置

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>3.0.0</version> 
    <configuration>
    <!-- 如果配置了MainClass,运行命令:mvn exec:java -->
    <mainClass>com.test.MainMsg</mainClass> 
    </configuration>
</plugin>

简单的用mvn exec:exec

mvn exec:exec -Dexec.mainClass=com.test.MainMsg -Dexec.data=dataxxxx

pom需要配置

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>3.5.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>java</executable>
                <includeProjectDependencies>true</includeProjectDependencies>
                <arguments>
                    <argument>-Dfile.encoding=UTF-8</argument>
                    <argument>-Xmx512m</argument>
                    <argument>-cp</argument>
                    <classpath />
                    <argument>${exec.mainClass}</argument>
                    <argument>${exec.data}</argument>
                </arguments>
            </configuration>
        </plugin>
    </plugins>
</build>

1.22、maven库搜索地址

http://search.maven.org/
http://mvnrepository.com/

1.23、maven 仓库

<repositories>
  	 	<repository>
  	      <id>alimaven</id>
  	      <name>aliyun maven</name>
  	      <url>https://maven.aliyun.com/repository/public/</url>
  	    </repository>
		<repository>
  	      <id>huaweicloud</id>
  	      <name>huawei maven</name>
  	      <url>https://mirrors.huaweicloud.com/repository/maven/</url>
  	    </repository>
  	    <repository>
  	      <id>ibiblio</id>
  	      <name>Human Readable Name for this Mirror.</name>
  	      <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
  	    </repository>
  	    <repository>
  	      <id>jboss-public-repository-group</id>
  	      <name>JBoss Public Repository Group</name>
  	      <url>http://repository.jboss.org/nexus/content/groups/public</url>
  	    </repository>
    	<repository>
	      <id>central</id>
	      <name>Maven Repository</name>
	      <url>https://repo.maven.apache.org/maven2</url>
    	</repository>
  </repositories>

2、Gradle常用命令

2.1、查看主要任务

gradlew tasks

2.2、查看所有任务,包括缓存任务

gradlew tasks --all

2.3、执行某个module的某个task

gradlew :app:builClean

2.4、快捷命令-查看版本

gradlew -v

2.5、快捷命令-清除build文件夹(包括app下的build)

gradlew clean

2.6、检查依赖并编译打包

#  release和debug都会输出
gradlew build

2.7、编译并安装debug包

gradlew installDebug

2.8、编译并打印日志

gradlew build --info

2.9、编译并输出性能报告

#  一般在根目录的build/reports/profile下
gradlew build --profile

2.10、调试模式构建并打印堆栈日志

gradlew build --info --debug --stacktrace

2.11、编译并打release包(debug同理)

gradlew assembleRelease
# 或
gradlew aR

2.12、安装release

gradlew installRelease

2.13、卸载release

gradlew uninstallRelease

2.14、查看根目录下依赖

gradlew dependencies

2.15、查看app目录下依赖

gradlew app:dependencies

网站公告

今日签到

点亮在社区的每一天
去签到