maven 编译强制使用本地库

今天在编译一个 maven 工程的时候发现一个问题,一个被依赖的包是自己的,在本地,但是 maven 总是试图去拉远程的 repo,例如这样

[ERROR] Failed to execute goal on project wxad.p2.stat: Could not resolve dependencies for project ....: Failed to collect dependencies at ...: Failed to read artifact descriptor for ...: Could not transfer artifact ...:${project.version} from/to central (http://maven.oa.com/nexus/content/groups/public): Illegal character in path at index 93: ... -> [Help 1]

那我们就想了,这里能不能强制让他读本地的,按照国际惯例,首先 Google 搜,看到这里 https://stackoverflow.com/ques… 提到说是 snapshot 的问题,但是我们这个应该不是 snapshot 的问题了

The dependency has a snapshot version. For snapshots, Maven will check the local repository and if the artifact found in the local repository is too old, it will attempt to find an updated one in the remote repositories. That is probably what you are seeing.

Note that this behavior is controlled by the updatePolicy directive in the repository configuration (which is daily by default for snapshot repositories).

又想到 eclipse 的 run configuration 里面有一个 offline 的选项,勾上试试呢,结果就报下面这个错,看来是非常的顽固,一定要从远程 repo 拉取

[ERROR] Failed to execute goal on project ... Could not resolve dependencies for project ... Failed to collect dependencies at ...: Failed to read artifact descriptor for ...: Cannot access central (http://maven.oa.com/nexus/content/groups/public) in offline mode and the artifact ...:pom:${project.version} has not been downloaded from it before. -> [Help 1]

继续放狗搜,看到这里 https://webcache.googleusercon… (顺便吐槽一下,现在 csdn 看一个博客帖子都要强制登录了吗,吃枣药丸啊)里面提到

笔者发现自己的${user.home}/.m2/repository 目录下的的确确也存在这个jar文件,那么为什么编译通不过呢?

后发现其里面有一个 _remote.reposiories 文件,其内容如下,

#NOTE: This is an Aether internal implementation file, its format can be changed without prior notice.
#Tue Mar 21 10:55:02 CST 2017
sqljdbc4-4.0.pom>clojars-repo=
sqljdbc4-4.0.jar>clojars-repo=

但是我的Maven的pom.xml已经把clojars-repo的repo删除掉了,是不是因为这个原因导致Maven的编译通不过呢?抱着尝试的心里,笔者删除了这个文件,再一次运行mvn clean verify, Awesome!!!! 通过了。

顺便说一下,他的 _remote.repositories 还拼错了

但是我把我这里的几个 _remote.repositories 都删除了,也还是不行,但是看报错信息,一直把 ${project.version} 的变量带着,感觉这个东西应该是要被转义的啊,于是就想是不是什么地方的这个变量没有定义清楚

并导致了 .pom.lastUpdated 这个文件和 .pom 分到了不同的目录中,那先不管是什么环节配置出错,先把这两个文件手工放在一起可以吗

答案是貌似不行。。。

那还是回到找文件配置是哪里出问题的思路上

找啊找,终于找到问题应该是在子工程中引用了父工程定义的变量,由于转义不了,于是就原样输出了

这个问题依然可以通过放狗搜找到答案,(啊,真是万能的 Google 和万能的 stackoverflow) https://stackoverflow.com/ques…

其中提到

Maven is not designed to work that way, but a workaround exists to achieve this goal (maybe with side effects, you will have to give a try). The trick is to tell the child project to find its parent via its relative path rather than its pure maven coordinates, and in addition to externalize the version number in a property :

Parent pom
com.dummy.bla
parent
${global.version} pom
0.1-SNAPSHOT
Child pom com.dummy.bla
parent
${global.version}
..

com.dummy.bla.sub
kid
I used that trick for a while for one of my project, with no specific problem, except the fact that maven logs a lot of warnings at the beginning of the build, which is not very elegant.

于是依葫芦画瓢,改了一下,但是这种写法依然不太好使

下面也说了

I used that trick for a while for one of my project, with no specific problem, except the fact that maven logs a lot of warnings at the beginning of the build, which is not very elegant.

于是就把这个 project.version 写死了,终于可以

Leave a Reply

Your email address will not be published. Required fields are marked *