MAVEN 开发 (2) -- 问题解决&注意事项
一、常见问题与解决方法
1.idea中如何转换普通项目为maven项目?
idea中右键 add as maven project,可以变普通项目为maven项目。
2.idea中maven依赖删除之后还在显示可能是因为缓存问题?
ide》file》invalidate caches
3.如何避免依赖冲突?
每次添加了依赖都应该检查是否有jar包冲突,推荐安装插件maven helper (包依赖关系分析与排查工具)
4.maven仓库无法正常获取依赖包?
手动引入-- jar目录下面运行:
mvn install:install-file -Dfile=jar包的路径 -DgroupId=gruopId中的内容 -DartifactId=actifactId的内容 -Dversion=version的内容 -Dpackaging=jar
5.新版本组件发布后依赖方拉取依然为老代码?
检查目标仓库是否存在gav相同等未正确发布情况,是否某级仓库取到老版本缓存,删除目标包后重新拉取
也可以在maven配置中配置远程包更新和拉取频率与策略
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
</profile>
二、注意事项
1.环境变量需要对应到maven的根目录
2.在cmd中测试mvn -v需要在配置之后开启cmd,类似缓存
3.需要下载网络资源
4.默认情况下maven的本地库指向:c:\Users\admin\.m2\repository
5.配置LocalRepository时使用'\'需要转译:E:\\envrionment\\ali_repo
6.配置外网访问

注:阿里私服(代理=>maven中央库,私有资源)
7.资源定位:
所有的maven相关的资源都会存在一个唯一的中央信息片段,做资源的定位;
由三个标签组成:
8.pom资源结构和意义
remote.repositories
**.jar 使用的jar包,和自定义手动导入项目的jar包一样
**.pom:maven的核心管理文件;所有的资源都是maven转化的
**.javadoc.jar:文档,api,资源的解释内容
**.sources.jar:源码包,可以导入源码包查看源码
**. sha1 散列计算判断丢包
8.选择哪种骨架(archetype)?
针对不同的需要维护的项目,本质上也是jar资源,根据maven创建维护的目的不同
创建骨架不同的项目,调用不同的jar包来实现不同项目维护,例如:
quickstart默认普通的java项目,webapp为web应用。本地需要有相应的骨架文件
最终在指定的workspace中创了一个maven项目
9.创建项目在workspace,操作项目命令在pom.xml的文件位置
10.清空项目
mvn clean
11.eclipse中的navigator按磁盘展示项目目录,用于粘贴的时候使用
12.jar包是被人团队开发的maven项目;maven项目之间是可以传递依赖的
13.maven使用jar包标签

14.设置外网仓库地址
<repositories>
<repository>
<id>aliyun</id>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</repository>
<repository>
<id>spring</id>
<url>https://maven.aliyun.com/repository/spring</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
15.协同工作时,其他人告诉我们,他们的snapshot版本发生了变化。我们想及时更新时,可以mvn clean install -U强制更新,或者删除对应版本的jar包都可以完成新版本jar包的下载
16.定义组件发布地址
<!--版本管理:定义你的项目的(git仓库位置)-->
<scm>
<connection>scm:git:https://gitee.com/xxxx/xxxx.git</connection>
<developerConnection>scm:git:https://gitee.com/xxxx/xxxx.git</developerConnection>
<url>https://gitee.com/xxxx/xxxx</url>
</scm>
<build>
<pluginManagement>
<plugins>
<!-- release插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.1</version>
<configuration>
<mavenExecutorId>forked-path</mavenExecutorId>
<useReleaseProfile>false</useReleaseProfile>
<arguments>-Paliyun-release</arguments>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<profiles>
<!-- 定义发布到仓库的 source 和 javadoc -->
<profile>
<id>aliyun-release</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
17.使用<scope>控制依赖编译的生命周期,使用<exclude>标签排除无用或冲突依赖

浙公网安备 33010602011771号