Maven使用各种问题汇总

本文介绍了Maven在项目构建过程中的实用技巧,包括如何查找损坏的Jar文件、配置JDK版本、创建可执行Jar包、将依赖项打包到Jar中等。此外,还提供了将测试类包含在Jar包中的方法及向仓库发布Jar包的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

查找损坏Jar

 find  ~/.m2/repository/ -name "*jar" | xargs -L 1 zip -T | grep error | grep invalid

Maven plugin备忘

<!-- maven-jar-plugin:设置JDK版本  -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <archive>
                        <manifest>
                            <mainClass>com.cetc.di.hellocetc.App</mainClass>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>

                    </archive>
                    <classesDirectory>
                    </classesDirectory>
                </configuration>
            </plugin>


<!-- maven-compiler-plugin:设置JDK版本  -->
    <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
    </plugin>

<!--  maven-shade-plugin:可执行jar包 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <shadedArtifactAttached>true</shadedArtifactAttached>
                            <shadedClassifierName>exe</shadedClassifierName> <!-- Any name that makes sense -->
                        </configuration>
                    </execution>
                </executions>
            </plugin>


<!--  maven-assembly-plugin:将依赖打入Jar包 -->
<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
</plugin>

Maven将test类打入jar包

<!--  maven plugin -->
<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <descriptor>src/main/java/assembly/assembly.xml</descriptor>
                    <!--<descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> 
                        </descriptorRefs> -->
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

<!-- assembly.xml -->
<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>fat-tests</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>true</unpack>
            <scope>test</scope>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/test-classes</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>**/*.class</include>
            </includes>
            <useDefaultExcludes>true</useDefaultExcludes>
        </fileSet>
    </fileSets>
</assembly>

向仓库中发布Jar包

mvn deploy:deploy-file -DgroupId=[GroupID] -DartifactId=[ArtifactID] -Dversion=[Jar Version] -Dpackaging=jar -Dfile=[Local Jar path]  -DrepositoryId=[Repository ID, define in POM] -Durl=[Remote Repository URL] -s [default setting.xml or special one] 

Mvn依赖冲突相关

## 查看依赖树
mvn dependency:tree [-Dverbose -D...]

## 查看有效pom文件
mvn help:effective-pom

常用命令汇总

## 强制更新依赖
mvn -U

## 只加载依赖的项目
mvn -pl XXXProject -am

Maven Eclipse预处理

## 预先处理一下
mvn ... ## 编译完成
mvn eclipse:clean ## 清理
mvn eclipse:eclipse -DdownloadSources -DdownloadJavadocs -Pxxx -Dyyy

父子模块版本号

## 修改父版本号,刷新子模块的版本号
mvn -N versions:update-child-modules

一键发布

## 配置setting.xml
## 在setting.xml中配置 </servers>\<profiles>

## 在pom中添加
<distributionManagement>
        <!-- release -->
        <!-- snapshot -->
        <repository>
            <id>snapshots</id>
            <name></name>
            <url></url>
        </repository>
</distributionManagement>

## 直接发布
mvn clean deploy -DskipTests -s [SettingsPath]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值