B4J Tutorial Using CustomBuildAction to your advantage

2016-11-11-08_20_18-compile-build_-default-png.49919


CustomBuildAction is a powerful ally.

Generally to do anything useful you need to call a custom JAR or Exe to implement logic. However if you know DOS/Batch files it is actually rather easy to implement generic logic based without passing variables.

Firstly there are two CustomBuildAction actions supported for B4J (there are more for B4A):

1 - Before the compilation steps.
2 - Before the compiled program is executed.

Secondly you can pass arguments to your target program like this;

B4X:
<step id>, <program to run>, <program arguments>

Example (from here);

B4X:
#CustomBuildAction: 1, c:\windows\system32\attrib.exe, +r res\*.* /s

However this relies on the developer providing arguments and possibly having to alter these argument for each project (for example if you want to pass the project name to the target program).

Ok, so say we have a CustomBuildAction that calls a batch file like this.

B4X:
#CustomBuildAction: 2, C:\Apps\B4J\Tools\Demo\demo.bat,

While no arguments are being passed to the batch file you can ascertain where on the file system we are by using the CD command

For example in the attached demo.bat we can do this;

B4X:
SET homedir="%cd%"

We now have a variable that contains where on the file system we are. This will be the "Objects" folder for the project.

Now that we know where we are we can determine the JAR file by looking in the objects folder for the first JAR file. Its not super scientific (what if you have two JAR files?) but will usually suffice.

B4X:
REM ***************************************
REM Get JAR file name
REM ***************************************
for %%x in (*.jar) do if not defined jarFile set jarFile=%%x
echo JAR File is %jarFile%

Now that we have the JAR file we can check some attributes on it like date time, file size etc.

B4X:
FOR %%? IN (%jarFile%) DO (
    ECHO File Name Only       : %%~n?
    ECHO File Extension       : %%~x?
    REM ECHO Name in 8.3 notation : %%~sn?
    REM ECHO File Attributes      : %%~a?
    REM ECHO Located on Drive     : %%~d?
    ECHO File Size            : %%~z?
    ECHO Last-Modified Date   : %%~t?
    REM ECHO Parent Folder        : %%~dp?
    REM ECHO Fully Qualified Path : %%~f?
    REM ECHO FQP in 8.3 notation  : %%~sf?
)

Moving on from there we can hop up one directory and determine the project file name using the same method.

Now we have the JAR file and the project file and we can implement some logic. In the attached file (demo.bat) we create a directory based on the project name and copy the JAR file to this folder (which happens to be on Dropbox). You could for example FTP the file if you wanted.

The second file (version.bat) creates an incrementing version number similar to Erels example - mine is obviously a simplified alternative.

The third file (compileonly.bat) shows how you can cancel running a build by returning an error code from the batch file using the EXIT /B 1 method.
 

Attachments

  • Demo.zip
    1.5 KB · Views: 335
  • 2016-11-11 08_20_18-Compile (Build_ Default).png
    2016-11-11 08_20_18-Compile (Build_ Default).png
    13.8 KB · Views: 905
Top