【问题标题】:Nokogiri loadError only inside Jenkins jobNokogiri loadError 仅在 Jenkins 工作中
【发布时间】:2015-04-24 20:40:02
【问题描述】:

我设置了一个 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:inrequire': 无法加载此类文件 -- nokogiri (LoadError) from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.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   
}

Jenkins 无法加载 Nokogiri,而在 Jenkins 之外,此脚本可以正常工作。

【问题讨论】:

  • 您是否尝试将 rvm 添加到 PATH?类似export PATH="$PATH:$HOME/.rvm/bin"
  • ya.. 下面是 PATH /usr/local/rvm/gems/ruby-2.1.5/bin:/usr/local/rvm/gems/ruby-2.1.5@global/bin:/usr/local/rvm/rubies/ruby-2.1.5/bin
  • 我在这里看不到 rmv 路径,仅适用于 ruby​​。无论如何,nokogiri 是在哪里下载和安装的?
  • 我做了gem install nokogiri 我什至尝试了rvm gemset use global 并安装了
  • 在像 Jenkins 这样的环境下运行作业时,确保环境设置正确非常重要。确认您的路径和环境变量对于在该作业中运行的用户是正确的。

标签: ruby maven jenkins nokogiri


【解决方案1】:

我通过在 jenkins 运行作业的 shell 中再次设置环境变量来使其正常工作。

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

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

【讨论】:

    猜你喜欢
    • 2012-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    • 2013-09-27
    • 2011-12-17
    • 2015-05-14
    相关资源
    最近更新 更多