Maven命令

Maven其他命令

  1. mvn clean 命令 清除 mvn compile 编译产生的target文件夹

    Microsoft Windows [版本 10.0.19044.2364]
    (c) Microsoft Corporation。保留所有权利。
    
    D:\dev\maven\testMaven>mvn clean
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ---------------------< com.test.maven:maven-test >----------------------
    [INFO] Building maven-test 1.0.0
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO]
    [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-test ---
    [INFO] Deleting D:\dev\maven\testMaven\target
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  0.416 s
    [INFO] Finished at: 2023-03-25T19:11:45+08:00
    [INFO] ------------------------------------------------------------------------
    
    
  2. mvn install 命令 将当前项目 按照pom.xml配置 生成jar到本地仓库localRepository

    	<groupId>com.test.maven</groupId>
        <artifactId>maven-test</artifactId>
        <version>1.0.0</version>
    	<!--com\test\maven\maven-test\1.0.0\maven-test-1.0.0.jar-->
    
  3. mvn test 命令

    新建一个项目 D:\dev\maven\testMaven2 目录结构与testMaven一致

    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                          http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <!--以下内容自定义-->
        <groupId>com.test.maven</groupId>
        <artifactId>maven-test2</artifactId>
        <version>1.0.0</version>
        <dependencies>
            <!--从本地仓库中获取maven-test-->
            <dependency>
                <groupId>com.test.maven</groupId>
                <artifactId>maven-test</artifactId>
                <version>1.0.0</version>
                <!--<scope>complie</scope>默认作用域,会打包到jar-->
            </dependency>
            <!--单元测试工具-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <!--只在测试中生效,不会打包到jar-->
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>
    

    test->java下 创建测试类TestMaven.java

    import org.junit.Test;
    
    public class TestMaven {
        
        @Test //必须添加@Test注解
        public void testMaven(){
            System.out.println("testMaven");
            //调用HelloMaven中的main方法
            HelloMaven.main(null);
        }
    }
    
    D:\dev\maven\testMaven2>mvn test
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ---------------------< com.test.maven:maven-test2 >---------------------
    [INFO] Building maven-test2 1.0.0
    [INFO] --------------------------------[ jar ]---------------------------------
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running TestMaven
    #输出结果
    testMaven
    Hello Maven!
    
  4. mvn package命令 自动编译、测试并使用默认插件构建jar包

    在main\java 目录和main\resources目录下分别新建文件TestPackage.java和testPackage.config

    D:\dev\maven\testMaven2>mvn package
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ---------------------< com.test.maven:maven-test2 >---------------------
    [INFO] Building maven-test2 1.0.0
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO]
    # 使用默认编译插件及配置进行编译 
    # 将main\resources下所有文件 以及main\java下的java文件编译至 target\classes
    # 注意! 默认配置 java目录下的其他类型文件将不会编译至 target\classes
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-test2 ---
    [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] Copying 1 resource
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-test2 ---
    [INFO] Changes detected - recompiling the module!
    [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
    [INFO] Compiling 1 source file to D:\dev\maven\testMaven2\target\classes
    # 编译test
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-test2 ---
    [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] Copying 0 resource
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-test2 ---
    [INFO] Changes detected - recompiling the module!
    [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
    [INFO] Compiling 1 source file to D:\dev\maven\testMaven2\target\test-classes
    [INFO]
    # 自动运行test
    [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-test2 ---
    [INFO] Surefire report directory: D:\dev\maven\testMaven2\target\surefire-reports
    
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running TestMaven
    testMaven
    Hello Maven!
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.083 sec
    
    Results :
    
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    
    [INFO]
    # 开始进行打包
    [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-test2 ---
    # 打包后jar包的位置
    [INFO] Building jar: D:\dev\maven\testMaven2\target\maven-test2-1.0.0.jar
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  3.375 s
    [INFO] Finished at: 2023-03-25T20:38:16+08:00
    [INFO] ------------------------------------------------------------------------
    
    # 解压 maven-test2-1.0.0.jar main\java以及main\resources下的文件均被添加至jar包中
     D:\dev\maven\testMaven2\target\maven-test2-1.0.0 的目录
    
    2023/03/25  20:44    <DIR>          .
    2023/03/25  20:44    <DIR>          ..
    2023/03/25  20:44    <DIR>          META-INF
    2023/03/25  20:38               258 TestPackage.class
    2023/03/25  20:38                 0 testPackage.config
                   2 个文件            258 字节
                   3 个目录 34,828,095,488 可用字节
    

    如需将java目录下其他类型文件编译至target/classes并且打包至jar中 ,需在pom.xm添加配置

     	<build>
            <resources>
                <resource>
                    <directory>
                        src/main/java
                    </directory>
                    <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
            </resources>
       </build>
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值