java 使用 jenkins 构建 maven 没有从本地 .m2 存储库中获取 jar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42159932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
maven build with jenkins not picking up jar from local .m2 repository
提问by Riyanka Dagaonkar
I have a jenkins job for building my maven project(say project A) which has a dependency on a jar created by another local project(say project B). The jar is present in my local .m2 repo but Jenkins fails to find it in the local repo and goes to search it in central (https://repo.maven.apache.org/maven2) which obviously it will not find and hence my job fails. I have performed the below checks before posting this question here :
我有一个 jenkins 工作来构建我的 maven 项目(比如项目 A),它依赖于另一个本地项目(比如项目 B)创建的 jar。该 jar 存在于我的本地 .m2 存储库中,但 Jenkins 未能在本地存储库中找到它,而是在中央 ( https://repo.maven.apache.org/maven2) 中搜索它,显然它找不到,因此我的工作失败了。在此处发布此问题之前,我已执行以下检查:
- The version of the artifact in the dependent and independent projects match
- The local .m2 repository of my machine has the required dependency jar version
- Wrote a separate jenkins job to build project B, it built successfully and the jar is present in the .m2 repository of Jenkins home as well the path to which is /var/lib/jenkins/.m2 (In case jenkins is trying to find the jar in its own .m2 repository)
- Changed the Jenkins"Global Tools configuration" for Maven installation from Manual (I had manually set up the maven home) to Automatic Installation(Version 3.3.9). NO luck, the issue remained the same.
- NO change has been made to the maven settings.xml.
- If I try to build Project A manually without Jenkins by using
mvn clean install
, it builds successfully.
- 依赖项目和独立项目中的工件版本匹配
- 我机器的本地 .m2 存储库具有所需的依赖 jar 版本
- 编写了一个单独的 jenkins 作业来构建项目 B,它构建成功并且 jar 存在于 Jenkins 主目录的 .m2 存储库中以及 /var/lib/jenkins/.m2 的路径(如果 jenkins 试图找到jar 在其自己的 .m2 存储库中)
- 将 Maven 安装的 Jenkins“全局工具配置”从手动(我手动设置了 Maven 主页)更改为自动安装(版本 3.3.9)。运气不好,问题还是一样。
- 没有对 maven settings.xml 进行任何更改。
- 如果我尝试在没有 Jenkins 的情况下使用 手动构建项目 A
mvn clean install
,它会成功构建。
So, Its only with Jenkins that I am not able to build the project.
所以,只有 Jenkins 我无法构建项目。
Here is my POM for Project A
这是我的项目 A 的 POM
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.akamai</groupId>
<artifactId>cobra-tracer-api</artifactId>
<version>1.0.2</version>
<packaging>war</packaging>
<name>cobra-tracer</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<finalName>cobra-tracer</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
//some dependencies here
<dependency>
<groupId>com.akamai.aps</groupId>
<artifactId>logger-service</artifactId>
<version>0.0.2</version>
</dependency>
</dependencies>
</project>
The POM for Project B is
项目 B 的 POM 是
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.akamai.aps</groupId>
<artifactId>logger-service</artifactId>
<version>0.0.3</version>
<packaging>jar</packaging>
<name>logger-service</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
采纳答案by GauravJ
The way you have integrated jenkins with maven is not correct. You have to use either artifactoryor nexus, both of them have free versions that are sufficiently good to use. You build your project B and deploy them to above repository manager and your project A downloads it from repository manager.
您将 jenkins 与 maven 集成的方式不正确。您必须使用artifactory或nexus,它们都有足够好用的免费版本。您构建您的项目 B 并将它们部署到上面的存储库管理器,您的项目 A 从存储库管理器下载它。
For your specific problem, check
对于您的具体问题,请检查
- In your project configuration, private repository optionis checked. This will create separate repository for each jenkins executor. If your executor in jenkins master are different for A and B, you will not see your jar files.
- In your setup, you can't add any slaves. All build should run on master. I hope you are not running them in separate slaves.
- I am assuming that you have run your project 'B' using
mvn install
. In your project 'A' run maven command using -U flag.mvn -U install
. - Look at your jenkins job command line. Ensure that it is not running maven with -s flag. This is a way to pass different setting.xml. Example
mvn -s /home/jenkins/setting.xml install
. There is an option to define a different settings file in jenkins project configuration. - Look at your maven installation path. It should be in jenkins-workspace/tools location. Check setting.xml inside /conf. Ensure that local repository is not defined. If jenkins is installing it, then it should not be set.
- Check if in your .m2 folder there is setting.xml file which has a different repo path.
- 在您的项目配置中,已选中私有存储库选项。这将为每个 jenkins 执行程序创建单独的存储库。如果你在 jenkins master 中的 executor 对于 A 和 B 是不同的,你将看不到你的 jar 文件。
- 在您的设置中,您不能添加任何从站。所有构建都应该在 master 上运行。我希望你不要在单独的奴隶中运行它们。
- 我假设您已经使用
mvn install
. 在您的项目“A”中,使用 -U 标志运行 maven 命令。mvn -U install
. - 查看您的 jenkins 作业命令行。确保它没有使用 -s 标志运行 maven。这是一种传递不同setting.xml 的方法。例子
mvn -s /home/jenkins/setting.xml install
。有一个选项可以在 jenkins 项目配置中定义不同的设置文件。 - 看看你的maven安装路径。它应该在 jenkins-workspace/tools 位置。检查 /conf 中的 setting.xml。确保未定义本地存储库。如果 jenkins 正在安装它,则不应设置它。
- 检查您的 .m2 文件夹中是否存在具有不同 repo 路径的 setting.xml 文件。
Let me know how it goes.
让我知道事情的后续。
Since you have added artifactory in your workflow now and facing plugin not found problem, add the following in your settings.xml
由于您现在已经在工作流程中添加了 artifactory 并且面临 plugin not found 问题,请在 settings.xml 中添加以下内容
<mirrors>
<mirror>
<mirrorOf>*</mirrorOf>
<name>remote-repos</name>
<url>http://<artifactory-ip>:<artifactory-port>/artifactory/repo</url>
<id>remote-repos</id>
</mirror>
</mirrors>
<pluginRepositories>
<pluginRepository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>plugins-release</name>
<url>http://<artifactory-id>:<artifactory-port>/artifactory/plugins-release</url>
</pluginRepository>
<pluginRepository>
<snapshots />
<id>snapshots</id>
<name>plugins-snapshot</name>
<url>http://<artifactory-id>:<artifactory-port>/artifactory/plugins-snapshot</url>
</pluginRepository>
</pluginRepositories>
I am assuming that you have already added repository tag.
我假设您已经添加了存储库标签。