Skip to content

Commit 9f0bbda

Browse files
committed
Merge pull request opencv#8386 from jtkb:feature/cmake-maven-it-module
2 parents 19464a3 + d9231af commit 9f0bbda

File tree

11 files changed

+576
-245
lines changed

11 files changed

+576
-245
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.opencv.osgi;
2+
3+
/**
4+
* Dummy interface to allow some integration testing within OSGi implementation.
5+
*/
6+
public interface OpenCVInterface
7+
{
8+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.opencv.osgi;
2+
3+
import java.util.logging.Level;
4+
import java.util.logging.Logger;
5+
6+
/**
7+
* This class is intended to provide a convenient way to load OpenCV's native
8+
* library from the Java bundle. If Blueprint is enabled in the OSGi container
9+
* this class will be instantiated automatically and the init() method called
10+
* loading the native library.
11+
*/
12+
public class OpenCVNativeLoader implements OpenCVInterface {
13+
14+
public void init() {
15+
System.loadLibrary("opencv_java320");
16+
Logger.getLogger("org.opencv.osgi").log(Level.INFO, "Successfully loaded OpenCV native library.");
17+
}
18+
}

platforms/maven/README.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,24 @@ This page describes the how to build OpenCV using [Apache Maven](http://maven.ap
44

55
The following assumes building on Debian-based Linux platform.
66

7-
## Overview
7+
## 1 - Overview
88
The Maven build process aims to:
99
1. Provide a simpler route to build OpenCV and Java bundles.
1010
2. Automatically check the required native dependencies.
1111
3. Make the Java libraries OSGi compatible.
1212
4. Include the native OpenCV native library inside the Java bundle.
13-
5. Allow the build to function on x86, x86_64 or amd architectures, Debian-based Linux platform.
13+
5. Integration testing of the bundle within an OSGi environment.
14+
6. Allow the build to function on x86, x86_64 or amd architectures, Debian-based Linux platform.
1415

15-
### Starting the build
16-
#### Environment variables
16+
### 2 - Preparing The Build environment
17+
To build using the Maven build process both `Maven` and and up-to-date `JDK` (Java Development Kit) need to be installed. If you know you already have these installed then continue to `Environment Variable` otherwise the easiest solution is to install them using the aptitude package manager:
18+
19+
`sudo aptitude install maven default-jdk`
20+
21+
Note that installing via `aptitude` you are unlikely to get the latest version of Maven or JDK although if you are not developing Java code this shouldn't matter for this build process.
22+
23+
### 3 - Starting the build
24+
#### 3.1 - Environment variables
1725
**Applicability:** All processors.
1826

1927
The following environment variables must be set otherwise the build will fail and halt:
@@ -28,37 +36,36 @@ It is recommended that advantage is taken of multiple processor cores to reduce
2836
However if this flag is not set the build will NOT fail. On a RaspberryPi 2 typical build times are 5 hours with `-j1` (which is the default if `$MAKEFLAGS` is not specified) and a little over 2 hours with `-j4`.
2937

3038
All of the above environment variables can be set on an ad-hoc basis using 'export'.
31-
#### Build Directory
39+
#### 3.2 - Build Directory
3240
**Applicability:** All processors
3341

3442
By default the following build directories are created.
3543

3644
`<OpenCV_root_dir>/build`
3745

38-
`<OpenCV_root_dir>/build/target`
46+
`<OpenCV_root_dir>/build/maven/opencv/target`
3947

40-
Under `build` are the standard OpenCV artifacts. Under `build/target` can be found the OSGi compatible Java bundle. When deploying the bundle into an OSGi framework e.g. [Apache Karaf](http://karaf.apache.org/), loading of the native library is automatically taken care of. The standard Java library as created by the CMake process is also available as specified in the existing OpenCV documentation.
48+
`<OpenCV_root_dir>/build/maven/opencv-it/target`
49+
50+
Under `build` are the standard OpenCV artifacts. Under `build/maven/opencv/target` can be found the OSGi compatible Java bundle. When deploying the bundle into an OSGi framework e.g. [Apache Karaf](http://karaf.apache.org/), loading of the native library is automatically taken care of. An integration testing module is created under the `open-cv` directory and is only of use during the build. The standard Java library as created by the CMake process is also available as specified in the existing OpenCV documentation.
4151

4252
The Maven build is initiated from the directory contain the `pom.xml` file.
43-
#### x86 or x86_64 Architecture:
53+
#### 3.3 - x86 or x86_64 Architecture:
4454
Generally all that is required is the standard Maven command:
4555

46-
`mvn clean install`
56+
`mvn clean install -Ddownload.cmake=false`
4757

4858
One of the first things the build will do is check the required native dependencies. The Maven build indicates the status of the required dependencies and will fail at this point if any are missing. Install using the package manager e.g. aptitude or apt-get, and restart the build with the above command.
4959

5060
Once the build succesfully completes the OSGi compatible artifacts are available as described above in 'Build Directory'.
5161

52-
#### ARM 32-bit Architecture - Raspbian Distribution
62+
#### 3.4 - ARM 32-bit Architecture - Raspbian Distribution
5363
Similar to the x86 architecture the native dependencies are first checked so install any that are missing, however at the time of writing there are no official `libtbb2` and `libtbb-dev` packages in Raspbian. Version 4.4.3 of Intel's Thread Building Blocks library are available [here](http://www.javatechnics.com/thread-building-blocks-tbb-4-4-3-for-raspbian) as a Raspbian-compatible Debian packages.
5464

5565
**PLEASE NOTE THESE ARE NOT OFFICIAL RASPBIAN PACKAGES. INSTALL AT YOUR OWN RISK.**
5666

5767
OpenCV is built using CMake and the Maven build process uses the the [cmake-maven plugin](https://github.com/cmake-maven-project/cmake-maven-project). The cmake-maven plugin by default downloads CMake at build time but unfortunately there is no binary for ARM architecture currently available. As a work around it is possible to use the native CMake (which is checked for availability in the above dependency checks). Assuming all native dependencies are available the build can be started with the following command:
5868

59-
`mvn clean install -Duse.native.cmake=true`
69+
`mvn clean install -Ddownload.cmake=false`
6070

6171
Upon a successful build the libraries will be available as described above in 'Build Directory'.
62-
63-
#### cmake-mave-plugin 3.4.1-b2-SNAPSHOT
64-
Should this plugin not be available in Maven central, the source can be found at my GitHub page [here](https://github.com/jtkb/cmake-maven-project), checkout the `raspberrypi` branch and install. On x86 it is a standard Maven build and install command. If building on Raspbian you also need to supply the `-Duse.native.cmake=true` command-line option.

platforms/maven/opencv-it/pom.xml

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.opencv</groupId>
6+
<artifactId>opencv-parent</artifactId>
7+
<version>3.2.0</version>
8+
</parent>
9+
<groupId>org.opencv</groupId>
10+
<artifactId>opencv-it</artifactId>
11+
<name>OpenCV Integration Test</name>
12+
<dependencies>
13+
<dependency>
14+
<groupId>org.ops4j.pax.exam</groupId>
15+
<artifactId>pax-exam-container-karaf</artifactId>
16+
<version>${pax.exam.version}</version>
17+
<scope>test</scope>
18+
</dependency>
19+
<dependency>
20+
<groupId>org.ops4j.pax.exam</groupId>
21+
<artifactId>pax-exam-junit4</artifactId>
22+
<version>${pax.exam.version}</version>
23+
<scope>test</scope>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.apache.geronimo.specs</groupId>
27+
<artifactId>geronimo-atinject_1.0_spec</artifactId>
28+
<version>1.0</version>
29+
<scope>test</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.ops4j.pax.url</groupId>
33+
<artifactId>pax-url-aether</artifactId>
34+
<version>1.6.0</version>
35+
<scope>test</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.osgi</groupId>
39+
<artifactId>org.osgi.core</artifactId>
40+
<version>4.3.1</version>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.osgi</groupId>
45+
<artifactId>org.osgi.compendium</artifactId>
46+
<version>4.3.0</version>
47+
<scope>test</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.slf4j</groupId>
51+
<artifactId>slf4j-api</artifactId>
52+
<version>1.7.24</version>
53+
<scope>test</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.slf4j</groupId>
57+
<artifactId>slf4j-simple</artifactId>
58+
<version>1.7.24</version>
59+
<scope>test</scope>
60+
</dependency>
61+
<dependency>
62+
<artifactId>org.apache.karaf.log.core</artifactId>
63+
<groupId>org.apache.karaf.log</groupId>
64+
<version>4.0.6</version>
65+
<scope>test</scope>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.ops4j.pax.logging</groupId>
69+
<artifactId>pax-logging-api</artifactId>
70+
<version>1.8.0</version>
71+
<scope>test</scope>
72+
</dependency>
73+
<dependency>
74+
<groupId>junit</groupId>
75+
<artifactId>junit</artifactId>
76+
<version>4.8.1</version>
77+
<scope>test</scope>
78+
</dependency>
79+
<dependency>
80+
<groupId>${project.groupId}</groupId>
81+
<artifactId>opencv</artifactId>
82+
<version>${project.version}</version>
83+
<scope>test</scope>
84+
</dependency>
85+
</dependencies>
86+
<build>
87+
<directory>../../../build/maven/opencv-it/target</directory>
88+
<testSourceDirectory>src/test/java</testSourceDirectory>
89+
<testOutputDirectory>../../../build/maven/opencv-it/target</testOutputDirectory>
90+
<plugins>
91+
<plugin>
92+
<groupId>org.apache.maven.plugins</groupId>
93+
<artifactId>maven-surefire-plugin</artifactId>
94+
<version>2.15</version>
95+
</plugin>
96+
</plugins>
97+
</build>
98+
</project>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package org.opencv.osgi;
2+
3+
import java.io.File;
4+
import javax.inject.Inject;
5+
import junit.framework.TestCase;
6+
import org.apache.karaf.log.core.LogService;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.ops4j.pax.exam.Configuration;
10+
import static org.ops4j.pax.exam.CoreOptions.maven;
11+
import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
12+
import org.ops4j.pax.exam.Option;
13+
import org.ops4j.pax.exam.junit.PaxExam;
14+
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
15+
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
16+
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;
17+
import org.ops4j.pax.exam.karaf.options.LogLevelOption;
18+
import org.ops4j.pax.exam.options.MavenArtifactUrlReference;
19+
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
20+
import org.ops4j.pax.exam.spi.reactors.PerClass;
21+
import org.ops4j.pax.logging.spi.PaxLoggingEvent;
22+
import org.osgi.framework.BundleContext;
23+
24+
/**
25+
*
26+
* @author Kerry Billingham <contact@AvionicEngineers.com>
27+
*/
28+
@ExamReactorStrategy(PerClass.class)
29+
@RunWith(PaxExam.class)
30+
public class DeployOpenCVTest {
31+
32+
/*
33+
The expected string in Karaf logs when the bundle has deployed and native library loaded.
34+
*/
35+
private static final String OPEN_CV_SUCCESSFUL_LOAD_STRING = "Successfully loaded OpenCV native library.";
36+
37+
private static final String KARAF_VERSION = "4.0.6";
38+
39+
@Inject
40+
protected BundleContext bundleContext;
41+
42+
@Inject
43+
private LogService logService;
44+
45+
/*
46+
This service is required to ensure that the native library has been loaded
47+
before any test is carried out.
48+
*/
49+
@Inject
50+
private OpenCVInterface openCVInterface;
51+
52+
@Configuration
53+
public static Option[] configuration() throws Exception {
54+
MavenArtifactUrlReference karafUrl = maven()
55+
.groupId("org.apache.karaf")
56+
.artifactId("apache-karaf")
57+
.version(KARAF_VERSION)
58+
.type("tar.gz");
59+
return new Option[]{
60+
karafDistributionConfiguration()
61+
.frameworkUrl(karafUrl)
62+
.unpackDirectory(new File("../../../build/target/exam"))
63+
.useDeployFolder(false),
64+
keepRuntimeFolder(),
65+
mavenBundle()
66+
.groupId("org.opencv")
67+
.artifactId("opencv")
68+
.version("3.2.0"),
69+
logLevel(LogLevelOption.LogLevel.INFO)
70+
};
71+
}
72+
73+
/**
74+
* Tests that the OpenCV bundle has been successfully deployed and that the
75+
* native library has been loaded.
76+
*/
77+
@Test
78+
public void testOpenCVNativeLibraryLoadSuccess() {
79+
80+
Iterable<PaxLoggingEvent> loggingEvents = logService.getEvents();
81+
boolean loadSuccessful = logsContainsMessage(loggingEvents, OPEN_CV_SUCCESSFUL_LOAD_STRING);
82+
83+
TestCase.assertTrue("Could not determine if OpenCV library successfully loaded from the logs.", loadSuccessful);
84+
85+
}
86+
87+
private boolean logsContainsMessage(Iterable<PaxLoggingEvent> logEnumeration, final String logMessageString) {
88+
boolean contains = false;
89+
for (PaxLoggingEvent logEntry : logEnumeration) {
90+
if (logEntry.getMessage().contains(logMessageString)) {
91+
contains = true;
92+
break;
93+
}
94+
}
95+
return contains;
96+
}
97+
}

0 commit comments

Comments
 (0)