Module: Java::Commands
- Defined in:
- lib/buildr/java/commands.rb
Overview
JDK commands: java, javac, javadoc, etc.
Class Method Summary collapse
-
.apt(*args) ⇒ Object
:call-seq: apt(*files, options).
-
.java(*args, &block) ⇒ Object
:call-seq: java(class, *args, options?).
-
.javac(*args) ⇒ Object
:call-seq: javac(*files, options).
-
.javadoc(*args) ⇒ Object
:call-seq: javadoc(*files, options).
Class Method Details
.apt(*args) ⇒ Object
:call-seq:
apt(*files, )
Runs Apt with the specified arguments.
The last argument may be a Hash with additional options:
-
:compile – If true, compile source files to class files.
-
:source – Specifies source compatibility with a given JVM release.
-
:output – Directory where to place the generated source files, or the generated class files when compiling.
-
:classpath – One or more file names, tasks or artifact specifications. These are all expanded into artifacts, and all tasks are invoked.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/buildr/java/commands.rb', line 76 def apt(*args) = Hash === args.last ? args.pop : {} , :compile, :source, :output, :classpath files = args.flatten.map(&:to_s). collect { |arg| File.directory?(arg) ? FileList["#{arg}/**/*.java"] : arg }.flatten cmd_args = [ Buildr.application..trace ? '-verbose' : '-nowarn' ] if [:compile] cmd_args << '-d' << [:output].to_s else cmd_args << '-nocompile' << '-s' << [:output].to_s end cmd_args << '-source' << [:source] if [:source] classpath = classpath_from() tools = Java.tools_jar classpath << tools if tools cmd_args << '-classpath' << classpath.join(File::PATH_SEPARATOR) unless classpath.empty? cmd_args += files unless Buildr.application..dryrun info 'Running apt' trace (['apt'] + cmd_args).join(' ') Java.load Java.com.sun.tools.apt.Main.process(cmd_args.to_java(Java.java.lang.String)) == 0 or fail 'Failed to process annotations, see errors above' end end |
.java(*args, &block) ⇒ Object
:call-seq:
java(class, *args, options?)
Runs Java with the specified arguments.
The last argument may be a Hash with additional options:
-
:classpath – One or more file names, tasks or artifact specifications. These are all expanded into artifacts, and all tasks are invoked.
-
:java_args – Any additional arguments to pass (e.g. -hotspot, -xms)
-
:properties – Hash of system properties (e.g. ‘path’=>base_dir).
-
:name – Shows this name, otherwise shows the first argument (the class name).
-
:verbose – If true, prints the command and all its argument.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/buildr/java/commands.rb', line 37 def java(*args, &block) = Hash === args.last ? args.pop : {} [:verbose] ||= Buildr.application..trace || false , :classpath, :java_args, :properties, :name, :verbose name = [:name] if name.nil? name = "java #{args.first}" end cmd_args = [path_to_bin('java')] classpath = classpath_from() cmd_args << '-classpath' << classpath.join(File::PATH_SEPARATOR) unless classpath.empty? [:properties].each { |k, v| cmd_args << "-D#{k}=#{v}" } if [:properties] cmd_args += ([:java_args] || (ENV['JAVA_OPTS'] || ENV['JAVA_OPTIONS']).to_s.split).flatten cmd_args += args.flatten.compact unless Buildr.application..dryrun info "Running #{name}" if name block = lambda { |ok, res| fail "Failed to execute #{name}, see errors above" unless ok } unless block puts cmd_args.join(' ') if Buildr.application..trace cmd_args = cmd_args.map(&:inspect).join(' ') if Util.win_os? sh(*cmd_args) do |ok, ps| block.call ok, ps end end end |
.javac(*args) ⇒ Object
:call-seq:
javac(*files, )
Runs Javac with the specified arguments.
The last argument may be a Hash with additional options:
-
:output – Target directory for all compiled class files.
-
:classpath – One or more file names, tasks or artifact specifications. These are all expanded into artifacts, and all tasks are invoked.
-
:sourcepath – Additional source paths to use.
-
:javac_args – Any additional arguments to pass (e.g. -extdirs, -encoding)
-
:name – Shows this name, otherwise shows the working directory.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/buildr/java/commands.rb', line 115 def javac(*args) = Hash === args.last ? args.pop : {} , :classpath, :sourcepath, :output, :javac_args, :name files = args.flatten.each { |f| f.invoke if f.respond_to?(:invoke) }.map(&:to_s). collect { |arg| File.directory?(arg) ? FileList["#{arg}/**/*.java"] : arg }.flatten name = [:name] || Dir.pwd cmd_args = [] classpath = classpath_from() cmd_args << '-classpath' << classpath.join(File::PATH_SEPARATOR) unless classpath.empty? cmd_args << '-sourcepath' << [:sourcepath].join(File::PATH_SEPARATOR) if [:sourcepath] cmd_args << '-d' << [:output].to_s if [:output] cmd_args += [:javac_args].flatten if [:javac_args] cmd_args += files unless Buildr.application..dryrun info "Compiling #{files.size} source files in #{name}" trace (['javac'] + cmd_args).join(' ') Java.load Java.com.sun.tools.javac.Main.compile(cmd_args.to_java(Java.java.lang.String)) == 0 or fail 'Failed to compile, see errors above' end end |
.javadoc(*args) ⇒ Object
:call-seq:
javadoc(*files, )
Runs Javadocs with the specified files and options.
This method accepts the following special options:
-
:output – The output directory
-
:classpath – Array of classpath dependencies.
-
:sourcepath – Array of sourcepaths (paths or tasks).
-
:name – Shows this name, otherwise shows the working directory.
All other options are passed to Javadoc as following:
-
true – As is, for example, :author=>true becomes -author
-
false – Prefixed, for example, :index=>false becomes -noindex
-
string – Option with value, for example, :windowtitle=>‘My project’ becomes -windowtitle “My project”
-
array – Option with set of values separated by spaces.
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 |
# File 'lib/buildr/java/commands.rb', line 155 def javadoc(*args) = Hash === args.last ? args.pop : {} cmd_args = [ '-d', [:output], Buildr.application..trace ? '-verbose' : '-quiet' ] .reject { |key, value| [:output, :name, :sourcepath, :classpath].include?(key) }. each { |key, value| value.invoke if value.respond_to?(:invoke) }. each do |key, value| case value when true, nil cmd_args << "-#{key}" when false cmd_args << "-no#{key}" when Hash value.each { |k,v| cmd_args << "-#{key}" << k.to_s << v.to_s } else cmd_args += Array(value).map { |item| ["-#{key}", item.to_s] }.flatten end end [:sourcepath, :classpath].each do |option| [option].to_a.flatten.tap do |paths| cmd_args << "-#{option}" << paths.flatten.map(&:to_s).join(File::PATH_SEPARATOR) unless paths.empty? end end cmd_args += args.flatten.uniq name = [:name] || Dir.pwd unless Buildr.application..dryrun info "Generating Javadoc for #{name}" trace (['javadoc'] + cmd_args).join(' ') Java.load Java.com.sun.tools.javadoc.Main.execute(cmd_args.to_java(Java.java.lang.String)) == 0 or fail 'Failed to generate Javadocs, see errors above' end end |