I'm using Maven to manage an Android project, and I've run into some trouble
trying to handle a native library dependency.

This is the general build strategy:

 1. Use SWIG to generate both sides of a JNI interface to the native library
 2. Use the Android NDK to package the C output from SWIG and the native
library into an `so` shared library
 3. Package the Java output from SWIG into an interface `jar` that other
modules of my application can include

<http://maven.40175.n5.nabble.com/file/n5752368/eo1v8.jpg> 

At the moment I'm trying to build both the `so` library and the interface
`jar` from the same project. I'm aware that this breaks the Maven convention
of 'one module, one artifact' and if there's a better way to organize the
projects then I'd love to hear it. I've chosen to break the convention
because both the `so` and the `jar` depend on the output from SWIG.


### SWIG ###

The first task is to use SWIG to generate both the Java and C components of
the JNI interface.  I'm using the `maven-antrun-plugin` to accomplish this:

    <build>
        ...
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                       <target>
                            <mkdir dir="${swig.java.directory}" />
                            <exec executable="${swig.path}">
                                <arg value="-java" />
                                <arg value="-package" />
                                <arg value="${swig.java.package}" />
                                <arg value="-outdir" />
                                <arg value="${swig.java.directory}" />
                                <arg
value="${swig.source.directory}/native.i" />
                            </exec>
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        ...
    </build>

This works well, taking my SWIG interface file `native.i` and generating the
associated Java classes and a `native_wrap.c` file.


### Native Library ###

To handle the native library creation, I've set the project packaging to
`so` and included the `android-maven-plugin`:

    <packaging>so</packaging>
    
    <build>
        ...
        <plugin>
            <groupId>com.jayway.maven.plugins.android.generation2</groupId>
            <artifactId>android-maven-plugin</artifactId>
            <extensions>true</extensions>
        </plugin>
        ...
    </build>

This also works great, and combined with my `Android.mk` file, it generates
the native `so` library.


### Jar ###

This is where I run into trouble. SWIG successfully creates the Java files
and places them in the `src/main/java` directory. However, I'm not sure how
to bundle them up into a `jar` and deploy them to the repository without
changing the project packaging. My first instinct was to use the
`maven-jar-plugin` to manually package up the Java classes:

    <build>
        ...
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
                <executions>
                <execution>
                    <id>interface</id>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <includes>
                           
<include>${project.build.outputDirectory}</include>
                        </includes>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        ...
    </build>

However, this doesn't quite give me the behavior I want.  There are two
problems:

 1. The `jar` that is created doesn't contain any of the Java classes. The
classes are being compiled, and I can see them in the target/classes
directory, but they don't appear in the `jar`.
 2. When the `jar` is deployed to my local repository, it gets renamed from
a `jar` to an `so` because of the project packaging.

Thoughts?



--
View this message in context: 
http://maven.40175.n5.nabble.com/Maven-Project-Multiple-Artifacts-for-JNI-Interface-jar-and-so-tp5752368.html
Sent from the Maven - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@maven.apache.org
For additional commands, e-mail: users-h...@maven.apache.org

Reply via email to