Create your own application
The purpose of this chapter is to show you how to create an application with some parameters and to see how easy it is. For simplicity, "laFab" script is used. It helps to create a laFabrique project for compiling and also to generate small pieces of script and code. Of course the use of "laFab" script is not mandatory and you can just use the jar file and write the source by hand. Now just follow the following points :
- Download the laFabrique_x.x_x.zip from the download page. Unzip it in any directory.
- Make sure that you have a JDK installed, equal or above the version 1.7, by typing :
>javac -version javac version "1.7.0_02"
- Go inside the directory of the unziped archive. Create a new laFabrique project :
>./laFab newProject MyFirstProject ~/temp [laFabrique] INFO : create all following sub-dirs at ~/temp/MyFirstProject [laFabrique] INFO : ~/temp/MyFirstProject/00_prj is created [laFabrique] INFO : ~/temp/MyFirstProject/01_src is created [laFabrique] INFO : ~/temp/MyFirstProject/02_lib is created [laFabrique] INFO : Project MyFirstProject has been created.
- Cool : ) now we have a laFabrique project. Just go inside your new project
>cd ~/temp/MyFirstProject
- Now create a new java source file inside the 01_src directory. Copy paste the following lines of code. This is your laFabrique first application.
- Compile your project by using laFab :
>./laFab build [laFabrique] INFO : Build default project. [laFabrique] INFO : Start building MyFirstProject project. [laFabrique] WARNING : Inside the project MyFirstProject, the following source package does not exist : prj [laFabrique] WARNING : prj will not be computed. [laFabrique] INFO : Version to be built is : 0.0.1 [laFabrique] INFO : start compile project MyFirstProject [laFabrique] INFO : Found 1 classes inside [01_src] [laFabrique] INFO : Compilation is successful. Classes are in 10_bin
- Your application is compiled and ready to be runned with a classic java command by setting "10_bin" directory and "lafabrique.jar" inside the classpath. Otherwise you can ask laFabrique to generate runner scripts for you.
To do so you just have to edit your project file which is "00_prj/prj/MyFirstProject.java" and add the red line of code.
package prj; import org.capcaval.lafabrique.project.Project; public class MyFirstProject extends Project{ @Override public void defineProject(){ name("MyFirstProject"); version("0.0.1"); author("CapCaval.org"); url("http://capcaval.org"); // default directories // prjDir("00_prj"); // srcDir("01_src"); // libDir("02_lib"); // prodDir("20_prod"); lib("laFabrique.jar"); // if you want a jar can be created for building //jar.name("MyFirstProject.jar"); //jar.includeSource(true); //jar.excludeDirectoryName("_test"); script.add("Calc", "Calculator"); } }
- Re-build to generate the script
> ./laFab build
[laFabrique] INFO : Build default project. [laFabrique] INFO : Start building MyFirstProject project. [laFabrique] INFO : ProjectInfo generated at : 01_src/prj/CalculatorProjectInfo.java [laFabrique] INFO : Version to be built is : 0.0.1 [laFabrique] INFO : start compile project MyFirstProject [laFabrique] INFO : Found 5 classes inside [01_src, 01_src/prj] [laFabrique] INFO : Compilation is successful. Classes are in 10_bin [laFabrique] INFO : Start creating all scripts [laFabrique] INFO : New Windows script has been created : Calc.bat [laFabrique] INFO : New Linux script has been created : ./Calc
-
Now your application is written compiled and ready to be used, just do :
>./Calc add 1 2 3
- At last, you have also available the auto-documentation provided by laFabrique by typing :
>./Calculator help ------------------------------------------------------------------------------- @@@@ @@ @@@ @@@@@@@ @@ @@@ @@ @@@ @ @@ @@@ @@ @@@ @@@@@@ @@ @@@@ @@@ @@ @@@ @@@@@@ @@@@@@ @@@@@ @@@ @@ @@@ @@@@@@@ @@ @@@@@ @@@ @@ @@@ @@@@@@@ @@@@@@ @@@@@@@ @@@@@@ @@ @@ @@ @@@ @@@ @@ @@@ @@ @@ @@ @@ @@@@ @@ .@@@@@ @@ @@ @@@ @@ @@@ @@@@@@ @@ @@@ @@@ @@@ @@@ @@@@@@@ @@ @@ @@@ @@ @@@ @@@@@@@ @@ @@@ @@@ @@@ @@@ @@ @@ @@ @@ @@@ @@@ @@@ .@@ @@ @@ @@@ @@@ @@@ @@@ @ @@ @@@ @@ @@@ .@@ @@@ @@@ .@@ @@@ @@ @@. @@ @@@ @@@@@@@ @@@@@@@ @@ @@@@@ @@@@@@@ @@@ @@@@@@@ @@@@ @@@@@@@ @@@ @@@@ @@@ @@ @@ @@@@ @@. @@ @@@ @@@ @@ @@@ .@@@. @@@ ------------------------------------------------------------------------------- Version : 0.0_1 by CapCaval.org About: describe here what your application do. ------------------------------------------------------------------------------- Properties : Commands : => add : perform addition. int -- : int -- :
import org.capcaval.lafabrique.application.ApplicationTools; import org.capcaval.lafabrique.application.annotations.AppInformation; import org.capcaval.lafabrique.common.CommandResult; import org.capcaval.lafabrique.commandline.Command; @AppInformation ( about= {"describe here what your application does.") public class Calculator{ public static void main(String[] args) { CommandResult r = ApplicationTools.runApplication(Calculator.class, args); System.out.println(r.toString()); } @Command(desc="perform addition.") public int add(int a, int b){ return a + b; } }