MAVEN使用技巧

发布时间 2023-04-23 11:16:28作者: 少年阿丁

maven

常用命令

mvn -v //查看版本 
mvn archetype:create //创建 Maven 项目 
mvn compile //编译源代码 
mvn test-compile //编译测试代码 
mvn test //运行应用程序中的单元测试 
mvn site //生成项目相关信息的网站 
mvn package //依据项目生成 jar 文件 
mvn install //在本地 Repository 中安装 jar 
mvn -Dmaven.test.skip=true //忽略测试文档编译 
mvn clean //清除目标目录中的生成结果 
mvn clean compile //将.java类编译为.class文件 
mvn clean package //进行打包 
mvn clean test //执行单元测试 
mvn clean deploy //部署到版本仓库 
mvn clean install //使其他项目使用这个jar,会安装到maven本地仓库中 
mvn archetype:generate //创建项目架构 
mvn dependency:list //查看已解析依赖 
mvn dependency:tree com.xx.xxx //看到依赖树 
mvn dependency:analyze //查看依赖的工具 
mvn help:system //从中央仓库下载文件至本地仓库 
mvn help:active-profiles //查看当前激活的profiles 
mvn help:all-profiles //查看所有profiles 
mvn help:effective -pom //查看完整的pom信息

#注意
执行maven命令需要当前目录有pom依赖文件

mvn clean install -Dmaven.test.skip=true --settings D:\project\PmallCLKA\code\CBG-iDeal2BP-PTCO\settings.xml

#安装第三方Jar到本地库中(用于在项目中方便引入)
mvn install:install-file -DgroupId=xxx.xx.xxxx -DartifactId=xxx -Dversion=0.0.1 -Dpackaging=jar -Dfile=本地路径(非maven仓库路径)/xxx-0.0.1.jar

#解决jar包冲突
mvn dependency:tree命令解决jar包冲突
当项目出现jar包冲突时,用命令mvn dependency:tree 查看依赖情况
mvn dependency:tree 查看依赖树,查看包结构间的依赖
mvn dependency:tree >d:/tmp 把结果输出到文件,
然后再pom.xml文件里排除掉冲突的jar包

#指定setting文件和本地仓库
注意:-Dmaven在powershell下需要添加单引号
mvn install -Dmaven.test.skip=true -Dmaven.repo.local=D:\maven\PMALLCLAKReps\ptco --settings D:\project\PmallCLKA\code\CBG-iDeal2BP-PTCO\settings.xml
mvn clean install -Dmaven.test.skip=true --settings D:\project\PmallCLKA\code\CBG-iDeal2BP-PTCO\settings.xml 
powershell中加''号
mvn install '-Dmaven.test.skip=true' '-Dmaven.repo.local=D:\maven\PMALLCLAKReps\ptco' --settings D:\project\PmallCLKA\code\CBG-iDeal2BP-PTCO\settings.xml

maven打包本地jar包的多种方式

1.引用不在本地仓库中的jar包

*首先在resources目录下创建lib目录,然后将本地jar包复制到lib目录下
*然后在我们项目的pom文件中使用如下方式引入该jar包
<dependency>
    <groupId>xx.xx.xx</groupId>
    <artifactId>xxxx</artifactId>
    <scope>system</scope>
    <systemPath>${basedir}/src/main/resources/lib/xxxx.jar</systemPath>
    <version>1.0.0</version>
</dependency>
*此时运行项目是可以找到jar包的,但是如果项目使用maven install成jar包后,运行是无法找到我们上面引入的本地jar的,因为maven编译时没有将本地引用的jar包一起打包,需要在pom中加一段配置
<build>    
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <!--maven编译时将本地引用的jar包一起打包-->
            <includeSystemScope>true</includeSystemScope>
        </configuration>
    </plugin>
</build> 
*重新编译即可解决问题

2.用Maven将jar包添加到本地仓库,然后直接通过pom文件调用

*添加到本地仓库(settings文件配置),并指定jar包在maven仓库的groupId,artifactId,version等信息
mvn install:install-file "-Dfile=jar包路径/xxxx.jar" "-DgroupId=xx.xx" "-DartifactId=xxxx" "-Dversion=1.0.0" "-Dpackaging=jar"
*项目中可直接引用
        <dependency>
            <groupId>xx.xx</groupId>
            <artifactId>xxx</artifactId>
            <version>1.0.0</version>
        </dependency>

其他技巧:

1.排除依赖

 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version>5.1.9.RELEASE</version>
     <exclusions>
         <exclusion>
             <groupId>org.springframework</groupId>
             <artifactId>spring-core</artifactId>
         </exclusion>
     </exclusions>
 </dependency>

2.maven编译后将项目中用到的jar包拷贝到target某文件夹下

<build>
    <!--拷贝依赖到jar外面的lib目录-->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
            <execution>
                <id>copy-lib</id>
                <phase>package</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                    <outputDirectory>target/lib</outputDirectory>
                    <excludeTransitive>false</excludeTransitive>
                    <stripVersion>false</stripVersion>
                    <includeScope>runtime</includeScope>
                </configuration>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
                <!-- 生成的jar中,不要包含pom.xml和pom.properties这两个文件 -->
                <addMavenDescriptor>false</addMavenDescriptor>
                <manifest>
                    <!-- 是否要把第三方jar加入到类构建路径 -->
                    <addClasspath>true</addClasspath>
                    <!-- 外部依赖jar包的最终位置 -->
                    <classpathPrefix>lib/</classpathPrefix>
                    <!-- 项目启动类 -->
                    <mainClass>com.saas.reptile.ReptileApplicatio</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>
</build>