使用Maven常见问题汇总

仓库存在jar包但依旧提示无法下载

最近服务器的迁移,把原来服务器的地址都更改了,所以私服的地址也改动了,原来项目下载到本地仓库的包,但是重新构建过程中竟然发现依然要提示下载,本地仓库里面明明有包,为什么还要下载?

解决办法:
去maven仓库包下面你会发现有_remote.repositories文件,下载不下来问题就是因为这个文件导致的。你可能会发现nexus=的信息在这个文件中,这个文件中已经连接不上原来的nexus私服了,所有会报错依然会生成lastUpdatedw文件,如果你可以从新的服务地址中下载,你可以选择删除掉这个包,重新下载,如果不能的话可以选择改掉nexus=的信息,改成可以连接的地址,那它就不会重新下载了,例如改成 central=,这代表从远程中央仓

下载异常导致的错误

was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]
[ERROR] 

或许你遇到过这个错误,我看到这个错误的时候以为我配置的私服无法连接网络,其实并不是私服的问题,我删除LastUpdate文件,但是有时候好使有时候不好使,可能是因为开着eclipse的原因吧,刚删除可能它自己有请求有生成了一个。

所以采取了一个简单的方法:

mvn -U clean install

就会重新下载了,或者也可以修改配置使用私服的地方:

<profiles>
    <profile>
      ...
      <repositories>
        <repository>
          <id>myRepo</id>
          <name>My Repository</name>
          <releases>
            <enabled>false</enabled>
            <updatePolicy>always</updatePolicy>
            <checksumPolicy>warn</checksumPolicy>
          </releases>
         </repository>
      </repositories>
      ...
    </profile>
  </profiles>

清除LastUpdated脚本

上面提到如果因为网络或者其他的原因,没有下载成功或者下载被中断,将会出现一个LastUpdated的文件,这个时候,就算网络好了,也不能正常使用这个构件。可以使用脚本进行批量清除:

  1. 在linux系统上可以切换到仓库的目录下执行:
find ./  -name "*.lastUpdated" -exec grep -q "Could not transfer" {} \; -print -exec rm {} \; 
  1. 在windows系统下可以保存到后缀为bat的文件中修改仓库的目录执行:
rem 这里写你的仓库路径
set REPOSITORY_PATH=E:\storage\maven\repository
rem 正在搜索...
for /f "delims=" %%i in ('dir /b /s "%REPOSITORY_PATH%\*lastUpdated*"') do (
    del /s /q %%i
)
rem 搜索完毕
pause 

执行上面的命令后删掉本地仓库的.lastupdate文件,重新构建,必要的情况可以开vpn下载。
某些一直报错的jar包,校验MD5,不对的请删掉重新构建

错误总结

Maven项目导入eclipse中很多时候都出现报错的情况,出错的原因大致可以分为下列几种:

  1. JDK版本不一致
  2. 网络问题无法下载,可以尝试开vpn下载
  3. 私有jar包

多个WEB模块打包实现问题

在项目开发中可能会遇到这种那种的maven问题,尤其是在企业原有的开发模式转换到mavne方式进行开发的时候,以下就列出在企业项目拆分的过程中总结的maven多个子web模块的整合遇到的问题。

在各个子模块中可以添加,注意relativePath,在子模块中设置,可以在项目构建时先在本地项目中查找,使用<relativePath>../..</relativePath> ,然后在去仓库和私服查找父pom:

<parent> 
	<groupId>com.founder.fix.fixflow</groupId> 
	<artifactId>fixflow-root</artifactId> 
	<version>5.0</version> 
	<relativePath>../..</relativePath> 
</parent>

在依赖中添加对另一个web模块打成jar包的依赖,使用<type>war</type>,如下:

<dependency>
    <groupId>org.loushang.framework</groupId>
    <artifactId>loushang-framework</artifactId>
    <version>${loushang-framework.version}</version>
    <type>war</type>
</dependency>

可以声明多个依赖;

build中添加相关设置,如果不添加的话可以设置是否:

<build> 
	<finalName>lousing</finalName> 
	<plugins> 
		<!-- 合并多个war --> 
		<plugin> 
			<groupId>org.apache.maven.plugins</groupId> 
			<artifactId>maven-war-plugin</artifactId> 
			<configuration> 
				<packagingExcludes>WEB-INF/web.xml</packagingExcludes> <
				overlays> 
				<!-- 声明合并war --> 
				<overlay> 
					<groupId>com.founder.fix.fixflow</groupId> 
					<artifactId>fixflow-webapp-taskcenter</artifactId> 
					<excludes>
						<!-- 声明排除war包中的部分 --> 
						<exclude>*/web.xml,WEB-INF/lib/*</exclude> 
					</excludes> 
				</overlay> 
				</overlays> 
			</configuration> 
		</plugin>
	</plugins> 
</build>

注意在被合并的子模块没有web.xml部分,要声明:


<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
 <!—- 设置将其他文件打包到war包 -->
<webResources>
<resource>
<directory>${basedir}/../commonFiles/jsp</directory>
<!-- <directory>src/main/resources</directory> -->
<filtering>true</filtering> 
<targetPath>/</targetPath>
</resource>
</webResources>
<!-- 解决web模块中没有web.xml报错的问题 -->
<failOnMissingWebXml>false</failOnMissingWebXml> 
<!-- 解决打包时候排除空文件不打包 -->
<includeEmptyDirectories>true</includeEmptyDirectories>
</configuration>
</plugin>
</plugins>

多WEB模块打包排除jar包建议

在原有的web项目基础上拆分出了多个web项目,后台代码还是在各个web模块中,现在的需求是要合并多个web模块整合到一个war中部署。

  1. 配置建议

新建打包的web模块用以合并多个web模块项目,在pom中声明合并打包的各个模块,例如要合并A模块和B模块,如下:

<dependencies>
<dependency>
<groupId>com.inspur.test</groupId>
<artifactId>A</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
<dependency>
<groupId>com.inspur.test</groupId>
<artifactId>A</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>pom</type>
</dependency>

<dependency>
<groupId>com.inspur.test</groupId>
<artifactId>B</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
<dependency>
<groupId>com.inspur.test</groupId>
<artifactId>B</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>pom</type>
</dependency>
</dependencies>

我们需要在合并打包的项目中依赖要合并的各个war包和各个模块的pom。

排除各个模块的lib:

<build>
<finalName>B</finalName>
<plugins> <!-- 合并多个war -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<overlays>  <!-- 声明合并war -->
<overlay>
<groupId>com.inspur.test</groupId>
<artifactId>A</artifactId>
<excludes><!-- 声明排除war包中的部分 -->
<exclude>WEB-INF/lib/*</exclude>
</excludes>
</overlay>
</overlays>
<overlays>  <!-- 声明合并war -->
<overlay>
<groupId>com.inspur.test</groupId>
<artifactId>B</artifactId>
<excludes><!-- 声明排除war包中的部分 -->
<exclude>WEB-INF/lib/*</exclude>
</excludes>
</overlay>
</overlays>
</configuration>
</plugin>
</plugins>
</build>

 

直接使用JAR包方法

通过Scope使用系统本地JAR包

有一次由于私服出了问题,我又不想将jar包安装到本地仓库,所以想直接引用本地的jar包,所以就使用了接下来的设置。
使用Dependency声明:

<dependency>  
    <groupId>org.ibm</groupId>  
    <artifactId>jms</artifactId>  
    <version>1.0.0</version>  
    <scope>system</scope>  
    <systemPath>${project.basedir}/lib/jms.jar</systemPath>  
</dependency>  

使用systemPath也存在着一些问题,当我们打包的时候,如果声明路径是不能把scope为system的依赖jar包打到war包中,我觉得如果非要采用这种方式进行引用构建的话,而且需要打包到war包中,将jar包放到webapp下WEBINF下的lib文件夹中引用,这样就会将jar包构建到结果项目中了。

<systemPath>${project.basedir}/lib/jms.jar</systemPath> 

scope的作用范围有五种:

compile,缺省值,适用于所有阶段,会随着项目一起发布。
provided,类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet.jar。
runtime,只在运行时使用,如JDBC驱动,适用运行和测试阶段。
test,只在测试时使用,用于编译和运行测试代码。不会随项目发布。
system,类似provided,需要显式提供包含依赖的jar,Maven不会在Repository中查找它。

如果感兴趣可以查看下面的官网对于相关类型的解释如下:

Dependency scopeis used to limit the transitivity of a dependency, and also to affect theclasspath used for various build tasks.

There are 6scopes available:

compile
This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
provided
This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
runtime
This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
test
This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. This scope is not transitive.
system
This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.
import (only available in Maven 2.0.9 or later)
This scope is only used on a dependency of type pom in the <dependencyManagement> section. It indicates that the specified POM should be replaced with the dependencies in that POM's <dependencyManagement> section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

安装JAR包到本地仓库

安装jar包到仓库可以使用命令进行安装

mvn install:install-file –Dfile=loushang-home-6.0.RELEASE.jar –DgroupId=org.loushang.framework –DartifactId=loushang-home –Dversion=6.0.RELEASE –Dpackaging=jar

可以替换掉里面对应参数对应的值就可以通过命令安装JAR包到本地仓库了,但是这个方法仅推进为个人使用,入托团队协作的话建议使用mvn deploy的方式将开发的jar包推到远程仓库进行协作,开发中话使用-SNAPSHOT的后缀命名构件名称;
如果大家有一堆遗留的项目需要推送安装到仓库,那可以使用脚本:

python脚本批量上传jar包到私服:
from xml.etree.ElementTree import Element, SubElement, tostring,ElementTree
import glob
import os


def mvn_script(filename):
       return  "mvn deploy:deploy-file  -DrepositoryId=open2-baidu -Durl=http://10.100.10.131:8119/nexus-2.3.0-04/content/repositories/open2-baidu  -DgroupId=com.pzoom -DartifactId="+filename[0:len(filename)-4]+" -Dversion=1.0 -Dpackaging=jar  -Dfile="+filename
def writeDependency(filename):
    dependencies = Element("dependencies")
    dependency = SubElement(dependencies,"dependency")
    groupId = SubElement(dependency, "groupId")
    groupId.text = "com.pzoom"
    artifactId = SubElement(dependency, "artifactId")
    artifactId.text = filename[0:len(filename)-4]
    version = SubElement(dependency, "version")
    version.text="1.0"
    ElementTree(dependencies).write("out.xml")


files = glob.glob('*.jar')
for file in files:
    writeDependency(file)
    print(file)
    os.system(mvn_script(file))
    print('upload success')
    print('delete the file : '+file)
    os.remove(file)

跳过测试

在编译大型的项目的时候例如hadoop、ranger等项目的时候,如果执行测试可能会耗费很多时间,这时候需要跳过测试,
执行命令:

mvn install -DskipTests
#或者执行命令:
mvn install -Dmaven.test.skip=true

或者我们使用maven-surefire-plugin插件的方式配置跳过测试

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值