MagicDraw or other modeling tool run as a simple Java program.
In order to launch the program from command line, the launcher is started using a java command line. The classpath has to include jar files from the lib folder and its subfolders (additional requirements exist for different command line implementations).
There is one mandatory property that has to be specified as well:
-Desi.system.config=data/application.conf
The property is specified as relative path to <modeling tool installation directory> therefore you either have to make your current directory before launching your program or to specify absolute paths for above listed properties.
There are two types of command lines.
- Core related batch program:
A program that does not call/depend on any functionality from any plugins. Should only depend/call core tool functionality.Must extend com.nomagic.magicdraw.commandline.CommandLine
Must contain the Java main method and implement com.nomagic.magicdraw.commandline.CommandLine#execute
- Plugin related batch program:
A program that can call/depend on any running modeling tool plugincom.nomagic.magicdraw.commandline.action
- specifies the batch mode action (implementation of com.nomagic.magicdraw.commandline.CommandLineAction) you want to execute, for example,-Dcom.nomagic.magicdraw.commandline.action=com.nomagic.magicdraw.examples.commandlineplugin.CommandLineActionExample
Must launch using com.nomagic.magicdraw.commandline.CommandLineActionLauncher
The CommandLineAction must be registered in CommandLineActionManager.getInstance().addAction(new CommandLineActionExample()); e.g. in your plugin init() method
The plugin where CommandLineAction is registered, must load and run, in order for the command line program to launch it. (this means plugin.xml and plugin .jar files must be in the /plugins directory)
The implemented examples can be found in :
- Core batch program: <modeling tool installation directory>\openapi\examples\imagegenerator
- Plugin batch program: <modeling tool installation directory>\openapi\examples\commandlineplugin
The examples of complete command lines that launches the batch mode programs:
C:\Program Files\MagicDraw>java -Xmx1200M -Xss1024K ^ -cp lib/patch.jar;lib/brand.jar;lib/brand_api.jar;lib/*;lib/graphics/*;openapi/examples/imagegenerator/imagegenerator.jar ^ -Desi.system.config=data/application.conf ^ com.nomagic.magicdraw.examples.imagegenerator.ExportDiagramImages project=project.mdzip destination_dir=out
C:\Program Files\MagicDraw>java -Xmx1200M -Xss1024K ^ -cp lib/patch.jar;lib/brand.jar;lib/brand_api.jar;lib/*;lib/graphics/* ^ -Desi.system.config=data/application.conf ^ -Dcom.nomagic.magicdraw.commandline.action=com.nomagic.magicdraw.examples.commandlineplugin.CommandLineActionExample ^ com.nomagic.magicdraw.commandline.CommandLineActionLauncher argument1 argument2
> cd MagicDraw_dir > java -Xmx1200M -Xss1024K \ -Desi.system.config=data/application.conf \ -cp lib/patch.jar\:lib/brand.jar\:lib/brand_api.jar\:lib/*\:lib/graphics/*\:openapi/examples/imagegenerator/imagegenerator.jar \ com.nomagic.magicdraw.examples.imagegenerator.ExportDiagramImages project=project.mdzip destination_dir=out
> cd MagicDraw_dir > java -Xmx1200M -Xss1024K \ -cp lib/patch.jar\:lib/brand.jar\:lib/brand_api.jar\:lib/*\:lib/graphics/* \ -Desi.system.config=data/application.conf \ -Dcom.nomagic.magicdraw.commandline.action=com.nomagic.magicdraw.examples.commandlineplugin.CommandLineActionExample \ com.nomagic.magicdraw.commandline.CommandLineActionLauncher argument1 argument2
You can modify the previous examples and create more sophisticated batch files. For example, you can define the directory to run the tool from, or specify to use the same java, as the tool uses itself. The exemplary scripts that can be used to start the command line program are shown in the following script examples. The scripts read and use the same classpath as the modeling tool when launching it manually(this is the preferred way to define classpaths).
These scripts can be launched from any directory. Can be modified to read other properties files (e.g. csm.properties, cea.properties)
A script that starts Windows Core batch command line program on Windows:
@echo off setlocal EnableExtensions :: either local script variable or environment variable MAGICDRAW_HOME should be defined set "MAGICDRAW_HOME=C:\Program Files\MagicDraw" if "%MAGICDRAW_HOME%" == "" ( echo MAGICDRAW_HOME environment variable not set, please set it to the MagicDraw installation folder exit /B 1 ) setlocal enableDelayedExpansion :: change slashes if needed set MAGICDRAW_HOME=!MAGICDRAW_HOME:\=/! setlocal disableDelayedExpansion pushd "%MAGICDRAW_HOME%" :: Reads CLASSPATH value from magicdraw.properties files (change to your properties file name) For /F "tokens=1* delims==" %%A IN (bin\magicdraw.properties) DO ( IF "%%A"=="CLASSPATH" set MD_CLASSPATH=%%B ) :: Modify classpath to a valid Windows style classpath. Replace \: with ; set MD_CLASSPATH=%MD_CLASSPATH:\:=;% java -Xmx1200M -Xss1024K ^ -cp "%MD_CLASSPATH%;openapi\examples\imagegenerator\imagegenerator.jar" ^ -Desi.system.config="data\application.conf" ^ com.nomagic.magicdraw.examples.imagegenerator.ExportDiagramImages %* popd
A script that starts Windows Plugin batch command line program on Windows:
@echo off setlocal EnableExtensions :: either local script variable or environment variable MAGICDRAW_HOME should be defined set "MAGICDRAW_HOME=C:\Program Files\MagicDraw" if "%MAGICDRAW_HOME%" == "" ( echo MAGICDRAW_HOME environment variable not set, please set it to the MagicDraw installation folder exit /B 1 ) setlocal enableDelayedExpansion :: change slashes if needed set MAGICDRAW_HOME=!MAGICDRAW_HOME:\=/! setlocal disableDelayedExpansion pushd "%MAGICDRAW_HOME%" :: Reads CLASSPATH value from magicdraw.properties files (change to your properties file name) For /F "tokens=1* delims==" %%A IN (bin\magicdraw.properties) DO ( IF "%%A"=="CLASSPATH" set MD_CLASSPATH=%%B ) :: Modify classpath to a valid Windows style classpath. Replace \: with ; set MD_CLASSPATH=%MD_CLASSPATH:\:=;% java -Xmx1200M -Xss1024K ^ -cp "%MD_CLASSPATH%" ^ -Desi.system.config="data\application.conf" ^ -Dcom.nomagic.magicdraw.commandline.action=com.nomagic.magicdraw.examples.commandlineplugin.CommandLineActionExample ^ com.nomagic.magicdraw.commandline.CommandLineActionLauncher %* popd
The shell script that starts a Core batch command line program on Mac OS-X, Linux, and Windows cygwin/msys:
#!/bin/bash # either local script variable or environment variable MAGICDRAW_HOME should be defined MAGICDRAW_HOME="/home/myuser/Desktop/MagicDraw" if [ -z "$MAGICDRAW_HOME" ]; then echo "MAGICDRAW_HOME environment variable not set, please set it to the MagicDraw installation folder" return fi cd "$MAGICDRAW_HOME" # Reads CLASSPATH value from magicdraw.properties files (change to your properties file name) MD_CLASSPATH=`grep "CLASSPATH" bin/magicdraw.properties | cut -d'=' -f 2` if [ "$OS" = Windows_NT ]; then MD_CLASSPATH=$(echo "$MD_CLASSPATH" | sed "s/\\\:/;/g") cp_delim=";" else MD_CLASSPATH=$(echo "$MD_CLASSPATH" | sed "s/\\\:/:/g") cp_delim=":" fi java -Xmx1200M -Xss1024K \ -cp "$MD_CLASSPATH${cp_delim}openapi/examples/imagegenerator/imagegenerator.jar" \ -Desi.system.config="data/application.conf" \ com.nomagic.magicdraw.examples.imagegenerator.ExportDiagramImages "$@"
The shell script that starts a Plugin batch command line program on Mac OS-X, Linux, and Windows cygwin/msys:
#!/bin/bash # either local script variable or environment variable MAGICDRAW_HOME should be defined MAGICDRAW_HOME="/home/myuser/Desktop/MagicDraw" if [ -z "$MAGICDRAW_HOME" ]; then echo "MAGICDRAW_HOME environment variable not set, please set it to the MagicDraw installation folder" return fi cd "$MAGICDRAW_HOME" # Reads CLASSPATH value from magicdraw.properties files (change to your properties file name) MD_CLASSPATH=`grep "CLASSPATH" bin/magicdraw.properties | cut -d'=' -f 2` if [ "$OS" = Windows_NT ]; then MD_CLASSPATH=$(echo "$MD_CLASSPATH" | sed "s/\\\:/;/g") else MD_CLASSPATH=$(echo "$MD_CLASSPATH" | sed "s/\\\:/:/g") fi java -Xmx1200M -Xss1024K \ -cp "$MD_CLASSPATH" \ -Desi.system.config="data/application.conf" \ -Dcom.nomagic.magicdraw.commandline.action=com.nomagic.magicdraw.examples.commandlineplugin.CommandLineActionExample \ com.nomagic.magicdraw.commandline.CommandLineActionLauncher "$@"