Maven使用中的常见问题整理

合集下载

Maven常用命令及其作用、常见问题、常用命令使用场景举例

Maven常用命令及其作用、常见问题、常用命令使用场景举例

Maven常⽤命令及其作⽤、常见问题、常⽤命令使⽤场景举例⼀、Maven常⽤命令及其作⽤1、 maven clean:对项⽬进⾏清理,删除target⽬录下编译的内容2、 maven compile:编译项⽬源代码3、 maven test:对项⽬进⾏运⾏测试4、 maven packet:打包⽂件并存放到项⽬的target⽬录下,打包好的⽂件通常都是编译后的class⽂件5、 maven install:在本地仓库⽣成仓库的安装包,可供其他项⽬引⽤,同时打包后的⽂件放到项⽬的target⽬录下⼆、常⽤命令使⽤场景举例1、mvn clean package依次执⾏了clean、resources、compile、testResources、testCompile、test、jar(打包)等7个阶段package命令完成了项⽬编译、单元测试、打包功能,但没有把打好的可执⾏jar包(war包或其它形式的包)布署到本地maven仓库和远程maven私服仓库2、mvn clean install依次执⾏了clean、resources、compile、testResources、testCompile、test、jar(打包)、install等8个阶段install命令完成了项⽬编译、单元测试、打包功能,同时把打好的可执⾏jar包(war包或其它形式的包)布署到本地maven仓库,但没有布署到远程maven私服仓库3、mvn clean deploy依次执⾏了clean、resources、compile、testResources、testCompile、test、jar(打包)、install、deploy等9个阶段deploy命令完成了项⽬编译、单元测试、打包功能,同时把打好的可执⾏jar包(war包或其它形式的包)布署到本地maven仓库和远程maven私服仓库三、常见问题(⼀)mvn clean install 和 mvn install 的区别1、根据maven在执⾏⼀个⽣命周期命令时,理论上讲,不做mvn install 得到的jar包应该是最新的,除⾮使⽤其他⽅式修改jar包的内容,但没有修改源代码2、平时可以使⽤mvn install ,不使⽤clean会节省时间,但是最保险的⽅式还是mvn clean install,这样可以⽣成最新的jar包或者其他包(⼆)maven两种跳过单元测试⽅法的区别1、 mvn package -Dmaven.test.skip=true不但跳过了单元测试的运⾏,同时也跳过了测试代码的编译2、 mvn package -DskipTests跳过单元测试,但是会继续编译。

maven问题-resolutionwillnotbereattempteduntilth。。。

maven问题-resolutionwillnotbereattempteduntilth。。。

maven问题-resolutionwillnotbereattempteduntilth。

最近在家⾥写maven程序的时候⽼是出现问题,有些问题到了公司就突然消失了。

在修改pom⽂件后保存的反应还是⽐较明显的,家⾥的⽹遇到有些依赖根本下载不了。

墙。

但是到了公司,不但速度快,⼏乎啥都能下载,,⼤概是因为这些需要的在私服上有存着有吧。

每次在家⾥构件项⽬失败,失败的次数多了,搞的我都以为是我pom⽂件哪⾥写错了。

这次其它的项⽬都好⽤(以前的是⼀⽚全坏了,,),就⼀个插件项⽬坏了,我也看它加载了好久都没反应,肯定是⽹络的原因,于是准备去公司⽤公司的jiluyou加载试试。

结果正巧今天的jiluyou也上不去国外的⽹了,只好⾃⼰找vpn,找了半天算是找到⽐较好⽤的了。

然后还是提⽰“resolution will not be reattempted until the update interval of MyRepo has elapsed”,
我看了看,⼤概的意思应该就是⽹络连接失败次数太多了,都懒得再尝试了。

但是我换了⽹络了啊,于是我试了试reindex,还是不⾏。

最后在上找到建议。

去本地仓库把plugin-plugin这个给删了,然后update⼀下项⽬就⾃动开始加载起来,然后就ok了。

总结:
1.reindex
2.删repository。

关于Maven工程依赖包的一系列问题

关于Maven工程依赖包的一系列问题

关于Maven⼯程依赖包的⼀系列问题本地仓库和镜像这些就不多说了。

创建Maven⼯程慢,转载⾃原因IDEA根据maven archetype的本质,其实是执⾏mvn archetype:generate命令,该命令执⾏时,需要指定⼀个archetype-catalog.xml⽂件。

该命令的参数-DarchetypeCatalog,可选值为:remote,internal ,local等,⽤来指定archetype-catalog.xml⽂件从哪⾥获取。

解决⽅法1.在下⾯界⾯添加⼀个属性,archetypeCatalog = internal⽅法⼆在maven的VM Options加上-DarchetypeCatalog=internal参数,如下:⽅法3: ⽤local (推荐这种⽅法完美)1.由于默认情况下,根据archetype创建maven项⽬会从⽹络下载catalog⽂件,导致创建maven项⽬缓慢Searching for remote catalog: /maven2/archetype-catalog.xml2.解决办法可以设置使⽤本地catalog⽂件,在IDEA中设置archetype的使⽤⽅式为local;-DarchetypeCatalog=local直接先把⽂件⽤搜狗浏览器或其它下载⼯具下载下来。

下载后放到哪⾥呢,这⾥与本地仓库的位置有关,假如本地仓库是maven默认的,并没有修改那么就需要放到 C:\Users\del-berlin\.m2\repository\org\apache\maven\archetype\archetype-catalog\2.4\下,本地仓库的默认位置:Default: ${user.home}/.m2/repositorymanven⽆法⾃动下载jar包转载⾃⾸先我的错误是在IDEA和ECLIPSE上都⽆法创建springboot与maven项⽬,都是同样的maven依赖不能⾃动下载的原因,所以排除了软件的问题,以下是解决路程:⼀:我先了解了⼀下maven仓库的构成与核⼼配置⽂件settings.xml中每个字段都代表的是什么意思1 <settings xmlns="/SETTINGS/1.0.0"2 xmlns:xsi="/2001/XMLSchema-instance"3 xsi:schemaLocation="/SETTINGS/1.0.04 https:///xsd/settings-1.0.0.xsd">5 <localRepository>${user.home}/.m2/repository</localRepository>6 <interactiveMode>true</interactiveMode>7 <usePluginRegistry>false</usePluginRegistry>8 <offline>false</offline>9 ...10 </settings><localRepository />:配置系统本地仓库的路径。

maven循环引用

maven循环引用

maven循环引用Maven 是 Java 项目管理的工具之一,但有时在使用 Maven 时可能会遇到循环依赖的问题。

循环依赖的产生主要是因为两个或多个Maven 项目相互引用,导致编译时无法正常完成,从而出现编译错误。

本文将介绍 Maven 循环引用的原因、表现、解决方法以及如何防止循环引用的产生。

一、循环依赖的原因循环依赖的产生是因为 Maven 项目之间相互引用,其中至少有两个项目在引用对方的模块。

例如,模块 A 依赖模块 B,在 B 模块中又依赖了模块 A,这样就会形成循环依赖。

这种情况下,Maven 就无法判断先编译那个模块,因此无法正常编译项目。

二、循环依赖的表现当存在循环依赖问题时,Maven 在编译时会出现以下错误:```[ERROR] Failed to execute goalorg.apache.maven.plugins:maven-compiler-plugin:x.x.x:compile (default-compile) on project xx: Compilation failure: Compilation failure:[ERROR] xx.java:[x,x] error: cannot find symbol[ERROR] symbol: class xx[ERROR] location: package xx```三、循环依赖的解决方法解决循环依赖问题的方法主要有以下两种:1. 优化依赖结构。

将两个或多个相互依赖的模块进行重构,让它们之间的相互依赖变为单向依赖。

例如,模块 A 只依赖模块 B,不再依赖模块 C,模块 C 只依赖模块 D,不再依赖模块 A。

这样可以让依赖结构更为清晰,避免循环依赖问题。

制项目中出现循环依赖的情况。

在 pom.xml 文件中添加以下配置即可:```<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-enforcer-plugin</artifactId><version>1.4.1</version><executions><execution><id>enforce</id><goals><goal>enforce</goal></goals><configuration><rules><banCircularDependencies>true</banCircularDependencies></rules></configuration></execution></executions></plugin>```四、防止循环依赖的产生为了避免循环依赖的产生,我们需要提前预防和规避,具体方法如下:1. 避免使用反射和静态变量等引用类型不确定的方式。

Maven中使用versionLATESTversion自动依赖最新版本引发的问题

Maven中使用versionLATESTversion自动依赖最新版本引发的问题

Maven中使⽤versionLATESTversion⾃动依赖最新版本引发的问题今天在打包项⽬的过程中出现了编译问题,奇怪的是这个项⽬已经好久没有修改过了,报错如下。

找不到符号[ERROR] 符号: ⽅法 intent(ng.String)[ERROR] 位置: 类型为com.paypal.orders.OrderRequest的变量 orderRequest找不到符号[ERROR] 符号: ⽅法 amount(com.paypal.orders.AmountWithBreakdown)[ERROR] 位置: 类 com.paypal.orders.PurchaseUnitRequest原因在于设置外部依赖包的版本时使⽤了LATEST,这样每次便会尝试拉取最新版本的包。

<dependency><groupId>com.paypal.sdk</groupId><artifactId>checkout-sdk</artifactId><version>LATEST</version></dependency>问题原因:问题的错误提⽰很明确,是找不到paypal的sdk⽅法,很⾃然的就把问题指向了版本依赖上⾯,经过⼀番搜索后在Maven仓库中发现checkout-sdk这个包在2019年7⽉份的时候发布了1.0.1版本,这个版本在⼀些功能上没有向上兼容旧版本,导致项⽬中依赖了1.0.0版本所使⽤的sdk,再依赖了新版本后便出现了编译问题。

找到问题原因之后,我们只需要把版本号改成原来的旧版本便可以了。

<dependency><groupId>com.paypal.sdk</groupId><artifactId>checkout-sdk</artifactId><version>1.0.0</version></dependency>建议在依赖外部包的时候,版本号不要直接写LATEST,要使⽤固定的版本号,否则有可能出现包升级,导致不兼容的问题。

maven常见问题汇总专题

maven常见问题汇总专题

maven常见问题汇总专题child module ….pom.xml does not exista.注意module的名称是否正确,有时候命名问题会导致找不到项⽬的b.注意⼀开始项⽬命名的规则问题注意⼀开始项⽬命名的规则问题报错信息:[INFO][INFO] --- maven-assembly-plugin:2.6:single (app) @ api ---[INFO] Reading assembly descriptor: src/assembly/assembly.xml[ERROR] OS=Windows and the assembly descriptor contains a *nix-specific root-relative-reference (starting with slash) /bin[ERROR] OS=Windows and the assembly descriptor contains a *nix-specific root-relative-reference (starting with slash) /lib[INFO] Building tar: F:\project\workspace\com\api\target\com-app.tar.gz[ERROR] OS=Windows and the assembly descriptor contains a *nix-specific root-relative-reference (starting with slash) /bin[ERROR] OS=Windows and the assembly descriptor contains a *nix-specific root-relative-reference (starting with slash) /lib[INFO] Building zip: F:\project\workspace\com\api\target\com-app.zip解决办法:The working solution is to specify the empty outputDirectory:<fileSets><fileSet><directory>${basedir}/src/main/resources</directory><outputDirectory></outputDirectory></fileSet></fileSets>有错误产⽣的assembly.xml<assembly xmlns="/ASSEMBLY/2.0.0"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/ASSEMBLY/2.0.0 /xsd/assembly-2.0.0.xsd"><id>app</id><formats><format>tar.gz</format><format>zip</format></formats><includeBaseDirectory>false</includeBaseDirectory><fileSets><fileSet><directory>${project.build.directory}/bin</directory><outputDirectory>/bin</outputDirectory></fileSet><fileSet><directory>${project.build.directory}</directory><includes><include>*.jar</include></includes><excludes><exclude>*sources.jar</exclude></excludes><outputDirectory>/lib</outputDirectory></fileSet></fileSets></assembly>https:///questions/28500401/maven-assembly-plugin-warning-the-assembly-descriptor-contains-a-filesystem-roo 使⽤maven-compiler-plugin 时POM⽂件如下:<plugins><plugin><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.6</source><target>1.6</target><encoding>UTF-8</encoding></configuration></plugin></plugins>[WARNING] Some problems were encountered while building the effective model for com.xxx.xxx:xxxx:jar:0.0.1-SNAPSHOT[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 72, column 12修改后如下,OK了<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.6.1</version><configuration><source>1.6</source><target>1.6</target></configuration></plugin></plugins></build>Configuring Your Compiler PluginSince the Compiler Plugin executes automatically during their phases, you don't have to put executions unlike many other plugins. However, you should specify the version of the Compiler Plugin.<project>...<build><pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.6.1</version><configuration><!-- put your configurations here --></configuration></plugin></plugins></pluginManagement></build>...</project>/plugins/maven-compiler-plugin/usage.htmlmaven install⽣成最终的构件包xxx-1.0.0.war(xxx-1.0.0.jar)后,在其下的WEB-INF/lib(xxx-1.0.0.jar\BOOT-INF\lib)中,会包含我们被标注为scope=compile的构件的jar包,⽽不会包含我们被标注为scope=provided的构件的jar包。

maven模块之间依赖不能使用

maven模块之间依赖不能使用如果你在使用Maven管理项目时发现模块之间的依赖无法正常工作,可能是由于多种原因造成的。

以下是一些常见的问题和解决方案:1. 依赖声明错误:确保在每个模块的``文件中正确声明了所需的依赖。

确保使用正确的版本号,特别是当依赖项来自外部仓库或本地仓库时。

2. 父项目和子项目:如果你是多模块项目的子模块,确保父项目的``中正确声明了所有需要的依赖。

使用`<dependencyManagement>`标签在父项目中管理依赖,这样子模块就不必重复声明这些依赖。

3. 仓库问题:确保Maven配置中包含了正确的仓库地址,以便下载依赖项。

尝试清理本地仓库(例如,使用`mvn clean`命令)并重新下载依赖项。

4. 网络问题:如果你的项目需要从外部仓库下载依赖项,确保网络连接正常,并且没有任何防火墙或代理服务器阻止Maven访问外部仓库。

5. 版本冲突:有时,不同的依赖项可能会引入相同库的不同版本,导致冲突。

使用`mvn dependency:tree`命令检查并解决版本冲突。

6. 插件问题:如果问题与插件相关,确保插件的依赖项已正确声明,并且版本兼容。

7. IDE设置:如果你使用IDE(如IntelliJ IDEA或Eclipse),确保已正确配置Maven设置,包括本地仓库路径和Maven安装路径。

8. 其他依赖问题:检查是否有循环依赖的问题。

如果有,尝试重构代码以消除循环依赖。

9. 查看错误日志:仔细查看Maven输出的错误日志,它通常会提供关于为什么依赖无法解析或下载的详细信息。

根据这些信息进行调试。

10. 手动安装依赖:如果可能,尝试手动下载所需的依赖并将其安装到本地仓库,然后再次运行Maven命令。

11. 更新Maven版本:有时,使用较旧的Maven版本可能会导致问题。

尝试更新到最新稳定版本。

12. 清理并重新构建项目:在IDE中或使用命令行工具执行清理和重新构建操作,以确保所有旧的构建文件都被清除。

SpringBootMavenPlugin打包异常解决方案

SpringBootMavenPlugin打包异常解决方案SpringBootMavenPlugin是一个用于打包和部署Spring Boot应用程序的Maven插件。

在使用该插件进行打包时,可能会遇到一些异常情况。

下面将提供一些常见的SpringBootMavenPlugin打包异常解决方案。

1.配置错误:可能是由于在配置文件中配置错误引起的异常。

可以通过检查pom.xml文件中的插件配置来解决此问题。

确保插件依赖项和插件版本正确配置。

2.依赖项冲突:如果在项目中使用了多个依赖项,并且这些依赖项之间存在冲突,那么可能会导致打包异常。

可以通过检查项目的依赖项,解决冲突并排除不必要的依赖项来解决此问题。

3.依赖项缺失:如果项目中缺少必要的依赖项,那么打包时可能会出现异常。

可以通过检查依赖项列表,并确保所有必要的依赖项都正确添加。

4.插件版本不兼容:可能是由于使用了不兼容的插件版本引起的异常。

可以尝试升级或降级插件版本来解决此问题。

5.系统配置错误:有时,打包异常也可能是由于系统配置错误引起的。

例如,Maven的环境变量配置错误,或者Maven的设置文件中的配置错误。

可以通过检查和修复系统配置来解决此问题。

6.构建过程中的资源冲突:如果在构建过程中使用了相同的资源文件名,并且这些资源文件在不同的位置存在冲突,那么可能会导致打包异常。

可以通过检查和解决资源冲突来解决此问题。

7.插件缺失:如果在pom.xml文件中没有正确配置SpringBootMavenPlugin插件,那么可能无法正确打包应用程序。

可以通过检查和添加正确的插件配置来解决此问题。

8. Maven仓库异常:9.其他异常:如果以上方法都无法解决异常,建议查看详细的错误信息和日志文件,以获取更多有关异常情况的信息。

有时可能需要进行更深入的调试来找到并解决异常。

总结起来,解决SpringBootMavenPlugin打包异常的关键是仔细检查和解决配置错误、依赖项冲突、依赖项缺失、插件版本不兼容、系统配置错误、资源冲突、插件缺失、Maven仓库异常等问题。

maven面试题

maven面试题Maven是一个流行的项目管理工具,常常在软件开发中使用。

对于那些正在寻找软件开发工作的人来说,熟悉Maven并能够回答相关面试题是非常重要的。

以下是一些常见的Maven面试题及其答案。

问题一:请介绍一下Maven的基本概念和作用。

答:Maven是一个基于项目对象模型(POM)的项目管理工具。

它负责项目构建、依赖管理和项目信息管理。

Maven的核心概念包括坐标、依赖关系和生命周期。

坐标用于唯一标识项目,依赖关系用于管理项目所需的外部依赖,而生命周期定义了构建过程中的不同阶段。

问题二:Maven的生命周期包括哪些阶段?答:Maven的生命周期包括三个主要的阶段:clean、default和site。

clean阶段负责清理构建过程中产生的临时文件和目录;default阶段负责实际的构建过程,其中包括编译、测试、打包等;site阶段负责生成项目的站点文档。

问题三:Maven的仓库有哪几种类型?答:Maven的仓库分为本地仓库和远程仓库两种类型。

本地仓库位于本地开发者的机器上,用于存储和管理项目的依赖。

远程仓库位于网络上,可以通过配置文件指定,用于下载项目的依赖。

问题四:如何创建一个Maven项目?答:可以通过使用Maven的命令行工具或者通过IDE(如Eclipse、IntelliJ IDEA等)来创建一个Maven项目。

使用命令行工具时,可以通过执行`mvn archetype:generate`命令,并选择对应的项目模板来创建项目。

问题五:如何管理项目的依赖?答:Maven使用POM文件来管理项目的依赖。

POM文件是一个XML文件,其中包含项目的基本信息和依赖信息。

在POM文件中,可以通过<dependencies>元素来声明项目所需的依赖,并指定对应的坐标和版本号。

问题六:Maven的插件是什么?答:Maven的插件是用于扩展Maven功能的工具。

插件可以在Maven构建的不同阶段执行特定的任务。

安装与配置windows版maven与常见错误

安装与配置windows版maven(前提:先安装jdk):1.到下载最新maven3.1.1并解压到D盘,并将解压目录改名为maven32.修改D:¥maven3¥bin¥mvn.bat,看脚本,在设置java.exe前设置JAVA_HOME(找到位置加上:set JAVA_HOME=d:¥jdk7)3.打开命令窗口,输入d:¥maven3¥bin¥mvn --version,就可以看到maven信息.4.输入d:¥maven3¥bin¥mvn help:system,就可以看到下载相关东东到库.默认的库位置在当前用户主目录的.m2目录,按ctrl+c可以终止下载.5.更改本地库到e:/repository,在默认的库位置所在目录C:¥Users¥<USER>¥.m2,把repository删了,然后复制D:¥maven3¥conf的settings.xml到C:¥Users¥<USER>¥.m2,在setting节点加入<localRepository>e:/repository</localRepository>就OK,再次输入d:¥maven3¥bin¥mvn help:system看结果生成项目:d:¥maven3¥bin¥mvn archetype:generate -DinteractiveMode=false-DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.test-DartifactId=testjava -Dversion=1.0-SNAPSHOTd:¥maven3¥bin¥mvn archetype:generate -DinteractiveMode=false-DarchetypeArtifactId=maven-archetype-webapp -DgroupId=com.test -DartifactId=testweb -Dversion=1.0-SNAPSHOT常见错误:一..在Eclipse导入已存在工作空间的项目,出现情况1:Scanning errors (1):1 Could not read pom.xml就是pom.xml出现了不能解析的配置,把不能解析的配置修改过来.例如去掉<build><finalName>testweb</finalName></build>情况2:No marketplace entries found to handle maven-compiler-plugin:2.3.2:testCompile in Eclipse.将Eclipse内置的Maven换成外部自己安装的Maven,重启Eclipse.然后,在命令行执行mvn clean install再导入项目.二.导入项目后出现:情况1:Project configuration is not up-to-date with pom.xml. Run Maven->Update Project or use Quick Fix.在Problems View的出错提示右键选Quick Fix,再按提示确定就OK.或者,右键项目->Mave n->Update Project情况2:JavaServer Faces 2.2 can not be installed : One or more constraints have not been satisfied. JavaServer Faces 2.2 requires Dynamic Web Module 2.5 or newer.先改web.xmlwebapp节点:<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="/xml/ns/javaee"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/xml/ns/javaee/xml/ns/javaee/web-app_3_1.xsd"version="3.1">然后关闭Eclipse,改项目下的.settings¥mon.project.facet.core.xml.将版本改成为3.1,将成后是<installed facet="jst.web" version="3.1"/>,再启动Eclipse.最后出现上面的情况1,按上面更新下配置就OK.三.<addMavenDescriptor>false</addMavenDescriptor>在Eclipse无效,仍然会将pom.xml,pom.properties打包入jar.在CLI(command-line interface)运行命令,此种情况拒绝使用Eclipse来执行命令(当然你测试打包入去没影响)四.启动tomcat报错:java.util.zip.ZipException: invalid LOC header (bad signature),启动jetty报错: java.util.zip.ZipException: invalid distance too far back以上两种错:一般是一个或多个下载的jar不完整.在eclipse的下载经常出现.而直接在cmd好像不会出错....tomcat\会提示是那些类...就可推是那个jar.但jetty可能不会指出是那个jar.按常理都是调用外部的maven,并且下载后做文件指纹校验的.真的不明白会有这样的情况.先用idea一段时间看看...至少eclipse4.3遇到两次了.我都清一次库了,抓狂啊.啊..啊....对于抛出的异常还是找不到下载不完整的jar,我就写了一个工具.在文末我会贴上.五.没用web.xml的web,打包成war文件,报错: Failed to execute goalorg.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project web: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)只需要在web模块下的pom.xml下的build节点增加以下代码,重写一下maven-war-plugin 组件就可以.重点在于下面的configuration. plugins是build下在同的节点,与pluginManagement是兄弟节点.其中,下面的executions节点可以删除,留默认值.[html]view plaincopy1.<plugins>2.<plugin>3.<artifactId>maven-war-plugin</artifactId>4.<version>2.2</version>5.<configuration>6.<failOnMissingWebXml>false</failOnMissingWebXml>7.</configuration>8.<executions>9.<execution>10.<id>default-war</id>11.<phase>package</phase>12.<goals>13.<goal>war</goal>14.</goals>15.</execution>16.</executions>17.</plugin>18.</plugins>安装jar到本地maven库例子.d:¥maven¥bin¥mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.4.0 -Dpackaging=jar -Dfile=ojdbc14-10.2.0.4.0.jarmaven的5种scope对于3种class是否有效:[html]view plaincopy1. compile test runtime examplepile Y Y Y spring-core3.test - Y - junit4.provided Y Y - servlet-api5.runtime - Y Y jdbc驱动6.system Y Y - 本地的,maven仓库之外的类库文件左边为第一直接依赖,上边为第二直接依赖,则对应的依赖传递如下:[html]view plaincopy1. compile test provided runtimepile compile - - runtime3.test test - - test4.provided provided - provided provided5.runtime runtime - - runtime依赖调解两原则:1.路径最近者优先2.路径长度相同,解析依赖声明靠前优先找出maven下载出错的文件工具类(我170多M左右的maven库,一分钟左右就检查完毕,最终确实有几个下载不完整的文件)[java]view plaincopy1.import java.io.*;2.import java.security.MessageDigest;3.import java.security.NoSuchAlgorithmException;4.5./**6. * 找出maven下载出错的文件7. * Created by xiejx618 on 14-4-1.8. */9.public class TestUtil {10.private static MessageDigest messageDigest = null;11.static {12.try {13. messageDigest= MessageDigest.getInstance("SHA1");14. }catch (NoSuchAlgorithmException e){15. e.printStackTrace();16. }17. }18.private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5','6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };19.private static final int BUFFER_SIZE = 4096;20.21./**22. * 以16进制字符串形式返回数据的sha123. * @param data24. * @return25. */26.public static String digestData(byte[] data) {27. messageDigest.update(data);28. data=messageDigest.digest();29.int len = data.length;30. StringBuilder buf = new StringBuilder(len * 2);31.for (int j = 0; j < len; j++) {32. buf.append(HEX_DIGITS[(data[j] >> 4) & 0x0f]).append(HEX_DIGITS[data[j] & 0x0f]);33. }34.return buf.toString();35. }36./**37. * 根据文件与编码以String形式返回文件的数据38. * @param file39. * @param charset40. * @return41. */42.public static String getFileString(File file, String charset){43. InputStreamReader reader=null;44. StringBuilder out = null;45.try {46. reader = new InputStreamReader(new FileInputStream(file), charset);47. out = new StringBuilder();48.char[] buffer = new char[BUFFER_SIZE];49.int bytesRead = -1;50.while ((bytesRead = reader.read(buffer)) != -1) {51. out.append(buffer, 0, bytesRead);52. }53. }catch (IOException e){54. e.printStackTrace();55. }finally {56.try {57.if (reader!=null) reader.close();58. } catch (IOException ex) {59. ex.printStackTrace();60. }61. }62.return out.toString();63. }64.65./**66. * 根据文件以byte[]形式返回文件的数据67. * @param file68. * @return69. */70.public static byte[] getFileData(File file){71. FileInputStream in=null;72. ByteArrayOutputStream out=null;73.try {74. in=new FileInputStream(file);75. out=new ByteArrayOutputStream(BUFFER_SIZE);76.int byteCount = 0;77.byte[] buffer = new byte[BUFFER_SIZE];78.int bytesRead = -1;79.while ((bytesRead = in.read(buffer)) != -1) {80. out.write(buffer, 0, bytesRead);81. byteCount += bytesRead;82. }83. out.flush();84. } catch (IOException e) {85. e.printStackTrace();86. } finally {87.try{88.if (in!=null) in.close();89.if (out!=null) out.close();90. }catch (IOException ex){91. ex.printStackTrace();92. }93. }94.return out.toByteArray();95. }96.97./**98. * 凡是以.sha1的文件结尾的文件,先将文件名去掉.sha1找到文件sourcefile,获取sourcefile文件的真实的sha1和从这个.sha1文件获取sha1,99. * 进行比照,如果不匹配,输出实际的sha1和期望的sha1100. *101. * @param file102. */103.public static final void handle(File file){104. String filename=file.getName();105.if(filename.endsWith(".sha1")){106. String sourcename=filename.substring(0, stIndexOf('.') );107. File sourcefile=new File(file.getParent(),sourcename);108.byte[] sourcedata=getFileData(sourcefile);109. String sha1Real=digestData(sourcedata);110. String content=getFileString(file,"UTF-8");111. String sha1Check=content.split(" ")[0].trim();112.if(!sha1Real.equalsIgnoreCase(sha1Check)){113. System.out.println(sourcefile.getAbsolutePath()+"的实际SHA1:"+sha1Real);114. System.out.println(file.getAbsolutePath()+"的验证SHA1:"+sha1Check);115. }116. }117. }118.119./**120. * 根据目录dir,递归所有的文件按handle方法处理121. * @param dir122. */123.public static final void showAllFiles(File dir){124. File[] fs = dir.listFiles();125.for(int i=0; i<fs.length; i++){126.if(fs[i].isDirectory()){127. showAllFiles(fs[i]);128. }129. handle(fs[i]);130. }131. }132.public static void main(String[] args) { 133. showAllFiles(new File("E:/mvnrepo0329")); 134. System.out.println("the end");135. }136.}。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Maven使用常见问题整理
1、更新eclipse的classpath加入新依赖
1、在dependencyManagement里面加入包括版本在内的依赖信息,如:
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>1.6.2</version>
</dependency>
2、在同级的dependencies节点里面加入该依赖,如:
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
3、使用mvn eclipse:clean删除当前的工程配置文件,并用mvn eclipse:eclipse重新生成。

4、导入或刷新该eclipse工程。

2、工程配置未使用pom.xml更新
问题:Project configuration is not up-to-date with pom.xml
导入maven工程后,出现如下错误:
Description Resource Path Location Type
Project configuration is not up-to-date with pom.xml. Run project configuration update rdc line 1 Maven Configuration Problem
解决办法就是:
右键项目,【Maven】--》【Update Project Configuration...】即可。

3、如何修改默认GBK的资源过滤
Using platform encoding (GBK actually) to copy filtered resources
需要指定字符集:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.2</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
4、不支持范型的JDK版本问题
maven打包时始终出现TripApplyAction.java:[267,6]-source 1.3中不支持泛型(请使用-source 5或更高版本以启用泛型)Map<String, Object> map = new HashMap<String, Object>();
解决方法,指定JDK版本:
在pom.xml文件中加入下一面一段语句来指定所用的版本,尽管在eclipse中配置了正确的“Build Path”:<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<target>1.5</target>
<source>1.5</source>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
重新运行mvn clean eclipse:eclipse -Dmaven.test.skip=true,接着mvn package,问题解决。

5、如何在使用命令方式下载依赖的同时下载源码
1. 使用maven命令:mvn dependency:sources 下载依赖包的源代码。

2. 使用参数:-DdownloadSources=true 下载源代码jar。

-DdownloadJavadocs=true 下载javadoc包。

7、maven打包,忽略test的参数
mvn -Dmaven.test.failure.ignore=true package
mvn -Dmaven.test.skip=true clean package
8、web.xml which will be ignored
[WARNING] Warning: selected war files include a WEB-INF/web.xml which will be ignored (webxml attribute is missing from war task, or ignoreWebxml attribute is specified as 'true')
在使用Maven 编译项目的时候会出现
[WARNING] Warning: selected war files include a WEB-INF/web.xml which will be ignored
解决方法:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<!-- /plugins/maven-war-plugin/ -->
<packagingExcludes>WEB-INF/web.xml</packagingExcludes>
</configuration>
</plugin>
9、Maven3.04暂时不支持Servlet3的无web.xml形式
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default-war) on project prospringmvc01: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) ->。

相关文档
最新文档