Nokogiri loadError仅在Jenkins作业内部



我已经设置了一个mvn-exec插件执行,它运行一个需要Nokogiri的Ruby脚本。创建Jenkins作业是为了构建Java项目。这个Ruby脚本应该在Maven构建的生成源代码阶段运行。

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<phase>generate-sources</phase>                           
<configuration>                           
<workingDirectory>src/main/resources</workingDirectory>
<arguments>                             
<argument>../../../test.rb</argument>
<argument>test.txt</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>/usr/local/rvm/rubies/ruby-2.1.5/bin/ruby</executable>                  
</configuration>
</plugin>

我在Jenkins盒子上安装了Nokogiri,当我在Jen金斯之外运行Ruby脚本时,它运行得很好,但当Jenkins构建被触发时,它失败了:

/usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:inrequire':无法从加载此类文件--nokogiri(LoadError)/usr/local/rvm/rubies/ruby-2.1.5/lib/rube/site_ruby/2.1.0/rubygems/core_ext/kernel_requiser.rb:54:in"require">

我的Ruby脚本:

require 'nokogiri'
if ARGV.size == 0 
abort("Please provide the file name as first argument!")
end
ARGV.each { |input_file|     
output_file = "_#{input_file}" 
puts "Processing --- #{input_file} ->  #{output_file}"
f = File.open(input_file)
doc = Nokogiri::XML(f)
##Move style tag to top of the body content  
style  = doc.at_css "style"
body = doc.at_css "body"  
style.parent = body
first_child = body.first_element_child
first_child.add_previous_sibling(style)
File.open(output_file, "w") do |fout|
fout.puts doc.to_html
end   
}

詹金斯无法加载Nokogiri,而在詹金斯之外,这个脚本运行良好。

我通过在jenkins运行作业的shell中再次设置环境变量来实现它。

source /usr/local/rvm/environments/ruby-2.1.5

添加了上述命令作为预构建shell命令。

最新更新