Jenkins Configuration(2)Build Enviroment with maven/ivy

本文介绍如何使用Jenkins配合Maven和Ivy进行项目的自动化构建,包括配置文件详解、常见命令使用及部署到Artifactory的方法。同时分享了一些实用的Jenkins配置技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Jenkins Configuration(2)Build Enviroment with maven/ivy

1. Some Linux Commands
1.1 tee
append the console output to file

-a append the file

eg:

>echo " " | tee -a $BUILD_HOME/logs/all.log


1.2 cp
cp file and directory

-f --force

-p --preserve keep the information of the file and directory

-r do the same things to the sub directory

eg:

>cp -pfr directory1 directory2


2. Build the Project with Maven
I should have this kind of confirmation in my pom.xml.

<distributionManagement>
<repository>
<id>central</id>
<name>libs-snapshot-local</name>
<url>http://some.server.com/artifactory/libs-snapshot-local</url>
</repository>
</distributionManagement>

And set the username and password in settings.xml

<servers>

<server>

<id>central</id>

<username>builder</username>

<password>password</password>

</server>

<server>

<id>snapshots</id>

<username>builder</username>

<password>password</password>

</server>

</servers>


When I execute the maven command as follow:

>clean source:jar package deploy:deploy --update-snapshots


The package will deploy to my artifactory.

Uploaded: http://some.server.com/artifactory/libs-snapshot-local/com/sillycat/1.0.1-SNAPSHOT/somclass.jar


3. Build the Project with Ivy
Here are the configuration in build.xml:

<propertyname="ivy.install.version"value="2.2.0"/>
<propertyname="ivy.home"value="${user.home}/.ant"/>
<propertyname="ivy.jar.dir"value="${ivy.home}/lib"/>
<propertyname="ivy.jar.file"value="${ivy.jar.dir}/ivy.jar"/>

<ivy:settingsfile="${store.dir}/ivy.settings.xml"/>
<ivy:resolvefile="${store.dir}/ivy.xml"/>

<taskdefresource="org/apache/ivy/ant/antlib.xml"uri="antlib:org.apache.ivy.ant"classpath="${ivy.jar.file}"/>
<targetname="download-ivy"]]>
<mkdirdir="${ivy.jar.dir}"/>
<getsrc="http://repo1.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar"dest="${ivy.jar.file}"usetimestamp="true"/>
</target]]>


<!-- ================================= target: resolve ================================= -->
<targetname="resolve"depends="download-ivy"description="--> retreive dependencies with ivy"]]>
<ivy:retrievepattern="${deploy.dir}/lib/[artifact]-[revision].[ext]"type="jar,bundle"sync="true"/>
</target]]>


<!-- ================================= target: clean-cache ================================= -->
<targetname="clean-cache-lib"description="--> clean the ivy cache"]]>
<deletedir="${ivy.home}/cache"/>
</target]]>

<targetname="publish"depends="download-ivy"description="publish package to artifactory"]]>
<ivy:publishresolver="sillycat-snapshots"forcedeliver="true"overwrite="true"publishivy="false"]]>
<artifactspattern="${deploy.dir}/[artifact].[ext]"/>
</ivy:publish]]>
</target]]>

I can run the command to manage the jar package

>ant resolve


I can run the command to push my release package to artifactory

>ant publish


Here are the configuration in ivy.settings.xml:

<ivysettings]]>

<settingsdefaultResolver="main"/>
<credentialshost="repository.sillycat.com"realm="ArtifactoryRealm"username="builder"passwd="password"/>
<propertyname="sillycat-snapshots"value="http://repository.sillycat.com/artifactory/libs-snapshot-local"/>

<resolvers]]>

<chain name="main"returnFirst="true"]]>
<filesystem name="libraries"]]>
<artifact pattern="${ivy.conf.dir}/lib/[artifact]-[revision].[type]"/>
</filesystem]]>
<ibiblio name="ibiblio" m2compatible="true"/>

<ibiblio name="java-net-maven1"root="http://download.java.net/maven/1"pattern="${java.net.maven.pattern}"m2compatible="false"/>
<ibiblio name="java-net-maven2"root="http://download.java.net/maven/2/"m2compatible="true"/>
<ibiblio name="sillycat-snapshots"m2compatible="true"root="${sillycat-snapshots}"/>
</chain]]>

</resolvers]]>

</ivysettings]]>


And my ivy.xml will be like this:

<ivy-module version="2.0"]]>

<info organisation="com.sillycat.stores" module="storename" revision="1.0.0-SNAPSHOT"/>

<configurations]]>
<conf name="sillycat-snapshots"/>
<conf name="compile"/>
<conf name="runtime" extends="compile"/>
</configurations]]>

<publications]]>
<artifact type="war" ext="war"conf="sillycat-snapshots"/>
</publications]]>
<dependencies]]>
<dependencyorg="commons-lang"name="commons-lang"rev="2.4"conf="runtime->default"/>
<dependencyorg="net.sf.dozer"name="dozer"rev="5.2.1"conf="runtime->default"]]>
<excludeorg="commons-logging"name="commons-logging"/>
<excludeorg="org.springframework"name="spring"/>
<excludeorg="commons-beanutils"name="commons-beanutils"/>
</dependency]]>
<dependencyorg="org.freemarker"name="freemarker"rev="2.3.15"conf="runtime->default"/>
<dependencyorg="ognl"name="ognl"rev="2.7.3"conf="runtime->default"]]>
<excludeorg="jboss"name="javassist"/>
</dependency]]>
<dependencyorg="org.hamcrest"name="hamcrest-core"rev="1.1"]]>
<excludeorg="jmock"name="jmock"/>
<excludeorg="org.easymock"name="easymock"/>
<excludeorg="junit"name="junit"/>
</dependency]]>
</dependencies]]>
</ivy-module]]>


Every thing works fine on my local machine. But on the server I got this error message.

Error Message:
Problem: failed to create task or type antlib:org.apache.ivy.ant:settings
Cause: The name is undefined.

Action: Check the spelling.

Action: Check that any custom tasks/types have been declared.

Action: Check that any <presetdef>/<macrodef> declarations have taken place.

No types or tasks have been defined in this namespace yet


Solution:
>wget http://archive.apache.org/dist/ant/ivy/2.2.0/apache-ivy-2.2.0-bin.tar.gz

>tar zxvf apache-ivy-2.2.0-bin.tar.gz

>cd apache-ivy-2.2.0

>cp ./ivy-2.2.0.jar /opt/apache-ant-1.8.1/lib/

It is working now.


When you want to resolve the war package from artifactory. Do like this:

<dependency org="com.sillycat.stores" name="storename" rev="1.0.0-SNAPSHOT"conf="runtime->default"]]>
<artifact name="storename" type="war"/>
</dependency]]>


And change the build.xml statements like this:

<target name="resolve" depends="download-ivy" description="--> retreive dependencies with ivy"]]>
<ivy:retrieve pattern="${basedir}/war/[artifact]-[revision].[ext]" type="war,jar,bundle"sync="true"/>
</target]]>


4. Some Tips in Jenkins
Poll SCM using this pattern * * * * *


Execute the maven command base on this >clean source:jar package deploy:deploy --update-snapshots


Trigger another project base on this configuration:

Post-build Actions---> Build other projects ----> Trigger only if build succeeds


Only get the dependency from this maven command >clean dependency:copy-dependencies


Execute the shell script here:

Build ----> Execute shell --->

Command ----> chmod 755 * .; ./some-shell.sh


references:
http://hi.baidu.com/hongszh/item/ae48d648ea62190be93504ee

http://www.2cto.com/os/201112/113221.html

http://www.ningoo.net/html/2007/shell_scripts_stderr_stdout.html


ivy

https://support.sonatype.com/entries/21612333-how-do-i-configure-my-ivy-build-to-publish-artifacts-to-nexus

http://sillycat.iteye.com/blog/564051

http://sillycat.iteye.com/blog/611299


http://www.draconianoverlord.com/2010/07/18/publishing-to-maven-repos-with-ivy.html
<think>好的,我现在需要帮助用户解决Jenkins构建过程中出现的Maven找不到Java路径的问题。用户提到错误信息是“maven build failed java not found exec jenkins_home java bin java”,看起来和Java环境配置有关。首先,我应该回顾用户提供的引用内容,看看有没有相关的解决方案。 根据引用[1],用户需要检查Jenkins服务器是否安装了Maven,是否安装了Maven插件,以及全局工具配置是否正确。不过用户的问题是关于Java路径的,所以可能更相关的是引用[2]、[3]、[5]。引用[2]和[3]指出JAVA_HOME可能指向了JRE而不是JDK,或者没有正确设置。引用[5]提到JDK版本问题,替换成特定版本解决了问题。 首先,我应该确认用户是否正确安装了JDK,而不仅仅是JRE。然后检查Jenkins的全局工具配置中JDK的路径是否正确。另外,可能涉及到环境变量的问题,比如在Jenkins的构建环境中是否传递了正确的JAVA_HOME。还有可能是Jenkins用户没有权限访问JDK目录,或者JDK安装路径有空格或特殊字符导致的问题。 接下来,用户提供的引用[4]提到在Jenkins服务器上安装了Maven和Docker,但问题出在Java路径。可能需要确认JDK的安装路径是否被正确配置到Jenkins的全局工具中,或者在构建步骤中是否指定了正确的JDK版本。 可能的解决步骤包括: 1. 检查服务器上JDK是否安装,确认路径。 2. 检查Jenkins的全局工具配置中的JDK路径是否正确。 3. 确保JAVA_HOME环境变量指向JDK而不是JRE。 4. 检查构建脚本中是否覆盖了JAVA_HOME或使用了错误的路径。 5. 查看具体的构建日志,确认错误详细信息。 6. 如果有版本问题,考虑更换JDK版本,如引用[5]提到的替换为jdk-8u231。 另外,用户提到的错误信息中有“exec: /var/jenkins_home/jdk1.8.0_271/bin/java”,可能该路径不存在或权限不足。需要验证该路径是否正确,以及Jenkins进程是否有权限访问该目录。 可能还需要检查Maven的配置,比如settings.xml中是否指定了正确的Java路径,或者在pom.xml中有无相关配置错误。不过更可能的是全局环境变量或Jenkins配置的问题。 总结一下,解决思路应该围绕JDK的安装、环境变量配置、Jenkins全局工具设置以及权限问题展开,逐步排查每个可能性。</think>### 解决Jenkins构建过程中Maven找不到Java路径的步骤 #### 1. **确认JDK是否正确安装** - **检查服务器是否安装JDK** 登录Jenkins服务器,执行以下命令确认JDK安装路径: ```bash which java # 查看默认Java路径 java -version # 检查Java版本 ``` 如果输出显示为JRE(例如路径包含`jre`),需重新安装JDK。 引用[^2][^3]:`JAVA_HOME`必须指向JDK而非JRE。 - **手动指定JDK路径** 若已安装JDK但未正确配置,记录其安装路径(如`/usr/lib/jvm/jdk1.8.0_231`)。 #### 2. **配置Jenkins全局工具** - **进入全局工具配置** 在Jenkins界面依次点击: `Manage Jenkins` → `Tools` → `JDK installations` → `Add JDK`。 - **指定JDK路径** 取消勾选`Install automatically`,手动填写JDK安装路径(例如`/usr/lib/jvm/jdk1.8.0_231`)。 ![JDK配置示例](https://via.placeholder.com/600x200?text=JDK+Configuration+in+Jenkins) #### 3. **检查环境变量`JAVA_HOME`** - **在Jenkins服务器上验证环境变量** 执行以下命令: ```bash echo $JAVA_HOME ``` 如果输出为空或指向JRE,需在`/etc/profile`或用户配置文件(如`~/.bashrc`)中添加: ```bash export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_231 export PATH=$JAVA_HOME/bin:$PATH ``` 执行`source ~/.bashrc`使配置生效。 - **在Jenkins构建任务中传递环境变量** 在任务配置页面的`Build Environment`中勾选`Pass environment variables to the build`,并添加: ``` JAVA_HOME=/usr/lib/jvm/jdk1.8.0_231 ``` #### 4. **检查JDK版本兼容性** - **替换低版本或损坏的JDK** 若错误提示涉及JDK版本(如引用[^5]中的`jdk1.8.0_271`问题),需下载兼容版本(如`jdk-8u231-linux-x64.tar.gz`)并更新路径。 #### 5. **验证文件权限** - **确保Jenkins用户有权访问JDK目录** 执行以下命令授权: ```bash chown -R jenkins:jenkins /usr/lib/jvm/jdk1.8.0_231 ``` #### 6. **检查Maven配置** - **确认Maven的`settings.xml`配置** 在`/var/jenkins_home/apache-maven-3.6.3/conf/settings.xml`中检查是否包含正确的JDK路径,例如: ```xml <profile> <id>jdk-1.8</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <java.home>/usr/lib/jvm/jdk1.8.0_231</java.home> </properties> </profile> ``` #### 7. **查看构建日志定位具体错误** - **分析日志中的路径提示** 如果日志显示`exec: /var/jenkins_home/jdk1.8.0_271/bin/java`,需检查该路径是否存在。若不存在,需修正为实际的JDK路径。 --- ### 完整修复流程示例 1. 下载JDK 8u231并解压到`/usr/lib/jvm/jdk1.8.0_231`。 2. 在Jenkins全局工具配置中指定该路径。 3. 在构建任务中注入`JAVA_HOME`变量。 4. 执行构建命令`mvn clean package -DskipTests`。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值