maven使用过程中遇到过的一些问题,或者疑问.

做~自己de王妃 提交于 2019-11-28 11:21:31

1.在Myeclipse中新建maven项目的时候无法选择Archetype(列表为空,卡死,或者抛出异常)

    描述:Myeclipse新建maven项目,Select an Archetype列表为空
    原因:缺少archetype文件
    解决思路:通过mvn命令来创建项目骨架,这时maven会自动去更新并且下载archetype文件.

mvn archetype:generate
// 首先它会去下载依赖的jar文件,
// 然后出现Generating project in interactive mode.(会等待较长时间,跟网速有关.)
// 接在它会下载最新的http://repo1.maven.org/maven2/archetype-catalog.xml
// 最后出现Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 909:
// 这是提示你创建新项目骨架,这是可以直接退出命令行,重启myeclipse创建新项目了.

    参考: http://blog.csdn.net/kingzone_2008/article/details/39621849
              http://tieba.baidu.com/p/2804486258
              http://www.cnblogs.com/yjmyzz/p/3495762.html

2.Myeclipse Maven报错 An internal error occurred during: "Retrieving archetypes:". GC overhead limit exceeded

     JVM内存太小了,重新设置一下Myeclipse的JVM内存,参考http://unixboy.iteye.com/blog/174173/

3.dependencies与dependencyManagement之间的关系

    dependencyManagement是声明本项目中可能会使用什么依赖,(可以用于版本统一管理)
    dependencies是定义项目中使用什么依赖
    当一个依赖出现在dependencyManagement中时dependencies中的定义就不需要版本号了

<!-- 声明项目用哪个版本的junit  -->
<!-- 通常这个是放在 parent项目中的 -->
<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
		</dependency>
	</dependencies>
</dependencyManagement>
<!-- 定义项目使用哪个版本的junit -->
<!-- 通常这些是定义在 子项目中的 -->
<dependencies>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<scope>test</scope>
		<!-- 建议不要设置version值, 它会默认找management中的version -->
		<!-- 如果项目有特殊差异,可以在此定义当前项目中需要的版本 -->
		<!-- <version>3.8</version> -->
	</dependency>
</dependencies>

    注意:只有依赖被定义时maven才会将依赖添加到项目中

4.定义的profiles/profile属性不生效,没有被编译到resource中

    需要为resource设置filtering为true,这样才会将profile属性编译到resouce文件中

<!-- 如果你使用了pros,就需要显式定义resource,并且设置filtering -->
<resources>
	<!-- resource 的具体情况请根据项目的路径来设定,这里只是参考 -->
	<resource>
        <!-- 主要是这个属性 -->
		<filtering>true</filtering>
		<directory>src/main/resources</directory>
        <!-- 如果不声明includes就表示目录下所有文件 -->
		<includes>
			<include>**/*</include>
		</includes>
	</resource>
</resources>

5.打包之后部分配置文件中,部分字符乱码.

    5.1.确定文件格式是utf-8
    5.2.确定配置文件显式声明为utf-8
    5.3.pom设置project.build.sourceEncoding,以及设置maven-compiler-plugin的编码方式


<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
.....
.....
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-compiler-plugin</artifactId>
	<configuration>
		<source>1.7</source>
		<target>1.7</target>
		<!--主要是这一句 -->
		<encoding>UTF-8</encoding>
	</configuration>
</plugin>

    这个问题参考地址:http://blog.csdn.net/chinazgr/article/details/48176089

6.通过maven设置不同环境的参数,

    思路,通过xml定义好不同环境的配置参数,通过命令-P或者activeByDefault的参数来定义用哪套来打包.
    6.1定义所有环境的配置参数

<!-- 可以通过mvn -P [ID 比如:mvn -PDEV install] 来使用 -->
<profiles>
	<!-- 开发环境配置参数 -->
	<profile>
		<id>DEV</id>
		<!-- 默认使用它来打包 -->
		<activation><activeByDefault>true</activeByDefault></activation>
		<properties>
			<!-- 参数配置, 标签是你自己定义的 -->
			<mvncfg.log.path><![CDATA[F:/logs]]></mvncfg.log.path>
		</properties>
	</profile>
	<!-- 测试环境配置参数 -->
	<profile>
		<id>TEST</id>
		<!-- 如果想要打包测试环境,将上面的activation注释掉,并且copy到这里 -->
		<properties>
			<!-- 日志输出路径 -->
			<mvncfg.log.path><![CDATA[F:/test/logs]]></mvncfg.log.path>
		</properties>
	</profile>
</profiles>

    6.2告诉maven哪些文件需要使用这些参数来替换占位符,或者哪些文件不需要替换占位符

<!-- 所有你需要先定义哪些是资源文件 -->
<resources>
	<resource>
		<!-- 这个是最关键的 -->
		<filtering>true</filtering>
		<directory>src/main/resources</directory>
		<includes>
			<include>**/*</include>
		</includes>
		<!-- 这个是为了排除部分不需要替换占位符,或者已有的占位符与maven占位符冲突的文件 -->
		<!-- 通常建议你避开一下maven本身自带的占位符,比如${id},这样就不需要定义排除了 -->
		<excludes>
			<exclude>config/execlude.xml</exclude>
		</excludes>
	</resource>
	<!-- execlude.xml本来是资源文件,只是不需要使用替换占位符而已 -->
	<!-- 所以这里还需要将execlude.xml声明为资源文件 -->
	<resource>
		<!-- 这个参数告诉maven不需要帮我替换占位符 -->
		<filtering>false</filtering>
		<directory>src/main/resources</directory>
		<includes>
			<include>config/execlude.xml</include>
		</includes>
	</resource>
</resources>

    6.3.使用占位符,(假设文件位置src/main/resources/properties/log4j.properties)

....
#如果打包开发者环境,他就是D:/logs/logfile.log 
#如果打包测试环境,他就是D:/logs/test/logfile.log 
log4j.appender.File.File =${mvncfg.log.path}/logfile.log  
....

    搜索备注:maven如何将配置信息打到某些包下,maven如何排除配置文件,maven的includes与excludes

7. maven中如何引用本地Jar包

    7.1 方法有两种,1自己搭建私服,2.直接使用systemPath来指定(这就是我想说的)

		<!-- 本地jar包 二维码相关工具 -->
		<dependency>
			<groupId>jp.sourceforge.qrcode</groupId>
			<artifactId>QRCode</artifactId>
			<version>1.0</version>
			<scope>system</scope>
            <!-- 在这里指定jar的路径就可以了,但是通常建议使用变量来配置,毕竟每个人的路径不一定相同 -->
			<systemPath>${localJarPath}/QRCode.jar</systemPath>
		</dependency>

 

希望能帮你解决问题
本文出处:https://my.oschina.net/longfong/blog/813146

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!