# ilib_build_jar

Build Java packages from sources into a JAR file

## Calling Sequence

                ilib_build_jar(jarFilePath, packageNames[, sourcePath[, classPath, [, manifestFilePath]]])

## Parameters

jarFilePath

  A string, sets the destination file path of the JAR.

packageNames

  A string matrix, contains the names of packages that will be built and stored the JAR.

sourcePaths

  A string matrix, sets the paths to the packages Java source directories.

classPaths

  A string matrix (optional), sets the class paths of the dependencies needed for the build.

manifestFilePath

  A string (optional), sets the file path of the manifest data to include in the JAR.

## Description

`ilib_build_jar` extends ilib_build, which purpose is to build C/C++/Fortran libraries, to Java. In other words, `ilib_build_jar` allows to build Java libraries from a set of Java sources. A library in Java is a JAR file (Java archive, extension is `.jar`), which contains Java compiled packages (a Java package contains a set of classes sharing the same purpose), and a manifest file (to describe its content).

`ilib_build_jar` works like following. All the Java sources found in the given source directory paths `sourcePaths` are compiled. Then the JAR file specified in `jarFilePath` is created from all the compiled classes of all the packages declared in `packageNames`. `ilib_build_jar` relies on the jcompile function for compilation, and jcreatejar for JAR creation, no external tool is needed.

The following Java conventions should be followed when using `ilib_build_jar`. A JAR file usually contains one package only, and its file name is often the same as the package. The Java package is usually organized in a class directory tree, and has a hierarchical naming pattern something like `org.company.software.package`, which the class directory tree follows. Each class location in the tree corresponds to its `package` declaration in the source.

`ilib_build_jar` can be used to create a JAR file with several packages, with any desired file name. The only requirement is that one package name at least must be declared, and each of the Java classes should belong to a package.

The build dependencies can be specified by setting the `classPaths` argument. It can be paths to JAR files or paths to directories containing Java compiled classes.

The file path to a manifest can be given, this one will be stored in the JAR, in the MANIFEST.MF file in META-INF folder. If the manifest file is not specified or does not exist, a default manifest will be created in the JAR. Note: if a manifest is given, it must contain a version attribute, otherwise the manifest in the JAR will be empty.

## Examples

    // Example of ilib_build_jar

    // Create org.scilab.test.mypackage package sources in TMPDIR
    packageName = 'org.scilab.test.mypackage';
    packageSrcPath = fullfile(TMPDIR, packageName);

    // Create a source file in the package (in org/scilab/test/mypackage folder)
    function addJavaSourceToPackage(className, packageName)
        packagePath = strsubst(packageName, '.', filesep());
        packagePath = fullfile(packageSrcPath, packagePath);
        mkdir(packagePath);
        filePath = fullfile(packagePath, className + '.java');

        sourceCode = [msprintf('package %s;', packageName); ..
            msprintf('public class %s {}\n', className);
            ];
        fd = mopen(filePath, 'wt');
        mputl(sourceCode, fd);
        mclose(fd);
    endfunction

    // Add 'Foo' and 'bar.Bar' classes to the package
    addJavaSourceToPackage('Foo', packageName);
    addJavaSourceToPackage('Bar', packageName + '.bar');

    // Build the package
    jarFilePath = fullfile(TMPDIR, packageName + '.jar');
    ilib_build_jar(jarFilePath, packageName, packageSrcPath);

    // Import and test the package
    javaclasspath(jarFilePath);
    jimport org.scilab.test.mypackage.Foo;
    foo = Foo.new();
    jimport org.scilab.test.mypackage.bar.Bar;
    bar = Bar.new();

## See Also

- jcompile
- jcreatejar
- javaclasspath

## History

5.5.0
Function introduced.