Nokogiri loadError仅在Jenkins作业中

时间:2015-04-23 10:43:42

标签: ruby maven jenkins nokogiri

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

<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,当我在Jenkins之外运行Ruby脚本时,它工作正常,但是当Jenkins构建被触发时,它失败了:

  

/usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in要求':无法加载此类文件 - 来自的nokogiri(LoadError)   /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in`requiret'

我的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   
}

Jenkins无法加载Nokogiri,而在Jenkins之外这个脚本运行正常。

1 个答案:

答案 0 :(得分:0)

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

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

将上述命令添加为pre build shell命令。