📜 ⬆️ ⬇️

Automating the build project in Sublime Text 2 using Ant

In the process of developing software, it is not entirely dependent on the code editor used. I succumbed to the fashion trend and decided to master the text editor Sublime Text 2, I hear too many positive reviews about it. I myself have enough of Eclipse. But over time in Eclipse, I was annoyed by slowing down the interface, and eventually I came to the conclusion that I don’t need all the Eclipse gadgets, and from Eclipse I only need syntax highlighting, project navigation, keyboard shortcuts, and working with Ant. Ant usually in projects I did a compilation or packaging of a project (here, packaging means distribution of sources according to a certain directory structure, as you need in PHP frameworks or RubyOnRails) and sending the total to a web server via FTP. When I started working with Sublime Text 2, I did not want to switch to the console to call Ant, but to use it directly in the Sublime Text 2 interface. And the solution was found.

1.Preparation


So, what we have is:

First of all, in Sublime Text 2 we open our source folder “File” -> “Open Folder ...” and save the project manifest “Project” -> “Save Project As ...”, let's say it is “helloworld.sublime-project , At the root.

2. Checking the health of an Ant call in Sublime Text 2


For example, create a file “build.xml” in the root directory of the project with the following content:
<?xml version="1.0"?> <project name="helloworld" default="test"> <target name="test"> <echo>Ant works in '${ant.project.name}' project!</echo> </target> <target name="init"> <echo>Some initial logic in '${ant.project.name}' project!</echo> </target> <target name="trial" depends="init"> <echo>Some trial logic!</echo> </target> <target name="build" depends="init"> <echo>Some build logic!</echo> </target> </project> 

and try Ant:
  1. “Tools” -> “Build System” -> “Ant”
  2. “Tools” -> “Build” or “Ctrl + B” or “F7”

and should appear at the bottom of Sublime Text 2:
 Buildfile: * \ helloworld \ build.xml
 test:
  [echo] Ant works in 'helloworld' project!
 BUILD SUCCESSFUL
 Total time: 0 seconds
 [Finished in 2.1s]

It was discovered that a situation may arise when Ant is not even called. The symptom was this: when launching “Tools” -> “Build” below, the status bar said “Building” and nothing happened. Open the Sublime console via "Ctrl +` "and see the errors:
  Running ant.bat
 Traceback (most recent call last):
  File ". \ Sublime_plugin.py", line 337, in run_
  File ". \ Exec.py", line 154, in run
  File ". \ Exec.py", line 45, in __init__
 UnicodeDecodeError: 'ascii' codec can't decode byte 0xc0 in position 9: ordinal not in range (128) 


It is necessary to replace in "exec.py" (* \ SublimeText2 \ Data \ Packages \ Default for the portable version) line:
 proc_env[k] = os.path.expandvars(v).encode(sys.getfilesystemencoding()) 
on:
 proc_env[k] = os.path.expandvars(v.decode(sys.getfilesystemencoding())).encode( sys.getfilesystemencoding() ) 

and now everything should work.
')

3. Create your Build in Sublime Text 2


In clause 2, it is demonstrated that Sublime Text 2 initially allows using only “default” target or the associated cascade of goals from “build.xml” (above it is just “test”). And, as I said earlier, I need to call the final goals of a trial and a build that are different from “default”. Since we made sure that Ant is working, we simply replace the “default” target with “test” by “build” and use the hot keys “Ctrl + B” or “F7”.
  Buildfile: * \ helloworld \ build.xml
 init:
  [echo] Some initial logic in 'helloworld' project!
 build:
  [echo] Some build logic!
 BUILD SUCCESSFUL
 Total time: 0 seconds
 [Finished in 0.4s] 

And for the purpose of “trial” I created my “Build System” using “Tools” -> “Build System” -> “New Build System ...”, and saved the file in the * \ SublimeText2 \ Data \ Packages \ User directory ( for the portable version), although the Sublime Text 2 directory will offer the correct one
 #  "Ant TRIAL.sublime-build" { "cmd": ["ant.bat", "trial"] } 

here you can also set a lot of interesting parameters.
Automatically the menu item “Tools” -> “Build System” -> “Ant TRIAL” should appear in the menu, select it and use “Ctrl + B” or “F7” to build the trial version. If you need to do the final assembly, then switch to “Tools” -> “Build System” -> “Ant” and use “Ctrl + B” or “F7”. But this is not enough for us.

4. Customizing Shortcut Keys in Sublime Text 2


Why constantly switch between “Ant TRIAL” and “Ant”, although this does not happen often in my case. Assign keyboard shortcuts “Preferences” -> “Key Bindings - User”:
 [ { "keys": ["f8"], "command": "exec", "args": {"cmd": ["ant.bat","trial"]} } ] 

This is spied in "Preferences" -> "Key Bindings - Default":
 ... { "keys": ["ctrl+break"], "command": "exec", "args": {"kill": true} } ... 

Let's go back to “Tools” -> “Build System” -> “Ant”, click “F8” and get “trial”:
  Buildfile: * \ helloworld \ build.xml
 init:
  [echo] Some initial logic in 'helloworld' project!
 trial:
  [echo] Some trial logic!
 BUILD SUCCESSFUL
 Total time: 0 seconds
 [Finished in 0.4s] 

and press “F7” and get “build”:
  Buildfile: * \ helloworld \ build.xml
 init:
  [echo] Some initial logic in 'helloworld' project!
 build:
  [echo] Some build logic!
 BUILD SUCCESSFUL
 Total time: 0 seconds
 [Finished in 1.0s] 


Conclusion


All, now we can use Ant directly in the Sublime Text 2 interface. The only nuance is that now all your projects have their “build.xml” to contain the specified targets - “trial” and “build” (or other than “build” specified in the “default” property of “project” in “build.xml”).

I think it’s not helpful to me alone to start with Sublime Text 2.

Thanks for attention.

Source: https://habr.com/ru/post/149087/


All Articles