Search code examples
repast-simphony

Packaging a Repast model as a jar file without including the source code?


I want to create a model jar file but not include the source code. Is it possible? The Repast model installer includes the source code by default, but I would like to hide it from the recipient of the model.


Solution

  • Yes, it’s possible but there is an important caveat. Compiled Java code distributed as binary files like jars can be trivially de-compiled back into the original source code with remarkable accuracy. If your goal is to protect proprietary source code then code obfuscation is required – see https://www.excelsior-usa.com/articles/java-obfuscators.html for more details. Code obfuscation is unfortunately a fairly complicated subject.

    There are two ways to omit the project source code from the Repast model installer:

    Method 1 – Remove /src elements from the model installer configuration files. This will instruct the model installer to omit /src files in the installer jar. The compiled agent classes will be in the usual project /bin folder.

    In the /installer/installation_compnents.xml file, around line 156 comment the following:

    <!-- 
           <pack name="Sources" required="no">
                 <description>The model source code</description>
                 <file src="$StagingOptionalArea/src" targetdir="$INSTALL_PATH/Geography"/>
           </pack>
    -->
    

    The “” denote the start and end of the code block that is commented. Next, in the /installer/installation_coordinator.xml file, comment around line 62:

    <!-- Copy optional files to a separate directory -->
           <copy todir="${StagingOptionalArea}" overwrite="true" failonerror="false">
                  <fileset dir="." casesensitive="no">
    <!--                 <include name="**/src/**" /> -->              
                         <include name="**/docs/**" />
                  </fileset>
           </copy>
    

    Here you only want to comment the single line that copies the /src folder and not the /docs folder (although you can if you like). Now just build the model installer as usual.

    Method 2 – export the /src folder to a jar file. This method does not require any changes to the default installer files as in the first method. However it requires deleting the source code after exporting it to a JAR file which means you would need to work on a copy of the project to preserve your source code. To export the model code to a JAR file, right click on the /src folder and select Export… -> Java -> JAR file. In the export dialog, make sure the src folder is checked in the “Select resources to export” box and then specify the JAR file name and location. The best place to export the jar file in in the project /lib folder. Most other options should be left as default. Make sure that “Export Java source files and resources” is unchecked, otherwise it will copy the source into the jar file. After the JAR files is generated and you verify that it exists in the /lib folder, then delete the contents of the /src folder but not the /src folder itself. This will permanently delete the model source code, so again please work on a copy of the project if you take this route. Simply renaming or deleting the /src folder will cause the installer to fail, so the delete is required for this method. Last, the user_path.xml file in the .rs folder needs to be updated to reflect the change in the source code location. Change the line to assuming that the exported model JAR file is in the /lib folder. All of the model code is now in the single JAR file in your project/lib folder. The /src and /bin folder should be empty at this point. build the model installer as usual. The option to install source code will still appear in the installer, but no source is contained in the installer JAR so no source will be copied upon installation.