Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JUnit测试的本机代码未使用'nar maven plugin'编译`_Java_C++_Maven_Java Native Interface_Maven Nar Plugin - Fatal编程技术网

Java JUnit测试的本机代码未使用'nar maven plugin'编译`

Java JUnit测试的本机代码未使用'nar maven plugin'编译`,java,c++,maven,java-native-interface,maven-nar-plugin,Java,C++,Maven,Java Native Interface,Maven Nar Plugin,我有一个JNI项目,使用narmaven插件配置了Maven。java和C++代码都驻留在项目中。主代码显然编译正确(C++和java)。问题在于测试代码(JUnit) 测试代码定义了一个本身具有本机方法的Java类。相应的本机代码驻留在目录中 <project root> +- src +- test +- c++ 有没有已知的方法来处理这个问题?(可能是额外的配置标签?我让它工作了,但它并不漂亮。我将在最后给出POM部分,但概括起来,以下是步骤: 按照编译主

我有一个JNI项目,使用
narmaven插件
配置了Maven。java和C++代码都驻留在项目中。主代码显然编译正确(C++和java)。问题在于测试代码(JUnit)

测试代码定义了一个本身具有本机方法的Java类。相应的本机代码驻留在目录中

<project root>
+- src
   +- test
      +- c++

有没有已知的方法来处理这个问题?(可能是额外的配置标签?

我让它工作了,但它并不漂亮。我将在最后给出POM部分,但概括起来,以下是步骤:

  • 按照编译主代码的问题设置项目
  • 使用nar maven plugin上的
    部分编译本机测试代码,注意这实际上是为了为本机测试创建可执行文件而不是创建DLL/SO支持Java测试
  • 为链接器指定
    ,使其成为DLL/SO而不是可执行文件
  • 进一步指定
    以使您的测试代码链接到主项目代码,因为当您选择
    jni
    (而不是
    shared
    static
    )库类型时,不会自动支持此操作
  • 将测试库移动到测试之前的路径中,并在测试之后将其删除
更详细地展开最后一项:即使您获得了要编译的测试代码,您也无法从Java加载它,因为测试运行时测试目录不在
Java.library.path
上!我能找到的最佳解决方案是将测试库临时移动到路径上的目录中。这似乎比更改路径更容易,因为配置选项很容易获得。如果您在路径上获得了库,那么您可以在JUnit测试执行期间像往常一样使用
System.loadLibrary

下面是实现上述功能的扩展POM部分。这是基于我在问题中所做的,但是使用新的片段需要完成答案开头的项目。请注意,支持测试的JNI代码位于
/src/test/c++
中的文件
TestWrapper.cpp
中。(我知道这不是JNI源文件的标准命名约定。)

注意:此时,我只计算了用于在我的Windows 10计算机上测试的链接器标志,它在
配置文件
部分中表示。同样,copy/delete具有显式的
.dll
扩展名,需要对其进行调整。(进一步注意,尽管你得到了一个
.dll
文件,但当插件成功的时候,它会有一个
.exe
扩展!)我的POM无疑在这一点上对其他机器/架构来说是失败的,但是从这里开始让它们工作有一条清晰的道路,所以现在就公布答案似乎是值得的

<profiles>
    <profile>
        <id>Windows-MinGW</id>
        <activation>
            <os>
                <family>Windows</family>
            </os>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>com.github.maven-nar</groupId>
                    <artifactId>nar-maven-plugin</artifactId>
                    <version>3.5.1</version>
                    <extensions>true</extensions>
                    <configuration>
                        <cpp>
                            <options>
                                <option>-std=c++1y</option>
                            </options>
                        </cpp>
                        <linker>
                            <name>g++</name>
                            <options>
                                <option>-Wl,--kill-at</option>
                            </options>
                            <testOptions>
                                <!-- Put the -shared flag onto the linker - That will force a DLL instead of an EXE -->
                                <testOption>-shared</testOption>
                                <!-- We cannot easily link to the *library* that was created for the main project but we can get the compiled object files with the following option -->
                                <testOption>${project.build.directory}/nar/obj/${nar.aol}/*.o</testOption>
                            </testOptions>
                        </linker>
                    </configuration>
                </plugin>                   
            </plugins>
        </build>
    </profile>
</profiles>

<build>
    <defaultGoal>integration-test</defaultGoal>

    <plugins>
        <plugin>
            <groupId>com.github.maven-nar</groupId>
            <artifactId>nar-maven-plugin</artifactId>
            <version>3.5.1</version>
            <extensions>true</extensions>
            <configuration>
                <cpp>
                    <defines>
                        <define>EXPORT_DLL</define>
                    </defines>
                </cpp>
                <libraries>
                    <library>
                        <type>jni</type>
                        <narSystemPackage>com.mycompany.mypackage</narSystemPackage>
                    </library>
                </libraries>
                <tests>
                    <test>
                        <name>TestWrapper</name>
                        <run>false</run>
                    </test>
                </tests>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <id>copy-test-lib-to-path</id>
                    <phase>pre-integration-test</phase>
                    <configuration>
                        <target>
                            <copy file="${project.build.directory}/test-nar/bin/${nar.aol}/TestWrapper.exe" tofile="${project.build.directory}/nar/${project.artifactId}-${project.version}-${nar.aol}-jni/lib/${nar.aol}/jni/TestWrapper.dll"/>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
                <execution>
                    <id>delete-test-lib-from-deployment</id>
                    <phase>post-integration-test</phase>
                    <configuration>
                        <target>
                            <delete file="${project.build.directory}/nar/${project.artifactId}-${project.version}-${nar.aol}-jni/lib/${nar.aol}/jni/TestWrapper.dll"/>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>

窗明
窗户
com.github.maven-nar
nar maven插件
3.5.1
真的
-std=c++1y
g++
-Wl,--杀死
-共享
${project.build.directory}/nar/obj/${nar.aol}/*.o
集成测试
com.github.maven-nar
nar maven插件
3.5.1
真的
导出动态链接库
jni
com.mycompany.mypackage
测试包装器
假的
org.apache.maven.plugins
maven antrun插件
1.7
将测试库复制到路径
预集成测试
跑
从部署中删除测试库
整合后测试
跑
<profiles>
    <profile>
        <id>Windows-MinGW</id>
        <activation>
            <os>
                <family>Windows</family>
            </os>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>com.github.maven-nar</groupId>
                    <artifactId>nar-maven-plugin</artifactId>
                    <version>3.5.1</version>
                    <extensions>true</extensions>
                    <configuration>
                        <cpp>
                            <options>
                                <option>-std=c++1y</option>
                            </options>
                        </cpp>
                        <linker>
                            <name>g++</name>
                            <options>
                                <option>-Wl,--kill-at</option>
                            </options>
                            <testOptions>
                                <!-- Put the -shared flag onto the linker - That will force a DLL instead of an EXE -->
                                <testOption>-shared</testOption>
                                <!-- We cannot easily link to the *library* that was created for the main project but we can get the compiled object files with the following option -->
                                <testOption>${project.build.directory}/nar/obj/${nar.aol}/*.o</testOption>
                            </testOptions>
                        </linker>
                    </configuration>
                </plugin>                   
            </plugins>
        </build>
    </profile>
</profiles>

<build>
    <defaultGoal>integration-test</defaultGoal>

    <plugins>
        <plugin>
            <groupId>com.github.maven-nar</groupId>
            <artifactId>nar-maven-plugin</artifactId>
            <version>3.5.1</version>
            <extensions>true</extensions>
            <configuration>
                <cpp>
                    <defines>
                        <define>EXPORT_DLL</define>
                    </defines>
                </cpp>
                <libraries>
                    <library>
                        <type>jni</type>
                        <narSystemPackage>com.mycompany.mypackage</narSystemPackage>
                    </library>
                </libraries>
                <tests>
                    <test>
                        <name>TestWrapper</name>
                        <run>false</run>
                    </test>
                </tests>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <id>copy-test-lib-to-path</id>
                    <phase>pre-integration-test</phase>
                    <configuration>
                        <target>
                            <copy file="${project.build.directory}/test-nar/bin/${nar.aol}/TestWrapper.exe" tofile="${project.build.directory}/nar/${project.artifactId}-${project.version}-${nar.aol}-jni/lib/${nar.aol}/jni/TestWrapper.dll"/>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
                <execution>
                    <id>delete-test-lib-from-deployment</id>
                    <phase>post-integration-test</phase>
                    <configuration>
                        <target>
                            <delete file="${project.build.directory}/nar/${project.artifactId}-${project.version}-${nar.aol}-jni/lib/${nar.aol}/jni/TestWrapper.dll"/>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>