MAVEN使用技巧

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到本地库中(将已有的jar安装到本地仓库,用于在项目中方便引入)(默认安装在当前maven的默认本地仓库)

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:get -DremoteRepositories=远程url -DgroupId=groupId -DartifactId=artifactId -Dversion=version
mvn dependency:get -DremoteRepositories=https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-starter-webmvc-ui -DgroupId=org.springdoc -DartifactId=springdoc-openapi-starter-webmvc-ui -Dversion=2.3.0

#解决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

使用mvn命令打包或者下载,不使用默认本地仓库,可以使用

-Dmaven.repo.local=D:\maven\Reps

参数配置自定义下载的本地仓库

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>

3.非spring项目打包

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <filters>
                            <filter>
                                <!--                                排除jar包META-INF目录中以下这些文件-->
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>

                        <transformers>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>org.example.ui.MainForm</mainClass>  <!--这里运行,主类! -->
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>

</build>
    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.5.5</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>J2CDecrypt</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>

                        <!-- 这个jar-with-dependencies是assembly预先写好的一个,组装描述引用 -->

                        <descriptorRef>jar-with-dependencies</descriptorRef>

                    </descriptorRefs>

                    <!--工程名-->

                    <finalName>${project.name}</finalName>

                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

上面两个区别就是maven-shade-plugin会将相同依赖的不同版本打包,maven-assembly-plugin相同依赖不同版本的只会打包其中一个,小型项目推荐shade。

用这个命令可以删除jar包中的特定文件:

zip -d spark_scala_demo.jar META-INF/*.RSA META-INF/*.DSA META-INF/*.SF

4.常用Maven打包插件

spring-boot-maven-plugin:这个插件是springboot的maven插件,能够将springboot项目打包为可执行的jar/war。
maven-assembly-plugin: 这个插件是maven结构定制化的打包,maven中针对打包任务而提供的标准插件,包含了以下几个插件的功能。
maven-shade-plugin:这个插件是把整个项目(包含它的依赖)都打包到一个可以执行的jar包中。
maven-jar-plugin:这个插件是将指定包目录打成单独的jar包,可以配置需要打包进去的文件,程序的入口类等。
maven-resources-plugin:这个插件是用来处理,拷贝resources下的资源文件。
maven-compiler-plugin:这个插件是用来编译Java代码。
maven-dependency-plugin:这个插件是用来拷贝依赖库。

详细参考:https://blog.csdn.net/z_xiao_qiang/article/details/129902148

5.maven官网

官网查找依赖包

官网地址: https://mvnrepository.com/

 

posted @ 2023-04-23 11:13  少年阿丁  阅读(48)  评论(0编辑  收藏  举报