| 
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191 | # File 'lib/buildr/core/generate.rb', line 92
def from_maven2_pom(path = 'pom.xml', root = true)
  pom = Buildr::POM.load(path)
  project = pom.project
  artifactId = project['artifactId'].first
  description = project['name'] || "The #{artifactId} project"
  project_name = File.basename(Dir.pwd)
  if root
    script = .split("\n")
    settings_file = ENV["M2_SETTINGS"] || File.join(ENV['HOME'], ".m2/settings.xml")
    settings = XmlSimple.xml_in(IO.read(settings_file)) if File.exists?(settings_file)
    if settings
      proxy = settings['proxies'].first['proxy'].find { |proxy|
        proxy["active"].nil? || proxy["active"].to_s =~ /true/
      } rescue nil
      
      if proxy
        url = %{#{proxy["protocol"].first}://#{proxy["host"].first}:#{proxy["port"].first}}
        exclude = proxy["nonProxyHosts"].to_s.gsub("|", ",") if proxy["nonProxyHosts"]
        script << "options.proxy.http = '#{url}'"
        script << "options.proxy.exclude << '#{exclude}'" if exclude
        script << ''
        
        Buildr.options.proxy.http = url
        Buildr.options.proxy.exclude << exclude if exclude
      end
    end
    repositories = project["repositories"].first["repository"].select { |repository|
      legacy = repository["layout"].to_s =~ /legacy/
      !legacy
    } rescue nil
    repositories = [{"name" => "Standard maven2 repository", "url" => "http://www.ibiblio.org/maven2/"}] if repositories.nil? || repositories.empty?
    repositories.each do |repository|
      name, url = repository["name"], repository["url"]
      script << "# #{name}"
      script << "repositories.remote << '#{url}'"
      
      Buildr.repositories.remote << url.to_s
    end
    script << ""
  else
    script = []
  end
  script << "desc '#{description}'"
  script << "define '#{project_name}' do"
  groupId = project['groupId']
  script << "  project.group = '#{groupId}'" if groupId
  version = project['version']
  script << "  project.version = '#{version}'" if version
  
  plugins = project['build'].first['plugins'].first['plugin'] rescue {}
  if plugins
    compile_plugin = plugins.find{|pl| (pl['groupId'].nil? or pl['groupId'].first == 'org.apache.maven.plugins') and pl['artifactId'].first == 'maven-compiler-plugin'}
    if compile_plugin
      source = compile_plugin.first['configuration'].first['source'] rescue nil
      target = compile_plugin.first['configuration'].first['target'] rescue nil
      script << "  compile.options.source = '#{source}'" if source
      script << "  compile.options.target = '#{target}'" if target
    end
  end
  compile_dependencies = pom.dependencies
  dependencies = compile_dependencies.sort.map{|d| "'#{d}'"}.join(', ')
  script <<  "  compile.with #{dependencies}" unless dependencies.empty?
  test_dependencies = (pom.dependencies(['test']) - compile_dependencies).reject{|d| d =~ /^junit:junit:jar:/ }
  
  use_testng = test_dependencies.find{|d| d =~ /^org.testng:testng:jar:/}
  if use_testng
    script <<  "  test.using :testng"
    test_dependencies = pom.dependencies(['test']).reject{|d| d =~ /^org.testng:testng:jar:/ }
  end
  test_dependencies = test_dependencies.sort.map{|d| "'#{d}'"}.join(', ')
  script <<  "  test.with #{test_dependencies}" unless test_dependencies.empty?
  packaging = project['packaging'] ? project['packaging'].first : 'jar'
  if %w(jar war).include?(packaging)
    script <<  "  package :#{packaging}, :id => '#{artifactId}'"
  end
  modules = project['modules'].first['module'] rescue nil
  if modules
    script << ""
    modules.each do |mod|
      script << from_maven2_pom(File.join(File.dirname(path), mod, 'pom.xml'), false).flatten.map { |line| "  " + line } << ""
    end
  end
  script << "end"
  script.flatten
end
 |