| 1 | Ruby DSL |
| 2 | ========== |
| 3 | |
| 4 | there are already ruby DSL which cover some functionality maven offers. like a [RubyGems](http://rubygems.org) is a package format which embeds meta data and dependencies about the ruby code. it is like a jar file packed with a pom. or [bundler](http://gembundler.com) deals with dependencies and how to manage them. instead of creating something new for maven the idea is to just take these existing ones use them with polyglot-maven. |
| 5 | |
| 6 | # Gemspec DSL # |
| 7 | |
| 8 | with having a gemspec as pom and you can use polyglot maven (pmaven) to run the test (spec or cucumber features), pack it and install as gem artifact. the description of that [RubyGem::Specification](http://docs.rubygems.org/read/chapter/20). a simple example looks like this: |
| 9 | |
| 10 | Gem::Specification.new do |s| |
| 11 | s.name = 'example' |
| 12 | s.version = '1.0' |
| 13 | s.summary = 'Example gem specification' |
| 14 | end |
| 15 | |
| 16 | with such gemspec in place you can setup the a local gem repository in target/rubygems |
| 17 | |
| 18 | $ pmvn initialize |
| 19 | |
| 20 | to run all test, i.e. rspec and cucumber - regular runit test is not yet supported. specs are run if there is a **rspec** dependency and features are run if there is a **cucumber** depdencency in the gemspec file. |
| 21 | |
| 22 | $ pmvn test |
| 23 | |
| 24 | or |
| 25 | |
| 26 | $ pmvn rspec:test |
| 27 | $ pmvn cucumber:test |
| 28 | |
| 29 | package the gem in target |
| 30 | |
| 31 | $ pmvn package |
| 32 | |
| 33 | or install into the local maven repository |
| 34 | |
| 35 | $ pmvn install |
| 36 | |
| 37 | and finally deploy it or push it to rubygems.org |
| 38 | |
| 39 | $ pmvn deploy |
| 40 | |
| 41 | you can also execute a ruby script in context of the gemspec file, i.e. you can use declared gem dependencies. |
| 42 | |
| 43 | $ pmvn gem:exec -Dexec.script="require 'fileutils'; FileUtils.ln_s('my.gemspec.pom', 'pom.xml')" |
| 44 | |
| 45 | similar you can start an irb session (i.e. a console) in the gemspec context |
| 46 | |
| 47 | $ pmvn gem:irb |
| 48 | |
| 49 | or as swing application |
| 50 | |
| 51 | $ pmvn gem:irb -Dirb.swing |
| 52 | |
| 53 | ## dependencies in the ruby world ## |
| 54 | |
| 55 | the common way of declaring dependencies in ruby is something like: __rspec ~>2.4.1__ which means all version bigger or equals then 2.4.1 and smaller then 2.5.0. so it is a version range and the dependency resolution varies over time depending on the availability of version for a given gem. and this can break your coed !!! bundler does have a possible solution for it - see below. |
| 56 | |
| 57 | ## getting more Maven along side the gemspec ## |
| 58 | |
| 59 | if you need more control over what pmaven does you can use the file **Mavenfile** to add more maven specific declarations. for more details see below. |
| 60 | |
| 61 | # Gemfile from Bundler as Maven Ruby DSL # |
| 62 | |
| 63 | Gemfile can declare dependencies and can group them into profiles (groups in bundler speak and profile in maven speak but with further small differences too). further it can include the dependencies of one or more gemspec files. |
| 64 | |
| 65 | one big thing about bundler is that it solves the uncertainty of version ranges. before you can "use" bunlder you need to run `bundle install` to set up the dependencies. this will write out a file _Gemfile.lock_ which contains the version resolution of the _Gemfile_. any successive run will use the versions of that _Gemfile.lock_ instead of resolve them with the latest gems in the repositories. bundler has some more commands to update and check _Gemfile_ and _Gemfile.lock_. |
| 66 | |
| 67 | pmaven just follows what bundler does in a maven way. the main difference is that with pmaven in general you do not need to run `bundle install` (only if you use gems from a git repository or from the local filesystem directly). when pmaven sees a Gemfile.lock it will obey these versions otherwise pmaven will resolve them with the version ranges from the _Gemfile_. so with pmaven to perform an **update** is to delete _Gemfile.lock_. |
| 68 | |
| 69 | in this sense _Gemfile.lock_ is part of the DSL and helps to keeps things stable and reproducable. |
| 70 | |
| 71 | the same goals work with _Gemfile_ in the same manner as with the _gemspec_ from above. |
| 72 | |
| 73 | ## Gemfile and Rails3 application ## |
| 74 | |
| 75 | the _Gemfile_ of Rails3 application will be translated to a more suitable pom as in a normal _Gemfile_ project. first the rails3-maven-plugin will be included, the packaging is war not gem as before and it allows to use jetty as development server. |
| 76 | |
| 77 | # Mavenfile # |
| 78 | |
| 79 | the name follows the *Gemfile* and you can use the same DSL as you use for Gemfile in Mavenfile. |
| 80 | |
| 81 | **NOTE:** bundler does use **ONLY** _Gemfile_ and _Gemfile.lock_ and will ignore _Mavenfile_. that _Mavenfile_ is meant for maven stuff ! |
| 82 | |
| 83 | ## gem dependencies ## |
| 84 | |
| 85 | ### NAME (required) |
| 86 | |
| 87 | For each gem requirement, list a single _gem_ line. |
| 88 | |
| 89 | gem "nokogiri" |
| 90 | |
| 91 | ### VERSION |
| 92 | |
| 93 | Each _gem_ *MAY* have one or more version specifiers. |
| 94 | |
| 95 | gem "nokogiri", ">= 1.4.2" |
| 96 | gem "RedCloth", ">= 4.1.0", "< 4.2.0" |
| 97 | |
| 98 | ## jar or test_jar dependencies ## |
| 99 | |
| 100 | ### NAME and VERSION (required) |
| 101 | |
| 102 | For each jar artifact, list a single _jar_ or _test\_jar_ line with version (range). the name is the _groupId:artifactId_ |
| 103 | |
| 104 | jar "org.slf4r:slf4r-simple", '1.6.2' |
| 105 | |
| 106 | ### SCOPE or CLASSIFIER |
| 107 | |
| 108 | jar("org.slf4r:slf4r-simple", '1.6.2').scope = :test |
| 109 | |
| 110 | jar("org.slf4r:slf4r-simple", '1.6.2').classifier = :source |
| 111 | |
| 112 | or use the nested block (this time as test_jar) |
| 113 | |
| 114 | test_jar("org.slf4r:slf4r-simple", '1.6.2') do |simple| |
| 115 | simple.scope :test |
| 116 | simple.classifier :source |
| 117 | end |
| 118 | |
| 119 | ## meta data ## |
| 120 | |
| 121 | ### GROUP\_ID, ARTIFACT\_ID, VERSION, PACKAGING |
| 122 | |
| 123 | group_id "rubygems" |
| 124 | artifact_id "one" |
| 125 | version "0.1.0" |
| 126 | packaging "gem" |
| 127 | |
| 128 | ### NAME, DESCRIPTION, URL ### |
| 129 | |
| 130 | name "some name" |
| 131 | description "longer description" |
| 132 | url "http://github.com" |
| 133 | |
| 134 | ### DEVELOPERS ### |
| 135 | |
| 136 | developers.new("my name", "[email protected]") |
| 137 | |
| 138 | or |
| 139 | |
| 140 | developers.new("my name <[email protected]>") |
| 141 | |
| 142 | ### LICENSES ### |
| 143 | |
| 144 | add the license file like |
| 145 | |
| 146 | licenses.new("MIT-LICENSE.txt") |
| 147 | |
| 148 | or the url |
| 149 | |
| 150 | licenses.new("http://www.gnu.org/licenses/gpl.html") |
| 151 | |
| 152 | ### REPOSITORIES and PLUGIN_REPOSITORIES ## |
| 153 | |
| 154 | add a repository with defaults |
| 155 | |
| 156 | repository("rubygems-releases", "http://gems.saumya.de/releases") |
| 157 | |
| 158 | or a snapshot repository for plugins |
| 159 | |
| 160 | plugin_repository("sonatype-snapshots") do |sonatype| |
| 161 | sonatype.url "http://oss.sonatype.org/content/repositories/snapshots" |
| 162 | sonatype.releases(:enabled => false) |
| 163 | sonatype.snapshots(:enabled => true) |
| 164 | end |
| 165 | |
| 166 | or a full fledge one |
| 167 | |
| 168 | repository("jboss-public-repository-group") do |jboss| |
| 169 | jboss.name "JBoss Public Maven Repository Group" |
| 170 | jboss.url "https://repository.jboss.org/nexus/content/groups/public-jboss/" |
| 171 | jboss.releases(:enabled => false, :updatePolicy => :never, :checksumPloicy => :strict) |
| 172 | jboss.snapshots(:enabled => true, :updatePolicy => :daily, :checksumPloicy => :ignore) |
| 173 | end |
| 174 | |
| 175 | you can retrieve a hashmap with repositories or plugin repositories with |
| 176 | |
| 177 | repositories |
| 178 | |
| 179 | respectively |
| 180 | |
| 181 | plugin_repositories |
| 182 | |
| 183 | |
| 184 | ### PROPERTIES ### |
| 185 | |
| 186 | it comes with a few default properties which can be overwritten |
| 187 | |
| 188 | properties["gem.home"] = "${project.build.directory}/rubygems" |
| 189 | properties["gem.path"] = "${project.build.directory}/rubygems" |
| 190 | properties["jruby.plugins.version"] = "0.27.0-SNAPSHOT" |
| 191 | properties["project.build.sourceEncoding"] = "UTF-8" |
| 192 | |
| 193 | to deal with ruby and rubygems specific stuff there are several maven plugins which are used: gem-maven-plugin, rspec-maven-plugin, cucumber-maven-plugin, etc. they are carry the version **${jruby.plugins.version}**. you an overwrite this property inside the **Mavenfile**. |
| 194 | |
| 195 | in general you can set properties like above or |
| 196 | |
| 197 | properties.merge!({ |
| 198 | "gem.home" => "~/gem" |
| 199 | }) |
| 200 | |
| 201 | or replace the existing through new ones |
| 202 | |
| 203 | properties = { "gem.home" => "~/gem" } |
| 204 | |
| 205 | ## build process ## |
| 206 | |
| 207 | in general the _build_ tag of a maven pom gets omited but is actually there. for example `plugin(name)` just delegates to `build.plugin(name)`. |
| 208 | |
| 209 | ### DEPENDENCIES, DEPENDENCY_MANAGEMENT ### |
| 210 | |
| 211 | all example are in context of the project or a profile or a plugin |
| 212 | |
| 213 | dependency_management do |deps| |
| 214 | deps.gem "slf4r", "0.4.2" |
| 215 | deps.jar "org.slf4j:slf4j-simple", "1.6.2" |
| 216 | end |
| 217 | |
| 218 | and dependencies via the collection |
| 219 | |
| 220 | dependencies do |d| |
| 221 | d.gem "rspec" |
| 222 | d.test_jar "org.slf4j:slf4j-noop", "1.6.2" |
| 223 | end |
| 224 | |
| 225 | or the same in direct way |
| 226 | |
| 227 | gem "rspec" |
| 228 | test_jar "org.slf4j:slf4j-noop", "1.6.2" |
| 229 | |
| 230 | and with exclusions |
| 231 | |
| 232 | jar 'org.xerial:sqlite-jdbc', '3.6.10' do |j| |
| 233 | j.exclude 'org.xerial.thirdparty:nestedvm' |
| 234 | end |
| 235 | |
| 236 | or the same via the exclusions collection |
| 237 | |
| 238 | jar 'org.xerial:sqlite-jdbc', '3.6.10' do |j| |
| 239 | j.exclusions << 'org.xerial.thirdparty:nestedvm' |
| 240 | end |
| 241 | |
| 242 | the latter can also be written differently. the object created can be used to call methods on it. |
| 243 | |
| 244 | sqlite = gem 'sqlite-jdbc', '3.6.10' |
| 245 | sqlite.exclusions << 'nestedvm' |
| 246 | |
| 247 | a dependency can be retrieved and call further methods on it. |
| 248 | |
| 249 | gem 'sqlite-jdbc', '3.6.10' |
| 250 | gem('sqlite-jdbc').exclusions << 'nestedvm' |
| 251 | |
| 252 | ### PLUGINS ### |
| 253 | |
| 254 | add a maven default plugin |
| 255 | |
| 256 | plugin 'release' |
| 257 | |
| 258 | or a maven default plugin with version |
| 259 | |
| 260 | plugin("clean", "2.4.1") |
| 261 | |
| 262 | or a maven default plugin with configuration |
| 263 | |
| 264 | plugin(:compile, "2.3.2").with :source => "1.5", :target => "1.5" |
| 265 | |
| 266 | also the jruby-maven-plugins can have a short name: |
| 267 | |
| 268 | * `:jruby` or `"jruby"` for jruby-maven-plugin |
| 269 | * `:gem` or `"gem"` for gem-maven-plugin |
| 270 | * `:cucumber` or `"cucumber"` for cucumber-maven-plugin |
| 271 | * `:rspec` or `"rspec"` for rspec-maven-plugin |
| 272 | * `:runit` or `"runit"` for runit-maven-plugin |
| 273 | * `:rake` or `"rake"` for rake-maven-plugin |
| 274 | * `:rails3` or `"rails3"` for rails3-maven-plugin |
| 275 | * `:rails2` or `"rails2"` for rails2-maven-plugin |
| 276 | * `:bundler` or `"bundler"` for bundler-maven-plugin |
| 277 | * `:gemify` or `"gemify"` for gemify-maven-plugin |
| 278 | |
| 279 | plugin with extentions |
| 280 | |
| 281 | plugin(:gem).extensions true |
| 282 | |
| 283 | or any other plugin with execution goals (default execution without id) |
| 284 | |
| 285 | plugin('org.codehaus.mojo:gwt-maven-plugin', '2.1.0').execution.goals = ["clean", "compile", "test"] |
| 286 | |
| 287 | or you want to execute a goal in a phase |
| 288 | |
| 289 | plugin(:gem).in_phase("pre-integration-test").execute_goal(:install) |
| 290 | |
| 291 | or a goal in a phase with configuration |
| 292 | |
| 293 | plugin(:cucumber).in_phase("integration-test").execute_goal(:test).with(:cucumberArgs => "--no-colors") |
| 294 | |
| 295 | if you can not use the short name then the plugin name is the combination of |
| 296 | _<groupId>:<artifactId>_ as with the jar dependencies. use the block notation when you need to do more with the plugin |
| 297 | |
| 298 | plugin('org.codehaus.mojo:gwt-maven-plugin', '2.1.0') do |gwt| |
| 299 | gwt.with({ :extraJvmArgs => "-Xmx512m", |
| 300 | :runTarget => "example.html" |
| 301 | }) |
| 302 | gwt.execution.goals << ["clean", "compile", "test"] |
| 303 | end |
| 304 | |
| 305 | in case the hashmap notation for the configuration part is not sufficient you can pass in XML directly (the hashmap notation does not attributes like _implementation_ in the _connector_ tag) |
| 306 | |
| 307 | plugin("org.mortbay.jetty:jetty-maven-plugin", "7.2.2.v20101205").with({ |
| 308 | :connectors => <<-XML |
| 309 | |
| 310 | <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> |
| 311 | <port>8080</port> |
| 312 | </connector> |
| 313 | <connector implementation="org.eclipse.jetty.server.ssl.SslSelectChannelConnector"> |
| 314 | <port>8443</port> |
| 315 | <keystore>${project.basedir}/src/test/resources/server.keystore</keystore> |
| 316 | <keyPassword>123456</keyPassword> |
| 317 | <password>123456</password> |
| 318 | </connector> |
| 319 | XML |
| 320 | }) |
| 321 | |
| 322 | sometimes you a nested array (here _webResources_ has a list of _resource_) |
| 323 | |
| 324 | plugin(:war).with({ |
| 325 | :webResources => Maven::Model::NamedArray.new(:resource) do |l| |
| 326 | l << { :directory => "public" } |
| 327 | l << { |
| 328 | :directory => ".", |
| 329 | :targetPath => "WEB-INF", |
| 330 | :includes => ['app/**', 'config/**', 'lib/**', 'vendor/**', 'Gemfile'] |
| 331 | } |
| 332 | l << { |
| 333 | :directory => '${gem.path}', |
| 334 | :targetPath => 'WEB-INF/gems' |
| 335 | } |
| 336 | end |
| 337 | }) |
| 338 | |
| 339 | ### EXECUTE\_IN\_PHASE ### |
| 340 | |
| 341 | you can write ruby code directly inside the _Mavenfile_ which will be executed in the specified phase |
| 342 | |
| 343 | execute_in_phase(:verify) do |
| 344 | puts "all OK" |
| 345 | end |
| 346 | |
| 347 | ## profiles ## |
| 348 | |
| 349 | profiles and groups from Gemfile are similiar but not the same. in maven you choose one or more profiles to be active and they participate with action you want to perform. the groups with bundler do all participate with dependency resolution so the resovled version are consistent over *all* groups. |
| 350 | |
| 351 | ### ACTIVATION ### |
| 352 | |
| 353 | activation of a profile by default |
| 354 | |
| 355 | profile(:development).activation.by_default |
| 356 | |
| 357 | or by property |
| 358 | |
| 359 | profile(:test).activation.property("rails.env", "test") |
| 360 | |
| 361 | or via OS (triggered by `family` or `arch` or `name` or `version`) |
| 362 | |
| 363 | profile("mac").activation.os.family = "mac" |
| 364 | |
| 365 | ### PLUGIN, PROPERTIES, DEPENDENCY, DEPENDENCY_MANAGEMENT ### |
| 366 | |
| 367 | any of these nested attributes can set with the help of the block notation |
| 368 | |
| 369 | profile(:production) do |prod| |
| 370 | prod.activation.property("rails.env", "production") |
| 371 | prod.properties.merge!({ |
| 372 | "gem.home" => "${project.build.directory}/rubygems-production", |
| 373 | "gem.path" => "${project.build.directory}/rubygems-production" |
| 374 | }) |
| 375 | prod.plugin("org.mortbay.jetty:jetty-maven-plugin", "${jetty.version}") |
| 376 | prod.gem "cucumber", nil |
| 377 | prod.dependency_management.gem "cucumber", '0.9.4' |
| 378 | end |
| 379 | |
| 380 | ### GEM, JAR ### |
| 381 | |
| 382 | the following group notation will add the gem and the jar to the "test" profile: |
| 383 | |
| 384 | profile :test do |
| 385 | gem "enforce-ssl" |
| 386 | jar "org.slf4j:slf4j-noop", "1.6.2" |
| 387 | end |
| 388 | |
| 389 | # missing parts from maven # |
| 390 | |
| 391 | there are still missing parts, like the *parent* tag or the *modules* or all this report related tags. |